Prepare Specs UI for Windows review
This commit is contained in:
Regular → Executable
+10
-104
@@ -6,50 +6,11 @@ import os
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from app.models import Base, PoolResource, PoolReceipt, PoolAllocation, Guardian, Key, Purchase, Specification, PurchaseSpecification
|
||||
from app.services.pool_service import PoolResourceService
|
||||
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_guardian_key_constraint():
|
||||
"""Test that one guardian cannot be assigned to multiple keys"""
|
||||
# Create in-memory SQLite database for testing
|
||||
engine = create_engine('sqlite:///:memory:')
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
# Create a session
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
|
||||
try:
|
||||
# Create Guardian
|
||||
guardian = Guardian(name="Test Guardian", description="Test guardian")
|
||||
session.add(guardian)
|
||||
session.flush() # Get the ID without committing
|
||||
|
||||
# Create first Key for this Guardian
|
||||
key1 = Key(name="Key 1", system_id=1, guardian_id=guardian.id)
|
||||
session.add(key1)
|
||||
session.commit()
|
||||
|
||||
# Try to create second Key for the same Guardian - should fail
|
||||
key2 = Key(name="Key 2", system_id=1, guardian_id=guardian.id)
|
||||
session.add(key2)
|
||||
session.commit() # This should raise an integrity constraint violation
|
||||
|
||||
# If we get here without exception, test failed
|
||||
assert False, "Should have raised an integrity error for duplicate guardian assignment"
|
||||
|
||||
except Exception as e:
|
||||
# Expected to fail due to unique constraint
|
||||
session.rollback()
|
||||
# We expect an IntegrityError but can't directly test for it without full SQLAlchemy
|
||||
# The important thing is that this test validates our model structure
|
||||
pass
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
def test_purchase_specification_m2m():
|
||||
"""Test M:N relationship between Purchase and Specification"""
|
||||
engine = create_engine('sqlite:///:memory:')
|
||||
@@ -74,17 +35,18 @@ def test_purchase_specification_m2m():
|
||||
session.add(specification2)
|
||||
session.flush()
|
||||
|
||||
# Create the M:N relationship
|
||||
# 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=1, specification_id=specification.id) # Different purchase
|
||||
|
||||
session.add_all([ps1, ps2, ps3])
|
||||
# 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(purchase.purchase_specifications) == 2
|
||||
assert len(specification.purchase_specifications) == 2
|
||||
assert len(specification.purchases) == 1 # Only one purchase relates to this spec
|
||||
assert len(purchase.specifications) == 2
|
||||
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
@@ -92,62 +54,6 @@ def test_purchase_specification_m2m():
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
def test_pool_resource_balance():
|
||||
"""Test pool resource balance calculation"""
|
||||
engine = create_engine('sqlite:///:memory:')
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
|
||||
try:
|
||||
# Create a PoolResource
|
||||
pool_resource = PoolResource(resource_pool_id=1, product_id=1, license_type_id=1)
|
||||
session.add(pool_resource)
|
||||
session.flush()
|
||||
|
||||
# Add receipt of 10 units
|
||||
receipt = PoolReceipt(pool_resource_id=pool_resource.id, quantity=10, receipt_date="2023-01-01")
|
||||
session.add(receipt)
|
||||
session.commit()
|
||||
|
||||
# Check balance after receipt
|
||||
service = PoolResourceService(session)
|
||||
balance = service.get_current_balance(pool_resource.id)
|
||||
assert balance == 10
|
||||
|
||||
# Add allocation of 7 units
|
||||
allocation = PoolAllocation(pool_resource_id=pool_resource.id, quantity=7, allocation_date="2023-01-02")
|
||||
session.add(allocation)
|
||||
session.commit()
|
||||
|
||||
# Check balance after allocation
|
||||
balance = service.get_current_balance(pool_resource.id)
|
||||
assert balance == 3
|
||||
|
||||
# Add another allocation of 3 units
|
||||
allocation2 = PoolAllocation(pool_resource_id=pool_resource.id, quantity=3, allocation_date="2023-01-03")
|
||||
session.add(allocation2)
|
||||
session.commit()
|
||||
|
||||
# Check balance after second allocation
|
||||
balance = service.get_current_balance(pool_resource.id)
|
||||
assert balance == 0
|
||||
|
||||
# Try to allocate 1 unit (should fail due to insufficient balance)
|
||||
# We test this by calling the service method directly, not creating DB record
|
||||
can_allocate = service.can_allocate(pool_resource.id, 1)
|
||||
assert can_allocate == False
|
||||
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
raise e
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run the tests directly if this file is executed
|
||||
test_guardian_key_constraint()
|
||||
test_purchase_specification_m2m()
|
||||
test_pool_resource_balance()
|
||||
print("All tests passed!")
|
||||
pytest.main([__file__, "-v"])
|
||||
|
||||
Reference in New Issue
Block a user