From ae85e66e58f09af0161005c2d49f9161f9baf6a7 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 10 Mar 2019 15:18:13 +0200 Subject: Add: /search to effiapi --- effiapi | 37 ++++++++++++++++++++++++++++++++++++- yarns/000.yarn | 25 ++++++++++++++++++++++++- yarns/lib.py | 6 ++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/effiapi b/effiapi index 5f92901..3e83fc0 100755 --- a/effiapi +++ b/effiapi @@ -114,6 +114,9 @@ class FakeHTTPAPI(HTTPAPI): if url.endswith('/status'): return self._get_status() + if url.endswith('/search'): + return self._get_search(body) + if url.endswith('/res'): return self._get_resource(headers) @@ -124,7 +127,37 @@ class FakeHTTPAPI(HTTPAPI): body = { 'resources': len(self._memb), } - return FakeResponse(200, {}, body=json.dumps(body)) + return FakeResponse(200, {}, json.dumps(body)) + + def _get_search(self, body): + cond = json.loads(body) + logging.debug('_get_search: cond: %r', cond) + matches = [ + rid + for rid in self._memb + if self._matches_cond(rid, cond) + ] + + headers = { + 'Content-Type': 'application/json', + } + result = { + 'resources': matches, + } + return FakeResponse(200, headers, json.dumps(result)) + + def _matches_cond(self, rid, cond): + assert cond == { + 'cond': [ + { + 'where': 'meta', + 'op': '>=', + 'field': 'id', + 'pattern': '', + } + ] + } + return True def _get_resource(self, headers): if headers is None: @@ -340,7 +373,9 @@ class API: return response(404, None, str(e)) def _search(self): + logging.debug('_search callback called') cond = bottle.request.json + logging.debug('_search callback: %r', cond) result = self._muck.search(cond) return response(200, None, result) diff --git a/yarns/000.yarn b/yarns/000.yarn index 79e398d..4e6719c 100644 --- a/yarns/000.yarn +++ b/yarns/000.yarn @@ -45,6 +45,11 @@ First make sure the register is empty. THEN HTTP status is 200 AND body matches { "resources": 0 } + WHEN admin requests GET /search with + ... {"cond": [{"where": "meta", "op": ">=", "field": "id", "pattern": ""}]} + THEN HTTP status is 200 + AND body matches { "resources": [] } + Create a member. WHEN admin requests POST /memb with body { "fullname": "James Bond" } @@ -56,6 +61,11 @@ Create a member. THEN HTTP status is 200 AND body matches { "resources": 1 } + WHEN admin requests GET /search with + ... {"cond": [{"where": "meta", "op": ">=", "field": "id", "pattern": ""}]} + THEN HTTP status is 200 + AND body matches { "resources": [ "${ID}" ] } + WHEN admin requests GET /memb with header Muck-Id: ${ID} THEN HTTP status is 200 AND body matches { "fullname": "James Bond" } @@ -82,6 +92,11 @@ Delete the member. THEN HTTP status is 200 AND body matches { "resources": 0 } + WHEN admin requests GET /search with + ... {"cond": [{"where": "meta", "op": ">=", "field": "id", "pattern": ""}]} + THEN HTTP status is 200 + AND body matches { "resources": [] } + WHEN admin requests GET /memb with header Muck-Id: ${ID} THEN HTTP status is 404 @@ -138,6 +153,14 @@ TODO: } effiapi.GET('/memb', headers, None) + IMPLEMENTS WHEN admin requests GET /search with (.+) + body = get_expanded_json_match() + print('body', repr(body)) + headers = { + 'Content-Type': 'application/json', + } + effiapi.GET('/search', headers, json.dumps(body)) + IMPLEMENTS WHEN admin requests DELETE /memb with id (\S+) rid = get_expanded_match() print('rid', repr(rid)) @@ -174,7 +197,7 @@ TODO: assertEqual(actual, expected) IMPLEMENTS THEN body matches (.+) - expected = get_json_match() + expected = get_expanded_json_match() actual = effiapi.get_json_body() print 'expected:', expected print 'actual: ', actual diff --git a/yarns/lib.py b/yarns/lib.py index 2e76b88..6fa8843 100644 --- a/yarns/lib.py +++ b/yarns/lib.py @@ -151,6 +151,12 @@ def get_expanded_match(): return expand(match, V) +def get_expanded_json_match(): + match = get_next_match() + expanded = expand(match, V) + return json.loads(expanded) + + def expand(text, variables): result = '' while text: -- cgit v1.2.1