test_schemas.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from app.models.schemas import VisitRequest, WaitUntilValue, ArasResponse
  2. from pydantic import ValidationError
  3. import pytest
  4. class TestWaitUntilValue:
  5. def test_valid_values(self):
  6. for val in ["load", "domcontentloaded", "networkidle", "commit"]:
  7. assert val in WaitUntilValue.__args__
  8. def test_invalid_value_raises_error(self):
  9. with pytest.raises(ValidationError):
  10. VisitRequest(url="https://example.com", waitUntil="invalid")
  11. class TestVisitRequest:
  12. def test_minimal_request(self):
  13. req = VisitRequest(url="https://example.com")
  14. assert str(req.url) == "https://example.com/"
  15. assert req.screenshot is False
  16. assert req.waitUntil == "load"
  17. assert req.proxy is None
  18. assert req.proxyAuth is None
  19. assert req.incognito is False
  20. def test_full_request(self):
  21. req = VisitRequest(
  22. url="https://example.com",
  23. screenshot=True,
  24. waitUntil="networkidle",
  25. proxy="http://proxy:8080",
  26. proxyAuth={"username": "user", "password": "pass"},
  27. incognito=True,
  28. )
  29. assert req.screenshot is True
  30. assert req.waitUntil == "networkidle"
  31. assert req.proxy == "http://proxy:8080"
  32. assert req.proxyAuth["username"] == "user"
  33. assert req.incognito is True
  34. def test_invalid_url_raises_error(self):
  35. with pytest.raises(ValidationError):
  36. VisitRequest(url="not-a-url")
  37. def test_all_waituntil_values(self):
  38. for val in ["load", "domcontentloaded", "networkidle", "commit"]:
  39. req = VisitRequest(url="https://example.com", waitUntil=val)
  40. assert req.waitUntil == val
  41. class TestArasResponse:
  42. def test_minimal_response(self):
  43. resp = ArasResponse(status="OK", message="test", data=None)
  44. assert resp.status == "OK"
  45. assert resp.message == "test"
  46. assert resp.data is None
  47. assert resp.extra is None
  48. assert resp.code == "UNKNOWN_CODE"
  49. def test_full_response(self):
  50. resp = ArasResponse(
  51. status="OK",
  52. message="test",
  53. data={"key": "value"},
  54. extra={"extra": "data"},
  55. code="CUSTOM_CODE",
  56. )
  57. assert resp.data == {"key": "value"}
  58. assert resp.extra == {"extra": "data"}
  59. assert resp.code == "CUSTOM_CODE"