def test_getitem_index(self): connection.connect(':memory:') connection.get_connection().rows = rows = [('row2', )] q = Select(sources=Sql('some_table')) self.assertEqual(q[1], rows[0]) self.assertEqual(connection.get_connection().statements, [('select * from some_table limit 1, 1', ())])
def test_len_with_limit(self): connection.connect(':memory:') q = Select(sources=Sql('some_table')) connection.get_connection().rows = [(5, )] self.assertEqual(len(q[3:]), 2) self.assertEqual(connection.get_connection().statements, [('select count(*) from some_table', ())])
def test_getitem_index_indexerror(self): connection.connect(':memory:') connection.get_connection().rows = rows = [] q = Select(sources=Sql('some_table')) self.assertRaises(IndexError, q.__getitem__, 1) self.assertEqual(connection.get_connection().statements, [('select * from some_table limit 1, 1', ())])
def test_save_insert(self): db = connection.connect(':memory:') db.lastrowid = 1 connection.get_connection().rows = rows = [ ('hello', 'world'), ] obj = SomeModel() obj.column1 = 'hello' obj.column2 = 'world' obj.save() self.assertFalse(obj.orm_new) self.assertEqual(obj.orm_dirty, {}) self.assertEqual(db.statements, [ ('insert into "some_table" ' '("some_column", "other_column") values (?, ?)', ('hello', 'world')), ('select "some_table"."some_column", ' '"some_table"."other_column", ' '"some_table"."oid" ' 'from "some_table" ' 'where "some_table"."oid" = ? ' 'limit 0, 1', (1, )), ]) self.assertColumnEqual(obj.column1, 'hello') self.assertColumnEqual(obj.column2, 'world')
def test_save_insert(self): db = connection.connect(':memory:') db.lastrowid = 1 connection.get_connection().rows = rows = [ ('hello', 'world'), ] obj = SomeModel() obj.column1 = 'hello' obj.column2 = 'world' obj.save() self.assertFalse(obj.orm_new) self.assertEqual(obj.orm_dirty, {}) self.assertEqual(db.statements, [ ( 'insert into "some_table" ' '("some_column", "other_column") values (?, ?)', ('hello', 'world') ), ( 'select "some_table"."some_column", ' '"some_table"."other_column", ' '"some_table"."oid" ' 'from "some_table" ' 'where "some_table"."oid" = ? ' 'limit 0, 1', (1,) ), ]) self.assertColumnEqual(obj.column1, 'hello') self.assertColumnEqual(obj.column2, 'world')
def test_reload(self): db = connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] obj = SomeModel.find()[0] del db.statements[:] connection.get_connection().rows = rows = [ ('row2_1', 'row2_2'), ] obj.reload() self.assertColumnEqual(obj.column1, 'row2_1') self.assertColumnEqual(obj.column2, 'row2_2') self.assertEqual( db.statements, [( 'select "some_table"."some_column", ' '"some_table"."other_column", ' '"some_table"."oid" ' 'from "some_table" ' 'where "some_table"."some_column" = ? ' 'limit 0, 1', ('row1_1',) )] )
def test_exists(self): connection.connect(':memory:') q = Select(sources=Sql('some_table')) self.assertFalse(q.exists()) self.assertEqual(connection.get_connection().statements, [('select 1 from some_table limit 1', ())]) connection.get_connection().rows = rows = [(1, )] self.assertTrue(q.exists())
def test_getitem_index(self): connection.connect(':memory:') connection.get_connection().rows = rows = [('row2',)] q = Select(sources=Sql('some_table')) self.assertEqual(q[1], rows[0]) self.assertEqual( connection.get_connection().statements, [('select * from some_table limit 1, 1', ())] )
def test_getitem_index_indexerror(self): connection.connect(':memory:') connection.get_connection().rows = rows = [] q = Select(sources=Sql('some_table')) self.assertRaises(IndexError, q.__getitem__, 1) self.assertEqual( connection.get_connection().statements, [('select * from some_table limit 1, 1', ())] )
def test_len_with_limit(self): connection.connect(':memory:') q = Select(sources=Sql('some_table')) connection.get_connection().rows = [(5,)] self.assertEqual(len(q[3:]), 2) self.assertEqual( connection.get_connection().statements, [('select count(*) from some_table', ())] )
def test_exists(self): connection.connect(':memory:') q = Select(sources=Sql('some_table')) self.assertFalse(q.exists()) self.assertEqual( connection.get_connection().statements, [('select 1 from some_table limit 1', ())] ) connection.get_connection().rows = rows = [(1,)] self.assertTrue(q.exists())
def test_get(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') a1.a2 = t = ToOne(a1.column1, a2.column1) self.assertTrue(a1.a2 is t) obj = a1.find(a1.column1 == 'row1_1')[0].a2 self.assertTrue(isinstance(obj, a2))
def test_dereference_other_column(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] class MyModel(Model): orm_table = 'my_table' my_id = Column() some_models = ToMany(my_id, 'SomeModel.column1') obj = MyModel() self.assertTrue(isinstance(obj.some_models[0], SomeModel))
def test_get_not_found(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') a1.a2 = ToOne(a1.column1, a2.column1) a1_obj = a1.find(a1.column1 == 'row1_1')[0] connection.get_connection().rows = rows = [] a2_obj = a1_obj.a2 self.assertTrue(a2_obj is None)
def test_iter(self): connection.connect(':memory:') connection.get_connection().rows = rows = [('col1', 'col2')] q = Select(sources=Sql('some_table')) self.assertEqual(list(q), rows) self.assertEqual( connection.get_connection().statements, [ ('select * from some_table', ()), # in cpython, list(x) calls len(x) ('select count(*) from some_table', ()), ])
def test_reload_with_converter(self): db = connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] obj = SomeModelAdapterConverter.find()[0] connection.get_connection().rows = rows = [ ('row2_1', 'row2_2'), ] obj.reload() self.assertColumnEqual(obj.column1, 'row2_1') self.assertColumnEqual(obj.column2, 'ROW2_2')
def test_del(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] a = SomeModel.as_alias('m') a.a = ToMany(a.column1, a.column1) obj = a() def del_col(): del obj.a self.assertRaises(AttributeError, del_col)
def test_iter(self): connection.connect(':memory:') connection.get_connection().rows = rows = [('col1', 'col2')] q = Select(sources=Sql('some_table')) self.assertEqual(list(q), rows) self.assertEqual( connection.get_connection().statements, [ ('select * from some_table', ()), # in cpython, list(x) calls len(x) ('select count(*) from some_table', ()), ] )
def test_dereference_other_column_set(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] class MyModel(Model): orm_table = 'my_table' my_id = Column() some_model = ToOne(my_id, 'SomeModel.column1') obj = MyModel() obj.some_model = SomeModel() other_column = obj.__class__.__dict__['some_model'].other_column self.assertTrue(other_column is SomeModel.column1)
def test_join(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2', 1, 'row1_1', 'row1_2', 1), ] a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') obj1, obj2 = ModelSelect(a1.orm_columns + a2.orm_columns, ExprList([a1, a2]))[0] for obj in (obj1, obj2): self.assertTrue(isinstance(obj, SomeModel)) self.assertColumnEqual(obj.column1, 'row1_1') self.assertColumnEqual(obj.column2, 'row1_2')
def test_get(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') a1.a2 = t = ManyToMany(a1.column1, SomeModelSomeModel.m1_column1, SomeModelSomeModel.m2_column1, a2.column1) self.assertTrue(a1.a2 is t) obj = a1.find(a1.column1 == 'row1_1')[0] res = obj.a2 self.assertTrue(isinstance(res, ModelSelect)) self.assertTrue(isinstance(res[0], a2))
def test_iter_with_converter(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ('row2_1', 'row2_2'), ] q = SomeModelAdapterConverter.find() self.assertTrue(isinstance(q, ModelSelect)) for row, obj in zip(rows, q): self.assertTrue(isinstance(obj, SomeModelAdapterConverter)) self.assertFalse(obj.orm_new) self.assertEqual(obj.orm_dirty, {}) self.assertColumnEqual(obj.column1, row[0]) self.assertColumnEqual(obj.column2, row[1].upper())
def test_del(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] a = SomeModel.as_alias('m') a.a = ToMany(a.column1, a.column1) obj = a() def del_col(): del obj.a self.assertRaises( AttributeError, del_col )
def test_join(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2', 1, 'row1_1', 'row1_2', 1), ] a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') obj1, obj2 = ModelSelect( a1.orm_columns + a2.orm_columns, ExprList([a1, a2]) )[0] for obj in (obj1, obj2): self.assertTrue(isinstance(obj, SomeModel)) self.assertColumnEqual(obj.column1, 'row1_1') self.assertColumnEqual(obj.column2, 'row1_2')
def test_set(self): connection.connect(':memory:') a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') a1.a2 = ToOne(a1.column1, a2.column1) connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] obj1 = a1.find()[0] connection.get_connection().rows = rows = [ ('row2_1', 'row2_2'), ] obj2 = a2.find()[0] obj1.a2 = obj2 self.assertColumnEqual(obj1.column1, 'row2_1')
def test_del(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') a1.a2 = t = ManyToMany(a1.column1, SomeModelSomeModel.m1_column1, SomeModelSomeModel.m2_column1, a2.column1) obj = a1() def del_col(): del obj.a2 self.assertRaises(AttributeError, del_col)
def test_get(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') a1.a2 = t = ManyToMany( a1.column1, SomeModelSomeModel.m1_column1, SomeModelSomeModel.m2_column1, a2.column1 ) self.assertTrue(a1.a2 is t) obj = a1.find(a1.column1 == 'row1_1')[0] res = obj.a2 self.assertTrue(isinstance(res, ModelSelect)) self.assertTrue(isinstance(res[0], a2))
def test_del(self): connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] a1 = SomeModel.as_alias('m1') a2 = SomeModel.as_alias('m2') a1.a2 = t = ManyToMany( a1.column1, SomeModelSomeModel.m1_column1, SomeModelSomeModel.m2_column1, a2.column1 ) obj = a1() def del_col(): del obj.a2 self.assertRaises( AttributeError, del_col )
def test_insert_column_adapter(self): db = connection.connect(':memory:') db.lastrowid = 1 connection.get_connection().rows = rows = [ ('a string', 'world'), ] obj = SomeModelAdapterConverter() obj.column1 = 'a string' obj.save() self.assertEqual(db.statements, [ ('insert into "some_table" ("some_column") values (?)', ('A STRING', )), ('select "some_table"."some_column", ' '"some_table"."other_column", ' '"some_table"."oid" ' 'from "some_table" ' 'where "some_table"."oid" = ? ' 'limit 0, 1', (1, )), ])
def test_reload(self): db = connection.connect(':memory:') connection.get_connection().rows = rows = [ ('row1_1', 'row1_2'), ] obj = SomeModel.find()[0] del db.statements[:] connection.get_connection().rows = rows = [ ('row2_1', 'row2_2'), ] obj.reload() self.assertColumnEqual(obj.column1, 'row2_1') self.assertColumnEqual(obj.column2, 'row2_2') self.assertEqual(db.statements, [('select "some_table"."some_column", ' '"some_table"."other_column", ' '"some_table"."oid" ' 'from "some_table" ' 'where "some_table"."some_column" = ? ' 'limit 0, 1', ('row1_1', ))])
def do_it(i, res, cond1, cond2): with cond1: connection.connect(i) cond1.notify_all() # wait for all threads to connect before appending to res with cond1: while len(sqlite3.Connection.instances) < n: cond1.wait() with cond2: con = connection.get_connection() res.append(con.path) cond2.notify()
def test_insert_column_adapter(self): db = connection.connect(':memory:') db.lastrowid = 1 connection.get_connection().rows = rows = [ ('a string', 'world'), ] obj = SomeModelAdapterConverter() obj.column1 = 'a string' obj.save() self.assertEqual(db.statements, [ ( 'insert into "some_table" ("some_column") values (?)', ('A STRING',) ), ( 'select "some_table"."some_column", ' '"some_table"."other_column", ' '"some_table"."oid" ' 'from "some_table" ' 'where "some_table"."oid" = ? ' 'limit 0, 1', (1,) ), ])
def test_get_connection(self): self.assertRaises(RuntimeError, connection.get_connection) con = connection.connect(':memory:') self.assertEqual(connection.get_connection(), con)