009-reservation.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import pathlib
  2. current = pathlib.Path(__file__).parent
  3. root = current.parent
  4. import sys
  5. sys.path.append(str(root))
  6. import assets
  7. import sqlmodel
  8. def drop_database() -> None:
  9. db = pathlib.Path("./009-reservation.db")
  10. if db.is_file():
  11. db.unlink()
  12. drop_database()
  13. connection = sqlmodel.create_engine("sqlite:///009-reservation.db")
  14. sqlmodel.SQLModel.metadata.create_all(connection)
  15. builder = assets.product_factory()
  16. builder.name = "Sample name"
  17. builder.author = "Simple UwU Artist"
  18. builder.description = "This is simple description"
  19. builder.barcode = "123456789012"
  20. builder.stock_count = 10
  21. product = builder.result
  22. print("Create first product:")
  23. print(product)
  24. print()
  25. with assets.product_loader(connection) as loader:
  26. print("Inserting product...")
  27. loader.store(product)
  28. print("Inserted:")
  29. print(product)
  30. print()
  31. with assets.product_loader(connection) as loader:
  32. print("Loading product to make reservation...")
  33. target = loader.get_by_barcode("123456789012")
  34. print("Loaded: ")
  35. print(target)
  36. before = target.on_stock
  37. reservation = assets \
  38. .reservation_factory() \
  39. .target(target) \
  40. .email("[email protected]") \
  41. .result()
  42. print(reservation)
  43. print()
  44. with assets.reservation_loader(connection) as loader:
  45. print("Inserting it into database...")
  46. loader.store(reservation)
  47. print(reservation)
  48. with assets.product_loader(connection) as loader:
  49. print("Loading product to make reservation...")
  50. target = loader.get_by_barcode("123456789012")
  51. print("Loaded: ")
  52. print(target)
  53. reservation = assets \
  54. .reservation_factory() \
  55. .target(target) \
  56. .email("[email protected]") \
  57. .result()
  58. print(reservation)
  59. print()
  60. with assets.reservation_loader(connection) as loader:
  61. print("Inserting it into database...")
  62. loader.store(reservation)
  63. print(reservation)
  64. with assets.product_loader(connection) as loader:
  65. print("Loading product to check that on stock calc work...")
  66. target = loader.get_by_barcode("123456789012")
  67. after = target.on_stock
  68. print("Before: " + str(before))
  69. print("After: " + str(after))
  70. print()
  71. before = after
  72. with assets.reservation_loader(connection) as loader:
  73. print("Loading reservation for product...")
  74. collection = loader \
  75. .get_by_target(target) \
  76. .by_email("[email protected]") \
  77. .results()
  78. if len(collection) != 1:
  79. print("Fail, collection:")
  80. print(collection)
  81. raise Exception("Collection is not propertly filtered.")
  82. reservation = collection[0]
  83. print("Loaded.")
  84. print(reservation)
  85. print()
  86. print("Removing it from database...")
  87. loader.drop(reservation)
  88. print("Removed.")
  89. print()
  90. with assets.product_loader(connection) as loader:
  91. print("Loading product to check that on stock calc work...")
  92. target = loader.get_by_barcode("123456789012")
  93. after = target.on_stock
  94. print("Before: " + str(before))
  95. print("After: " + str(after))
  96. print()
  97. print("Trying to drop product...")
  98. loader.drop(target)
  99. print("Removed.")
  100. drop_database()