/
test_schemas_api.py
571 lines (509 loc) · 22.2 KB
/
test_schemas_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# -*- coding: utf-8 -*-
#
# This file is part of EUDAT B2Share.
# Copyright (C) 2016 CERN.
#
# B2Share is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# B2Share is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with B2Share; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""Test B2Share schemas module's API."""
import json
import uuid
from collections import namedtuple
import jsonschema
import pytest
from flask import url_for
from invenio_db import db
from jsonpatch import JsonPatchConflict
from b2share.modules.communities import B2ShareCommunities, Community
from b2share.modules.communities.errors import CommunityDoesNotExistError
from b2share.modules.schemas import B2ShareSchemas, BlockSchema, \
CommunitySchema, RootSchema
from b2share.modules.schemas.errors import BlockSchemaDoesNotExistError, \
BlockSchemaIsDeprecated, InvalidBlockSchemaError, InvalidJSONSchemaError, \
InvalidRootSchemaError, RootSchemaDoesNotExistError, \
InvalidSchemaVersionError, SchemaVersionExistsError
from doschema.errors import JSONSchemaCompatibilityError
from b2share_unit_tests.communities.helpers import community_metadata
from b2share_unit_tests.schemas.data import (
communities_metadata, root_schemas_json_schemas,
block_schemas_json_schemas,
backward_incompatible_root_schemas_json_schemas,
backward_incompatible_block_schemas_json_schemas
)
def test_root_schema(app):
"""Test valid usage of the RootSchema API."""
with app.app_context():
version = 0
for json_schema in root_schemas_json_schemas:
RootSchema.create_new_version(
version=version,
json_schema=json_schema,
)
version += 1
db.session.commit()
with app.app_context():
version = 0
for json_schema in root_schemas_json_schemas:
root_schema = RootSchema.get_root_schema(version)
assert root_schema.version == version
assert json.loads(root_schema.json_schema) == json_schema
version += 1
def test_root_schemas_backward_compatibility(app):
"""Test non backward compatible root schemas."""
with app.app_context():
version = 0
for json_schema in root_schemas_json_schemas:
RootSchema.create_new_version(
version=version,
json_schema=json_schema,
)
version += 1
# try creating a new not backward compatbile version of the root schema
for json_schema in backward_incompatible_root_schemas_json_schemas:
with pytest.raises(JSONSchemaCompatibilityError):
RootSchema.create_new_version(
version=version,
json_schema=json_schema,
)
db.session.commit()
# check that no invalid schema was created
with app.app_context():
with pytest.raises(RootSchemaDoesNotExistError):
RootSchema.get_root_schema(len(root_schemas_json_schemas) + 1)
def test_root_schema_errors(app):
"""Test invalid usage of the RootSchema API."""
with app.app_context():
# creating first root schema with a version != 0 should fail
with pytest.raises(InvalidRootSchemaError):
RootSchema.create_new_version(
version=42,
json_schema=root_schemas_json_schemas[0],
)
# test creating a root schema with a negative version when no schema
# exist
with pytest.raises(InvalidRootSchemaError):
RootSchema.create_new_version(
version=-1,
json_schema=root_schemas_json_schemas[0],
)
# test skipping a version when creating a root schema
RootSchema.create_new_version(
version=0,
json_schema=root_schemas_json_schemas[0],
)
with pytest.raises(InvalidRootSchemaError):
RootSchema.create_new_version(
version=2,
json_schema=root_schemas_json_schemas[0],
)
# test creating a root schema with a negative version when one schema
# exists
with pytest.raises(InvalidRootSchemaError):
RootSchema.create_new_version(
version=-1,
json_schema=root_schemas_json_schemas[0],
)
# test creating a root schema with an already used version
with pytest.raises(InvalidRootSchemaError):
RootSchema.create_new_version(
version=0,
json_schema=root_schemas_json_schemas[0],
)
# test creating a root schema with no JSON Schema
with pytest.raises(InvalidJSONSchemaError):
RootSchema.create_new_version(
version=1,
json_schema=None,
)
with pytest.raises(InvalidJSONSchemaError):
RootSchema.create_new_version(
version=1,
json_schema={},
)
with pytest.raises(InvalidJSONSchemaError):
RootSchema.create_new_version(
version=1,
json_schema={'$schema': 'invalid-url'},
)
with pytest.raises(InvalidJSONSchemaError):
RootSchema.create_new_version(
version=1,
json_schema={'$schema': 'http://examples.com'},
)
# try getting a non existing root schema
with pytest.raises(RootSchemaDoesNotExistError):
RootSchema.get_root_schema(42)
def test_block_schemas(app):
"""Test valid usage of the BlockSchema API."""
with app.app_context():
new_community = Community.create_community(**communities_metadata[0])
new_community_id = new_community.id
db.session.commit()
block_schema_name = "schema 1"
block_schema_1 = BlockSchema.create_block_schema(
community_id=new_community.id, name=block_schema_name
)
block_schema_1_id = block_schema_1.id
db.session.commit()
with app.app_context():
retrieved_block_schema = BlockSchema.get_block_schema(
block_schema_1_id)
assert retrieved_block_schema.name == block_schema_name
assert retrieved_block_schema.community == new_community_id
# test changing the community maintaining the block schema
new_community_2 = Community.create_community(**communities_metadata[1])
retrieved_block_schema.community = new_community_2.id
assert retrieved_block_schema.community == new_community_2.id
# test setting the schema name
retrieved_block_schema.name = new_name = 'new name'
assert retrieved_block_schema.name == new_name
def test_block_schema_errors(app):
"""Test invalid usage of the BlockSchema API."""
with app.app_context():
unknown_uuid = uuid.uuid4()
# test with an invalid community ID
with pytest.raises(InvalidBlockSchemaError):
BlockSchema.create_block_schema(community_id=unknown_uuid,
name='test')
new_community = Community.create_community(**communities_metadata[0])
db.session.commit()
# test with name == None
with pytest.raises(InvalidBlockSchemaError):
BlockSchema.create_block_schema(community_id=new_community.id,
name=None)
# test with len(name) too short
with pytest.raises(InvalidBlockSchemaError):
BlockSchema.create_block_schema(community_id=new_community.id,
name='t')
# test with len(name) too long
with pytest.raises(InvalidBlockSchemaError):
BlockSchema.create_block_schema(community_id=new_community.id,
name='t'*500)
# test getting non existing block schema
with pytest.raises(BlockSchemaDoesNotExistError):
BlockSchema.get_block_schema(unknown_uuid)
block_schema = BlockSchema.create_block_schema(
community_id=new_community.id, name='test'
)
# test setting the community to an unknown uuid
with pytest.raises(CommunityDoesNotExistError):
block_schema.community = unknown_uuid
assert block_schema.community == new_community.id
# test setting the name to None
with pytest.raises(InvalidBlockSchemaError):
block_schema.name = None
# test setting the name to a too short string
with pytest.raises(InvalidBlockSchemaError):
block_schema.name = 't'
# test setting the name to a too long string
with pytest.raises(InvalidBlockSchemaError):
block_schema.name = 't'*500
db.session.commit()
def test_block_schemas_versions(app):
"""Test valid usage of the BlockSchemaVersion API."""
with app.app_context():
new_community = Community.create_community(**communities_metadata[0])
db.session.commit()
block_schema_name = "schema 1"
block_schema_1 = BlockSchema.create_block_schema(
community_id=new_community.id, name=block_schema_name
)
# test the versions iterator with no versions created
assert len(block_schema_1.versions) == 0
for version in block_schema_1.versions:
pytest.fail('No versions have been added yet.')
# create the versions
for json_schema in block_schemas_json_schemas[0]:
block_schema_1.create_version(
json_schema=json_schema,
)
block_schema_1_id = block_schema_1.id
db.session.commit()
with app.app_context():
retrieved_block_schema = BlockSchema.get_block_schema(
block_schema_1_id)
all_versions = retrieved_block_schema.versions
assert len(all_versions) == 2
expected_version = 0
for version in all_versions:
# retrieving by version number should return the same
# BlockSchemaVersion
retrieved_version = all_versions[expected_version]
assert json.loads(version.json_schema) == \
json.loads(retrieved_version.json_schema) == \
block_schemas_json_schemas[0][version.version]
# test sorting
assert version.version == retrieved_version.version == \
expected_version
# test __contains__
assert expected_version in all_versions
expected_version += 1
# check access for not existing version
assert len(block_schemas_json_schemas[0]) not in all_versions
with pytest.raises(IndexError):
all_versions[len(block_schemas_json_schemas[0])]
with app.app_context():
retrieved_block_schema = BlockSchema.get_block_schema(
block_schema_1_id)
all_versions = retrieved_block_schema.versions
assert len(all_versions) == 2
retrieved_block_schema.create_version(
block_schemas_json_schemas[0][0],
len(all_versions)
)
with pytest.raises(SchemaVersionExistsError):
retrieved_block_schema.create_version(
block_schemas_json_schemas[0][0],
1
)
def test_block_schemas_versions_backward_compatibility(app):
"""Test non backward compatible root schemas."""
with app.app_context():
new_community = Community.create_community(**communities_metadata[0])
db.session.commit()
block_schema_name = "schema 1"
block_schema_1 = BlockSchema.create_block_schema(
community_id=new_community.id, name=block_schema_name
)
block_schema_id = block_schema_1.id
# test the versions iterator with no versions created
assert len(block_schema_1.versions) == 0
for version in block_schema_1.versions:
pytest.fail('No versions have been added yet.')
# create the versions
for json_schema in block_schemas_json_schemas[0]:
block_schema_1.create_version(
json_schema=json_schema,
)
block_schema_1_id = block_schema_1.id
# try creating a new not backward compatbile version of the schema
for json_schema in backward_incompatible_block_schemas_json_schemas:
with pytest.raises(JSONSchemaCompatibilityError):
block_schema_1.create_version(
json_schema=json_schema,
)
db.session.commit()
# check that no invalid schema was created
with app.app_context():
block_schema_1 = BlockSchema.get_block_schema(block_schema_id)
assert len(block_schema_1.versions) == \
len(block_schemas_json_schemas[0])
def test_block_schema_version_errors(app):
"""Test invalid usage of the BlockSchemaVersion API."""
with app.app_context():
new_community = Community.create_community(**communities_metadata[0])
block_schema = BlockSchema.create_block_schema(
community_id=new_community.id, name='test'
)
db.session.commit()
# test create block schema version with json_schema == None
with pytest.raises(InvalidJSONSchemaError):
block_schema.create_version(None)
# test invalid $schema URLs
with pytest.raises(InvalidJSONSchemaError):
block_schema.create_version({})
with pytest.raises(InvalidJSONSchemaError):
block_schema.create_version({'$schema': 'invalid-url'})
with pytest.raises(InvalidJSONSchemaError):
block_schema.create_version({'$schema': 'http://examples.com'})
block_schema.deprecated = True
# test adding a version to a deprecated schema
with pytest.raises(BlockSchemaIsDeprecated):
block_schema.create_version(block_schemas_json_schemas[0][0])
# try accessing a non existing version
with pytest.raises(IndexError):
block_schema.versions[0]
assert len(block_schema.versions) == 0
def test_community_schema(app, flask_http_responses):
"""Test valid usage of the CommunitySchema API."""
with app.app_context():
new_community = Community.create_community(**communities_metadata[0])
db.session.commit()
BlockSchemaRef = namedtuple('BlockSchemaRef', [
'block_schema',
'versions'
])
BlockSchemaVersionRef = namedtuple('BlockSchemaVersionRef', [
'version',
'url'
])
# create root schemas
root_schemas = [
RootSchema.create_new_version(
version=version,
json_schema=root_schemas_json_schemas[version],
) for version in range(len(root_schemas_json_schemas))
]
# create block schemas
block_schemas = []
for block_index in range(len(block_schemas_json_schemas)):
block_schema = BlockSchema.create_block_schema(
community_id=new_community.id,
name="schema {}".format(block_index)
)
block_schemas.append(
BlockSchemaRef(block_schema=block_schema, versions=[
BlockSchemaVersionRef(
version=block_schema.create_version(
json_schema=block_schemas_json_schemas[
block_index][version],
),
url=url_for(
'b2share_schemas.block_schema_versions_item',
schema_id=block_schema.id,
schema_version_nb=version,
_external=True,
))
for version in
range(len(block_schemas_json_schemas[block_index]))
])
)
community_schemas = []
# create a community schema
community_schema_v1_json_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'properties': {
str(block_schemas[0].block_schema.id): {
'$ref': "{}#/json_schema".format(
block_schemas[0].versions[0].url),
},
str(block_schemas[1].block_schema.id): {
'$ref': "{}#/json_schema".format(
block_schemas[1].versions[0].url),
},
},
'additionalProperties': False,
}
community_schemas.append(CommunitySchema.create_version(
community_id=new_community.id,
root_schema_version=root_schemas[0].version,
community_schema=community_schema_v1_json_schema
))
# test with another block schema version
community_schema_v2_json_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'properties': {
str(block_schemas[0].block_schema.id): {
'$ref': "{}#/json_schema".format(
block_schemas[0].versions[1].url),
},
str(block_schemas[1].block_schema.id): {
'$ref': "{}#/json_schema".format(
block_schemas[1].versions[0].url),
},
},
'additionalProperties': False,
}
community_schemas.append(CommunitySchema.create_version(
community_id=new_community.id,
root_schema_version=root_schemas[0].version,
community_schema=community_schema_v2_json_schema
))
# test with another root schema
community_schemas.append(CommunitySchema.create_version(
community_id=new_community.id,
root_schema_version=root_schemas[1].version,
community_schema=community_schema_v2_json_schema
))
db.session.commit()
# create a metadata blcok matching each community schema
metadatas = [
{
'authors': ['C. Arthur', 'D. Albert'],
'community_specific': {
str(block_schemas[0].block_schema.id): {
'experiment_nb': 42,
},
str(block_schemas[1].block_schema.id): {
'analysis_result': 'success',
},
}
},
{
'authors': ['C. Arthur', 'D. Albert'],
'community_specific': {
str(block_schemas[0].block_schema.id): {
'experiment_nb': 42,
'experiment_date': '4242'
}
}
},
{
'authors': ['C. Arthur', 'D. Albert'],
'files': ['/path/to/the/file.txt'],
'community_specific': {
str(block_schemas[0].block_schema.id): {
'experiment_nb': 42,
'experiment_date': '4242'
}
}
}
]
validation_schemas = [schema.build_json_schema()
for schema in community_schemas]
for index in range(len(community_schemas)):
with flask_http_responses():
# check that the community schema validates the corresponding
# JSON metadata
jsonschema.validate(metadatas[index],
validation_schemas[index])
for index2 in range(len(community_schemas)):
if index != index2:
with pytest.raises(
jsonschema.exceptions.ValidationError):
# check that the community schema does not validate
# the others JSON metadata
jsonschema.validate(metadatas[index2],
validation_schemas[index])
def test_patch(app):
"""Test BlockSchema.patch()."""
with app.app_context():
created_community = Community.create_community(**community_metadata)
block_schema = BlockSchema.create_block_schema(created_community.id, 'abc')
block_schema_id = block_schema.id
db.session.commit()
retrieved = BlockSchema.get_block_schema(block_schema_id)
retrieved.patch([{'op': 'replace', 'path': '/name', 'value': 'patched'}])
db.session.commit()
with app.app_context():
patched = BlockSchema.get_block_schema(block_schema_id)
assert block_schema_id == patched.id
assert getattr(patched, 'name') == 'patched'
with pytest.raises(JsonPatchConflict):
patched.patch([{'op': 'replace', 'path': '/non_exist_name', 'value': None}])
assert getattr(patched, 'name') == 'patched'
def test_update(app):
"""Test BlockSchema.update()."""
with app.app_context():
created_community = Community.create_community(**community_metadata)
block_schema = BlockSchema.create_block_schema(created_community.id, 'abc')
block_schema_id = block_schema.id
db.session.commit()
with app.app_context():
retrieved = BlockSchema.get_block_schema(block_schema_id)
retrieved.update({'name': 'updated'})
db.session.commit()
with app.app_context():
updated = BlockSchema.get_block_schema(block_schema_id)
assert block_schema_id == updated.id
assert getattr(updated, 'name') == 'updated'
with pytest.raises(InvalidBlockSchemaError):
updated.update({'name': None})
assert getattr(updated, 'name') == 'updated'