test_work_record.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """Tests for WorkRecord API endpoints."""
  2. import pytest
  3. from datetime import date
  4. class TestWorkRecordAPI:
  5. """Test cases for WorkRecord API."""
  6. def test_create_work_record_success(self, client, db_session, auth_headers):
  7. """Test creating a work record with valid data."""
  8. person_resp = client.post('/api/persons/create', json={'name': 'Test Worker'}, headers=auth_headers)
  9. person_id = person_resp.get_json()['data']['id']
  10. item_resp = client.post('/api/items/create', json={'name': 'Test Item', 'unit_price': 10.50}, headers=auth_headers)
  11. item_id = item_resp.get_json()['data']['id']
  12. response = client.post('/api/work-records/create', json={
  13. 'person_id': person_id,
  14. 'item_id': item_id,
  15. 'work_date': '2024-01-15',
  16. 'quantity': 5
  17. }, headers=auth_headers)
  18. assert response.status_code == 200
  19. data = response.get_json()
  20. assert data['success'] is True
  21. assert data['data']['person_id'] == person_id
  22. assert data['data']['item_id'] == item_id
  23. assert data['data']['quantity'] == 5
  24. assert data['data']['total_price'] == 52.50
  25. def test_get_all_work_records(self, client, db_session, auth_headers):
  26. """Test getting all work records."""
  27. person_resp = client.post('/api/persons/create', json={'name': 'Test Worker'}, headers=auth_headers)
  28. person_id = person_resp.get_json()['data']['id']
  29. item_resp = client.post('/api/items/create', json={'name': 'Test Item', 'unit_price': 10.0}, headers=auth_headers)
  30. item_id = item_resp.get_json()['data']['id']
  31. client.post('/api/work-records/create', json={
  32. 'person_id': person_id,
  33. 'item_id': item_id,
  34. 'work_date': '2024-01-15',
  35. 'quantity': 5
  36. }, headers=auth_headers)
  37. response = client.get('/api/work-records', headers=auth_headers)
  38. assert response.status_code == 200
  39. data = response.get_json()
  40. assert data['success'] is True
  41. assert len(data['data']) >= 1
  42. def test_get_work_record_not_found(self, client, db_session, auth_headers):
  43. """Test getting a non-existent work record."""
  44. response = client.get('/api/work-records/99999', headers=auth_headers)
  45. assert response.status_code == 404
  46. data = response.get_json()
  47. assert data['success'] is False
  48. def test_unauthorized_access(self, client, db_session):
  49. """Test that endpoints require authentication."""
  50. response = client.get('/api/work-records')
  51. assert response.status_code == 401
  52. data = response.get_json()
  53. assert data['success'] is False
  54. assert data['code'] == 'UNAUTHORIZED'