Example #1
0
 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', ())])
Example #2
0
 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', ())])
Example #3
0
 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', ())])
Example #4
0
 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')
Example #5
0
 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')
Example #6
0
 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',)
         )]
     )
Example #7
0
 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())
Example #8
0
 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', ())]
     )
Example #9
0
 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', ())]
     )
Example #10
0
 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', ())]
     )
Example #11
0
 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())
Example #12
0
 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))
Example #13
0
 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))
Example #14
0
 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))
Example #15
0
 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)
Example #16
0
 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)
Example #17
0
 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', ()),
         ])
Example #18
0
 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')
Example #19
0
 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')
Example #20
0
    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)
Example #21
0
    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))
Example #22
0
 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', ()),
         ]
     )
Example #23
0
 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)
Example #24
0
 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')
Example #25
0
 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))
Example #26
0
 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())
Example #27
0
 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
     )
Example #28
0
 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())
Example #29
0
 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')
Example #30
0
 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')
Example #31
0
    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)
Example #32
0
    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)
Example #33
0
 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')
Example #34
0
 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))
Example #35
0
 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
     )
Example #36
0
 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, )),
     ])
Example #37
0
 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', ))])
Example #38
0
 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()
Example #39
0
 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,)
         ),
     ])
Example #40
0
 def test_get_connection(self):
     self.assertRaises(RuntimeError, connection.get_connection)
     con = connection.connect(':memory:')
     self.assertEqual(connection.get_connection(), con)