Skip to content

Commit c242ae7

Browse files
feat(agent): say when a test key could not confirm a typed code
1 parent 00a9cec commit c242ae7

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

‎agent/src/ng_address/resolve.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
LOCATION_RADIUS_M = 50.0
3232
MAX_QUERIES = 3
3333
MAX_QUERY_CHARS = 200
34+
TEST_KEY = " It was asked with a test key, which may not see assigned codes."
3435
NO_NIPOST = "NG_POSTCODE_API_KEY is not set, so NIPOST cannot be asked for the postcode."
3536

3637

@@ -74,10 +75,11 @@ async def _assigned(self, code: Postcode) -> tuple[bool | None, str]:
7475
result = await self.nipost.send(lookup(code, 1))
7576
if isinstance(result, ApiError | TransportError):
7677
return None, f"Not confirmed with NIPOST: {result}."
77-
note = (
78-
"NIPOST confirms it is assigned." if result.valid else "NIPOST says it is not assigned."
79-
)
80-
return result.valid, note
78+
if result.valid:
79+
return True, "NIPOST confirms it is assigned."
80+
# An older client does not say what kind of key it holds.
81+
test = getattr(self.nipost, "test_key", False)
82+
return False, "NIPOST says it is not assigned." + TEST_KEY * test
8183

8284
async def _by_location(self, at: Coordinate) -> Resolution:
8385
if self.nipost is None:

‎agent/tests/test_resolve.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def anyio_backend() -> str:
2929
return "asyncio"
3030

3131

32-
def nipost(distance_m: float = 6.0, *, valid: bool = True, found: bool = True) -> AsyncClient:
32+
def nipost(
33+
distance_m: float = 6.0, *, valid: bool = True, found: bool = True, key: str = "key"
34+
) -> AsyncClient:
3335
def handle(request: httpx.Request) -> httpx.Response:
3436
if request.url.path == "/v1/lookup":
3537
return httpx.Response(
@@ -48,7 +50,7 @@ def handle(request: httpx.Request) -> httpx.Response:
4850
data |= {"unit": unit, "area": "EK-01-A03-FK", "district": "EK-01-A03", "state": "EK"}
4951
return httpx.Response(200, json={"data": data})
5052

51-
return AsyncClient("key", http=httpx.AsyncClient(transport=httpx.MockTransport(handle)))
53+
return AsyncClient(key, http=httpx.AsyncClient(transport=httpx.MockTransport(handle)))
5254

5355

5456
def reading(relation: Relation | None, question: str | None = None) -> ParsedAddress:
@@ -211,6 +213,16 @@ async def test_an_unassigned_typed_code_falls_back_to_the_pin() -> None:
211213
assert result.evidence[1] == "NIPOST says it is not assigned."
212214

213215

216+
@pytest.mark.anyio
217+
async def test_a_code_a_test_key_cannot_confirm_says_so() -> None:
218+
resolver = Resolver(nipost=nipost(valid=False, key="nipost_pk_test_1"))
219+
result = await resolver.resolve("EK-01-A03-FK-01, NTA Road")
220+
assert result.evidence[1] == (
221+
"NIPOST says it is not assigned. It was asked with a test key, which may not see "
222+
"assigned codes."
223+
)
224+
225+
214226
@pytest.mark.anyio
215227
async def test_a_spaced_code_counts_only_when_nipost_confirms_it() -> None:
216228
text = "No 12 Oba St 45, Ikeja"

0 commit comments

Comments
 (0)