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_guess_format(self):
     """Test guessing and importing 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)
         data_integrity(self, session, loaded_objects, label="import")
 def test_application_rdf_xml_guess_format(self):
     """Test guessing and importing the `application/rdf+xml` mime type."""
     with CoreSession() as session:
         test_data_path = str(
             Path(__file__).parent / "test_importexport_data.owl")
         loaded_objects = import_cuds(test_data_path)
         data_integrity(self, session, loaded_objects, label="import")
    def test_text_turtle_another_session(self):
        """Test to a non-default session."""
        another_session = CoreSession()
        with CoreSession() as session:
            test_data_path = str(
                Path(__file__).parent / "test_importexport_data.ttl")
            with open(test_data_path, "r") as test_data_file:
                test_data = test_data_file.read()
            test_data = io.StringIO(test_data)
            loaded_objects = import_cuds(test_data,
                                         format="text/turtle",
                                         session=another_session)
            # The expected objects will not be found in session, they will
            # be none.
            expected_objects = tuple(
                session.load_from_iri(
                    rdflib.URIRef(
                        f"http://example.org/test-ontology#x_{i}")).first()
                for i in range(1, 5))
            self.assertTrue(all(x is None for x in expected_objects))

            # Test correctness in the other session.
            data_integrity(self,
                           another_session,
                           loaded_objects,
                           label="import")
 def test_text_turtle_file_handle(self):
     """Test importing the `text/turtle` mime type from a file handle."""
     with CoreSession() as session:
         test_data_path = str(
             Path(__file__).parent / "test_importexport_data.ttl")
         with open(test_data_path, "r") as test_data_file:
             loaded_objects = import_cuds(test_data_file,
                                          format="text/turtle")
             data_integrity(self, session, loaded_objects, label="import")
    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)