Project

General

Profile

« Previous | Next » 

Revision b75a5717

Added by koszko over 1 year ago

add a repo querying HTML interface

View differences:

test/world_wide_library.py
107 107

  
108 108
# Mock a Hydrilla repository.
109 109

  
110
make_handler = lambda txt: lambda c, g, p: (200, {}, txt)
111

  
110 112
# Mock files in the repository.
111 113
sample_contents = [f'Mi povas manĝi vitron, ĝi ne damaĝas min {i}'
112 114
                   for i in range(9)]
113
sample_hashes   = [sha256(c.encode()).digest().hex() for c in sample_contents]
115
sample_hashes = [sha256(c.encode()).digest().hex() for c in sample_contents]
114 116

  
115
file_url     = lambda hashed:   f'https://hydril.la/file/sha256-{hashed}'
116
file_handler = lambda contents: lambda c, g, p: (200, {}, contents)
117
file_url = lambda hashed: f'https://hydril.la/file/sha256-{hashed}'
117 118

  
118
sample_files_catalog = dict([(file_url(h), file_handler(c))
119
sample_files_catalog = dict([(file_url(h), make_handler(c))
119 120
                             for h, c in zip(sample_hashes, sample_contents)])
120 121

  
121 122
# Mock resources and mappings in the repository.
......
145 146

  
146 147
sample_resources_catalog = {}
147 148
sample_mappings_catalog = {}
149
sample_queries = {}
148 150

  
149 151
for srt in sample_resource_templates:
150 152
    resource = make_sample_resource()
......
160 162
        file_ref = {'file': f'file_{i}', 'sha256': sample_hashes[i]}
161 163
        resource[('source_copyright', 'scripts')[i & 1]].append(file_ref)
162 164

  
163
    # Keeping it simple - just make one corresponding mapping for each resource.
164
    payloads = {'https://example.com/*': {'identifier': resource['identifier']}}
165
    resource_versions = [resource['version'], resource['version'].copy()]
166
    resource_versions[1][-1] += 1
165 167

  
166 168
    mapping = make_sample_mapping()
167 169
    mapping['api_schema_version']  = [1]
......
170 172
    mapping['long_name']           = mapping['identifier'].upper()
171 173
    mapping['uuid']                = str(uuid4())
172 174
    mapping['source_copyright']    = resource['source_copyright']
173
    mapping['payloads']            = payloads
174 175

  
175
    make_handler = lambda txt: lambda c, g, p: (200, {}, txt)
176
    mapping_versions = [mapping['version'], mapping['version'].copy()]
177
    mapping_versions[1][-1] += 1
178

  
179
    sufs = [srt["id_suffix"], *[l for l in srt["id_suffix"] if l.isalpha()]]
180
    patterns = [f'https://example_{suf}.com/*' for suf in set(sufs)]
181
    payloads = {}
182

  
183
    for pat in patterns:
184
        payloads[pat] = {'identifier': resource['identifier']}
185

  
186
        queryable_url = pat.replace('*', 'something')
187
        if queryable_url not in sample_queries:
188
            sample_queries[queryable_url] = []
189

  
190
        sample_queries[queryable_url].append({
191
            'identifier': mapping['identifier'],
192
            'long_name':  mapping['long_name'],
193
            'version':    mapping_versions[1]
194
        })
176 195

  
177
    for item, catalog in [
178
        (resource, sample_resources_catalog),
179
        (mapping,  sample_mappings_catalog)
196
    mapping['payloads'] = payloads
197

  
198
    for item, versions, catalog in [
199
        (resource, resource_versions, sample_resources_catalog),
200
        (mapping,  mapping_versions,  sample_mappings_catalog)
180 201
    ]:
181 202
        fmt = f'https://hydril.la/{item["type"]}/{item["identifier"]}%s.json'
182 203
        # Make 2 versions of each item so that we can test updates.
183
        for i in range(2):
204
        for ver in versions:
205
            item['version'] = ver
184 206
            for fmt_arg in ('', '/' + item_version_string(item)):
185 207
                catalog[fmt % fmt_arg] = make_handler(json.dumps(item))
186
            item['version'][-1] += 1
208

  
209
def serve_query(command, get_params, post_params):
210
    response = {
211
        'api_schema_version':  [1],
212
        'api_schema_revision': 1,
213
        'mappings':            sample_queries[get_params['url'][0]]
214
    }
215

  
216
    return (200, {}, json.dumps(response))
217

  
218
sample_queries_catalog = dict([(f'https://hydril.la/{suf}query', serve_query)
219
                               for suf in ('', '1/', '2/', '3/', '4/')])
187 220

  
188 221
catalog = {
189 222
    'http://gotmyowndoma.in':
......
233 266

  
234 267
    **sample_files_catalog,
235 268
    **sample_resources_catalog,
236
    **sample_mappings_catalog
269
    **sample_mappings_catalog,
270
    **sample_queries_catalog
237 271
}

Also available in: Unified diff