def test_text_turtle(self): """Test importing and exporting the `text/turtle` mime type.""" with CoreSession() as session: test_data_path = str( Path(__file__).parent / "test_importexport_data.ttl") loaded_objects = import_cuds(test_data_path, format="text/turtle") data_integrity(self, session, loaded_objects, label="import") exported_file = io.StringIO() export_cuds(file=exported_file, format="text/turtle") exported_file.seek(0) with CoreSession() as session: exported_objects = import_cuds(exported_file, format="text/turtle") data_integrity(self, session, exported_objects, label="export")
def test_application_json(self): """Test importing and exporting the `application/ld+json` mime type.""" with CoreSession() as session: test_data_path = str( Path(__file__).parent / "test_importexport_data.json") loaded_objects = import_cuds(test_data_path, format="application/ld+json") data_integrity(self, session, loaded_objects, label="import") exported_file = io.StringIO() export_cuds(file=exported_file, format="application/ld+json") exported_file.seek(0) with CoreSession() as session: exported_objects = import_cuds(exported_file, format="application/ld+json") data_integrity(self, session, exported_objects, label="export")
def test_text_turtle_cuds_triples(self): """Test exporting the `text/turtle` mime type from a cuds object. This test uses the city ontology. """ # Exporting c = city.City(name="Freiburg", coordinates=[47, 7]) p1 = city.Citizen(name="Peter") p2 = city.Citizen(name="Anne") c.add(p1, rel=city.hasInhabitant) c.add(p2, rel=city.hasInhabitant) exported_file = io.StringIO() export_cuds(c, file=exported_file, format="text/turtle") exported_file.seek(0) cuds = import_cuds(exported_file, format="text/turtle") self.assertIs(type(cuds), Cuds)
def test_application_json_doc_city(self): """Test importing the `application/ld+json` mime type from doc dict. This test uses a city ontology instead. """ # Importing test_data_path = str( Path(__file__).parent / "test_importexport_city_import.json") with open(test_data_path, "r") as file: json_doc = json.loads(file.read()) with CoreSession(): cuds = import_cuds(json_doc, format="application/ld+json") self.assertTrue(cuds.is_a(city.Citizen)) self.assertEqual(cuds.name, "Peter") self.assertEqual(cuds.age, 23) export_file = io.StringIO() export_cuds(cuds, file=export_file, format="application/ld+json") export_file.seek(0) assertJsonLdEqual(self, json_doc, json.loads(export_file.read())) # Exporting test_data_path = str( Path(__file__).parent / "test_importexport_city_export.json") with open(test_data_path, "r") as file: json_doc = json.loads(file.read()) with CoreSession(): c = branch( city.City(name="Freiburg", uid=1), branch( city.Neighborhood(name="Littenweiler", uid=2), city.Street(name="Schwarzwaldstraße", uid=3), ), ) export_file = io.StringIO() export_cuds(c, file=export_file, format="application/ld+json") export_file.seek(0) assertJsonLdEqual(self, json.loads(export_file.read()), json_doc)