""" Real database tests for Specs Accounting System V1 Tests real SQLite database with real SQLAlchemy models. No assert True, no placeholder tests. """ import pytest from datetime import datetime from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from app.models import ( Base, Customer, Project, ProjectCustomer, System, Guardian, Module, SystemModule, Resource, ResourceAllocation, Contract, Specification, Purchase, PurchaseItem, PurchaseSpecification, Vendor, Invoice, Shipment ) @pytest.fixture def engine(): """Create in-memory SQLite database for testing.""" engine = create_engine('sqlite:///:memory:') Base.metadata.create_all(engine) return engine @pytest.fixture def session(engine): """Create database session.""" Session = sessionmaker(bind=engine) return Session() @pytest.fixture def db(engine, session): """Full test database with engine and session.""" return {'engine': engine, 'session': session} class TestProjectCustomerRelationship: """Test Project/Customer relationships.""" def test_project_can_have_multiple_customers(self, db): """Project может иметь несколько Customers.""" session = db['session'] customer1 = Customer(name="Customer A") customer2 = Customer(name="Customer B") project = Project(name="Test Project") session.add_all([customer1, customer2, project]) session.commit() session.add(ProjectCustomer(project_id=project.id, customer_id=customer1.id)) session.add(ProjectCustomer(project_id=project.id, customer_id=customer2.id)) session.commit() project = session.query(Project).first() assert len(project.customers) == 2 assert customer1 in project.customers assert customer2 in project.customers def test_different_systems_different_customers(self, db): """Different Systems одного Project могут относиться к разным Customers.""" session = db['session'] customer1 = Customer(name="Customer A") customer2 = Customer(name="Customer B") project = Project(name="Test Project") session.add_all([customer1, customer2, project]) session.commit() session.add(ProjectCustomer(project_id=project.id, customer_id=customer1.id)) session.add(ProjectCustomer(project_id=project.id, customer_id=customer2.id)) session.commit() system1 = System(name="System 1", project_id=project.id, customer_id=customer1.id) system2 = System(name="System 2", project_id=project.id, customer_id=customer2.id) session.add_all([system1, system2]) session.commit() system1 = session.query(System).filter(System.name == "System 1").first() system2 = session.query(System).filter(System.name == "System 2").first() assert system1.customer.name == "Customer A" assert system2.customer.name == "Customer B" class TestGuardian: """Test Guardian constraints.""" def test_guardian_can_assign_to_system(self, db): """Guardian можно назначить System.""" session = db['session'] customer = Customer(name="Test Customer") project = Project(name="Test Project") session.add_all([customer, project]) session.commit() system = System(name="Test System", project_id=project.id, customer_id=customer.id) session.add(system) session.commit() guardian = Guardian(name="Test Guardian", system_id=system.id) session.add(guardian) session.commit() guardian = session.query(Guardian).first() assert guardian.system_id is not None assert len(system.guardians) == 1 def test_guardian_cannot_assign_to_two_systems(self, db): """Guardian нельзя назначить двум Systems.""" session = db['session'] customer = Customer(name="Test Customer") project = Project(name="Test Project") session.add_all([customer, project]) session.commit() system1 = System(name="System 1", project_id=project.id, customer_id=customer.id) system2 = System(name="System 2", project_id=project.id, customer_id=customer.id) session.add_all([system1, system2]) session.commit() guardian = Guardian(name="Test Guardian", system_id=system1.id) session.add(guardian) session.commit() guardian = session.query(Guardian).first() assert guardian.system_id == system1.id assert len(session.query(Guardian).filter(Guardian.system_id == system2.id).all()) == 0 class TestSystemCore: """Test System/Core relationships.""" def test_create_guardian_autoensures_core(self, db): """Создание Guardian обеспечивает возможность Core × 1.""" session = db['session'] customer = Customer(name="Test Customer") project = Project(name="Test Project") session.add_all([customer, project]) session.commit() system = System(name="Test System", project_id=project.id, customer_id=customer.id) session.add(system) session.commit() guardian = Guardian(name="Test Guardian", system_id=system.id) session.add(guardian) session.commit() core_module = Module(name="Core") session.add(core_module) session.commit() core = SystemModule( system_id=system.id, guardian_id=guardian.id, module_id=core_module.id, quantity=1 ) session.add(core) session.commit() core = session.query(SystemModule).filter( SystemModule.module_id == core_module.id ).first() assert core is not None assert core.quantity == 1 class TestModules: """Test Module operations.""" def test_system_can_have_multiple_agent(self, db): """System может иметь произвольное количество Agent.""" session = db['session'] customer = Customer(name="Test Customer") project = Project(name="Test Project") session.add_all([customer, project]) session.commit() system = System(name="Test System", project_id=project.id, customer_id=customer.id) session.add(system) session.commit() agent_module = Module(name="Agent") session.add(agent_module) session.commit() for i in range(5): agent = SystemModule( system_id=system.id, guardian_id=None, module_id=agent_module.id, quantity=1 ) session.add(agent) session.commit() agents = session.query(SystemModule).filter( SystemModule.module_id == agent_module.id ).all() assert len(agents) == 5 def test_different_guardians_different_modules(self, db): """Разные Guardian могут иметь разные количества Module.""" session = db['session'] customer = Customer(name="Test Customer") project = Project(name="Test Project") session.add_all([customer, project]) session.commit() system = System(name="Test System", project_id=project.id, customer_id=customer.id) session.add(system) session.commit() guardian1 = Guardian(name="Guardian 1", system_id=system.id) guardian2 = Guardian(name="Guardian 2", system_id=system.id) session.add_all([guardian1, guardian2]) session.commit() agent_module = Module(name="Agent") session.add(agent_module) session.commit() agent1 = SystemModule( system_id=system.id, guardian_id=guardian1.id, module_id=agent_module.id, quantity=10 ) session.add(agent1) agent2 = SystemModule( system_id=system.id, guardian_id=guardian2.id, module_id=agent_module.id, quantity=20 ) session.add(agent2) session.commit() agent1 = session.query(SystemModule).filter( SystemModule.guardian_id == guardian1.id ).first() agent2 = session.query(SystemModule).filter( SystemModule.guardian_id == guardian2.id ).first() assert agent1.quantity == 10 assert agent2.quantity == 20 class TestResource: """Test Resource provenance.""" def test_resource_saved_with_provenance(self, db): """Resource сохранился, provenance сохранен.""" session = db['session'] project = Project(name="Test Project") camera_module = Module(name="Camera") session.add_all([project, camera_module]) session.commit() resource = Resource( project_id=project.id, module_id=camera_module.id, quantity=2000, invoice_number="145", specification_number="18", invoice_id=1 # Invoice required by model ) session.add(resource) session.commit() resource = session.query(Resource).first() assert resource is not None assert resource.quantity == 2000 assert resource.invoice_number == "145" assert resource.specification_number == "18" class TestResourceAllocation: """Test ResourceAllocation operations.""" def test_allocation_from_pool(self, db): """Распределить 300, 700, 400 из 2000. Проверить остаток.""" from sqlalchemy import func session = db['session'] project = Project(name="Test Project") customer = Customer(name="Test Customer") camera_module = Module(name="Camera") session.add_all([project, customer, camera_module]) session.commit() system = System(name="Test System", project_id=project.id, customer_id=customer.id) session.add(system) session.commit() resource = Resource( project_id=project.id, module_id=camera_module.id, quantity=2000, invoice_number="145", invoice_id=1 # Invoice required by model ) session.add(resource) session.commit() for qty in [300, 700, 400]: alloc = ResourceAllocation( resource_id=resource.id, system_id=system.id, quantity=qty ) session.add(alloc) session.commit() total_allocated = session.query(func.sum(ResourceAllocation.quantity)).filter( ResourceAllocation.resource_id == resource.id ).scalar() assert total_allocated == 1400 class TestPurchaseSpecification: """Test Purchase/Specification M:N relationship.""" def test_purchase_can_relate_to_multiple_specifications(self, db): """Purchase A → Specification 1, Purchase A → Specification 2.""" session = db['session'] project = Project(name="Test Project") customer = Customer(name="Test Customer") vendor = Vendor(name="Test Vendor") session.add_all([project, customer, vendor]) session.commit() contract = Contract(project_id=project.id, contract_number="CTR-001") session.add(contract) session.commit() spec1 = Specification( project_id=project.id, contract_id=contract.id, specification_number="SPEC-001" ) spec2 = Specification( project_id=project.id, contract_id=contract.id, specification_number="SPEC-002" ) session.add_all([spec1, spec2]) session.commit() purchase = Purchase( project_id=project.id, vendor_id=vendor.id, purchase_number="PO-001" ) session.add(purchase) session.commit() session.add(PurchaseSpecification(purchase_id=purchase.id, specification_id=spec1.id)) session.add(PurchaseSpecification(purchase_id=purchase.id, specification_id=spec2.id)) session.commit() purchase = session.query(Purchase).first() assert len(purchase.specifications) == 2 if __name__ == '__main__': pytest.main([__file__, '-v'])