Exemplo n.º 1
0
    def test_reference_states(self):

        with self.db as con:
            SchemaBase.populate(
                con,
                [SchemaBaseTests.Ownership, SchemaBaseTests.Visibility]
            )

            rv = list(SchemaBase.reference(
                con,
                [SchemaBaseTests.Ownership.lost, SchemaBaseTests.Visibility.visible]
            ))
            self.assertEqual(2, len(rv))
            self.assertEqual(SchemaBaseTests.Ownership.lost.value, rv[0]["value"])
            self.assertEqual(SchemaBaseTests.Visibility.visible.value, rv[1]["value"])
Exemplo n.º 2
0
    def test_note_transitive(self):

        Thing = namedtuple("Thing", ["name"])
        with self.db as con:
            rv = SchemaBase.populate(
                con, [
                    SchemaBaseTests.Ownership,
                    Thing("cat"),
                    Thing("hat")
                ]
            )

            rv = SchemaBase.note(
                con,
                Thing("cat"),
                SchemaBaseTests.Ownership.acquired,
                Thing("hat"),
                text="A cat in a hat!"
            )

            cur = con.cursor()
            cur.execute("select count(*) from touch")
            rv = tuple(cur.fetchone())[0]
            self.assertEqual(1, rv)

            cur.execute(
                "select s.name, state.name, o.name, note.text "
                "from state join touch on state.id = touch.state "
                "join entity as s on touch.sbjct = s.id "
                "left outer join entity as o on touch.objct = o.id "
                "left outer join note on note.touch = touch.id"
            )
            self.assertEqual(("cat", "acquired", "hat", "A cat in a hat!"), tuple(cur.fetchone()))
Exemplo n.º 3
0
    def test_touch_intransitive(self):

        Thing = namedtuple("Thing", ["name"])
        with self.db as con:
            rv = SchemaBase.populate(
                con, [
                    SchemaBaseTests.Visibility,
                    Thing("cat")
                ]
            )

            rv = SchemaBase.touch(
                con,
                Thing("cat"),
                SchemaBaseTests.Visibility.visible
            )

            cur = con.cursor()
            cur.execute("select count(*) from touch")
            rv = tuple(cur.fetchone())[0]
            self.assertEqual(1, rv)

            cur.execute(
                "select s.name, state.name, o.name "
                "from state join touch on state.id = touch.state "
                "join entity as s on touch.sbjct = s.id "
                "left outer join entity as o on touch.objct = o.id"
            )
            self.assertEqual(("cat", "visible", None), tuple(cur.fetchone()))
Exemplo n.º 4
0
    def test_reference_entity(self):

        Thing = namedtuple("Thing", ["name"])
        with self.db as con:
            rv = SchemaBase.populate(
                con, [
                    Thing("apple"), Thing("ball"), Thing("cat")
                ]
            )
            rv = list(SchemaBase.reference(
                con,
                [Thing("ball"), Thing("dog")]
            ))
            self.assertEqual(2, len(rv))
            self.assertEqual("ball", rv[0]["name"])
            self.assertIs(None, rv[1])
Exemplo n.º 5
0
    def test_populate_states(self):

        with self.db as con:
            rv = SchemaBase.populate(
                con,
                [SchemaBaseTests.Ownership, SchemaBaseTests.Visibility]
            )
            self.assertEqual(4, rv)

            cur = con.cursor()
            cur.execute("select count(*) from state")
            rv = tuple(cur.fetchone())[0]
            self.assertEqual(4, rv)

            cur.execute("select * from state")
            self.assertEqual(
                {"Ownership", "Visibility"},
                {row["class"] for row in cur.fetchall()}
            )
Exemplo n.º 6
0
    def test_populate(self):

        Thing = namedtuple("Thing", ["name"])
        with self.db as con:
            rv = SchemaBase.populate(
                con, [
                    SchemaBaseTests.Ownership, SchemaBaseTests.Visibility,
                    Thing("apple"), Thing("ball"), Thing("cat")
                ]
            )
            self.assertEqual(7, rv)

            cur = con.cursor()
            cur.execute("select count(*) from entity")
            rv = tuple(cur.fetchone())[0]
            self.assertEqual(3, rv)

            cur.execute("select * from entity")
            self.assertEqual(
                {"apple", "ball", "cat"},
                {row["name"] for row in cur.fetchall()}
            )
Exemplo n.º 7
0
 def handle_references(self, obj):
     with self.con as db:
         rv = SchemaBase.populate(db, obj)
         self.log.info("Populated {0} rows.".format(rv))
         return rv