| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 3 |
# |
| 4 |
# License: ISC |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# . |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 10 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 14 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 |
# PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
"""Smoke tests for the codesearch-script test harness (tests/dcs_helpers.py).""" |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import urllib.error |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
from tests.dcs_helpers import ( |
| 26 |
FakeResponse, |
| 27 |
FakeTransport, |
| 28 |
load_script, |
| 29 |
make_http_error, |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
def test_load_script_loads_hyphenated_rank_module() -> None: |
| 34 |
module = load_script("rank-license-usage-codesearch.py") |
| 35 |
# The importlib path resolved the script's own decopy / sibling imports. |
| 36 |
assert hasattr(module, "DcsClient") |
| 37 |
assert hasattr(module, "Candidate") |
| 38 |
assert callable(module.fetch_results) |
| 39 |
|
| 40 |
|
| 41 |
def test_fake_response_read_truncates_to_amt() -> None: |
| 42 |
resp = FakeResponse(b"abcdef") |
| 43 |
assert resp.read() == b"abcdef" |
| 44 |
assert resp.read(3) == b"abc" |
| 45 |
|
| 46 |
|
| 47 |
def test_fake_transport_returns_bytes_as_response() -> None: |
| 48 |
transport = FakeTransport([b"payload"]) |
| 49 |
with transport("ignored-request") as resp: |
| 50 |
assert resp.read() == b"payload" |
| 51 |
assert transport.calls == 1 |
| 52 |
|
| 53 |
|
| 54 |
def test_fake_transport_raises_queued_exception() -> None: |
| 55 |
boom = TimeoutError("stalled") |
| 56 |
transport = FakeTransport([boom]) |
| 57 |
with pytest.raises(TimeoutError): |
| 58 |
transport("ignored-request") |
| 59 |
assert transport.calls == 1 |
| 60 |
|
| 61 |
|
| 62 |
def test_fake_transport_exhausted_without_repeat_is_a_test_error() -> None: |
| 63 |
transport = FakeTransport([b"once"]) |
| 64 |
transport("first") |
| 65 |
with pytest.raises(AssertionError): |
| 66 |
transport("second") |
| 67 |
|
| 68 |
|
| 69 |
def test_fake_transport_repeat_reuses_last_step() -> None: |
| 70 |
expected_calls = 3 |
| 71 |
transport = FakeTransport([make_http_error(503)], repeat=True) |
| 72 |
for _ in range(expected_calls): |
| 73 |
with pytest.raises(urllib.error.HTTPError): |
| 74 |
transport("request") |
| 75 |
assert transport.calls == expected_calls |
| 76 |
|
| 77 |
|
| 78 |
def test_make_http_error_carries_code_retry_after_and_body() -> None: |
| 79 |
too_many_requests = 429 |
| 80 |
exc = make_http_error(too_many_requests, retry_after=7, body=b"slow down") |
| 81 |
assert exc.code == too_many_requests |
| 82 |
assert exc.headers.get("Retry-After") == "7" |
| 83 |
assert exc.read() == b"slow down" |