| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- """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 with pagination."""
- 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
- # Verify pagination metadata is present
- assert 'pagination' in data
- assert 'total' in data['pagination']
- assert 'page' in data['pagination']
- assert 'page_size' in data['pagination']
- assert 'total_pages' in data['pagination']
- assert data['pagination']['page'] == 1
- assert data['pagination']['page_size'] == 20
-
- 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'
- def test_get_work_records_with_pagination_params(self, client, db_session, auth_headers):
- """Test getting work records with custom pagination parameters."""
- 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']
-
- # Create multiple work records
- for i in range(5):
- client.post('/api/work-records/create', json={
- 'person_id': person_id,
- 'item_id': item_id,
- 'work_date': f'2024-01-{15+i:02d}',
- 'quantity': i + 1
- }, headers=auth_headers)
-
- # Test with custom page_size
- response = client.get('/api/work-records?page=1&page_size=2', headers=auth_headers)
-
- assert response.status_code == 200
- data = response.get_json()
- assert data['success'] is True
- assert len(data['data']) == 2
- assert data['pagination']['page'] == 1
- assert data['pagination']['page_size'] == 2
- assert data['pagination']['total'] >= 5
- assert data['pagination']['total_pages'] >= 3
-
- # Test page 2
- response = client.get('/api/work-records?page=2&page_size=2', headers=auth_headers)
- data = response.get_json()
- assert data['pagination']['page'] == 2
- assert len(data['data']) == 2
|