def test_sub_objects_with_blank_nodes(self): class SubModel(object): def __init__(self): self.one = "Field One" class Model(object): def __init__(self): self.sub_field = SubModel() self.id = "http://api.nickswebsite.net/data#1" class SubModelSerializer(RdfSerializer): one = RdfStringField(predicate="nws:one") class Meta: rdf_prefixes = {"nws": "http://api.nickswebsite.net/ns/"} class ModelSerializer(RdfSerializer): sub_field = RdfObjectField(SubModelSerializer, "nws:sub-field", collapse=False) class Meta: rdf_prefixes = {"nws": "http://api.nickswebsite.net/ns/"} rdf_subject = "id" m = Model() s = ModelSerializer(object=m) s.validate() g = s.build_graph() result = get_triples(g, "http://api.nickswebsite.net/data#1", "http://api.nickswebsite.net/ns/sub-field", None) result = list(result) result_submodel_triples = get_triples(g, result[0][-1], None, None) result_submodel_triples = list(result_submodel_triples) self.assert_uri_equal("http://api.nickswebsite.net/ns/one", result_submodel_triples[0][1]) self.assert_literal_equal("Field One", result_submodel_triples[0][2])
def test_non_required_none_fields(self): class Model(object): def __init__(self): self.prop = "value" self.none = None self.id = "http://api.nickswebsite.net/data#9" class ModelSerializer(RdfSerializer): prop = RdfStringField( predicate="http://api.nickswebsite.net/ns/prop") none = RdfIriField(predicate="http://api.nickswebsite.net/ns/none", required=False) class Meta: rdf_subject = "id" m = Model() s = ModelSerializer(object=m) s.validate() g = s.build_graph() prop_triples = get_triples(g, m.id, "http://api.nickswebsite.net/ns/prop", None) self.assertEqual(1, len(prop_triples)) self.assertEqual(m.prop, prop_triples[0][-1].toPython()) none_triples = get_triples(g, m.id, "http://api.nickswebsite.net/ns/none", None) self.assertEqual(0, len(none_triples))
def test_object_list_fields(self): class SubModel(object): def __init__(self, field): self.field = field class Model(object): def __init__(self, *args): self.fields = [SubModel(arg) for arg in args] self.id = "http://api.nickswebsite.net/data#1" class SubModelSerializer(r2dto.Serializer): field = r2dto.fields.StringField() class Meta: rdf_prefixes = { "nws": "http://api.nickswebsite.net/ns/", } class Rdf: field = "nws:field" class ModelSerializer(r2dto.Serializer): fields = r2dto.fields.ListField( r2dto.fields.ObjectField(SubModelSerializer)) class Meta: rdf_prefixes = { "nws": "http://api.nickswebsite.net/ns/", } rdf_subject = "id" class Rdf: fields = "nws:fields" RdfModelSerializer = create_rdf_serializer_from_r2dto_serializer( ModelSerializer) m = Model("One", "Two") s = RdfModelSerializer(object=m) g = s.build_graph() ### # @prefix nws: <http://api.nickswebsite.net/ # # nws:data#1 nws:ns/fields [ nws:field "One" ], # [ nws:field "Two" ] . model_triples = get_triples(g, m.id, "http://api.nickswebsite.net/ns/fields", None) self.assertEqual(2, len(model_triples)) submodel_triples_a = get_triples( g, model_triples[0][2], "http://api.nickswebsite.net/ns/field", None) submodel_triples_b = get_triples( g, model_triples[1][2], "http://api.nickswebsite.net/ns/field", None) self.assertEqual( {"One", "Two"}, {submodel_triples_a[0][2].value, submodel_triples_b[0][2].value})
def test_object_list_fields(self): class SubModel(object): def __init__(self, field): self.field = field class Model(object): def __init__(self, *args): self.fields = [SubModel(arg) for arg in args] self.id = "http://api.nickswebsite.net/data#1" class SubModelSerializer(r2dto.Serializer): field = r2dto.fields.StringField() class Meta: rdf_prefixes = { "nws": "http://api.nickswebsite.net/ns/", } class Rdf: field = "nws:field" class ModelSerializer(r2dto.Serializer): fields = r2dto.fields.ListField(r2dto.fields.ObjectField(SubModelSerializer)) class Meta: rdf_prefixes = { "nws": "http://api.nickswebsite.net/ns/", } rdf_subject = "id" class Rdf: fields = "nws:fields" RdfModelSerializer = create_rdf_serializer_from_r2dto_serializer(ModelSerializer) m = Model("One", "Two") s = RdfModelSerializer(object=m) g = s.build_graph() ### # @prefix nws: <http://api.nickswebsite.net/ # # nws:data#1 nws:ns/fields [ nws:field "One" ], # [ nws:field "Two" ] . model_triples = get_triples(g, m.id, "http://api.nickswebsite.net/ns/fields", None) self.assertEqual(2, len(model_triples)) submodel_triples_a = get_triples(g, model_triples[0][2], "http://api.nickswebsite.net/ns/field", None) submodel_triples_b = get_triples(g, model_triples[1][2], "http://api.nickswebsite.net/ns/field", None) self.assertEqual({"One", "Two"}, {submodel_triples_a[0][2].value, submodel_triples_b[0][2].value})
def test_uuid_field(self): f = RdfUuidField("http://api.nickswebsite.net/ns/data-type") self.assertNotEqual("@id", f.datatype) expected = uuid.uuid4() self.assertEqual(str(expected), f.render(expected)) f.validate(uuid.uuid4()) f.validate(str(uuid.uuid4())) f.validate(uuid.uuid4().hex) self.assertRaises(ValidationError, f.validate, "xyz") self.assertRaises(ValidationError, f.validate, True) iri_field = RdfUuidField("http://api.nickswebsite.net/ns/data-type", iri=True) self.assertEqual(iri_field.datatype, "@id") test = "4e0b25c1-0792-4e7d-89b5-fe26460dff5b" self.assertEqual("urn:uuid:{}".format(test), iri_field.render(uuid.UUID(test))) # Make sure that the serializers will parse this correctly class ModelSerializer(RdfSerializer): f = RdfUuidField("http://api.nickswebsite.net/data#1", iri=True) class Model(object): def __init__(self): self.f = uuid.uuid4() m = Model() s = ModelSerializer(object=m) g = s.build_graph() t = get_triples(g, None, "http://api.nickswebsite.net/data#1", None) self.assertEqual(URIRef("urn:uuid:{}".format(m.f)), t[0][-1])
def test_objects_in_lists(self): class SubModel(object): def __init__(self, val): self.val = val class Model(object): def __init__(self): self.values = [SubModel("One"), SubModel("Two")] self.id = "http://api.nickswebsite.net/data#8" class SubModelSerializer(RdfSerializer): val = RdfStringField( predicate="http://api.nickswebsite.net/ns/value") class ModelSerializer(RdfSerializer): values = RdfSetField( RdfObjectField(SubModelSerializer), predicate="http://api.nickswebsite.net/ns/sub-object") class Meta: rdf_subject = "id" # @prefix nws: <http://api.nickswebsite.net/> # # nws:data#8 nws:ns/sub-object [ nws:ns/value "One" ], # [ nws:ns/value "Two" ] . # m = Model() s = ModelSerializer(object=m) s.validate() g = s.build_graph() model_triples = get_triples( g, m.id, "http://api.nickswebsite.net/ns/sub-object", None) self.assertEqual(2, len(model_triples)) submodel_triples_a = get_triples( g, model_triples[0][2], "http://api.nickswebsite.net/ns/value", None) submodel_triples_b = get_triples( g, model_triples[1][2], "http://api.nickswebsite.net/ns/value", None) self.assertEqual( {"One", "Two"}, {submodel_triples_a[0][2].value, submodel_triples_b[0][2].value})
def test_objects_in_lists(self): class SubModel(object): def __init__(self, val): self.val = val class Model(object): def __init__(self): self.values = [SubModel("One"), SubModel("Two")] self.id = "http://api.nickswebsite.net/data#8" class SubModelSerializer(RdfSerializer): val = RdfStringField(predicate="http://api.nickswebsite.net/ns/value") class ModelSerializer(RdfSerializer): values = RdfSetField( RdfObjectField(SubModelSerializer), predicate="http://api.nickswebsite.net/ns/sub-object" ) class Meta: rdf_subject = "id" # @prefix nws: <http://api.nickswebsite.net/> # # nws:data#8 nws:ns/sub-object [ nws:ns/value "One" ], # [ nws:ns/value "Two" ] . # m = Model() s = ModelSerializer(object=m) s.validate() g = s.build_graph() model_triples = get_triples(g, m.id, "http://api.nickswebsite.net/ns/sub-object", None) self.assertEqual(2, len(model_triples)) submodel_triples_a = get_triples(g, model_triples[0][2], "http://api.nickswebsite.net/ns/value", None) submodel_triples_b = get_triples(g, model_triples[1][2], "http://api.nickswebsite.net/ns/value", None) self.assertEqual({"One", "Two"}, {submodel_triples_a[0][2].value, submodel_triples_b[0][2].value})
def test_non_required_none_fields(self): class Model(object): def __init__(self): self.prop = "value" self.none = None self.id = "http://api.nickswebsite.net/data#9" class ModelSerializer(RdfSerializer): prop = RdfStringField(predicate="http://api.nickswebsite.net/ns/prop") none = RdfIriField(predicate="http://api.nickswebsite.net/ns/none", required=False) class Meta: rdf_subject = "id" m = Model() s = ModelSerializer(object=m) s.validate() g = s.build_graph() prop_triples = get_triples(g, m.id, "http://api.nickswebsite.net/ns/prop", None) self.assertEqual(1, len(prop_triples)) self.assertEqual(m.prop, prop_triples[0][-1].toPython()) none_triples = get_triples(g, m.id, "http://api.nickswebsite.net/ns/none", None) self.assertEqual(0, len(none_triples))
def test_objects_as_ids(self): class Model(object): def __init__(self): self.id = "http://api.nickswebsite.net/data#3" self.link = "http://api.nickswebsite.net/data#3" class ModelSerializer(RdfSerializer): class Meta: rdf_subject = "id" link = RdfIriField(predicate="http://api.nickswebsite.net/ns/link") model = Model() s = ModelSerializer(object=model) s.validate() g = s.build_graph() triples = list(get_triples(g, model.id, "http://api.nickswebsite.net/ns/link", None)) self.assertEqual(URIRef(model.link), triples[0][-1])
def test_objects_as_ids(self): class Model(object): def __init__(self): self.id = "http://api.nickswebsite.net/data#3" self.link = "http://api.nickswebsite.net/data#3" class ModelSerializer(RdfSerializer): class Meta: rdf_subject = "id" link = RdfIriField(predicate="http://api.nickswebsite.net/ns/link") model = Model() s = ModelSerializer(object=model) s.validate() g = s.build_graph() triples = list( get_triples(g, model.id, "http://api.nickswebsite.net/ns/link", None)) self.assertEqual(URIRef(model.link), triples[0][-1])