009-attachment.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import sys
  2. import pathlib
  3. test_file = pathlib.Path(__file__)
  4. project = test_file.parent.parent
  5. sys.path.append(str(project))
  6. import server_source as source
  7. from test import test
  8. import asyncio
  9. import tortoise
  10. def prepare_dir() -> pathlib.Path:
  11. test = pathlib.Path(__file__).parent
  12. resources = test / pathlib.Path("test_resources")
  13. if not resources.exists() or not resources.is_dir():
  14. resources.mkdir()
  15. for count in resources.iterdir():
  16. if count.is_file():
  17. count.unlink()
  18. return resources
  19. async def main():
  20. modules = {
  21. source.model.Meta.app: [ "server_source" ]
  22. }
  23. await tortoise.Tortoise.init(
  24. db_url = "sqlite://:memory:",
  25. modules = modules
  26. )
  27. await tortoise.Tortoise.generate_schemas()
  28. content = "IyBVd1UKICogRmlyc3QgcG9pbnQKICogU2Vjb25kIHBvaW50Cg=="
  29. manager = source.attachments_manager(prepare_dir())
  30. proxy = await manager.upload(content, "md")
  31. attachment = proxy \
  32. .set_name("sample_file") \
  33. .set_description("This describe it.") \
  34. .result()
  35. print("Attachment before insert to DB:")
  36. print(repr(attachment))
  37. print()
  38. await attachment.save()
  39. print("Attachment after inserting to DB:")
  40. print(repr(attachment))
  41. print()
  42. readed = await manager.restore(attachment).load()
  43. print("Data loaded from Base64:")
  44. print("\"\"\"")
  45. print(readed.decode("UTF-8"))
  46. print("\"\"\"")
  47. print()
  48. await tortoise.Tortoise.close_connections()
  49. asyncio.run(main())