コード例 #1
0
ファイル: test_orm.py プロジェクト: nod/databag
 def setUp(self):
     self.dbag = DictBag(Faker.table_name())
     Faker.set_db(self.dbag)
コード例 #2
0
ファイル: test_dbag.py プロジェクト: nod/databag
 def setUpClass(cls):
     cls.dbag = DictBag(table='testdbag', indexes=(('x', 'y'), ))
     cls.add_data()
コード例 #3
0
 def _db(cls):
     if cls.__db is None:
         cls.__db = DictBag(cls.table_name(), dbconn.dbpath)
     return cls.__db
コード例 #4
0
ファイル: test_dbag.py プロジェクト: nod/databag
 def setUpClass(cls):
     cls.dbag = DictBag(table='testdbag')
     cls.add_data()
コード例 #5
0
ファイル: test_dbag.py プロジェクト: nod/databag
 def setUp(self):
     self.dbag = DictBag(table='testdbag', indexes=(('name', 'age'), ))
コード例 #6
0
ファイル: test_dbag.py プロジェクト: nod/databag
class TestDictBag(unittest.TestCase):
    def setUp(self):
        self.dbag = DictBag(table='testdbag', indexes=(('name', 'age'), ))

    def test_make_index_name(self):
        self.assertEqual(self.dbag._make_index_name(('name', 'age')),
                         'idx_testdbag_age_name')

    def test_ensure_index(self):
        # test simple index
        self.dbag.ensure_index(('xx', ))

        cur = self.dbag._db.cursor()
        cur.execute("""
            select count(1) as cnt from sqlite_master
                where type='table' and name='idx_testdbag_xx'
            """)
        self.assertEqual(1, cur.fetchone()['cnt'])

        cur.execute("""
            select count(1) as cnt from sqlite_master
                where type='index' and name='i_idx_testdbag_xx'
            """)
        self.assertEqual(1, cur.fetchone()['cnt'])

        self.dbag.ensure_index(('yy', 'xx'))
        cur = self.dbag._db.cursor()
        cur.execute("""
            select count(1) as cnt from sqlite_master
                where type='table' and name='idx_testdbag_xx_yy'
            """)
        self.assertEqual(1, cur.fetchone()['cnt'])

        cur.execute("""
            select count(1) as cnt from sqlite_master
                where type='index' and name='i_idx_testdbag_xx_yy'
            """)
        self.assertEqual(1, cur.fetchone()['cnt'])

    def test_Q_queries(self):
        x = Q('x')
        x = x < 44
        where, params = x.query()
        self.assertEqual(where, ' "x" < ? ')
        self.assertEqual(params, [44])

        x = x >= 33
        where, params = x.query()
        self.assertEqual(where, ' "x" < ? and "x" >= ? ')
        self.assertEqual(params, [44, 33])

        self.assertEqual(x._and_ops, set([(operator.lt, 44),
                                          (operator.ge, 33)]))

        x = x == 'stop'
        where, params = x.query()
        self.assertEqual(where, ' "x" < ? and "x" >= ? and "x" = ? ')
        self.assertEqual(params, [44, 33, 'stop'])

        # test a compound statement
        x = Q('x')
        x = 10 < x < 20
        where, params = x.query()
        self.assertEqual(where, ' "x" > ? and "x" < ? ')
        self.assertEqual(params, [10, 20])

    def test_matching_index(self):
        self.dbag.ensure_index(('yy', 'xx'))
        self.dbag.ensure_index(('yy', ))
        self.dbag.ensure_index(('zz', ))

        self.assertEqual(('xx', 'yy'), self.dbag._find_matching_index(
            ('xx', )))

        self.assertEqual(('zz', ), self.dbag._find_matching_index(('zz', )))

        self.assertEqual(('xx', 'yy'), self.dbag._find_matching_index(
            ('xx', )))

        self.assertEqual(None, self.dbag._find_matching_index(('xx', 'zz')))

        self.assertEqual(None, self.dbag._find_matching_index(('not there', )))

    def test_only_dicts(self):
        with self.assertRaises(ValueError):
            self.dbag.add('your mom')

    def test_add_to_index(self):
        self.dbag.ensure_index(('x'))
        self.dbag.ensure_index(('x', 'y'))
        self.dbag.ensure_index(('z', 'y'))

        key = self.dbag.add({'x': 23})
        cur = self.dbag._db.cursor()

        idx = self.dbag._make_index_name(('x'))
        cur.execute(
            '''
            select count(1) as cnt from {} where keyf = ?
            '''.format(idx), (key, ))
        self.assertEqual(1, cur.fetchone()['cnt'])

        idx = self.dbag._make_index_name(('x', 'y'))
        cur.execute('''
            select count(1) as cnt from {} where x = 23
            '''.format(idx))
        self.assertEqual(1, cur.fetchone()['cnt'])

        idx = self.dbag._make_index_name(('y', 'z'))
        cur.execute(
            '''
            select count(1) as cnt from {} where keyf = ?
            '''.format(idx), (key, ))
        self.assertEqual(0, cur.fetchone()['cnt'])

    def test_del_from_index(self):
        self.dbag.ensure_index(('x', 'y'))
        self.dbag.add({'x': 22})

    def test_find_kwargs_with_index(self):
        first, second = {'x': 10, 'y': 99}, {'x': 100, 'y': 999}
        self.dbag.ensure_index(('x', 'y'))
        self.dbag.add(first)
        self.dbag.add(second)

        key, found = self.dbag.find_one(x=first['x'])
        self.assertEqual(found['y'], first['y'])

    def test_find_with_Q(self):
        first, second = {'x': 10, 'y': 99}, {'x': 100, 'y': 999}
        self.dbag.ensure_index(('x', 'y'))
        self.dbag.add(first)
        self.dbag.add(second)
        k, ret = next(self.dbag.find(10 < Q('x') < 101))
        self.assertEqual(ret, second)

    def test_not_implemented_search(self):
        with self.assertRaises(NotImplementedError):
            self.dbag.find({'x': {'$zzz': 44}})

    def test_search(self):
        first, second = {'x': 10, 'y': 99}, {'x': 100, 'y': 999}
        self.dbag.ensure_index(('x', 'y'))
        self.dbag.add(first)
        self.dbag.add(second)

        k, ret = next(self.dbag.find({'x': 10}))
        self.assertEqual(ret, first)

        k, ret = next(self.dbag.find({'x': {'$gt': 10}}))
        self.assertEqual(ret, second)

    def test_not_unique(self):
        first, second = {'x': 'same', 'y': 99}, {'x': 'same', 'y': 999}
        self.dbag.ensure_index('x')
        # will throw exception if problems
        self.dbag.add(first)
        self.dbag.add(second)
コード例 #7
0
ファイル: test_dbag.py プロジェクト: baddriverdave/databag
 def setUp(self):
     self.dbag = DictBag( indexes=(('name', 'age'),) )
コード例 #8
0
ファイル: test_dbag.py プロジェクト: baddriverdave/databag
class TestDictBag(unittest.TestCase):

    def setUp(self):
        self.dbag = DictBag( indexes=(('name', 'age'),) )

    def test_make_index_name(self):
        self.assertEqual(
            self.dbag._make_index_name(('name', 'age')),
            'idx_DictBag_age_name'
            )

    def testensure_index(self):

        # test simple index
        self.dbag.ensure_index(('xx',))

        cur = self.dbag._db.cursor()
        cur.execute(
            """
            select count(1) as cnt from sqlite_master
                where type='table' and name='idx_DictBag_xx'
            """
            )
        self.assertEqual( 1, cur.fetchone()['cnt'] )

        cur.execute(
            """
            select count(1) as cnt from sqlite_master
                where type='index' and name='i_idx_DictBag_xx'
            """
            )
        self.assertEqual( 1, cur.fetchone()['cnt'] )

        self.dbag.ensure_index(('yy','xx'))
        cur = self.dbag._db.cursor()
        cur.execute(
            """
            select count(1) as cnt from sqlite_master
                where type='table' and name='idx_DictBag_xx_yy'
            """
            )
        self.assertEqual( 1, cur.fetchone()['cnt'] )

        cur.execute(
            """
            select count(1) as cnt from sqlite_master
                where type='index' and name='i_idx_DictBag_xx_yy'
            """
            )
        self.assertEqual( 1, cur.fetchone()['cnt'] )


    def test_Q_queries(self):
        x = Q('x')
        x = x < 44
        where, params = x.query()
        self.assertEqual( where, ' "x" < ? ')
        self.assertEqual(params, [44])

        x = x >= 33
        where, params = x.query()
        self.assertEqual( where, ' "x" < ? and "x" >= ? ')
        self.assertEqual(params, [44, 33])

        x = x == 'stop'
        where, params = x.query()
        self.assertEqual( where, ' "x" < ? and "x" >= ? and "x" = ? ')
        self.assertEqual(params, [44, 33, 'stop'])

        # test a compound statement
        x = Q('x')
        x = 10 < x < 20
        where, params = x.query()
        self.assertEqual( where, ' "x" > ? and "x" < ? ')
        self.assertEqual(params, [10, 20])

    def test_find_matching_index(self):
        self.dbag.ensure_index(('yy','xx'))
        self.dbag.ensure_index(('yy',))
        self.dbag.ensure_index(('zz',))

        self.assertEqual(
            ('xx', 'yy'),
            self.dbag._find_matching_index(('xx',))
            )

        self.assertEqual(
            ('zz',),
            self.dbag._find_matching_index(('zz',))
            )

        self.assertEqual(
            ('xx','yy'),
            self.dbag._find_matching_index(('xx',))
            )

        self.assertEqual(
            None,
            self.dbag._find_matching_index(('xx','zz'))
            )

        self.assertEqual(
            None,
            self.dbag._find_matching_index(('not there',))
            )

    def test_only_dicts(self):
        with self.assertRaises( ValueError ):
            self.dbag.add('your mom')

    def test_add_to_index(self):
        self.dbag.ensure_index(('x'))
        self.dbag.ensure_index(('x', 'y'))
        self.dbag.ensure_index(('z', 'y'))

        key = self.dbag.add({'x':23})
        cur = self.dbag._db.cursor()

        idx = self.dbag._make_index_name(('x'))
        cur.execute('''
            select count(1) as cnt from {} where keyf = ?
            '''.format( idx ), (key,))
        self.assertEqual( 1, cur.fetchone()['cnt'] )

        idx = self.dbag._make_index_name(('x', 'y'))
        cur.execute('''
            select count(1) as cnt from {} where x = 23
            '''.format( idx ))
        self.assertEqual( 1, cur.fetchone()['cnt'] )

        idx = self.dbag._make_index_name(('y', 'z'))
        cur.execute('''
            select count(1) as cnt from {} where keyf = ?
            '''.format( idx ), (key,))
        self.assertEqual( 0, cur.fetchone()['cnt'] )

    def test_del_from_index(self):
        self.dbag.ensure_index(('x', 'y'))
        self.dbag.add({'x':22})

    def test_find_kwargs(self):
        first, second = {'x':10, 'y':99}, {'x':100, 'y':999}
        self.dbag.ensure_index(('x','y'))
        self.dbag.add(first)
        self.dbag.add(second)

        with self.assertRaises( IndexNotFound ):
            self.dbag.findone(abc=23)

        key, found = self.dbag.findone(x=first['x'])
        self.assertEqual(found['y'], first['y'])

    def test_find_with_query(self):
        first, second = {'x':10, 'y':99}, {'x':100, 'y':999}
        self.dbag.ensure_index(('x','y'))
        self.dbag.add(first)
        self.dbag.add(second)
        k,ret = self.dbag.find( 10 < Q('x') < 101 ).next()
        self.assertEqual( ret, second )