test_service_init.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import pytest
  2. from clean_python.fastapi import Resource
  3. from clean_python.fastapi import Service
  4. from clean_python.fastapi import v
  5. class V1Foo(Resource, version=v(1), name="foo"):
  6. pass
  7. class V1BetaFoo(V1Foo, version=v(1, "beta"), name="foo"):
  8. pass
  9. class V1AlphaFoo(V1BetaFoo, version=v(1, "alpha"), name="foo"):
  10. pass
  11. class V2AlphaFoo(Resource, version=v(2, "alpha"), name="foo"):
  12. pass
  13. @pytest.mark.parametrize(
  14. "resource_classes",
  15. [
  16. (V1AlphaFoo,),
  17. (V1BetaFoo, V1AlphaFoo),
  18. (V1Foo, V1BetaFoo, V1AlphaFoo),
  19. (V1AlphaFoo, V2AlphaFoo),
  20. ],
  21. )
  22. def test_service_init(resource_classes):
  23. resources = [cls() for cls in resource_classes]
  24. service = Service(*resources)
  25. assert set(service.resources) == set(resources)
  26. @pytest.mark.parametrize(
  27. "resource_classes,expected_versions",
  28. [
  29. ((V1BetaFoo,), {v(1, "beta"), v(1, "alpha")}),
  30. ((V1Foo,), {v(1), v(1, "beta"), v(1, "alpha")}),
  31. ],
  32. )
  33. def test_service_init_dynamic_gen(resource_classes, expected_versions):
  34. resources = [cls() for cls in resource_classes]
  35. service = Service(*resources)
  36. assert set(x.version for x in service.resources) == expected_versions