Skip to content
This repository
Browse code

Adding some more tests based on the old versions

  • Loading branch information...
commit 3597298f47a35a9824d687f93f133c19357b9966 1 parent 120ab5c
Charles Leifer authored October 02, 2012

Showing 2 changed files with 117 additions and 5 deletions. Show diff stats Hide diff stats

  1. 23  peewee.py
  2. 99  tests.py
23  peewee.py
@@ -1418,12 +1418,12 @@ def is_closed(self):
1418 1418
     def get_cursor(self):
1419 1419
         return self.get_conn().cursor()
1420 1420
 
  1421
+    def _close(self, conn):
  1422
+        conn.close()
  1423
+
1421 1424
     def _connect(self, database, **kwargs):
1422 1425
         raise NotImplementedError
1423 1426
 
1424  
-    def close(self, conn):
1425  
-        return conn.close()
1426  
-
1427 1427
     def last_insert_id(self, cursor, model):
1428 1428
         if model._meta.auto_increment:
1429 1429
             return cursor.lastrowid
@@ -1491,6 +1491,23 @@ def drop_table(self, model_class, fail_silently=False):
1491 1491
     def transaction(self):
1492 1492
         return transaction(self)
1493 1493
 
  1494
+    def commit_on_success(self, func):
  1495
+        def inner(*args, **kwargs):
  1496
+            orig = self.get_autocommit()
  1497
+            self.set_autocommit(False)
  1498
+            self.begin()
  1499
+            try:
  1500
+                res = func(*args, **kwargs)
  1501
+                self.commit()
  1502
+            except:
  1503
+                self.rollback()
  1504
+                raise
  1505
+            else:
  1506
+                return res
  1507
+            finally:
  1508
+                self.set_autocommit(orig)
  1509
+        return inner
  1510
+
1494 1511
 
1495 1512
 class SqliteDatabase(Database):
1496 1513
     op_overrides = {
99  tests.py
@@ -1200,8 +1200,94 @@ def test_db_column(self):
1200 1200
 
1201 1201
 
1202 1202
 class TransactionTestCase(ModelTestCase):
1203  
-    requires = []
1204  
-    # TODO
  1203
+    requires = [User, Blog]
  1204
+
  1205
+    def tearDown(self):
  1206
+        super(TransactionTestCase, self).tearDown()
  1207
+        test_db.set_autocommit(True)
  1208
+
  1209
+    def test_autocommit(self):
  1210
+        test_db.set_autocommit(False)
  1211
+
  1212
+        u1 = User.create(username='u1')
  1213
+        u2 = User.create(username='u2')
  1214
+
  1215
+        # open up a new connection to the database, it won't register any blogs
  1216
+        # as being created
  1217
+        new_db = database_class(database_name)
  1218
+        res = new_db.execute_sql('select count(*) from users;')
  1219
+        self.assertEqual(res.fetchone()[0], 0)
  1220
+
  1221
+        # commit our blog inserts
  1222
+        test_db.commit()
  1223
+
  1224
+        # now the blogs are query-able from another connection
  1225
+        res = new_db.execute_sql('select count(*) from users;')
  1226
+        self.assertEqual(res.fetchone()[0], 2)
  1227
+
  1228
+    def test_commit_on_success(self):
  1229
+        self.assertTrue(test_db.get_autocommit())
  1230
+
  1231
+        @test_db.commit_on_success
  1232
+        def will_fail():
  1233
+            u = User.create(username='u1')
  1234
+            b = Blog.create() # no blog, will raise an error
  1235
+            return u, b
  1236
+
  1237
+        self.assertRaises(Exception, will_fail)
  1238
+        self.assertEqual(User.select().count(), 0)
  1239
+        self.assertEqual(Blog.select().count(), 0)
  1240
+
  1241
+        @test_db.commit_on_success
  1242
+        def will_succeed():
  1243
+            u = User.create(username='u1')
  1244
+            b = Blog.create(title='b1', user=u)
  1245
+            return u, b
  1246
+
  1247
+        u, b = will_succeed()
  1248
+        self.assertEqual(User.select().count(), 1)
  1249
+        self.assertEqual(Blog.select().count(), 1)
  1250
+
  1251
+    def test_context_mgr(self):
  1252
+        def will_fail():
  1253
+            u = User.create(username='u1')
  1254
+            b = Blog.create() # no blog, will raise an error
  1255
+            return u, b
  1256
+
  1257
+        def do_will_fail():
  1258
+            with transaction(test_db):
  1259
+                will_fail()
  1260
+
  1261
+        def do_will_fail2():
  1262
+            with test_db.transaction():
  1263
+                will_fail()
  1264
+
  1265
+        self.assertRaises(Exception, do_will_fail)
  1266
+        self.assertEqual(Blog.select().count(), 0)
  1267
+
  1268
+        self.assertRaises(Exception, do_will_fail2)
  1269
+        self.assertEqual(Blog.select().count(), 0)
  1270
+
  1271
+        def will_succeed():
  1272
+            u = User.create(username='u1')
  1273
+            b = Blog.create(title='b1', user=u)
  1274
+            return u, b
  1275
+
  1276
+        def do_will_succeed():
  1277
+            with transaction(test_db):
  1278
+                will_succeed()
  1279
+
  1280
+        def do_will_succeed2():
  1281
+            with test_db.transaction():
  1282
+                will_succeed()
  1283
+
  1284
+        do_will_succeed()
  1285
+        self.assertEqual(User.select().count(), 1)
  1286
+        self.assertEqual(Blog.select().count(), 1)
  1287
+
  1288
+        do_will_succeed2()
  1289
+        self.assertEqual(User.select().count(), 2)
  1290
+        self.assertEqual(Blog.select().count(), 2)
1205 1291
 
1206 1292
 
1207 1293
 class ConcurrencyTestCase(ModelTestCase):
@@ -1240,6 +1326,15 @@ class Meta:
1240 1326
         self.assertTrue(deferred_db.deferred)
1241 1327
 
1242 1328
 
  1329
+class ConnectionStateTestCase(BasePeeweeTestCase):
  1330
+    def test_connection_state(self):
  1331
+        conn = test_db.get_conn()
  1332
+        self.assertFalse(test_db.is_closed())
  1333
+        test_db.close()
  1334
+        self.assertTrue(test_db.is_closed())
  1335
+        conn = test_db.get_conn()
  1336
+        self.assertFalse(test_db.is_closed())
  1337
+
1243 1338
 
1244 1339
 class ForUpdateTestCase(ModelTestCase):
1245 1340
     requires = []

0 notes on commit 3597298

Please sign in to comment.
Something went wrong with that request. Please try again.