9
9
from .google_imports import apiproxy_errors
10
10
from .google_imports import datastore
11
11
from .google_imports import datastore_errors
12
+ from .google_imports import datastore_pbs
12
13
from .google_imports import datastore_rpc
13
14
from .google_imports import memcache
14
15
from .google_imports import taskqueue
15
16
from .google_test_imports import unittest
17
+ from .google_test_imports import real_unittest
16
18
17
19
from . import context
18
20
from . import eventloop
@@ -42,15 +44,7 @@ def wrap(todo, options):
42
44
super (MyAutoBatcher , self ).__init__ (wrap , limit )
43
45
44
46
45
- class ContextTests (test_utils .NDBTest ):
46
-
47
- def setUp (self ):
48
- super (ContextTests , self ).setUp ()
49
- MyAutoBatcher .reset_log ()
50
- self .ctx = context .Context (
51
- conn = model .make_connection (default_model = model .Expando ),
52
- auto_batcher_class = MyAutoBatcher )
53
- tasklets .set_context (self .ctx )
47
+ class ContextTestMixin (object ):
54
48
55
49
the_module = context
56
50
@@ -98,6 +92,9 @@ def create_entities(self):
98
92
key3 = yield fut3
99
93
raise tasklets .Return ([key1 , key2 , key3 ])
100
94
95
+ def make_bad_transaction (* arg , ** kwargs ):
96
+ raise NotImplementedError
97
+
101
98
def testContext_AutoBatcher_Put (self ):
102
99
keys = self .create_entities ().get_result ()
103
100
self .assertEqual (len (keys ), 3 )
@@ -752,15 +749,14 @@ def testContext_TransactionRollbackException(self):
752
749
753
750
class CustomException (Exception ):
754
751
pass
755
- def bad_transaction (* arg , ** kwargs ):
756
- return datastore_rpc .datastore_pb .Transaction ()
757
752
@tasklets .tasklet
758
753
def foo ():
759
754
ent = model .Expando (key = key , bar = 1 )
760
755
@tasklets .tasklet
761
756
def callback ():
762
757
# Cause rollback to return an exception
763
- tasklets .get_context ()._conn ._end_transaction = bad_transaction
758
+ ctx = tasklets .get_context ()
759
+ ctx ._conn ._end_transaction = self .make_bad_transaction
764
760
yield ent .put_async ()
765
761
raise CustomException ()
766
762
yield self .ctx .transaction (callback )
@@ -786,25 +782,6 @@ def callback():
786
782
yield self .ctx .transaction (callback )
787
783
foo ().check_success ()
788
784
789
- def testContext_TransactionXG (self ):
790
- self .ExpectWarnings ()
791
- # The XG option only works on the HRD datastore
792
- self .HRTest ()
793
-
794
- key1 = model .Key ('Foo' , 1 )
795
- key2 = model .Key ('Foo' , 2 )
796
- @tasklets .tasklet
797
- def tx ():
798
- ctx = tasklets .get_context ()
799
- ent1 = model .Expando (key = key1 , foo = 1 )
800
- ent2 = model .Expando (key = key2 , bar = 2 )
801
- yield ctx .put (ent1 ), ctx .put (ent2 )
802
- raise tasklets .Return (42 )
803
- self .assertRaises (datastore_errors .BadRequestError ,
804
- self .ctx .transaction (tx ).check_success )
805
- res = self .ctx .transaction (tx , xg = True ).get_result ()
806
- self .assertEqual (res , 42 )
807
-
808
785
def testContext_TransactionMemcache (self ):
809
786
class Foo (model .Model ):
810
787
name = model .StringProperty ()
@@ -1459,6 +1436,66 @@ class EmptyModel(model.Model):
1459
1436
self .assertTrue (e1 is e2 )
1460
1437
1461
1438
1439
+ class ContextV3Tests (ContextTestMixin , test_utils .NDBTest ):
1440
+ """Context tests that use a Datastore V3 connection."""
1441
+
1442
+ def setUp (self ):
1443
+ super (ContextV3Tests , self ).setUp ()
1444
+ MyAutoBatcher .reset_log ()
1445
+ self .ctx = context .Context (
1446
+ conn = model .make_connection (default_model = model .Expando ),
1447
+ auto_batcher_class = MyAutoBatcher )
1448
+ tasklets .set_context (self .ctx )
1449
+
1450
+ def make_bad_transaction (* arg , ** kwargs ):
1451
+ return datastore_rpc .datastore_pb .Transaction ()
1452
+
1453
+ def testContext_TransactionAddTask (self ):
1454
+ self .ExpectWarnings ()
1455
+ key = model .Key ('Foo' , 1 )
1456
+ @tasklets .tasklet
1457
+ def foo ():
1458
+ ent = model .Expando (key = key , bar = 1 )
1459
+ @tasklets .tasklet
1460
+ def callback ():
1461
+ ctx = tasklets .get_context ()
1462
+ yield ctx .put (ent )
1463
+ taskqueue .add (url = '/' , transactional = True )
1464
+ yield self .ctx .transaction (callback )
1465
+ foo ().check_success ()
1466
+
1467
+ @real_unittest .skipUnless (datastore_pbs ._CLOUD_DATASTORE_ENABLED ,
1468
+ "V1 must be supported to run V1 tests." )
1469
+ class ContextV1Tests (ContextTestMixin , test_utils .NDBCloudDatastoreV1Test ):
1470
+ """Context tests that use a Cloud Datastore V1 connection."""
1471
+
1472
+ def setUp (self ):
1473
+ super (ContextV1Tests , self ).setUp ()
1474
+ self .HRTest ()
1475
+ MyAutoBatcher .reset_log ()
1476
+ self .ctx = context .Context (
1477
+ conn = model .make_connection (default_model = model .Expando ,
1478
+ _api_version = datastore_rpc ._CLOUD_DATASTORE_V1 ),
1479
+ auto_batcher_class = MyAutoBatcher )
1480
+ tasklets .set_context (self .ctx )
1481
+
1482
+ def make_bad_transaction (* arg , ** kwargs ):
1483
+ return ''
1484
+
1485
+ def testContext_TransactionAddTask (self ):
1486
+ self .ExpectWarnings ()
1487
+ key = model .Key ('Foo' , 1 )
1488
+ @tasklets .tasklet
1489
+ def foo ():
1490
+ ent = model .Expando (key = key , bar = 1 )
1491
+ @tasklets .tasklet
1492
+ def callback ():
1493
+ ctx = tasklets .get_context ()
1494
+ yield ctx .put (ent )
1495
+ taskqueue .add (url = '/' , transactional = True )
1496
+ yield self .ctx .transaction (callback )
1497
+ self .assertRaises (ValueError , foo ().check_success )
1498
+
1462
1499
class ContextFutureCachingTests (test_utils .NDBTest ):
1463
1500
# See issue 62. http://goo.gl/5zLkK
1464
1501
0 commit comments