"""Tests for WorkRecord API endpoints.""" import pytest from datetime import date class TestWorkRecordAPI: """Test cases for WorkRecord API.""" def test_create_work_record_success(self, client, db_session, auth_headers): """Test creating a work record with valid data.""" person_resp = client.post('/api/persons/create', json={'name': 'Test Worker'}, headers=auth_headers) person_id = person_resp.get_json()['data']['id'] item_resp = client.post('/api/items/create', json={'name': 'Test Item', 'unit_price': 10.50}, headers=auth_headers) item_id = item_resp.get_json()['data']['id'] response = client.post('/api/work-records/create', json={ 'person_id': person_id, 'item_id': item_id, 'work_date': '2024-01-15', 'quantity': 5 }, headers=auth_headers) assert response.status_code == 200 data = response.get_json() assert data['success'] is True assert data['data']['person_id'] == person_id assert data['data']['item_id'] == item_id assert data['data']['quantity'] == 5 assert data['data']['total_price'] == 52.50 def test_get_all_work_records(self, client, db_session, auth_headers): """Test getting all work records.""" person_resp = client.post('/api/persons/create', json={'name': 'Test Worker'}, headers=auth_headers) person_id = person_resp.get_json()['data']['id'] item_resp = client.post('/api/items/create', json={'name': 'Test Item', 'unit_price': 10.0}, headers=auth_headers) item_id = item_resp.get_json()['data']['id'] client.post('/api/work-records/create', json={ 'person_id': person_id, 'item_id': item_id, 'work_date': '2024-01-15', 'quantity': 5 }, headers=auth_headers) response = client.get('/api/work-records', headers=auth_headers) assert response.status_code == 200 data = response.get_json() assert data['success'] is True assert len(data['data']) >= 1 def test_get_work_record_not_found(self, client, db_session, auth_headers): """Test getting a non-existent work record.""" response = client.get('/api/work-records/99999', headers=auth_headers) assert response.status_code == 404 data = response.get_json() assert data['success'] is False def test_unauthorized_access(self, client, db_session): """Test that endpoints require authentication.""" response = client.get('/api/work-records') assert response.status_code == 401 data = response.get_json() assert data['success'] is False assert data['code'] == 'UNAUTHORIZED'