test_context.py 908 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import asyncio
  2. import os
  3. from pydantic import HttpUrl
  4. from clean_python import ctx
  5. from clean_python import Tenant
  6. from clean_python import User
  7. def test_default_context():
  8. assert str(ctx.path) == "file://" + os.getcwd()
  9. assert ctx.user.id == "ANONYMOUS"
  10. assert ctx.user.name == "anonymous"
  11. assert ctx.tenant is None
  12. async def test_task_isolation():
  13. async def get_set(user):
  14. ctx.user = user
  15. await asyncio.sleep(0.01)
  16. assert ctx.user == user
  17. await asyncio.gather(*[get_set(User(id=str(i), name="piet")) for i in range(10)])
  18. assert ctx.user.id == "ANONYMOUS"
  19. async def test_tenant():
  20. tenant = Tenant(id=2, name="foo")
  21. ctx.tenant = tenant
  22. assert ctx.tenant == tenant
  23. ctx.tenant = None
  24. assert ctx.tenant is None
  25. async def test_path():
  26. url = HttpUrl("http://testserver/foo?a=b")
  27. ctx.path = url
  28. assert ctx.path == url