003-product_app.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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("./003-product_app.db")
  10. if db.is_file():
  11. db.unlink()
  12. drop_database()
  13. connection = sqlmodel.create_engine("sqlite:///003-product_app.db")
  14. sqlmodel.SQLModel.metadata.create_all(connection)
  15. app = assets.product_app(connection)
  16. print("App initialized.")
  17. create = app.create({
  18. "barcode": "123456789012",
  19. "name": "Sample",
  20. "description": "This is sample name.",
  21. "author": "John Snow",
  22. "image": "https://uuu.owo.pl",
  23. "stock_count": "10"
  24. })
  25. print("Create:")
  26. print(create)
  27. print()
  28. select = app.get_barcode("123456789012")
  29. print("Select by barcode:")
  30. print(select)
  31. print()
  32. select = app.get_name("Sample")
  33. print("Select by name:")
  34. print(select)
  35. print()
  36. create = app.create({
  37. "barcode": "210987654321",
  38. "name": "Second sample",
  39. "description": "This is sample item second.",
  40. "author": "other",
  41. "image": "https://test.pl",
  42. "stock_count": "20"
  43. })
  44. print("Create second:")
  45. print(create)
  46. print()
  47. alls = app.all()
  48. print("Select all:")
  49. print(alls)
  50. print()
  51. drop_database()