fixup! WIP
This commit is contained in:
@@ -56,8 +56,6 @@ _EXPECTED_FIELDS: dict[TransactionType, set[str]] = {
|
|||||||
TransactionType.ADJUST_INTEREST: {"interest_rate_percent"},
|
TransactionType.ADJUST_INTEREST: {"interest_rate_percent"},
|
||||||
TransactionType.ADJUST_PENALTY: {"penalty_multiplier_percent", "penalty_threshold"},
|
TransactionType.ADJUST_PENALTY: {"penalty_multiplier_percent", "penalty_threshold"},
|
||||||
TransactionType.ADJUST_STOCK: {"product_count", "product_id"},
|
TransactionType.ADJUST_STOCK: {"product_count", "product_id"},
|
||||||
# TODO: remove amount from BUY_PRODUCT
|
|
||||||
# this requires modifications to user credit calculations
|
|
||||||
TransactionType.BUY_PRODUCT: {"product_count", "product_id"},
|
TransactionType.BUY_PRODUCT: {"product_count", "product_id"},
|
||||||
TransactionType.TRANSFER: {"amount", "transfer_user_id"},
|
TransactionType.TRANSFER: {"amount", "transfer_user_id"},
|
||||||
}
|
}
|
||||||
@@ -91,6 +89,10 @@ class Transaction(Base):
|
|||||||
_transaction_type_field_constraints(transaction_type, expected_fields)
|
_transaction_type_field_constraints(transaction_type, expected_fields)
|
||||||
for transaction_type, expected_fields in _EXPECTED_FIELDS.items()
|
for transaction_type, expected_fields in _EXPECTED_FIELDS.items()
|
||||||
],
|
],
|
||||||
|
CheckConstraint(
|
||||||
|
f"type <> '{TransactionType.TRANSFER}' OR user_id <> transfer_user_id",
|
||||||
|
name="trx_type_transfer_no_self_transfers",
|
||||||
|
),
|
||||||
# Speed up product count calculation
|
# Speed up product count calculation
|
||||||
Index("product_user_time", "product_id", "user_id", "time"),
|
Index("product_user_time", "product_id", "user_id", "time"),
|
||||||
# Speed up product owner calculation
|
# Speed up product owner calculation
|
||||||
|
@@ -40,3 +40,81 @@ def test_transaction_no_duplicate_timestamps(sql_session: Session):
|
|||||||
|
|
||||||
with pytest.raises(IntegrityError):
|
with pytest.raises(IntegrityError):
|
||||||
sql_session.commit()
|
sql_session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def test_user_not_allowed_to_transfer_to_self(sql_session: Session) -> None:
|
||||||
|
user, _ = insert_test_data(sql_session)
|
||||||
|
|
||||||
|
transaction = Transaction.transfer(
|
||||||
|
time=datetime(2023, 10, 1, 12, 0, 0),
|
||||||
|
user_id=user.id,
|
||||||
|
transfer_user_id=user.id,
|
||||||
|
amount=50,
|
||||||
|
)
|
||||||
|
|
||||||
|
sql_session.add(transaction)
|
||||||
|
|
||||||
|
with pytest.raises(IntegrityError):
|
||||||
|
sql_session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def test_product_foreign_key_constraint(sql_session: Session) -> None:
|
||||||
|
user, product = insert_test_data(sql_session)
|
||||||
|
|
||||||
|
transaction = Transaction.add_product(
|
||||||
|
time=datetime(2023, 10, 1, 12, 0, 0),
|
||||||
|
user_id=user.id,
|
||||||
|
product_id=product.id,
|
||||||
|
amount=27,
|
||||||
|
per_product=27,
|
||||||
|
product_count=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
sql_session.add(transaction)
|
||||||
|
sql_session.commit()
|
||||||
|
|
||||||
|
# Attempt to add a transaction with a non-existent product
|
||||||
|
invalid_transaction = Transaction.add_product(
|
||||||
|
time=datetime(2023, 10, 1, 12, 0, 1),
|
||||||
|
user_id=user.id,
|
||||||
|
product_id=9999, # Non-existent product ID
|
||||||
|
amount=27,
|
||||||
|
per_product=27,
|
||||||
|
product_count=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
sql_session.add(invalid_transaction)
|
||||||
|
|
||||||
|
with pytest.raises(IntegrityError):
|
||||||
|
sql_session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def test_user_foreign_key_constraint(sql_session: Session) -> None:
|
||||||
|
user, product = insert_test_data(sql_session)
|
||||||
|
|
||||||
|
transaction = Transaction.add_product(
|
||||||
|
time=datetime(2023, 10, 1, 12, 0, 0),
|
||||||
|
user_id=user.id,
|
||||||
|
product_id=product.id,
|
||||||
|
amount=27,
|
||||||
|
per_product=27,
|
||||||
|
product_count=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
sql_session.add(transaction)
|
||||||
|
sql_session.commit()
|
||||||
|
|
||||||
|
# Attempt to add a transaction with a non-existent user
|
||||||
|
invalid_transaction = Transaction.add_product(
|
||||||
|
time=datetime(2023, 10, 1, 12, 0, 1),
|
||||||
|
user_id=9999, # Non-existent user ID
|
||||||
|
product_id=product.id,
|
||||||
|
amount=27,
|
||||||
|
per_product=27,
|
||||||
|
product_count=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
sql_session.add(invalid_transaction)
|
||||||
|
|
||||||
|
with pytest.raises(IntegrityError):
|
||||||
|
sql_session.commit()
|
||||||
|
@@ -1,12 +0,0 @@
|
|||||||
from sqlalchemy.orm import Session
|
|
||||||
|
|
||||||
|
|
||||||
def test_user_not_allowed_to_transfer_to_self(sql_session: Session) -> None:
|
|
||||||
raise NotImplementedError("This test is not implemented yet.")
|
|
||||||
# insert_test_data(sql_session)
|
|
||||||
# ...
|
|
||||||
|
|
||||||
# user1 = sql_session.scalars(select(User).where(User.name == "Test User 1")).one()
|
|
||||||
|
|
||||||
# with pytest.raises(ValueError, match="Cannot transfer to self"):
|
|
||||||
# user1.transfer(sql_session, user1, 10) # Attempting to transfer to self
|
|
Reference in New Issue
Block a user