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()))
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()))
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"])
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])
def handle_memory(self, obj): """Handle a memory event. This function accesses the internal database. It writes a record containing state information and an optional note. :param obj: A :py:class:`~turberfield.dialogue.model.Model.Memory` object. :return: The supplied object. """ if obj.subject is not None: with self.con as db: SchemaBase.note( db, obj.subject, obj.state, obj.object, text=obj.text, html=obj.html, ) return obj
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()} )
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()} )
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