Exemplo n.º 1
0
    def test_get_neighbor_diff(self):
        """Test get_neighbor_diff method."""
        c1 = city.City(name="Paris")
        c2 = city.City(name="Berlin")
        c3 = city.City(name="London")
        n1 = city.Neighborhood(name="Zähringen")
        n2 = city.Neighborhood(name="Herdern")
        s1 = city.Street(name="Waldkircher Straße")
        s2 = city.Street(name="Habsburger Straße")
        s3 = city.Street(name="Lange Straße")

        n1.add(c1, c2, rel=city.isPartOf)
        n2.add(c2, c3, rel=city.isPartOf)
        n1.add(s1, s2)
        n2.add(s2, s3)

        self.assertEqual(set(get_neighbor_diff(n1, n2)),
                         {(c1.uid, city.isPartOf), (s1.uid, city.hasPart)})

        self.assertEqual(set(get_neighbor_diff(n2, n1)),
                         {(c3.uid, city.isPartOf), (s3.uid, city.hasPart)})

        self.assertEqual(
            set(get_neighbor_diff(n1, None)), {(c1.uid, city.isPartOf),
                                               (s1.uid, city.hasPart),
                                               (c2.uid, city.isPartOf),
                                               (s2.uid, city.hasPart)})

        self.assertEqual(set(get_neighbor_diff(None, n2)), set())
Exemplo n.º 2
0
    def test_get_not_reachable(self):
        """Test the pruning method."""
        cities = list()
        for i in range(3):
            c = city.City(name="city %s" % i)
            cities.append(c)
            for j in range(2):
                n = city.Neighborhood(name="neighborhood %s %s" % (i, j))
                c.add(n)
                for k in range(2):
                    s = city.Street(name="street %s %s %s" % (i, j, k))
                    n.add(s)
        registry = cities[0].session._registry
        result = registry._get_not_reachable(cities[2].uid)
        self.assertEqual(
            set([k.name for k in result]),
            set([
                "city 0", "city 1", "neighborhood 0 0", "neighborhood 0 1",
                "neighborhood 1 0", "neighborhood 1 1", "street 0 0 0",
                "street 0 0 1", "street 0 1 0", "street 0 1 1", "street 1 0 0",
                "street 1 0 1", "street 1 1 0", "street 1 1 1"
            ]))

        roots = [
            n for n in cities[0].get() if n.name.startswith("neighborhood 0")
        ]
        registry.prune(*roots, rel=cuba.passiveRelationship)
        self.assertEqual(
            set([k.name for k in registry.values()]),
            set(["neighborhood 0 0", "neighborhood 0 1", "city 0"]))
Exemplo n.º 3
0
    def test_prune(self):
        """Test the pruning method."""
        cities = list()
        for i in range(3):
            c = city.City(name="city %s" % i)
            cities.append(c)
            for j in range(2):
                n = city.Neighborhood(name="neighborhood %s %s" % (i, j))
                c.add(n)
                for k in range(2):
                    s = city.Street(name="street %s %s %s" % (i, j, k))
                    n.add(s)
        registry = cities[0].session._registry
        registry.prune(*[c.uid for c in cities[0:2]])
        self.assertEqual(
            set([k.name for k in registry.values()]),
            set([
                "city 0", "city 1", "neighborhood 0 0", "neighborhood 0 1",
                "neighborhood 1 0", "neighborhood 1 1", "street 0 0 0",
                "street 0 0 1", "street 0 1 0", "street 0 1 1", "street 1 0 0",
                "street 1 0 1", "street 1 1 0", "street 1 1 1"
            ]))

        root, = [n for n in cities[0].get() if n.name == "neighborhood 0 0"]
        registry.prune(root, rel=cuba.activeRelationship)
        self.assertEqual(
            set([k.name for k in registry.values()]),
            set(["neighborhood 0 0", "street 0 0 0", "street 0 0 1"]))
Exemplo n.º 4
0
 def test_serialize(self):
     """Test the serialize function."""
     c = branch(
         city.City(name="Freiburg", uid=1),
         branch(city.Neighborhood(name="Littenweiler", uid=2),
                city.Street(name="Schwarzwaldstraße", uid=3)))
     self.maxDiff = None
     assertJsonLdEqual(self, json.loads(serialize(c)), CUDS_LIST)
     assertJsonLdEqual(self, serialize(c, json_dumps=False), CUDS_LIST)
 def fill_db(self, c, random_uid=True):
     """Fill the database with data."""
     for i in range(self.iterations):
         j = i * 9
         uids = iter([None for i in range(9)])
         if not random_uid:
             uids = iter(range(j * 9 + 1, (j + 1) * 9 + 1))
         c.add(city.Citizen(uid=next(uids)), rel=city.hasInhabitant)
         c.add(city.Citizen(uid=next(uids)), rel=city.encloses)
         c.add(city.Citizen(uid=next(uids)), rel=city.hasPart)
         c.add(city.Neighborhood(name="", uid=next(uids)),
               rel=city.hasInhabitant)
         c.add(city.Neighborhood(name="", uid=next(uids)),
               rel=city.encloses)
         c.add(city.Neighborhood(name="", uid=next(uids)), rel=city.hasPart)
         c.add(city.Street(name="", uid=next(uids)), rel=city.hasInhabitant)
         c.add(city.Street(name="", uid=next(uids)), rel=city.encloses)
         c = c.add(city.Street(name="", uid=next(uids)), rel=city.hasPart)
Exemplo n.º 6
0
 def setUp(self):
     """Start the timer."""
     if not RUN_PERFORMANCE_TEST:
         return
     self.iterations = 100000
     self.c = city.City(name="A big city")
     for i in range(10):
         j = i * 9
         self.c.add(city.Citizen(uid=j + 0), rel=city.hasInhabitant)
         self.c.add(city.Citizen(uid=j + 1), rel=city.encloses)
         self.c.add(city.Citizen(uid=j + 2), rel=city.hasPart)
         self.c.add(city.Neighborhood(name="", uid=j + 3),
                    rel=city.hasInhabitant)
         self.c.add(city.Neighborhood(name="", uid=j + 4),
                    rel=city.encloses)
         self.c.add(city.Neighborhood(name="", uid=j + 5), rel=city.hasPart)
         self.c.add(city.Street(name="", uid=j + 6), rel=city.hasInhabitant)
         self.c.add(city.Street(name="", uid=j + 7), rel=city.encloses)
         self.c.add(city.Street(name="", uid=j + 8), rel=city.hasPart)
     self.start = time.time()
Exemplo n.º 7
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)
Exemplo n.º 8
0
 def _benchmark_set_up(self):
     self.city = city.City(name="Freiburg")
     citizen = city.Citizen(name="Citizen")
     streets = tuple(
         city.Street(name=f"street {i}") for i in range(self.size - 1)
     )
     position = random.randint(0, (self.size - 1) - 1)
     things = list(streets)
     things.insert(position, citizen)
     things = tuple(things)
     rel = {position: city.hasInhabitant}
     for i, thing in enumerate(things):
         self.city.add(thing, rel=rel.get(i, city.hasPart))
Exemplo n.º 9
0
def get_test_city():
    """Set up a test City for the tests."""
    c = city.City(name="Freiburg", coordinates=[1, 2])
    p1 = city.Citizen(name="Rainer")
    p2 = city.Citizen(name="Carlos")
    p3 = city.Citizen(name="Maria")
    n1 = city.Neighborhood(name="Zähringen", coordinates=[2, 3])
    n2 = city.Neighborhood(name="St. Georgen", coordinates=[3, 4])
    s1 = city.Street(name="Lange Straße", coordinates=[4, 5])

    c.add(p1, p2, p3, rel=city.hasInhabitant)
    p1.add(p3, rel=city.hasChild)
    p2.add(p3, rel=city.hasChild)
    c.add(n1, n2)
    n1.add(s1)
    n2.add(s1)
    s1.add(p2, p3, rel=city.hasInhabitant)
    return [c, p1, p2, p3, n1, n2, s1]
Exemplo n.º 10
0
 def test_get_subtree(self):
     """Tests the get_subtree method."""
     c = city.City(name="a city")
     p = city.Citizen()
     n = city.Neighborhood(name="a neighborhood")
     s = city.Street(name="The street")
     c.add(p, rel=city.hasInhabitant)
     c.add(n)
     n.add(s)
     registry = c.session._registry
     self.assertEqual(registry.get_subtree(c.uid), set([c, p, n, s]))
     self.assertEqual(
         registry.get_subtree(c.uid, rel=cuba.activeRelationship),
         set([c, p, n, s]))
     self.assertEqual(registry.get_subtree(n.uid), set([c, p, n, s]))
     self.assertEqual(
         registry.get_subtree(n.uid, rel=cuba.activeRelationship),
         set([n, s]))
Exemplo n.º 11
0
    def test_update(self):
        """Tests the standard, normal behavior of the update() method."""
        c = city.City(name="a city")
        n = city.Neighborhood(name="a neighborhood")
        new_n = create_from_cuds_object(n, CoreSession())
        new_s = city.Street(name="a new street")
        new_n.add(new_s)
        c.add(n)

        old_neighborhood = c.get(n.uid)
        old_streets = old_neighborhood.get(oclass=city.Street)
        self.assertEqual(old_streets, [])

        c.update(new_n)

        new_neighborhood = c.get(n.uid)
        self.assertIs(new_neighborhood, n)
        new_streets = new_neighborhood.get(oclass=city.Street)
        self.assertEqual(new_streets, [new_s])

        self.assertRaises(ValueError, c.update, n)
Exemplo n.º 12
0
 def test_notify_delete_call(self):
     """Tests if notify_delete is called, when we call prune."""
     deleted = set()
     session = TestSession(notify_delete=lambda x: deleted.add(x))
     w = city.CityWrapper(session=session)
     cities = list()
     for i in range(3):
         c = city.City(name="city %s" % i)
         cw = w.add(c)
         cities.append(cw)
         for j in range(2):
             n = city.Neighborhood(name="neighborhood %s %s" % (i, j))
             cw.add(n)
             nw = cw.get(n.uid)
             for k in range(2):
                 s = city.Street(name="street %s %s %s" % (i, j, k))
                 nw.add(s)
     w.remove(cities[1].uid, cities[2].uid)
     expected_deletion = {
         x.uid
         for x in session._registry.values()
         if (hasattr(x, "name") and x.name in {
             "city 2", "neighborhood 2 0", "neighborhood 2 1",
             "street 2 0 0", "street 2 0 1", "street 2 1 0", "street 2 1 1",
             "city 1", "neighborhood 1 0", "neighborhood 1 1",
             "street 1 0 0", "street 1 0 1", "street 1 1 0", "street 1 1 1"
         })
     }
     session.prune(rel=None)
     self.assertEqual(
         set([
             "wrapper" if k.is_a(cuba.Wrapper) else k.name
             for k in session._registry.values()
         ]),
         set([
             "city 0", "neighborhood 0 0", "neighborhood 0 1",
             "street 0 0 0", "street 0 0 1", "street 0 1 0", "street 0 1 1",
             "wrapper"
         ]))
     self.assertEqual(set([d.uid for d in deleted]), expected_deletion)
Exemplo n.º 13
0
    def test_get_subtree(self):
        """Tests the get_subtree method."""
        c = city.City(name="a city")
        p = city.Citizen()
        n = city.Neighborhood(name="a neighborhood")
        s = city.Street(name="The street")
        c.add(p, rel=city.hasInhabitant)
        c.add(n)
        n.add(s)
        registry = c.session._registry
        self.assertEqual(registry.get_subtree(c.uid), {c, p, n, s})
        self.assertEqual(
            registry.get_subtree(c.uid, rel=cuba.activeRelationship),
            {c, p, n, s},
        )
        self.assertEqual(registry.get_subtree(n.uid), {c, p, n, s})
        self.assertEqual(
            registry.get_subtree(n.uid, rel=cuba.activeRelationship), {n, s})

        c_o = city.City(name="other city")
        n.add(c_o, rel=city.isPartOf)
        self.assertEqual(registry.get_subtree(c.uid), {c, p, n, s, c_o})
        self.assertEqual(
            registry.get_subtree(c.uid, rel=cuba.activeRelationship),
            {c, p, n, s},
        )
        self.assertEqual(registry.get_subtree(n.uid), {c, p, n, s, c_o})
        self.assertEqual(
            registry.get_subtree(n.uid, rel=cuba.activeRelationship), {n, s})

        # Test whether cycles are a problem
        c_o.add(c, rel=cuba.relationship)
        self.assertEqual(registry.get_subtree(c_o.uid), {c, p, n, s, c_o})

        # Disconnected items
        c_f = city.City(name="far city")
        self.assertEqual(registry.get_subtree(c.uid), {c, p, n, s, c_o})
        self.assertEqual(registry.get_subtree(c_f.uid), {c_f})
Exemplo n.º 14
0
    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)
        "server"]
try:
    p = subprocess.Popen(args)
except FileNotFoundError:
    args[0] = "python"
    p = subprocess.Popen(args)
time.sleep(5)

try:
    # Construct the Datastructure.
    c = city.City(name="Freiburg")
    p1 = city.Citizen(name="Peter")
    p2 = city.Citizen(name="Hans")
    p3 = city.Citizen(name="Michel")
    n = city.Neighborhood(name="Zähringen")
    s = city.Street(name="Le street")
    b = city.Building(name="Theater")
    a = city.Address(postalCode=79123, name='Le street', number=12)
    c.add(p1, p2, p3, rel=city.hasInhabitant)
    c.add(n).add(s).add(b).add(a)

    print("Connect to DB via transport session")
    with TransportSessionClient(
        SqliteSession, "ws://localhost:8688", path="test.db"
    ) as session:
        wrapper = city.CityWrapper(session=session)
        wrapper.add(c)
        wrapper.session.commit()

    print("Reconnect and check if data is still there")
    with TransportSessionClient(
emmo_town.add(city.Citizen(name='Emanuele Ghedini'), rel=city.hasInhabitant)
emmo_town.add(city.Citizen(name='Adham Hashibon'), rel=city.hasInhabitant)
emmo_town.add(city.Citizen(name='Jesper Friis'),
              city.Citizen(name='Gerhard Goldbeck'),
              city.Citizen(name='Georg Schmitz'),
              city.Citizen(name='Anne de Baas'),
              rel=city.hasInhabitant)

emmo_town.add(city.Neighborhood(name="Ontology"))
emmo_town.add(city.Neighborhood(name="User cases"))

ontology_uid = None
for neighborhood in emmo_town.get(oclass=city.Neighborhood):
    if neighborhood.name == "Ontology":
        ontology_uid = neighborhood.uid
        neighborhood.add(city.Street(name="Relationships"), rel=city.hasPart)
        neighborhood.add(city.Street(name="Entities"), rel=city.hasPart)

onto = emmo_town.get(ontology_uid)

# We can go through inverse relationships
print(onto.get(rel=city.isPartOf)[0].name + ' is my city!')

# Working with a DB-wrapper: Store in the DB.
with SqlAlchemyWrapperSession(postgres_url) as session:
    wrapper = city.CityWrapper(session=session)
    wrapper.add(emmo_town)
    session.commit()

# Load from the DB.
with SqlAlchemyWrapperSession(postgres_url) as db_session: