From 40744e44bad9cb3be65bd52408d2c0682b9efc25 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Wed, 10 Dec 2025 16:02:05 +0900 Subject: [PATCH] seed_test_data: use new queries --- dibbler/subcommands/seed_test_data.py | 138 +++++++++++++++++++------- 1 file changed, 103 insertions(+), 35 deletions(-) diff --git a/dibbler/subcommands/seed_test_data.py b/dibbler/subcommands/seed_test_data.py index 07454ea..87c5fcc 100644 --- a/dibbler/subcommands/seed_test_data.py +++ b/dibbler/subcommands/seed_test_data.py @@ -1,48 +1,116 @@ -import json -from dibbler.db import Session - +from datetime import datetime from pathlib import Path -from dibbler.models.Product import Product - -from dibbler.models.User import User +from dibbler.db import Session +from dibbler.models import Product, Transaction, User +from dibbler.queries import joint_buy_product JSON_FILE = Path(__file__).parent.parent.parent / "mock_data.json" -def clear_db(session): - session.query(Product).delete() - session.query(User).delete() - session.commit() +# TODO: integrate this as a part of create-db, either asking interactively +# whether to seed test data, or by using command line arguments for +# automatating the answer. + + +def clear_db(sql_session): + sql_session.query(Product).delete() + sql_session.query(User).delete() + sql_session.commit() def main(): - session = Session() - clear_db(session) - product_items = [] - user_items = [] + # TODO: There is some leftover json data in the mock_data.json file. + # It should be dealt with before merging this PR, either by removing + # it or using it here. + sql_session = Session() + clear_db(sql_session) - with open(JSON_FILE) as f: - json_obj = json.load(f) + # Add users + user1 = User("Test User 1") + user2 = User("Test User 2") + user3 = User("Test User 3") - for product in json_obj["products"]: - product_item = Product( - bar_code=product["bar_code"], - name=product["name"], - price=product["price"], - stock=product["stock"], - ) - product_items.append(product_item) + sql_session.add(user1) + sql_session.add(user2) + sql_session.add(user3) + sql_session.commit() - for user in json_obj["users"]: - user_item = User( - name=user["name"], - card=user["card"], - rfid=user["rfid"], - credit=user["credit"], - ) - user_items.append(user_item) + # Add products + product1 = Product("1234567890123", "Test Product 1") + product2 = Product("9876543210987", "Test Product 2") + sql_session.add(product1) + sql_session.add(product2) + sql_session.commit() - session.add_all(product_items) - session.add_all(user_items) - session.commit() + # Add transactions + transactions = [ + Transaction.adjust_balance( + time=datetime(2023, 10, 1, 10, 0, 0), + amount=100, + user_id=user1.id, + ), + Transaction.adjust_balance( + time=datetime(2023, 10, 1, 10, 0, 1), + amount=50, + user_id=user2.id, + ), + Transaction.adjust_balance( + time=datetime(2023, 10, 1, 10, 0, 2), + amount=-50, + user_id=user1.id, + ), + Transaction.add_product( + time=datetime(2023, 10, 1, 12, 0, 0), + amount=27 * 2, + per_product=27, + product_count=2, + user_id=user1.id, + product_id=product1.id, + ), + Transaction.buy_product( + time=datetime(2023, 10, 1, 12, 0, 1), + product_count=1, + user_id=user2.id, + product_id=product1.id, + ), + ] + + sql_session.add_all(transactions) + sql_session.flush() + + joint_buy_product( + sql_session, + time=datetime(2023, 10, 1, 12, 0, 2), + instigator=user1, + product_count=1, + users=[user1, user2, user3], + product=product2, + ) + + joint_buy_product( + sql_session, + time=datetime(2023, 10, 1, 13, 0, 2), + instigator=user3, + product_count=2, + users=[user2, user3], + product=product2, + ) + + transactions = [ + Transaction.buy_product( + time=datetime(2023, 10, 2, 14, 0, 0), + product_count=1, + user_id=user1.id, + product_id=product1.id, + ), + Transaction.buy_product( + time=datetime(2023, 10, 2, 14, 0, 1), + product_count=1, + user_id=user2.id, + product_id=product2.id, + ), + ] + + sql_session.add_all(transactions) + sql_session.commit()