import pytest from fastapi.testclient import TestClient from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from main import app, get_db from models import Base TEST_DB = "sqlite:///./test.db" test_engine = create_engine(TEST_DB, connect_args={"check_same_thread": False}) TestSession = sessionmaker(autocommit=False, autoflush=False, bind=test_engine) Base.metadata.create_all(bind=test_engine) def override_get_db(): db = TestSession() try: yield db finally: db.close() app.dependency_overrides[get_db] = override_get_db client = TestClient(app) def test_create_product(): response = client.post('/products/', json={"product_id": 1, "name": "Test name", "description": "Test description", "category": "Test category"}) assert response.status_code == 201 data = response.json() assert "id" in data def test_list_products(): client.post('/products/', json={"product_id": 1, "name": "Test name", "description": "Test description", "category": "Test category"}) response = client.get('/products/') assert response.status_code == 200 assert len(response.json()) >= 1 def test_get_product_by_id(): created = client.post('/products/', json={"product_id": 1, "name": "Test name", "description": "Test description", "category": "Test category"}).json() item_id = created['id'] response = client.get(f'/products/{item_id}') assert response.status_code == 200 assert response.json()['id'] == item_id def test_get_product_not_found(): response = client.get('/products/99999') assert response.status_code == 404 def test_update_product(): created = client.post('/products/', json={"product_id": 1, "name": "Test name", "description": "Test description", "category": "Test category"}).json() item_id = created['id'] response = client.put(f'/products/{item_id}', json={"product_id": 1, "name": "Updated name", "description": "Test description", "category": "Test category"}) assert response.status_code == 200 def test_delete_product(): created = client.post('/products/', json={"product_id": 1, "name": "Test name", "description": "Test description", "category": "Test category"}).json() item_id = created['id'] response = client.delete(f'/products/{item_id}') assert response.status_code == 204 response = client.get(f'/products/{item_id}') assert response.status_code == 404 def test_create_storagelocation(): response = client.post('/storage_locations/', json={"location_id": 1, "name": "Test name", "capacity": 0}) assert response.status_code == 201 data = response.json() assert "id" in data def test_list_storagelocations(): client.post('/storage_locations/', json={"location_id": 1, "name": "Test name", "capacity": 0}) response = client.get('/storage_locations/') assert response.status_code == 200 assert len(response.json()) >= 1 def test_get_storagelocation_by_id(): created = client.post('/storage_locations/', json={"location_id": 1, "name": "Test name", "capacity": 0}).json() item_id = created['id'] response = client.get(f'/storage_locations/{item_id}') assert response.status_code == 200 assert response.json()['id'] == item_id def test_get_storagelocation_not_found(): response = client.get('/storage_locations/99999') assert response.status_code == 404 def test_update_storagelocation(): created = client.post('/storage_locations/', json={"location_id": 1, "name": "Test name", "capacity": 0}).json() item_id = created['id'] response = client.put(f'/storage_locations/{item_id}', json={"location_id": 1, "name": "Updated name", "capacity": 0}) assert response.status_code == 200 def test_delete_storagelocation(): created = client.post('/storage_locations/', json={"location_id": 1, "name": "Test name", "capacity": 0}).json() item_id = created['id'] response = client.delete(f'/storage_locations/{item_id}') assert response.status_code == 204 response = client.get(f'/storage_locations/{item_id}') assert response.status_code == 404 def test_create_transfer(): response = client.post('/transfers/', json={"transfer_id": 1, "product_id": 1, "from_location_id": 1, "to_location_id": 1, "quantity": 0}) assert response.status_code == 201 data = response.json() assert "id" in data def test_list_transfers(): client.post('/transfers/', json={"transfer_id": 1, "product_id": 1, "from_location_id": 1, "to_location_id": 1, "quantity": 0}) response = client.get('/transfers/') assert response.status_code == 200 assert len(response.json()) >= 1 def test_get_transfer_by_id(): created = client.post('/transfers/', json={"transfer_id": 1, "product_id": 1, "from_location_id": 1, "to_location_id": 1, "quantity": 0}).json() item_id = created['id'] response = client.get(f'/transfers/{item_id}') assert response.status_code == 200 assert response.json()['id'] == item_id def test_get_transfer_not_found(): response = client.get('/transfers/99999') assert response.status_code == 404 def test_update_transfer(): created = client.post('/transfers/', json={"transfer_id": 1, "product_id": 1, "from_location_id": 1, "to_location_id": 1, "quantity": 0}).json() item_id = created['id'] response = client.put(f'/transfers/{item_id}', json={"transfer_id": 1, "product_id": 1, "from_location_id": 1, "to_location_id": 1, "quantity": 0}) assert response.status_code == 200 def test_delete_transfer(): created = client.post('/transfers/', json={"transfer_id": 1, "product_id": 1, "from_location_id": 1, "to_location_id": 1, "quantity": 0}).json() item_id = created['id'] response = client.delete(f'/transfers/{item_id}') assert response.status_code == 204 response = client.get(f'/transfers/{item_id}') assert response.status_code == 404