def test_manual(self): test_db.set_autocommit(False) # Will not be rolled back. self.assertRaises(IntegrityError, Blog.create) if issubclass(database_class, PostgresqlDatabase): self.assertRaises(InternalError, User.create, username='******') test_db.rollback() u = User.create(username='******') test_db.commit() u_db = User.get(User.username == 'u') self.assertEqual(u.id, u_db.id)
def test_autocommit(self): test_db.set_autocommit(False) test_db.begin() u1 = User.create(username='******') u2 = User.create(username='******') # open up a new connection to the database, it won't register any blogs # as being created new_db = self.new_connection() res = new_db.execute_sql('select count(*) from users;') self.assertEqual(res.fetchone()[0], 0) # commit our blog inserts test_db.commit() # now the blogs are query-able from another connection res = new_db.execute_sql('select count(*) from users;') self.assertEqual(res.fetchone()[0], 2)
def test_outer_loop_inner_commit(self): # By default we are in autocommit mode (isolation_level=None). self.assertEqual(test_db.get_conn().isolation_level, None) for username in ['u1', 'u2', 'u3']: User.create(username=username) for user in User.select(): Blog.create(user=user, title='b-%s' % user.username) # These statements are auto-committed. new_db = self.new_connection() count = new_db.execute_sql('select count(*) from blog;').fetchone() self.assertEqual(count[0], 3) self.assertEqual(Blog.select().count(), 3) blog_titles = [b.title for b in Blog.select().order_by(Blog.title)] self.assertEqual(blog_titles, ['b-u1', 'b-u2', 'b-u3']) self.assertEqual(Blog.delete().execute(), 3) # If we disable autocommit, we need to explicitly call begin(). test_db.set_autocommit(False) test_db.begin() for user in User.select(): Blog.create(user=user, title='b-%s' % user.username) # These statements have not been committed. new_db = self.new_connection() count = new_db.execute_sql('select count(*) from blog;').fetchone() self.assertEqual(count[0], 0) self.assertEqual(Blog.select().count(), 3) blog_titles = [b.title for b in Blog.select().order_by(Blog.title)] self.assertEqual(blog_titles, ['b-u1', 'b-u2', 'b-u3']) test_db.commit() count = new_db.execute_sql('select count(*) from blog;').fetchone() self.assertEqual(count[0], 3)