def declare_models(dialect, db, module=__name__, mixins=[], excludes=[], includes=[]): """ Declare model types read from database in the specified module. Parameters ---------- dialect: module A module exporting `read_schema` function and `mixins` classes. db: pyracmon.connection.Connection Wrapper of DB-API 2.0 Connection. module: str | module A module name where the declarations are located. mixins: [type] Additional mixin classes for declaring model types. excludes: [str] Excluding table names. includes: [str] Including table names. All tables excluding `excludes` are declared as models if this argument is omitted. """ tables = dialect.read_schema(db, excludes, includes) for t in tables: if isinstance(module, types.ModuleType): module.__dict__[t.name] = define_model( t, mixins + dialect.mixins + [CRUDMixin, GraphEntityMixin]) else: sys.modules[module].__dict__[t.name] = define_model( t, mixins + dialect.mixins + [CRUDMixin, GraphEntityMixin])
def test_model_graph(): t1 = Table("t1", [ Column("c1", int, None, True, False, None), Column("c2", int, None, False, False, None), ]) t2 = Table("t2", [ Column("c1", int, None, True, False, None), Column("c2", int, None, False, False, None), ]) m1 = define_model(t1, [CRUDMixin, GraphEntityMixin]) m2 = define_model(t2, [CRUDMixin, GraphEntityMixin]) t = graph_template( m1 = m1, m2 = m2, ) t.m2 >> t.m1 graph = Graph(t) graph.append( m1 = m1(c1 = 1, c2 = "a"), m2 = m2(c1 = 1, c2 = 1), ) graph.append( m1 = m1(c1 = 2, c2 = "b"), m2 = m2(c1 = 2, c2 = 2), ) graph.append( m1 = m1(c1 = 2, c2 = "dummy"), m2 = m2(c1 = 3, c2 = 2), ) graph.append( m1 = m1(c1 = None, c2 = None), m2 = m2(c1 = 3, c2 = 2), ) graph.append( m1 = m1(c1 = 3, c2 = "c"), m2 = m2(c1 = 1, c2 = 3), ) graph.append( m1 = m1(c1 = 4, c2 = "d"), m2 = m2(c1 = None, c2 = None), ) assert graph_dict( graph.view, m1 = (), m2 = (), ) == dict( m1 = [ dict(c1 = 1, c2 = "a", m2 = [dict(c1 = 1, c2 = 1)]), dict(c1 = 2, c2 = "b", m2 = [dict(c1 = 2, c2 = 2), dict(c1 = 3, c2 = 2)]), dict(c1 = 3, c2 = "c", m2 = [dict(c1 = 1, c2 = 3)]), dict(c1 = 4, c2 = "d", m2 = []), ] )
def test_last_sequences(self): with _connect() as db: tables = read_schema(db, includes=["t1"]) m = define_model(tables[0], mixins=[PostgreSQLMixin, CRUDMixin]) c = db.cursor() c.execute("TRUNCATE t1 RESTART IDENTITY CASCADE") c.execute( "INSERT INTO t1 (c12, c13) VALUES (%s, %s), (%s, %s), (%s, %s)", [1, "abc", 2, "def", 3, "ghi"] ) assert [(m.columns[0], 3)] == m.last_sequences(db, 3)
def test_last_sequences(self): with _connect() as db: tables = read_schema(db, includes=["t1"]) m = define_model(tables[0], mixins=[MySQLMixin, CRUDMixin]) c = db.cursor() c.execute("DELETE FROM t1") c.execute("ALTER TABLE t1 AUTO_INCREMENT = 1") c.execute( "INSERT INTO t1 (c12, c13) VALUES (%s, %s), (%s, %s), (%s, %s)", [1, "abc", 2, "def", 3, "ghi"] ) assert [(m.columns[0], 3)] == m.last_sequences(db, 3)
def test_no_pk(): t1 = Table("t1", [ Column("c1", int, None, False, False, None), Column("c2", int, None, False, False, None), ]) m1 = define_model(t1, [CRUDMixin, GraphEntityMixin]) t = graph_template(m1 = m1) graph = Graph(t) graph.append(m1 = m1(c1 = 1, c2 = "a")) graph.append(m1 = m1(c1 = 1, c2 = "a")) assert len(graph.view.m1) == 2
def test_add_serializer(): t1 = Table("t1", [ Column("c1", int, None, True, False, None), Column("c2", int, None, False, False, None), ]) m1 = define_model(t1, [CRUDMixin, GraphEntityMixin]) t = graph_template(m1 = m1) graph = Graph(t) graph.append(m1 = m1(c1 = 1, c2 = "a")) add_serializer(m1, lambda s, x: dict(c1 = x.c1 * 2, c2 = f"__{x.c2}__")) assert graph_dict(graph.view, m1 = ()) == dict(m1 = [dict(c1 = 2, c2 = "__a__")])
def test_add_identifier(): t1 = Table("t1", [ Column("c1", int, None, True, False, None), Column("c2", int, None, False, False, None), ]) m1 = define_model(t1, [CRUDMixin, GraphEntityMixin]) add_identifier(m1, lambda x: x.c2) t = graph_template(m1 = m1) graph = Graph(t) graph.append(m1 = m1(c1 = 1, c2 = "a")) graph.append(m1 = m1(c1 = 2, c2 = "a")) assert len(graph.view.m1) == 1 v = graph.view.m1[0]() assert v.c1 == 1 assert v.c2 == "a"
def test_add_entity_filter(): t1 = Table("t1", [ Column("c1", int, None, True, False, None), Column("c2", int, None, False, False, None), ]) m1 = define_model(t1, [CRUDMixin, GraphEntityMixin]) add_entity_filter(m1, lambda x: x.c1 >= 0) t = graph_template(m1 = m1, v = int) t.m1 << t.v graph = Graph(t) graph.append(m1 = m1(c1 = -1, c2 = "a"), v = 1) graph.append(m1 = m1(c1 = 0, c2 = "b"), v = 2) graph.append(m1 = m1(c1 = 1, c2 = "c"), v = 3) assert [(n().c1, n().c2) for n in graph.view.m1] == [(0, "b"), (1, "c")] assert [n() for n in graph.view.v] == [2, 3] assert [n() for n in graph.view.m1[0].v] == [2] assert [n() for n in graph.view.m1[1].v] == [3]