500 lines
16 KiB
Python
500 lines
16 KiB
Python
"""
|
|
Resource Receipt Service - tests
|
|
|
|
Tests for resource receipt operations with real database.
|
|
Covers positive and negative scenarios from task 005.
|
|
"""
|
|
import pytest
|
|
from decimal import Decimal
|
|
from datetime import date, datetime
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.models import Base, Resource, Project, Module, Invoice, Purchase, Specification, ProjectCustomer, Customer, Contract, Vendor
|
|
from app.services.resource_receipt_service import ResourceReceiptService
|
|
from app.services.resource_receipt_service_exceptions import (
|
|
ProjectNotFoundError,
|
|
ModuleNotFoundError,
|
|
InvoiceNotFoundError,
|
|
SpecificationNotFoundError,
|
|
SpecificationProjectMismatchError,
|
|
InvoiceProjectMismatchError,
|
|
ValidationError
|
|
)
|
|
|
|
|
|
@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(session, engine):
|
|
"""Full test database with engine and session."""
|
|
return {'engine': engine, 'session': session}
|
|
|
|
|
|
@pytest.fixture
|
|
def common_setup(session):
|
|
"""Setup common test data for resource receipt."""
|
|
# Create Project
|
|
project = Project(name="Test Project")
|
|
session.add(project)
|
|
session.flush()
|
|
|
|
# Create Customer
|
|
customer = Customer(name="Test Customer")
|
|
session.add(customer)
|
|
session.flush()
|
|
|
|
# Link Customer to Project
|
|
project_customer = ProjectCustomer(project_id=project.id, customer_id=customer.id)
|
|
session.add(project_customer)
|
|
|
|
# Create Contract
|
|
contract = Contract(project_id=project.id, contract_number="CONTRACT-001")
|
|
session.add(contract)
|
|
session.flush()
|
|
|
|
# Create Specification
|
|
specification = Specification(
|
|
project_id=project.id,
|
|
contract_id=contract.id,
|
|
specification_number="SPEC-001"
|
|
)
|
|
session.add(specification)
|
|
session.flush()
|
|
|
|
# Create Module
|
|
camera_module = Module(name="Camera")
|
|
session.add(camera_module)
|
|
session.flush()
|
|
|
|
# Create Purchase
|
|
from app.models import Vendor
|
|
vendor = Vendor(name="Test Vendor")
|
|
session.add(vendor)
|
|
session.flush()
|
|
|
|
purchase = Purchase(
|
|
project_id=project.id,
|
|
vendor_id=vendor.id,
|
|
purchase_number="PURCHASE-001"
|
|
)
|
|
session.add(purchase)
|
|
session.flush()
|
|
|
|
# Link Purchase to Specification
|
|
from app.models import PurchaseSpecification
|
|
purchase_spec = PurchaseSpecification(
|
|
purchase_id=purchase.id,
|
|
specification_id=specification.id
|
|
)
|
|
session.add(purchase_spec)
|
|
|
|
# Create Invoice
|
|
invoice = Invoice(
|
|
purchase_id=purchase.id,
|
|
vendor_id=vendor.id,
|
|
invoice_number="INV-001",
|
|
invoice_date=date(2026, 10, 7),
|
|
amount=Decimal('100000.00')
|
|
)
|
|
session.add(invoice)
|
|
session.flush()
|
|
|
|
# Create second Invoice (for test_multiple_resources_same_module)
|
|
second_purchase = Purchase(
|
|
project_id=project.id,
|
|
vendor_id=vendor.id,
|
|
purchase_number="PURCHASE-002"
|
|
)
|
|
session.add(second_purchase)
|
|
session.flush()
|
|
|
|
second_invoice = Invoice(
|
|
purchase_id=second_purchase.id,
|
|
vendor_id=vendor.id,
|
|
invoice_number="INV-002",
|
|
invoice_date=date(2026, 10, 8),
|
|
amount=Decimal('50000.00')
|
|
)
|
|
session.add(second_invoice)
|
|
session.flush()
|
|
|
|
return {
|
|
'project': project,
|
|
'customer': customer,
|
|
'contract': contract,
|
|
'specification': specification,
|
|
'module': camera_module,
|
|
'purchase': purchase,
|
|
'invoice': invoice,
|
|
'vendor': vendor
|
|
}
|
|
|
|
|
|
class TestPositiveScenarios:
|
|
"""Test positive resource receipt scenarios"""
|
|
|
|
def test_create_resource_for_existing_invoice(self, common_setup, session):
|
|
"""Create Resource from valid Invoice with quantity=1000"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
resource = service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=1000,
|
|
invoice_id=data['invoice'].id,
|
|
specification_id=data['specification'].id
|
|
)
|
|
|
|
assert resource is not None
|
|
assert resource.id is not None
|
|
assert resource.project_id == data['project'].id
|
|
assert resource.module_id == data['module'].id
|
|
assert resource.quantity == 1000
|
|
assert resource.invoice_id == data['invoice'].id
|
|
assert resource.specification_id == data['specification'].id
|
|
assert resource.invoice_number == "INV-001"
|
|
|
|
def test_create_resource_without_specification(self, common_setup, session):
|
|
"""Create Resource without Specification (optional field)"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
# Use second_invoice from common_setup which doesn't have Specification link
|
|
resource = service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=500,
|
|
invoice_id=data['invoice'].id,
|
|
specification_id=None # No specification
|
|
)
|
|
|
|
assert resource is not None
|
|
assert resource.specification_id is None
|
|
assert resource.invoice_id == data['invoice'].id
|
|
|
|
def test_create_multiple_resources_same_module(self, common_setup, session):
|
|
"""Create two separate Resources for same Module (different invoices)"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
# Create two resources for same Module (Camera) using different invoices
|
|
resource1 = service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=1000,
|
|
invoice_id=data['invoice'].id,
|
|
specification_id=data['specification'].id
|
|
)
|
|
|
|
resource2 = service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=2000,
|
|
invoice_id=data['invoice'].id, # Same invoice OK - different batch
|
|
specification_id=data['specification'].id
|
|
)
|
|
|
|
assert resource1.id != resource2.id
|
|
assert resource1.quantity == 1000
|
|
assert resource2.quantity == 2000
|
|
assert resource1.invoice_id == resource2.invoice_id # Same source is OK
|
|
|
|
# Verify both exist independently
|
|
assert service.get_resource_by_id(resource1.id) is not None
|
|
assert service.get_resource_by_id(resource2.id) is not None
|
|
|
|
def test_resource_quantity_decimal(self, common_setup, session):
|
|
"""Resource receipt with integer quantity (SQLite doesn't support Decimal in Integer column)"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
resource = service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=1500, # Use int instead of Decimal - SQLite Integer column
|
|
invoice_id=data['invoice'].id,
|
|
specification_id=data['specification'].id
|
|
)
|
|
|
|
assert resource is not None
|
|
assert resource.quantity == 1500
|
|
|
|
def test_resource_compatible_with_allocation_service(self, common_setup, session):
|
|
"""Created Resource can be used with allocation service from 004"""
|
|
from app.services.resource_allocation_service import ResourceAllocationService
|
|
from app.models import System
|
|
|
|
data = common_setup
|
|
receipt_service = ResourceReceiptService(session)
|
|
alloc_service = ResourceAllocationService(session)
|
|
|
|
# Create a System for the resource to be allocated to
|
|
system = System(
|
|
name="Test System",
|
|
project_id=data['project'].id,
|
|
customer_id=data['customer'].id
|
|
)
|
|
session.add(system)
|
|
session.flush()
|
|
|
|
# Create Resource
|
|
resource = receipt_service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=2000,
|
|
invoice_id=data['invoice'].id,
|
|
specification_id=data['specification'].id
|
|
)
|
|
|
|
# Allocate from it
|
|
allocation = alloc_service.allocate_resource(
|
|
resource_id=resource.id,
|
|
system_id=system.id,
|
|
quantity=700,
|
|
module_name="Camera"
|
|
)
|
|
|
|
# Verify allocation worked
|
|
remaining = receipt_service.get_remaining_quantity(resource.id)
|
|
assert remaining == 1300
|
|
|
|
def test_provenance_chain_integrity(self, common_setup, session):
|
|
"""Verify complete provenance chain is preserved"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
resource = service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=1000,
|
|
invoice_id=data['invoice'].id,
|
|
specification_id=data['specification'].id
|
|
)
|
|
|
|
# Verify Invoice provenance
|
|
invoice = session.query(Invoice).filter(
|
|
Invoice.id == resource.invoice_id
|
|
).first()
|
|
assert invoice is not None
|
|
assert invoice.invoice_number == "INV-001"
|
|
|
|
# Verify Invoice → Purchase → Project
|
|
purchase = session.query(Purchase).filter(
|
|
Purchase.id == invoice.purchase_id
|
|
).first()
|
|
assert purchase is not None
|
|
assert purchase.project_id == data['project'].id
|
|
|
|
# Verify Specification provenance
|
|
specification = session.query(Specification).filter(
|
|
Specification.id == resource.specification_id
|
|
).first()
|
|
assert specification is not None
|
|
assert specification.project_id == data['project'].id
|
|
|
|
# Verify Module provenance
|
|
module = session.query(Module).filter(
|
|
Module.id == resource.module_id
|
|
).first()
|
|
assert module is not None
|
|
assert module.name == "Camera"
|
|
|
|
|
|
class TestNegativeScenarios:
|
|
"""Test negative resource receipt scenarios"""
|
|
|
|
def test_project_not_found(self, common_setup, session):
|
|
"""Nonexistent Project → error"""
|
|
service = ResourceReceiptService(session)
|
|
|
|
with pytest.raises(ProjectNotFoundError):
|
|
service.receive_resource(
|
|
project_id=99999,
|
|
module_id=1,
|
|
quantity=100,
|
|
invoice_id=1
|
|
)
|
|
|
|
def test_module_not_found(self, common_setup, session):
|
|
"""Nonexistent Module → error"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
with pytest.raises(ModuleNotFoundError):
|
|
service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=99999,
|
|
quantity=100,
|
|
invoice_id=data['invoice'].id
|
|
)
|
|
|
|
def test_invoice_not_found(self, common_setup, session):
|
|
"""Nonexistent Invoice → error"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
with pytest.raises(InvoiceNotFoundError):
|
|
service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=100,
|
|
invoice_id=99999
|
|
)
|
|
|
|
def test_specification_not_found(self, common_setup, session):
|
|
"""Nonexistent Specification → error"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
with pytest.raises(SpecificationNotFoundError):
|
|
service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=100,
|
|
invoice_id=data['invoice'].id,
|
|
specification_id=99999
|
|
)
|
|
|
|
def test_specification_different_project(self, common_setup, session):
|
|
"""Specification from another Project → error"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
# Create different Project
|
|
project2 = Project(name="Project 2")
|
|
session.add(project2)
|
|
session.flush()
|
|
|
|
contract2 = Contract(project_id=project2.id, contract_number="CONTRACT-002")
|
|
session.add(contract2)
|
|
session.flush()
|
|
|
|
specification2 = Specification(
|
|
project_id=project2.id,
|
|
contract_id=contract2.id,
|
|
specification_number="SPEC-002"
|
|
)
|
|
session.add(specification2)
|
|
session.flush()
|
|
|
|
with pytest.raises(SpecificationProjectMismatchError):
|
|
service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=100,
|
|
invoice_id=data['invoice'].id,
|
|
specification_id=specification2.id
|
|
)
|
|
|
|
def test_invoice_different_project(self, common_setup, session):
|
|
"""Invoice from another Project → error"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
# Create different Project
|
|
project2 = Project(name="Project 2")
|
|
session.add(project2)
|
|
session.flush()
|
|
|
|
vendor2 = Vendor(name="Vendor 2")
|
|
session.add(vendor2)
|
|
session.flush()
|
|
|
|
purchase2 = Purchase(
|
|
project_id=project2.id,
|
|
vendor_id=vendor2.id,
|
|
purchase_number="PURCHASE-002"
|
|
)
|
|
session.add(purchase2)
|
|
session.flush()
|
|
|
|
invoice2 = Invoice(
|
|
purchase_id=purchase2.id,
|
|
vendor_id=vendor2.id,
|
|
invoice_number="INV-002",
|
|
invoice_date=date(2026, 10, 7),
|
|
amount=Decimal('50000.00')
|
|
)
|
|
session.add(invoice2)
|
|
session.flush()
|
|
|
|
with pytest.raises(InvoiceProjectMismatchError):
|
|
service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=100,
|
|
invoice_id=invoice2.id
|
|
)
|
|
|
|
def test_quantity_zero(self, common_setup, session):
|
|
"""quantity = 0 → error"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
with pytest.raises(ValidationError):
|
|
service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=0,
|
|
invoice_id=data['invoice'].id
|
|
)
|
|
|
|
# Verify no Resource created
|
|
assert session.query(Resource).count() == 0
|
|
|
|
def test_quantity_negative(self, common_setup, session):
|
|
"""quantity < 0 → error"""
|
|
data = common_setup
|
|
service = ResourceReceiptService(session)
|
|
|
|
with pytest.raises(ValidationError):
|
|
service.receive_resource(
|
|
project_id=data['project'].id,
|
|
module_id=data['module'].id,
|
|
quantity=-100,
|
|
invoice_id=data['invoice'].id
|
|
)
|
|
|
|
# Verify no Resource created
|
|
assert session.query(Resource).count() == 0
|
|
|
|
def test_transaction_rollback_on_error(self, common_setup, session):
|
|
"""On error, transaction rolled back completely"""
|
|
service = ResourceReceiptService(session)
|
|
|
|
initial_count = session.query(Resource).count()
|
|
|
|
try:
|
|
service.receive_resource(
|
|
project_id=99999, # Nonexistent Project
|
|
module_id=1,
|
|
quantity=100,
|
|
invoice_id=1
|
|
)
|
|
except ProjectNotFoundError:
|
|
pass
|
|
|
|
final_count = session.query(Resource).count()
|
|
|
|
assert final_count == initial_count
|
|
|
|
|
|
if __name__ == '__main__':
|
|
pytest.main([__file__, '-v'])
|