009-attachment.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import sys
  2. import pathlib
  3. test_file = pathlib.Path(__file__)
  4. test_dir = test_file.parent
  5. project = test_dir.parent
  6. sys.path.append(str(project))
  7. import server_source as source
  8. from test import test
  9. import asyncio
  10. import tortoise
  11. def prepare_dir() -> pathlib.Path:
  12. test = pathlib.Path(__file__).parent
  13. resources = test / pathlib.Path("test_resources")
  14. if not resources.exists() or not resources.is_dir():
  15. resources.mkdir()
  16. for count in resources.iterdir():
  17. if count.is_file():
  18. count.unlink()
  19. return resources
  20. async def main():
  21. global test_dir
  22. modules = {
  23. source.model.Meta.app: [ "server_source" ]
  24. }
  25. await tortoise.Tortoise.init(
  26. db_url = "sqlite://:memory:",
  27. modules = modules
  28. )
  29. await tortoise.Tortoise.generate_schemas()
  30. content = "IyBVd1UKICogRmlyc3QgcG9pbnQKICogU2Vjb25kIHBvaW50Cg=="
  31. decoded = await source.encoded(content).decode()
  32. decoded = decoded.result()
  33. proxy = await source.attachments_directory(test_dir).store(decoded, "txt")
  34. proxy.set_name("Attach")
  35. proxy.set_description("That is it.")
  36. result = proxy.result()
  37. await result.save()
  38. in_file = await source.attachments_directory(test_dir).remove(result)
  39. test(in_file, decoded)
  40. await tortoise.Tortoise.close_connections()
  41. asyncio.run(main())