def test_invalid_type_uri_raises_valuerror(): with pytest.raises(ValueError) as e: oparl.from_json('''{ "id": "does-not-exist", "type": "invalid" }''') assert 'Invalid type URI' in str(e.value)
def test_unknown_type_raises_valueerror(): with pytest.raises(ValueError) as e: oparl.from_json('''{ "id": "does-not-exist", "type": "not/known" }''') assert 'Unknown type' in str(e.value)
def test_invalid_datetime_string_triggers_contentwarning(): with pytest.warns(oparl.ContentWarning) as record: oparl.from_json('''{ "id": "object-with-invalid-datetime", "type": "https://schema.oparl.org/1.0/Organization", "created": "this is not a date-time" }''') assert len(record) == 1 assert 'invalid date-time string' in str(record[0].message)
def test_invalid_schema_uri_triggers_specificationwarning(): with pytest.warns(oparl.SpecificationWarning) as record: obj = oparl.from_json('''{ "id": "object-with-invalid-schema-uri", "type": "this-is-not-the-correct-schema-uri/System" }''') assert len(record) == 2 assert 'Invalid schema URI' in str(record[0].message) assert str(record[0].message) == str(record[1].message) assert obj['type'] == 'this-is-not-the-correct-schema-uri/System'
def test_reference_instead_of_object_triggers_specificationwarning(): with pytest.warns(oparl.SpecificationWarning) as record: obj = oparl.from_json('''{ "id": "object-with-reference-instead-of-object", "type": "https://schema.oparl.org/1.0/Body", "location": "a-location" }''') assert len(record) == 1 assert 'must contain an object' in str(record[0].message) location = obj['location'] assert isinstance(location, oparl.objects.Location) assert location['id'] == 'a-location'
def test_reference_instead_of_object_in_list_triggers_specificationwarning(): with pytest.warns(oparl.SpecificationWarning) as record: obj = oparl.from_json('''{ "id": "object-with-reference-instead-of-object-in-list", "type": "https://schema.oparl.org/1.0/Body", "legislativeTerm": ["a-legislativeterm"] }''') assert len(record) == 1 assert 'must contain objects' in str(record[0].message) terms = obj['legislativeTerm'] assert isinstance(terms, list) assert len(terms) == 1 assert isinstance(terms[0], oparl.objects.LegislativeTerm) assert terms[0]['id'] == 'a-legislativeterm'
def test_object_instead_of_reference_triggers_specificationwarning(): with pytest.warns(oparl.SpecificationWarning) as record: obj = oparl.from_json('''{ "id": "object-with-object-instead-of-reference", "type": "https://schema.oparl.org/1.0/Body", "system": { "id": "does-not-exist", "type": "https://schema.oparl.org/1.0/System" } }''') assert len(record) == 1 assert 'must contain an object reference' in str(record[0].message) system = obj['system'] assert isinstance(system, oparl.objects.System) assert system['id'] == 'does-not-exist'
def test_scalar_instead_of_list_triggers_specificationwarning(): with pytest.warns(oparl.SpecificationWarning) as record: obj = oparl.from_json('''{ "id": "object-with-scalar-instead-of-list", "type": "https://schema.oparl.org/1.0/Person", "membership": { "id": "does-not-exist", "type": "https://schema.oparl.org/1.0/Membership" } }''') assert len(record) == 1 assert 'non-list value' in str(record[0].message) membership = obj['membership'] assert isinstance(membership, list) assert len(membership) == 1 assert membership[0]['id'] == 'does-not-exist'
def test_object_instead_of_reference_in_list_triggers_specificationwarning(): with pytest.warns(oparl.SpecificationWarning) as record: obj = oparl.from_json('''{ "id": "object-with-object-instead-of-reference-in-list", "type": "https://schema.oparl.org/1.0/System", "otherOparlVersions": [{ "id": "does-not-exist", "type": "https://schema.oparl.org/1.0/System" }] }''') assert len(record) == 1 assert 'must contain references' in str(record[0].message) others = obj['otherOparlVersions'] assert isinstance(others, list) assert len(others) == 1 assert isinstance(others[0], oparl.objects.System) assert others[0]['id'] == 'does-not-exist'
def test_missing_type_raises_valueerror(): with pytest.raises(ValueError) as e: oparl.from_json('''{ "id": "does-not-exist" }''') assert 'does not have a `type` field' in str(e.value)
def test_missing_id_raises_valueerror(): with pytest.raises(ValueError) as e: oparl.from_json('''{ "type": "https://schema.oparl.org/1.0/System" }''') assert 'does not have an `id` field' in str(e.value)