"""Tests for Item API endpoints.""" import pytest class TestItemAPI: """Test cases for Item CRUD operations.""" def test_create_item_success(self, client, db_session, auth_headers): """Test creating an item with valid data.""" response = client.post('/api/items/create', json={ 'name': 'Test Item', 'unit_price': 10.50 }, headers=auth_headers) assert response.status_code == 200 data = response.get_json() assert data['success'] is True assert data['data']['name'] == 'Test Item' assert data['data']['unit_price'] == 10.50 assert 'id' in data['data'] def test_create_item_empty_name(self, client, db_session, auth_headers): """Test creating an item with empty name fails.""" response = client.post('/api/items/create', json={ 'name': '', 'unit_price': 10.50 }, headers=auth_headers) assert response.status_code == 400 data = response.get_json() assert data['success'] is False assert '不能为空' in data['error'] or '空' in data['error'] def test_create_item_whitespace_name(self, client, db_session, auth_headers): """Test creating an item with whitespace-only name fails.""" response = client.post('/api/items/create', json={ 'name': ' ', 'unit_price': 10.50 }, headers=auth_headers) assert response.status_code == 400 data = response.get_json() assert data['success'] is False def test_create_item_zero_price(self, client, db_session, auth_headers): """Test creating an item with zero price fails.""" response = client.post('/api/items/create', json={ 'name': 'Test Item', 'unit_price': 0 }, headers=auth_headers) assert response.status_code == 400 data = response.get_json() assert data['success'] is False assert '正数' in data['error'] or '正' in data['error'] def test_create_item_negative_price(self, client, db_session, auth_headers): """Test creating an item with negative price fails.""" response = client.post('/api/items/create', json={ 'name': 'Test Item', 'unit_price': -5.00 }, headers=auth_headers) assert response.status_code == 400 data = response.get_json() assert data['success'] is False def test_get_all_items(self, client, db_session, auth_headers): """Test getting all items.""" # Create some items first client.post('/api/items/create', json={'name': 'Item A', 'unit_price': 10.00}, headers=auth_headers) client.post('/api/items/create', json={'name': 'Item B', 'unit_price': 20.50}, headers=auth_headers) response = client.get('/api/items', headers=auth_headers) assert response.status_code == 200 data = response.get_json() assert data['success'] is True assert len(data['data']) >= 2 def test_get_item_by_id(self, client, db_session, auth_headers): """Test getting an item by ID.""" # Create an item first create_response = client.post('/api/items/create', json={ 'name': 'Test Item', 'unit_price': 15.75 }, headers=auth_headers) item_id = create_response.get_json()['data']['id'] response = client.get(f'/api/items/{item_id}', headers=auth_headers) assert response.status_code == 200 data = response.get_json() assert data['success'] is True assert data['data']['name'] == 'Test Item' assert data['data']['unit_price'] == 15.75 def test_get_item_not_found(self, client, db_session, auth_headers): """Test getting a non-existent item.""" response = client.get('/api/items/99999', headers=auth_headers) assert response.status_code == 404 data = response.get_json() assert data['success'] is False assert data['code'] == 'NOT_FOUND' def test_update_item_success(self, client, db_session, auth_headers): """Test updating an item.""" # Create an item first create_response = client.post('/api/items/create', json={ 'name': 'Original Item', 'unit_price': 10.00 }, headers=auth_headers) item_id = create_response.get_json()['data']['id'] response = client.post('/api/items/update', json={ 'id': item_id, 'name': 'Updated Item', 'unit_price': 25.50 }, headers=auth_headers) assert response.status_code == 200 data = response.get_json() assert data['success'] is True assert data['data']['name'] == 'Updated Item' assert data['data']['unit_price'] == 25.50 def test_update_item_partial(self, client, db_session, auth_headers): """Test updating only the name of an item.""" # Create an item first create_response = client.post('/api/items/create', json={ 'name': 'Original Item', 'unit_price': 10.00 }, headers=auth_headers) item_id = create_response.get_json()['data']['id'] response = client.post('/api/items/update', json={ 'id': item_id, 'name': 'Updated Name Only' }, headers=auth_headers) assert response.status_code == 200 data = response.get_json() assert data['success'] is True assert data['data']['name'] == 'Updated Name Only' assert data['data']['unit_price'] == 10.00 # Price unchanged def test_update_item_not_found(self, client, db_session, auth_headers): """Test updating a non-existent item.""" response = client.post('/api/items/update', json={ 'id': 99999, 'name': 'Updated Item' }, headers=auth_headers) assert response.status_code == 404 data = response.get_json() assert data['success'] is False assert data['code'] == 'NOT_FOUND' def test_update_item_empty_name(self, client, db_session, auth_headers): """Test updating an item with empty name fails.""" # Create an item first create_response = client.post('/api/items/create', json={ 'name': 'Original Item', 'unit_price': 10.00 }, headers=auth_headers) item_id = create_response.get_json()['data']['id'] response = client.post('/api/items/update', json={ 'id': item_id, 'name': '' }, headers=auth_headers) assert response.status_code == 400 data = response.get_json() assert data['success'] is False def test_update_item_invalid_price(self, client, db_session, auth_headers): """Test updating an item with invalid price fails.""" # Create an item first create_response = client.post('/api/items/create', json={ 'name': 'Original Item', 'unit_price': 10.00 }, headers=auth_headers) item_id = create_response.get_json()['data']['id'] response = client.post('/api/items/update', json={ 'id': item_id, 'unit_price': -5.00 }, headers=auth_headers) assert response.status_code == 400 data = response.get_json() assert data['success'] is False def test_delete_item_success(self, client, db_session, auth_headers): """Test deleting an item.""" # Create an item first create_response = client.post('/api/items/create', json={ 'name': 'Item to Delete', 'unit_price': 10.00 }, headers=auth_headers) item_id = create_response.get_json()['data']['id'] response = client.post('/api/items/delete', json={'id': item_id}, headers=auth_headers) assert response.status_code == 200 data = response.get_json() assert data['success'] is True # Verify item is deleted get_response = client.get(f'/api/items/{item_id}', headers=auth_headers) assert get_response.status_code == 404 def test_delete_item_not_found(self, client, db_session, auth_headers): """Test deleting a non-existent item.""" response = client.post('/api/items/delete', json={'id': 99999}, headers=auth_headers) assert response.status_code == 404 data = response.get_json() assert data['success'] is False assert data['code'] == 'NOT_FOUND' def test_unauthorized_access(self, client, db_session): """Test that endpoints require authentication.""" response = client.get('/api/items') assert response.status_code == 401 data = response.get_json() assert data['success'] is False assert data['code'] == 'UNAUTHORIZED'