summaryrefslogtreecommitdiff
path: root/test/world_wide_library.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/world_wide_library.py')
-rw-r--r--test/world_wide_library.py62
1 files changed, 48 insertions, 14 deletions
diff --git a/test/world_wide_library.py b/test/world_wide_library.py
index 31864fb..b3febd7 100644
--- a/test/world_wide_library.py
+++ b/test/world_wide_library.py
@@ -107,15 +107,16 @@ def serve_counter(command, get_params, post_params):
# Mock a Hydrilla repository.
+make_handler = lambda txt: lambda c, g, p: (200, {}, txt)
+
# Mock files in the repository.
sample_contents = [f'Mi povas manĝi vitron, ĝi ne damaĝas min {i}'
for i in range(9)]
-sample_hashes = [sha256(c.encode()).digest().hex() for c in sample_contents]
+sample_hashes = [sha256(c.encode()).digest().hex() for c in sample_contents]
-file_url = lambda hashed: f'https://hydril.la/file/sha256-{hashed}'
-file_handler = lambda contents: lambda c, g, p: (200, {}, contents)
+file_url = lambda hashed: f'https://hydril.la/file/sha256-{hashed}'
-sample_files_catalog = dict([(file_url(h), file_handler(c))
+sample_files_catalog = dict([(file_url(h), make_handler(c))
for h, c in zip(sample_hashes, sample_contents)])
# Mock resources and mappings in the repository.
@@ -145,6 +146,7 @@ for i in range(10):
sample_resources_catalog = {}
sample_mappings_catalog = {}
+sample_queries = {}
for srt in sample_resource_templates:
resource = make_sample_resource()
@@ -160,8 +162,8 @@ for srt in sample_resource_templates:
file_ref = {'file': f'file_{i}', 'sha256': sample_hashes[i]}
resource[('source_copyright', 'scripts')[i & 1]].append(file_ref)
- # Keeping it simple - just make one corresponding mapping for each resource.
- payloads = {'https://example.com/*': {'identifier': resource['identifier']}}
+ resource_versions = [resource['version'], resource['version'].copy()]
+ resource_versions[1][-1] += 1
mapping = make_sample_mapping()
mapping['api_schema_version'] = [1]
@@ -170,20 +172,51 @@ for srt in sample_resource_templates:
mapping['long_name'] = mapping['identifier'].upper()
mapping['uuid'] = str(uuid4())
mapping['source_copyright'] = resource['source_copyright']
- mapping['payloads'] = payloads
- make_handler = lambda txt: lambda c, g, p: (200, {}, txt)
+ mapping_versions = [mapping['version'], mapping['version'].copy()]
+ mapping_versions[1][-1] += 1
+
+ sufs = [srt["id_suffix"], *[l for l in srt["id_suffix"] if l.isalpha()]]
+ patterns = [f'https://example_{suf}.com/*' for suf in set(sufs)]
+ payloads = {}
+
+ for pat in patterns:
+ payloads[pat] = {'identifier': resource['identifier']}
+
+ queryable_url = pat.replace('*', 'something')
+ if queryable_url not in sample_queries:
+ sample_queries[queryable_url] = []
+
+ sample_queries[queryable_url].append({
+ 'identifier': mapping['identifier'],
+ 'long_name': mapping['long_name'],
+ 'version': mapping_versions[1]
+ })
- for item, catalog in [
- (resource, sample_resources_catalog),
- (mapping, sample_mappings_catalog)
+ mapping['payloads'] = payloads
+
+ for item, versions, catalog in [
+ (resource, resource_versions, sample_resources_catalog),
+ (mapping, mapping_versions, sample_mappings_catalog)
]:
fmt = f'https://hydril.la/{item["type"]}/{item["identifier"]}%s.json'
# Make 2 versions of each item so that we can test updates.
- for i in range(2):
+ for ver in versions:
+ item['version'] = ver
for fmt_arg in ('', '/' + item_version_string(item)):
catalog[fmt % fmt_arg] = make_handler(json.dumps(item))
- item['version'][-1] += 1
+
+def serve_query(command, get_params, post_params):
+ response = {
+ 'api_schema_version': [1],
+ 'api_schema_revision': 1,
+ 'mappings': sample_queries[get_params['url'][0]]
+ }
+
+ return (200, {}, json.dumps(response))
+
+sample_queries_catalog = dict([(f'https://hydril.la/{suf}query', serve_query)
+ for suf in ('', '1/', '2/', '3/', '4/')])
catalog = {
'http://gotmyowndoma.in':
@@ -233,5 +266,6 @@ catalog = {
**sample_files_catalog,
**sample_resources_catalog,
- **sample_mappings_catalog
+ **sample_mappings_catalog,
+ **sample_queries_catalog
}