003-product_app.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. users = assets.users_collection()
  16. factory = assets.user_factory()
  17. factory.nick = "test"
  18. factory.password = "12345678"
  19. users.add(factory.result)
  20. app = assets.product_app(connection, users)
  21. print("App initialized.")
  22. create = app.create({
  23. "barcode": "123456789012",
  24. "name": "Sample",
  25. "description": "This is sample name.",
  26. "author": "John Snow",
  27. "image": "https://uuu.owo.pl",
  28. "stock_count": "10",
  29. "apikey": factory.apikey
  30. })
  31. print("Create:")
  32. print(create)
  33. print()
  34. select = app.get_barcode("123456789012")
  35. print("Select by barcode:")
  36. print(select)
  37. print()
  38. select = app.get_name("Sample")
  39. print("Select by name:")
  40. print(select)
  41. print()
  42. create = app.create({
  43. "barcode": "210987654321",
  44. "name": "Second sample",
  45. "description": "This is sample item second.",
  46. "author": "other",
  47. "image": "https://test.pl",
  48. "stock_count": "20",
  49. "apikey": factory.apikey
  50. })
  51. print("Create second:")
  52. print(create)
  53. print()
  54. alls = app.all()
  55. print("Select all:")
  56. print(alls)
  57. print()
  58. modify = app.update({
  59. "barcode": "210987654321",
  60. "target_barcode": "210987654321",
  61. "name": "Second sample",
  62. "description": "This is sample item second.",
  63. "author": "other",
  64. "image": "https://test.pl",
  65. "stock_count": "20",
  66. "apikey": factory.apikey
  67. })
  68. print("Modify:")
  69. print(modify)
  70. print()
  71. drop_database()