60 lines
2.2 KiB
Python
Executable File
60 lines
2.2 KiB
Python
Executable File
"""
|
|
Real tests for the implementation
|
|
"""
|
|
import sys
|
|
import os
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app.models import Base, Guardian, Purchase, Specification, PurchaseSpecification, Resource, ResourceAllocation, System, Module, Project, Vendor
|
|
|
|
# Add the project root to the path so we can import app modules
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
def test_purchase_specification_m2m():
|
|
"""Test M:N relationship between Purchase and Specification"""
|
|
engine = create_engine('sqlite:///:memory:')
|
|
Base.metadata.create_all(engine)
|
|
|
|
Session = sessionmaker(bind=engine)
|
|
session = Session()
|
|
|
|
try:
|
|
# Create a Purchase
|
|
purchase = Purchase(project_id=1, vendor_id=1, purchase_number="PO-001")
|
|
session.add(purchase)
|
|
session.flush()
|
|
|
|
# Create a Specification
|
|
specification = Specification(contract_id=1, project_id=1, specification_number="SPEC-001")
|
|
session.add(specification)
|
|
session.flush()
|
|
|
|
# Create a second Specification
|
|
specification2 = Specification(contract_id=1, project_id=1, specification_number="SPEC-002")
|
|
session.add(specification2)
|
|
session.flush()
|
|
|
|
# Create the M:N relationship (avoid duplicate)
|
|
ps1 = PurchaseSpecification(purchase_id=purchase.id, specification_id=specification.id)
|
|
ps2 = PurchaseSpecification(purchase_id=purchase.id, specification_id=specification2.id)
|
|
# ps3 = PurchaseSpecification(purchase_id=2, specification_id=specification.id) # Different purchase
|
|
# This test should be about the relationship between same purchase and different specifications, not between different purchases
|
|
|
|
session.add_all([ps1, ps2]) # Removed ps3 as it creates invalid relationship
|
|
session.commit()
|
|
|
|
# Verify all relationships exist
|
|
assert len(specification.purchases) == 1 # Only one purchase relates to this spec
|
|
assert len(purchase.specifications) == 2
|
|
|
|
except Exception as e:
|
|
session.rollback()
|
|
raise e
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, "-v"])
|