Initial commit
This commit is contained in:
@@ -0,0 +1,806 @@
|
||||
"""
|
||||
Procurement Flow Service Tests
|
||||
|
||||
Tests for ProcurementFlowService with positive and negative scenarios:
|
||||
- Positive: normal receive, partial delivery, multiple invoices, multiple specs
|
||||
- Negative: module not in Purchase, over-fulfillment, spec provenance, etc.
|
||||
"""
|
||||
import pytest
|
||||
from datetime import datetime, date
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.models import (
|
||||
Base, Project, Customer, Vendor, Contract, Module,
|
||||
Specification, SpecificationItem, Purchase, PurchaseItem,
|
||||
PurchaseSpecification, Invoice, Resource
|
||||
)
|
||||
from app.services.procurement_flow_service import ProcurementFlowService
|
||||
from app.services.procurement_flow_service_exceptions import (
|
||||
ProcurementFlowError,
|
||||
InvoiceNotFoundError,
|
||||
PurchaseNotFoundError,
|
||||
SpecificationNotFoundError,
|
||||
ModuleNotFoundError,
|
||||
ProjectNotFoundError,
|
||||
ProjectMismatchError,
|
||||
SpecificationNotLinkedToPurchaseError,
|
||||
ModuleNotInPurchaseError,
|
||||
OrderedQuantityExceededError,
|
||||
SpecificationItemNotFoundError,
|
||||
SpecificationQuantityExceededError,
|
||||
InvalidQuantityError,
|
||||
IntegrityError
|
||||
)
|
||||
|
||||
|
||||
@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}
|
||||
|
||||
|
||||
# === Common fixture for test data ===
|
||||
@pytest.fixture
|
||||
def full_setup(db):
|
||||
"""
|
||||
Setup complete test data:
|
||||
|
||||
Project: Test Project
|
||||
Customer: Test Customer
|
||||
Vendor: Test Vendor
|
||||
Contract: CTR-001
|
||||
Module: Camera, Agent
|
||||
Specification #12: Camera ×1000, Agent ×100
|
||||
Specification #18: Camera ×2000, Agent ×50
|
||||
Purchase #10: Camera ×100, Agent ×100
|
||||
PurchaseItem: Camera ×100
|
||||
PurchaseItem: Agent ×100
|
||||
PurchaseSpecification → Spec #12, Spec #18
|
||||
Invoice #145: Purchase #10
|
||||
"""
|
||||
session = db['session']
|
||||
|
||||
# Create Project and Customer
|
||||
customer = Customer(name="Test Customer")
|
||||
project = Project(name="Test Project")
|
||||
session.add_all([customer, project])
|
||||
session.commit()
|
||||
|
||||
# Create Vendor
|
||||
vendor = Vendor(name="Test Vendor")
|
||||
session.add(vendor)
|
||||
session.commit()
|
||||
|
||||
# Create Contract
|
||||
contract = Contract(project_id=project.id, contract_number="CTR-001")
|
||||
session.add(contract)
|
||||
session.commit()
|
||||
|
||||
# Create Modules
|
||||
camera_module = Module(name="Camera")
|
||||
agent_module = Module(name="Agent")
|
||||
session.add_all([camera_module, agent_module])
|
||||
session.commit()
|
||||
|
||||
# Create Specifications
|
||||
spec12 = Specification(
|
||||
project_id=project.id,
|
||||
contract_id=contract.id,
|
||||
specification_number="SPEC-012"
|
||||
)
|
||||
spec18 = Specification(
|
||||
project_id=project.id,
|
||||
contract_id=contract.id,
|
||||
specification_number="SPEC-018"
|
||||
)
|
||||
session.add_all([spec12, spec18])
|
||||
session.commit()
|
||||
|
||||
# Create SpecificationItems
|
||||
spec12_camera = SpecificationItem(
|
||||
specification_id=spec12.id,
|
||||
module_id=camera_module.id,
|
||||
quantity=1000,
|
||||
description="Camera for Spec 12"
|
||||
)
|
||||
spec12_agent = SpecificationItem(
|
||||
specification_id=spec12.id,
|
||||
module_id=agent_module.id,
|
||||
quantity=100,
|
||||
description="Agent for Spec 12"
|
||||
)
|
||||
spec18_camera = SpecificationItem(
|
||||
specification_id=spec18.id,
|
||||
module_id=camera_module.id,
|
||||
quantity=2000,
|
||||
description="Camera for Spec 18"
|
||||
)
|
||||
spec18_agent = SpecificationItem(
|
||||
specification_id=spec18.id,
|
||||
module_id=agent_module.id,
|
||||
quantity=50,
|
||||
description="Agent for Spec 18"
|
||||
)
|
||||
session.add_all([spec12_camera, spec12_agent, spec18_camera, spec18_agent])
|
||||
session.commit()
|
||||
|
||||
# Create Purchase
|
||||
purchase = Purchase(
|
||||
project_id=project.id,
|
||||
vendor_id=vendor.id,
|
||||
purchase_number="PO-010",
|
||||
purchase_date=date(2026, 10, 7)
|
||||
)
|
||||
session.add(purchase)
|
||||
session.commit()
|
||||
|
||||
# Create PurchaseItems
|
||||
purchase_item_camera = PurchaseItem(
|
||||
purchase_id=purchase.id,
|
||||
module_id=camera_module.id,
|
||||
quantity=100,
|
||||
unit_price=1000.00
|
||||
)
|
||||
purchase_item_agent = PurchaseItem(
|
||||
purchase_id=purchase.id,
|
||||
module_id=agent_module.id,
|
||||
quantity=100,
|
||||
unit_price=500.00
|
||||
)
|
||||
session.add_all([purchase_item_camera, purchase_item_agent])
|
||||
session.commit()
|
||||
|
||||
# Create PurchaseSpecifications (link Purchase to Specs)
|
||||
session.add(PurchaseSpecification(purchase_id=purchase.id, specification_id=spec12.id))
|
||||
session.add(PurchaseSpecification(purchase_id=purchase.id, specification_id=spec18.id))
|
||||
session.commit()
|
||||
|
||||
# Create Invoice
|
||||
invoice = Invoice(
|
||||
purchase_id=purchase.id,
|
||||
vendor_id=vendor.id,
|
||||
invoice_number="INV-145",
|
||||
invoice_date=date(2026, 10, 7),
|
||||
amount=150000.00
|
||||
)
|
||||
session.add(invoice)
|
||||
session.commit()
|
||||
|
||||
# Reload all for fresh IDs
|
||||
session.refresh(purchase)
|
||||
session.refresh(spec12)
|
||||
session.refresh(spec18)
|
||||
session.refresh(camera_module)
|
||||
session.refresh(agent_module)
|
||||
session.refresh(invoice)
|
||||
|
||||
return {
|
||||
'session': session,
|
||||
'project': project,
|
||||
'customer': customer,
|
||||
'vendor': vendor,
|
||||
'contract': contract,
|
||||
'spec12': spec12,
|
||||
'spec18': spec18,
|
||||
'purchase': purchase,
|
||||
'invoice': invoice,
|
||||
'camera_module': camera_module,
|
||||
'agent_module': agent_module,
|
||||
'purchase_item_camera': purchase_item_camera,
|
||||
'purchase_item_agent': purchase_item_agent,
|
||||
'spec12_camera': spec12_camera,
|
||||
'spec12_agent': spec12_agent,
|
||||
'spec18_camera': spec18_camera,
|
||||
'spec18_agent': spec18_agent,
|
||||
'service': ProcurementFlowService(session)
|
||||
}
|
||||
|
||||
|
||||
class TestPositiveScenarios:
|
||||
"""Positive test scenarios for receive_resource."""
|
||||
|
||||
def test_normal_receive_with_spec(self, full_setup):
|
||||
"""Обычный receive: Camera ×50 с Specification."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
spec12 = full_setup['spec12']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
# Receive Camera ×50 with Spec 12
|
||||
resource = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=50,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=spec12.id
|
||||
)
|
||||
|
||||
assert resource is not None
|
||||
assert resource.quantity == 50
|
||||
assert resource.module_id == camera_module.id
|
||||
assert resource.specification_id == spec12.id
|
||||
assert resource.invoice_id == invoice.id
|
||||
assert service.db.query(Resource).filter(Resource.id == resource.id).first() is not None
|
||||
|
||||
def test_partial_delivery_40_then_60(self, full_setup):
|
||||
"""Частичная поставка: 40 + 60 при ordered=100."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
# First partial delivery: 40
|
||||
resource1 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=40,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
assert resource1.quantity == 40
|
||||
|
||||
# Second partial delivery: 60
|
||||
resource2 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=60,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
assert resource2.quantity == 60
|
||||
|
||||
# Total received should be 100 = ordered
|
||||
total = service.get_received_quantity(purchase.id, camera_module.id)
|
||||
assert total == 100
|
||||
|
||||
def test_multiple_invoices_one_purchase(self, full_setup):
|
||||
"""Один Purchase → несколько Invoice; Resources разных Invoice учитываются."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
session = full_setup['session']
|
||||
|
||||
# Create second invoice
|
||||
invoice2 = Invoice(
|
||||
purchase_id=purchase.id,
|
||||
vendor_id=purchase.vendor_id,
|
||||
invoice_number="INV-146",
|
||||
invoice_date=date(2026, 10, 8),
|
||||
amount=75000.00
|
||||
)
|
||||
session.add(invoice2)
|
||||
session.commit()
|
||||
|
||||
# Receive from first invoice
|
||||
resource1 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=50,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
# Receive from second invoice
|
||||
resource2 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=30,
|
||||
invoice_id=invoice2.id
|
||||
)
|
||||
|
||||
# Total should be 80
|
||||
total = service.get_received_quantity(purchase.id, camera_module.id)
|
||||
assert total == 80
|
||||
|
||||
def test_multiple_resources_one_invoice(self, full_setup):
|
||||
"""Один Invoice → несколько Resource."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
# First Resource
|
||||
resource1 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=30,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
# Second Resource (same Invoice)
|
||||
resource2 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=40,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
assert resource1.id != resource2.id
|
||||
total = service.get_received_quantity(purchase.id, camera_module.id)
|
||||
assert total == 70
|
||||
|
||||
def test_one_purchase_multiple_specifications(self, full_setup):
|
||||
"""Один Purchase → несколько Specification; Resource разрешён для любой Specification."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
spec12 = full_setup['spec12']
|
||||
spec18 = full_setup['spec18']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
# Receive with Spec 12
|
||||
resource1 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=20,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=spec12.id
|
||||
)
|
||||
|
||||
# Receive with Spec 18 (different specification, same Purchase)
|
||||
resource2 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=30,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=spec18.id
|
||||
)
|
||||
|
||||
assert resource1.specification_id == spec12.id
|
||||
assert resource2.specification_id == spec18.id
|
||||
|
||||
|
||||
class TestNegativeScenarios:
|
||||
"""Negative test scenarios - should all fail with appropriate exceptions."""
|
||||
|
||||
def test_module_not_in_purchase(self, full_setup):
|
||||
"""Module отсутствует в PurchaseItem."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
session = full_setup['session']
|
||||
|
||||
# Create Camera module, but Purchase only has Camera and Agent
|
||||
# We'll try a module that doesn't exist in Purchase
|
||||
other_module = Module(name="OtherModule")
|
||||
session.add(other_module)
|
||||
session.commit()
|
||||
|
||||
with pytest.raises(ModuleNotInPurchaseError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=other_module.id,
|
||||
quantity=10,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
def test_ordered_exceeded(self, full_setup):
|
||||
"""Превышено ordered quantity для Module."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
# PurchaseItem: Camera ×100
|
||||
# Try to receive 101
|
||||
with pytest.raises(OrderedQuantityExceededError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=101,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
def test_ordered_exceeded_after_partial(self, full_setup):
|
||||
"""Превышение после частичной поставки."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
# First: receive 50
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=50,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
# Try to receive 60 more (total would be 110 > 100)
|
||||
with pytest.raises(OrderedQuantityExceededError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=60,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
def test_specification_not_in_project(self, full_setup):
|
||||
"""Specification не принадлежит Project."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
session = full_setup['session']
|
||||
|
||||
# Create other Project first
|
||||
other_project = Project(name="Other Project")
|
||||
session.add(other_project)
|
||||
session.commit()
|
||||
|
||||
# Then create Contract for it
|
||||
other_contract = Contract(project_id=other_project.id, contract_number="CTR-002")
|
||||
session.add(other_contract)
|
||||
session.commit()
|
||||
|
||||
# Now create Specification for other project
|
||||
other_spec = Specification(
|
||||
project_id=other_project.id,
|
||||
contract_id=other_contract.id,
|
||||
specification_number="OTHER-SPEC"
|
||||
)
|
||||
session.add(other_spec)
|
||||
session.commit()
|
||||
|
||||
with pytest.raises(ProjectMismatchError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=10,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=other_spec.id
|
||||
)
|
||||
|
||||
def test_specification_not_linked_to_purchase(self, full_setup):
|
||||
"""Specification не связана с Purchase через PurchaseSpecification."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
session = full_setup['session']
|
||||
|
||||
# Create Specification, but don't link it to Purchase
|
||||
other_contract = full_setup['contract']
|
||||
other_spec = Specification(
|
||||
project_id=purchase.project_id,
|
||||
contract_id=other_contract.id,
|
||||
specification_number="OTHER-SPEC"
|
||||
)
|
||||
session.add(other_spec)
|
||||
session.commit()
|
||||
|
||||
with pytest.raises(SpecificationNotLinkedToPurchaseError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=10,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=other_spec.id
|
||||
)
|
||||
|
||||
def test_specification_item_not_found(self, full_setup):
|
||||
"""SpecificationItem отсутствует для Module."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
spec12 = full_setup['spec12']
|
||||
camera_module = full_setup['camera_module']
|
||||
session = full_setup['session']
|
||||
|
||||
# First, increase PurchaseItem Camera to 1001 (so ordered check passes)
|
||||
purchase_item_camera = session.query(PurchaseItem).filter(
|
||||
PurchaseItem.purchase_id == purchase.id,
|
||||
PurchaseItem.module_id == camera_module.id
|
||||
).first()
|
||||
purchase_item_camera.quantity = 1001
|
||||
session.commit()
|
||||
|
||||
# Spec 12 has Camera item with quantity=1000
|
||||
# Let's try to receive a module that's NOT in Spec 12
|
||||
# Use Agent (which is in Spec 12), but we need something that's NOT there
|
||||
# Actually, Spec 12 has Camera and Agent, so we need to use a different module
|
||||
# Let's create Tracker module and try to use it with Spec 12
|
||||
tracker_module = Module(name="Tracker")
|
||||
session.add(tracker_module)
|
||||
session.commit()
|
||||
|
||||
# But Tracker is also not in PurchaseItem, so we need to add it first
|
||||
purchase_item_tracker = PurchaseItem(
|
||||
purchase_id=purchase.id,
|
||||
module_id=tracker_module.id,
|
||||
quantity=100,
|
||||
unit_price=200.00
|
||||
)
|
||||
session.add(purchase_item_tracker)
|
||||
session.commit()
|
||||
|
||||
# Now try to receive Tracker with Spec 12 (no Tracker in Spec 12)
|
||||
with pytest.raises(SpecificationItemNotFoundError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=tracker_module.id,
|
||||
quantity=10,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=full_setup['spec12'].id
|
||||
)
|
||||
|
||||
def test_specification_quantity_exceeded(self, full_setup):
|
||||
"""Превышено quantity SpecificationItem."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
session = full_setup['session']
|
||||
|
||||
# First, increase PurchaseItem Camera to 2000 (so ordered check passes)
|
||||
purchase_item_camera = session.query(PurchaseItem).filter(
|
||||
PurchaseItem.purchase_id == purchase.id,
|
||||
PurchaseItem.module_id == camera_module.id
|
||||
).first()
|
||||
purchase_item_camera.quantity = 2000
|
||||
session.commit()
|
||||
|
||||
# Now try to receive 1001 (Spec 12 Camera = 1000, so > 1000)
|
||||
with pytest.raises(SpecificationQuantityExceededError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=1001,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=full_setup['spec12'].id
|
||||
)
|
||||
|
||||
def test_specification_quantity_exceeded_after_partial(self, full_setup):
|
||||
"""Превышение Specification после частичной поставки."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
spec12 = full_setup['spec12']
|
||||
session = full_setup['session']
|
||||
|
||||
# First, increase PurchaseItem Camera to 2000
|
||||
purchase_item_camera = session.query(PurchaseItem).filter(
|
||||
PurchaseItem.purchase_id == purchase.id,
|
||||
PurchaseItem.module_id == camera_module.id
|
||||
).first()
|
||||
purchase_item_camera.quantity = 2000
|
||||
session.commit()
|
||||
|
||||
# Spec12 Camera = 1000
|
||||
# First receive 500
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=500,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=spec12.id
|
||||
)
|
||||
|
||||
# Try to receive 600 more (total would be 1100 > 1000)
|
||||
with pytest.raises(SpecificationQuantityExceededError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=600,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=spec12.id
|
||||
)
|
||||
|
||||
def test_invalid_quantity_zero(self, full_setup):
|
||||
"""quantity = 0."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
with pytest.raises(InvalidQuantityError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=0,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
def test_invalid_quantity_negative(self, full_setup):
|
||||
"""quantity < 0."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
with pytest.raises(InvalidQuantityError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=-10,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
def test_invoice_not_found(self, full_setup):
|
||||
"""Invoice не существует."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
with pytest.raises(InvoiceNotFoundError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=10,
|
||||
invoice_id=99999 # Non-existent
|
||||
)
|
||||
|
||||
def test_project_not_found(self, full_setup):
|
||||
"""Project не существует."""
|
||||
service = full_setup['service']
|
||||
camera_module = full_setup['camera_module']
|
||||
invoice = full_setup['invoice']
|
||||
|
||||
with pytest.raises(ProjectNotFoundError):
|
||||
service.receive_resource(
|
||||
project_id=99999, # Non-existent
|
||||
module_id=camera_module.id,
|
||||
quantity=10,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
def test_module_not_found(self, full_setup):
|
||||
"""Module не существует."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
|
||||
with pytest.raises(ModuleNotFoundError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=99999, # Non-existent
|
||||
quantity=10,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
def test_specification_not_found(self, full_setup):
|
||||
"""Specification не существует."""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
with pytest.raises(SpecificationNotFoundError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=10,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=99999 # Non-existent
|
||||
)
|
||||
|
||||
|
||||
class TestRegressionScenarios:
|
||||
"""Regression tests from Task 011."""
|
||||
|
||||
def test_over_fulfillment_prohibited_one_module(self, full_setup):
|
||||
"""
|
||||
Regression: Purchase Camera ×100, receive ×101.
|
||||
|
||||
Должно быть запрещено, несмотря на достаточную общую сумму Purchase.
|
||||
"""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
# Purchase: Camera ×100, Agent ×100
|
||||
# Try to receive Camera ×101
|
||||
with pytest.raises(OrderedQuantityExceededError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=101,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
def test_partial_delivery_40_plus_60(self, full_setup):
|
||||
"""
|
||||
Regression: Частичная поставка 40 + 60 при ordered=100.
|
||||
"""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
|
||||
# First: 40
|
||||
resource1 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=40,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
assert resource1.quantity == 40
|
||||
|
||||
# Second: 60 (total = 100 = ordered)
|
||||
resource2 = service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=60,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
assert resource2.quantity == 60
|
||||
|
||||
# Verify total
|
||||
total = service.get_received_quantity(purchase.id, camera_module.id)
|
||||
assert total == 100
|
||||
|
||||
def test_specification_over_fulfillment_prohibited(self, full_setup):
|
||||
"""
|
||||
Over-fulfillment Specification запрещён.
|
||||
"""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
spec12 = full_setup['spec12']
|
||||
camera_module = full_setup['camera_module']
|
||||
session = full_setup['session']
|
||||
|
||||
# First, increase PurchaseItem Camera to 2000 so we don't hit ordered limit
|
||||
purchase_item_camera = session.query(PurchaseItem).filter(
|
||||
PurchaseItem.purchase_id == purchase.id,
|
||||
PurchaseItem.module_id == camera_module.id
|
||||
).first()
|
||||
purchase_item_camera.quantity = 2000
|
||||
session.commit()
|
||||
|
||||
# Try to receive Camera ×1001 with Spec 12 (need = 1000)
|
||||
with pytest.raises(SpecificationQuantityExceededError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=1001,
|
||||
invoice_id=invoice.id,
|
||||
specification_id=full_setup['spec12'].id
|
||||
)
|
||||
|
||||
def test_transaction_atomicity_rollback(self, full_setup):
|
||||
"""
|
||||
При ошибке Resource не создаётся, транзакция откатывается.
|
||||
"""
|
||||
service = full_setup['service']
|
||||
purchase = full_setup['purchase']
|
||||
invoice = full_setup['invoice']
|
||||
camera_module = full_setup['camera_module']
|
||||
session = full_setup['session']
|
||||
|
||||
# Count before
|
||||
before_count = session.query(Resource).count()
|
||||
|
||||
# Try to receive 101 (should fail)
|
||||
with pytest.raises(OrderedQuantityExceededError):
|
||||
service.receive_resource(
|
||||
project_id=purchase.project_id,
|
||||
module_id=camera_module.id,
|
||||
quantity=101,
|
||||
invoice_id=invoice.id
|
||||
)
|
||||
|
||||
# Count after should be same (rollback)
|
||||
after_count = session.query(Resource).count()
|
||||
assert before_count == after_count
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main([__file__, '-v'])
|
||||
Reference in New Issue
Block a user