Exemplo n.º 1
0
 def test_serializable(self):
     """Test function to make Cuds objects json serializable."""
     p = city.Citizen(age=23, name="Peter", uid=uuid.UUID(int=123))
     c = city.City(name="Freiburg", uid=uuid.UUID(int=1))
     c1 = city.Person(uid=uuid.UUID(int=2))
     c2 = city.Person(uid=uuid.UUID(int=3))
     p.add(c, rel=city.INVERSE_OF_hasInhabitant)
     p.add(c1, c2, rel=city.hasChild)
     assertJsonLdEqual(self, CUDS_DICT, serializable(p))
     assertJsonLdEqual(self, [CUDS_DICT], serializable([p]))
     assertJsonLdEqual(self, None, serializable(None))
     assertJsonLdEqual(self, [None, None], serializable([None, None]))
     assertJsonLdEqual(self,
                       {"UUID": "00000000-0000-0000-0000-000000000001"},
                       serializable(uuid.UUID(int=1)))
     assertJsonLdEqual(self, [{
         "UUID": "00000000-0000-0000-0000-000000000001"
     }, {
         "UUID": "00000000-0000-0000-0000-000000000002"
     }], serializable([uuid.UUID(int=1), uuid.UUID(int=2)]))
     assertJsonLdEqual(self, {"ENTITY": "city.Citizen"},
                       serializable(city.Citizen))
     assertJsonLdEqual(self, [{
         "ENTITY": "city.Citizen"
     }, {
         "ENTITY": "city.City"
     }], serializable([city.Citizen, city.City]))
     assertJsonLdEqual(self, [1, 1.2, "hallo"],
                       serializable([1, 1.2, "hallo"]))
Exemplo n.º 2
0
    def test_dummy_sim_wrapper(self):
        """Create a dummy simulation syntactic layer + test."""
        with SimDummySession() as session:
            wrapper = city.CitySimWrapper(numSteps=1, session=session)
            c = city.City(name="Freiburg")
            p1 = city.Person(name="Hans", age=34)
            p2 = city.Person(name="Renate", age=54)
            cw, _, _ = wrapper.add(c, p1, p2)

            session.run()

            self.assertEqual(len(
                wrapper.get(oclass=city.Person,
                            rel=city.hasPart)), 1)
            self.assertEqual(len(
                cw.get(oclass=city.Citizen,
                       rel=city.hasInhabitant)), 1)
            self.assertEqual(wrapper.get(p2.uid).name, "Renate")
            self.assertEqual(wrapper.get(p2.uid).age, 55)
            self.assertEqual(cw.get(p1.uid).name, "Hans")
            self.assertEqual(cw.get(p1.uid).age, 35)

            session.run()
            wrapper.add(city.Person(name="Peter"))
            self.assertRaises(RuntimeError, session.run)
Exemplo n.º 3
0
 def test_pretty_print(self):
     """Test printing cuds objects in a human readable way."""
     c, p1, p2, p3, n1, n2, s1 = get_test_city()
     px = city.Person()
     c.add(px, rel=city.encloses)
     f = io.StringIO()
     pretty_print(c, file=f)
     self.maxDiff = 5000
     self.assertEqual(
         f.getvalue(), "\n".join([
             "- Cuds object named <Freiburg>:",
             "  uuid: %s" % c.uid, "  type: city.City",
             "  superclasses: city.City, city.GeographicalPlace, " +
             "city.PopulatedPlace, cuba.Entity",
             "  values: coordinates: [1 2]", "  description: ",
             "    To Be Determined", "", "   |_Relationship city.encloses:",
             "   | -  city.Person cuds object named <John Smith>:",
             "   |    uuid: %s" % px.uid, "   |    age: 25",
             "   |_Relationship city.hasInhabitant:",
             "   | -  city.Citizen cuds object named <Carlos>:",
             "   | .  uuid: %s" % p2.uid, "   | .  age: 25",
             "   | .   |_Relationship city.hasChild:",
             "   | .     -  city.Citizen cuds object named <Maria>:",
             "   | .        uuid: %s" % p3.uid, "   | .        age: 25",
             "   | -  city.Citizen cuds object named <Maria>:",
             "   | .  uuid: %s" % p3.uid, "   | .  (already printed)",
             "   | -  city.Citizen cuds object named <Rainer>:",
             "   |    uuid: %s" % p1.uid, "   |    age: 25",
             "   |     |_Relationship city.hasChild:",
             "   |       -  city.Citizen cuds object named <Maria>:",
             "   |          uuid: %s" % p3.uid,
             "   |          (already printed)",
             "   |_Relationship city.hasPart:",
             "     -  city.Neighborhood cuds object named <St. Georgen>:",
             "     .  uuid: %s" % n2.uid, "     .  coordinates: [3 4]",
             "     .   |_Relationship city.hasPart:",
             "     .     -  city.Street cuds object named <Lange Straße>:",
             "     .        uuid: %s" % s1.uid,
             "     .        coordinates: [4 5]",
             "     .         |_Relationship city.hasInhabitant:",
             "     .           -  city.Citizen cuds object named <Carlos>:",
             "     .           .  uuid: %s" % p2.uid,
             "     .           .  (already printed)",
             "     .           -  city.Citizen cuds object named <Maria>:",
             "     .              uuid: %s" % p3.uid,
             "     .              (already printed)",
             "     -  city.Neighborhood cuds object named <Zähringen>:",
             "        uuid: %s" % n1.uid, "        coordinates: [2 3]",
             "         |_Relationship city.hasPart:",
             "           -  city.Street cuds object named <Lange Straße>:",
             "              uuid: %s" % s1.uid,
             "              (already printed)", ""
         ]))
Exemplo n.º 4
0
    def test_creation(self):
        """Tests the instantiation and type of the objects."""
        self.assertRaises(
            TypeError,
            city.City,
            name="name",
            coordinates=[1, 2],
            uid=0,
            unwanted="unwanted",
        )
        self.assertRaises(TypeError, city.City)

        c = city.City(name="a city")
        p = city.Person()
        self.assertEqual(c.oclass, city.City)
        self.assertEqual(p.oclass, city.Person)

        self.assertRaises(TypeError, cuba.Nothing)
        self.assertRaises(TypeError, cuba.Wrapper)
        cuba.Wrapper(session=CoreSession())
    wrapper = city.CityWrapper(session=session)
    wrapper.add(emmo_town)
    session.commit()

# Load from the DB.
with SqlAlchemyWrapperSession(postgres_url) as db_session:
    db_wrapper = city.CityWrapper(session=db_session)
    db_emmo_town = db_wrapper.get(emmo_town.uid)
    print("The database contains the following information about the city:")
    pretty_print(db_emmo_town)

    # Working with a Simulation wrapper
    with SimDummySession() as sim_session:
        sim_wrapper = city.CitySimWrapper(numSteps=1,
                                          session=sim_session)
        new_inhabitant = city.Person(age=31, name="Peter")
        sim_emmo_town, _ = sim_wrapper.add(db_emmo_town, new_inhabitant)
        sim_session.run()
        print("The city has a new inhabitant:")
        pretty_print(sim_emmo_town.get(new_inhabitant.uid))

    # update database
    db_wrapper.update(sim_emmo_town)
    db_session.commit()

# Check if database contains the changes of the simulation.
with SqlAlchemyWrapperSession(postgres_url) as db_session:
    db_wrapper = city.CityWrapper(session=db_session)
    db_emmo_town = db_wrapper.get(emmo_town.uid)
    print("The database contains the following information about the city:")
    pretty_print(db_emmo_town)