Example #1
0
    def test_validate_tree_against_schema(self):
        """Test validation of CUDS tree against schema.yml."""
        schema_file = os.path.join(os.path.dirname(__file__),
                                   'test_validation_schema_city.yml')
        schema_file_with_missing_entity = os.path.join(
            os.path.dirname(__file__),
            'test_validation_schema_city_with_missing_entity.yml')
        schema_file_with_missing_relationship = os.path.join(
            os.path.dirname(__file__),
            'test_validation_schema_city_with_missing_relationship.yml')
        schema_file_with_optional_subtree = os.path.join(
            os.path.dirname(__file__),
            'test_validation_schema_city_with_optional_subtree.yml')

        c = city.City(name='freiburg')

        # empty city is not valid
        self.assertRaises(ConsistencyError, validate_tree_against_schema, c,
                          schema_file)

        # unless I do not specify relationships for it
        validate_tree_against_schema(c, schema_file_with_missing_relationship)

        # but it at least should be a city,
        # even when no relationships are defined
        wrong_object = cuba.File(path='some path')
        self.assertRaises(ConsistencyError, validate_tree_against_schema,
                          wrong_object, schema_file_with_missing_relationship)

        # with opional inhabitants an empty city is ok
        validate_tree_against_schema(c, schema_file_with_optional_subtree)

        # but the optional subtree should follow its own constraints
        # (here a citizen needs to work in a city)
        c.add(city.Citizen(name='peter'), rel=city.hasInhabitant)
        self.assertRaises(CardinalityError, validate_tree_against_schema, c,
                          schema_file_with_optional_subtree)

        c.add(city.Neighborhood(name='some hood'))
        c.add(city.Citizen(name='peter'), rel=city.hasInhabitant)

        # street of neighborhood violated
        self.assertRaises(CardinalityError, validate_tree_against_schema, c,
                          schema_file)

        c.get(oclass=city.Neighborhood)[0].add(city.Street(name='abc street'))

        # now the city is valid and validation should pass
        validate_tree_against_schema(c, schema_file)

        # entity that was defined is completely missing in cuds tree
        self.assertRaises(ConsistencyError, validate_tree_against_schema, c,
                          schema_file_with_missing_entity)
Example #2
0
"""An example explaining how to upload files using the transport layer."""

import sys
import logging
from osp.wrappers.sqlite import SqliteSession
from osp.core.session import TransportSessionServer
from osp.core.namespaces import cuba
from osp.wrappers.dataspace import DataspaceSession

logging.getLogger("osp.core.session.transport").setLevel(logging.DEBUG)

if sys.argv[-1] == "client":
    print("Please specify where you want to cache the files on the client:")
    with DataspaceSession("ws://127.0.0.1:4587",
                          input("file destination: > ")) as session:
        wrapper = cuba.Wrapper(session=session)
        file = cuba.File(path=input("file to upload: > "))
        wrapper.add(file, rel=cuba.activeRelationship)
        session.commit()

else:
    print("Please specify where you want to cache the files on the server:")
    file_destination = input("file destination: > ")
    print("Starting server now.")
    print("Please call 'python %s client' to connect" % __file__)
    TransportSessionServer(
        SqliteSession, "localhost", 4587,
        session_kwargs={"path": "test.db"},
        file_destination=file_destination
    ).startListening()
Example #3
0
    def test_path(self):
        """Tests that the path of the CUBA file on the client is correct.

        Created due to issue #652.
        """
        # Upload file and retrieve twice. Both paths should be correct.
        with TransportSessionClient(
            SqliteSession, URI, file_destination=CLIENT_DIR
        ) as session:
            wrapper = city.CityWrapper(session=session)
            file = cuba.File(path=FILE_PATHS[1])  # `f1.jpg`
            wrapper.add(file)
            session.commit()

        with TransportSessionClient(
            SqliteSession, URI, file_destination=CLIENT_DIR
        ) as session:
            wrapper = city.CityWrapper(session=session)
            file = wrapper.get(file.uid)
            path1 = file.path

        with TransportSessionClient(
            SqliteSession, URI, file_destination=CLIENT_DIR
        ) as session:
            wrapper = city.CityWrapper(session=session)
            file = wrapper.get(file.uid)
            path2 = file.path

        self.assertEqual(os.path.dirname(path1), CLIENT_DIR)
        self.assertEqual(os.path.dirname(path2), CLIENT_DIR)

        # Upload two identical instances of the same file, also upload two
        # different files with the same name.
        # The CLIENT_DIR is not cleaned up on purpose.
        with TransportSessionClient(
            SqliteSession, URI, file_destination=CLIENT_DIR
        ) as session:
            wrapper = city.CityWrapper(session=session)
            file1 = cuba.File(path=FILE_PATHS[0])  # `f0`
            file2 = cuba.File(path=FILE_PATHS[0])  # `f0`
            file3 = cuba.File(path=FILE_PATHS[1])  # `f1.jpg`
            file4 = cuba.File(path=SECOND_FILE_PATHS[1])  # `f1.jpg` (second
            # file)
            wrapper.add(file1, file2, file3, file4)
            session.commit()

        with TransportSessionClient(
            SqliteSession, URI, file_destination=CLIENT_DIR
        ) as session:
            wrapper = city.CityWrapper(session=session)
            path1 = wrapper.get(file1.uid).path
            path2 = wrapper.get(file2.uid).path
            path3 = wrapper.get(file3.uid).path
            path4 = wrapper.get(file4.uid).path

        self.assertTrue(
            all(
                path.startswith(CLIENT_DIR)
                for path in (path1, path2, path3, path4)
            ),
            True,
        )
        self.assertEqual(path1, path2)
        self.assertNotEqual(path3, path4)

        # Now clean up the folder (make sure that the server can find the
        # files, that is, the path is correct for the server).
        for file in os.listdir(CLIENT_DIR):
            os.remove(os.path.join(CLIENT_DIR, file))
        with TransportSessionClient(
            SqliteSession, URI, file_destination=CLIENT_DIR
        ) as session:
            wrapper = city.CityWrapper(session=session)
            path1 = wrapper.get(file1.uid).path
            path2 = wrapper.get(file2.uid).path
            path3 = wrapper.get(file3.uid).path
            path4 = wrapper.get(file4.uid).path

        self.assertTrue(
            all(
                path.startswith(CLIENT_DIR)
                for path in (path1, path2, path3, path4)
            ),
            True,
        )
        self.assertEqual(path1, path2)
        self.assertNotEqual(path3, path4)