test_schemas.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.waitTime == 0
  18. assert req.proxy is None
  19. assert req.proxyAuth is None
  20. assert req.incognito is False
  21. assert req.screenshotSelector is None
  22. assert req.screenshotDelay == 0
  23. assert req.clickSelector is None
  24. assert req.selectorTimeout == 1000
  25. def test_full_request(self):
  26. req = VisitRequest(
  27. url="https://example.com",
  28. screenshot=True,
  29. waitUntil="networkidle",
  30. waitTime=10,
  31. proxy="http://proxy:8080",
  32. proxyAuth={"username": "user", "password": "pass"},
  33. incognito=True,
  34. screenshotSelector="#product",
  35. screenshotDelay=500,
  36. clickSelector="button#load-more",
  37. selectorTimeout=2000,
  38. )
  39. assert req.screenshot is True
  40. assert req.waitUntil == "networkidle"
  41. assert req.waitTime == 10
  42. assert req.proxy == "http://proxy:8080"
  43. assert req.proxyAuth["username"] == "user"
  44. assert req.incognito is True
  45. assert req.screenshotSelector == "#product"
  46. assert req.screenshotDelay == 500
  47. assert req.clickSelector == "button#load-more"
  48. assert req.selectorTimeout == 2000
  49. def test_screenshot_delay_boundary_values(self):
  50. VisitRequest(url="https://example.com", screenshotDelay=0)
  51. VisitRequest(url="https://example.com", screenshotDelay=60000)
  52. def test_screenshot_delay_invalid_values(self):
  53. with pytest.raises(ValidationError):
  54. VisitRequest(url="https://example.com", screenshotDelay=-1)
  55. with pytest.raises(ValidationError):
  56. VisitRequest(url="https://example.com", screenshotDelay=60001)
  57. def test_selector_timeout_boundary_values(self):
  58. VisitRequest(url="https://example.com", selectorTimeout=100)
  59. VisitRequest(url="https://example.com", selectorTimeout=30000)
  60. def test_selector_timeout_invalid_values(self):
  61. with pytest.raises(ValidationError):
  62. VisitRequest(url="https://example.com", selectorTimeout=99)
  63. with pytest.raises(ValidationError):
  64. VisitRequest(url="https://example.com", selectorTimeout=30001)
  65. def test_wait_time_negative_rejected(self):
  66. with pytest.raises(ValidationError):
  67. VisitRequest(url="https://example.com", waitTime=-1)
  68. def test_wait_time_above_max_rejected(self):
  69. with pytest.raises(ValidationError):
  70. VisitRequest(url="https://example.com", waitTime=61)
  71. def test_wait_time_boundary_values(self):
  72. # Both 0 and 60 should be valid
  73. VisitRequest(url="https://example.com", waitTime=0)
  74. VisitRequest(url="https://example.com", waitTime=60)
  75. def test_invalid_url_raises_error(self):
  76. with pytest.raises(ValidationError):
  77. VisitRequest(url="not-a-url")
  78. def test_all_waituntil_values(self):
  79. for val in ["load", "domcontentloaded", "networkidle", "commit"]:
  80. req = VisitRequest(url="https://example.com", waitUntil=val)
  81. assert req.waitUntil == val
  82. class TestArasResponse:
  83. def test_minimal_response(self):
  84. resp = ArasResponse(status="OK", message="test", data=None)
  85. assert resp.status == "OK"
  86. assert resp.message == "test"
  87. assert resp.data is None
  88. assert resp.extra is None
  89. assert resp.code == "UNKNOWN_CODE"
  90. def test_full_response(self):
  91. resp = ArasResponse(
  92. status="OK",
  93. message="test",
  94. data={"key": "value"},
  95. extra={"extra": "data"},
  96. code="CUSTOM_CODE",
  97. )
  98. assert resp.data == {"key": "value"}
  99. assert resp.extra == {"extra": "data"}
  100. assert resp.code == "CUSTOM_CODE"