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_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(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_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_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_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'