Exemplo n.º 1
0
    def setUp(self):
        database.execute('drop table if exists test_t')
        database.execute('''
create table test_t (
    id bigint not null,
    name varchar(100) not null,
    created_at double not null,
    primary key (id)
)''')
Exemplo n.º 2
0
 def test_execute(self):
     id = 1892421
     # insert:
     n = database.execute(
         'insert into test_t (id, name, created_at) values (?, ?, ?)',
         (id, 'Bob', time.time()))
     self.assertEqual(n, 1)
     # query:
     ts1 = database.select('select * from test_t where id=?', (id, ))
     self.assertEqual(len(ts1), 1)
     self.assertEqual(ts1[0].name, 'Bob')
     # update:
     n = database.execute(
         'update test_t set name=?, created_at=? where id=?',
         ('Tom', time.time(), id))
     self.assertEqual(n, 1)
     # query:
     ts2 = database.select('select * from test_t where id=?', (id, ))
     self.assertEqual(len(ts2), 1)
     self.assertEqual(ts2[0].name, 'Tom')
Exemplo n.º 3
0
    def test_tx(self):
        @database.transactional
        def run_in_tx():
            n = database.execute(
                'insert into test_t (id, name, created_at) values (?, ?, ?)',
                (12345, 'Bob', time.time()))
            self.assertEqual(n, 1)
            raise Exception('will rollback')

        n = database.execute(
            'insert into test_t (id, name, created_at) values (?, ?, ?)',
            (23456, 'Tom', time.time()))
        self.assertEqual(n, 1)
        try:
            run_in_tx()
            self.fail('not rollback')
        except AssertionError:
            raise
        except BaseException:
            pass
        rs = database.select('select * from test_t')
        self.assertEqual(len(rs), 1)
        self.assertEqual(rs[0].name, 'Tom')
Exemplo n.º 4
0
 def run_in_tx():
     n = database.execute(
         'insert into test_t (id, name, created_at) values (?, ?, ?)',
         (12345, 'Bob', time.time()))
     self.assertEqual(n, 1)
     raise Exception('will rollback')
Exemplo n.º 5
0
 def remove(self):
     args = [self.getValue('id')]
     rows = database.execute(self.__delete__, args)
     if rows != 1:
         logging.warning(
             'failed to remove by primary key: affected rows: %s' % rows)
Exemplo n.º 6
0
 def update(self):
     args = list(map(self.getValue, self.__fields__))
     rows = database.execute(self.__update__, args)
     if rows == 0:
         logging.debug('Not update: 0 rows affected.')
Exemplo n.º 7
0
 def save(self):
     args = list(map(self.getValueOrDefault, self.__fields__))
     rows = database.execute(self.__insert__, args)
     if rows != 1:
         logging.warning('failed to save: %s rows affected.' % rows)
Exemplo n.º 8
0
 def setUp(self):
     database.execute('drop table if exists %s' % TestSimple.__table__)
     database.execute(TestSimple.__ddl__)