test_int_sync_api_provider.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # This module is a copy paste of test_int_api_provider.py
  2. from http import HTTPStatus
  3. import pytest
  4. from clean_python import ctx
  5. from clean_python import Tenant
  6. from clean_python.api_client import ApiException
  7. from clean_python.api_client import FileFormPost
  8. from clean_python.api_client import SyncApiProvider
  9. @pytest.fixture
  10. def provider(fastapi_example_app) -> SyncApiProvider:
  11. ctx.tenant = Tenant(id=2, name="")
  12. yield SyncApiProvider(
  13. fastapi_example_app + "/", lambda: {"Authorization": "Bearer token"}
  14. )
  15. ctx.tenant = None
  16. def test_request_params(provider: SyncApiProvider):
  17. response = provider.request("GET", "v1/books", params={"limit": 10, "offset": 2})
  18. assert isinstance(response, dict)
  19. assert response["limit"] == 10
  20. assert response["offset"] == 2
  21. def test_request_json_body(provider: SyncApiProvider):
  22. response = provider.request(
  23. "POST", "v1/books", json={"title": "test_body", "author": {"name": "foo"}}
  24. )
  25. assert isinstance(response, dict)
  26. assert response["title"] == "test_body"
  27. assert response["author"] == {"name": "foo"}
  28. def test_request_form_body(provider: SyncApiProvider):
  29. response = provider.request("POST", "v1/form", fields={"name": "foo"})
  30. assert isinstance(response, dict)
  31. assert response["name"] == "foo"
  32. def test_request_form_file(provider: SyncApiProvider):
  33. response = provider.request(
  34. "POST",
  35. "v1/file",
  36. fields={"description": "bla"},
  37. file=FileFormPost(file_name="x.txt", file=b"foo"),
  38. )
  39. assert isinstance(response, dict)
  40. assert response == {"x.txt": "foo", "description": "bla"}
  41. @pytest.fixture
  42. def book(provider: SyncApiProvider):
  43. return provider.request(
  44. "POST", "v1/books", json={"title": "fixture", "author": {"name": "foo"}}
  45. )
  46. def test_no_content(provider: SyncApiProvider, book):
  47. response = provider.request("DELETE", f"v1/books/{book['id']}")
  48. assert response is None
  49. def test_not_found(provider: SyncApiProvider):
  50. with pytest.raises(ApiException) as e:
  51. provider.request("GET", "v1/book")
  52. assert e.value.status is HTTPStatus.NOT_FOUND
  53. assert e.value.args[0] == {"detail": "Not Found"}
  54. def test_bad_request(provider: SyncApiProvider):
  55. with pytest.raises(ApiException) as e:
  56. provider.request("GET", "v1/books", params={"limit": "foo"})
  57. assert e.value.status is HTTPStatus.BAD_REQUEST
  58. assert e.value.args[0]["detail"][0]["loc"] == ["query", "limit"]
  59. def test_no_json_response(provider: SyncApiProvider):
  60. with pytest.raises(ApiException) as e:
  61. provider.request("GET", "v1/text")
  62. assert e.value.args[0] == "Unexpected content type 'text/plain; charset=utf-8'"
  63. def test_urlencode(provider: SyncApiProvider):
  64. response = provider.request("PUT", "v1/urlencode/x?")
  65. assert isinstance(response, dict)
  66. assert response["name"] == "x?"
  67. def test_request_raw(provider: SyncApiProvider, book):
  68. response = provider.request_raw("GET", f"v1/books/{book['id']}")
  69. assert response.status is HTTPStatus.OK
  70. assert len(response.data) > 0
  71. assert response.content_type == "application/json"