@@ -19,26 +19,74 @@ class Purchase(ndb.Model):
19
19
price = ndb .IntegerProperty
20
20
21
21
22
- class AncestorTests (unittest .TestCase ):
23
- """Tests if we can load a JSON file"""
22
+ class AncestorKeyTests (unittest .TestCase ):
24
23
def setUp (self ):
25
24
self .testbed = testbed .Testbed ()
26
25
self .testbed .activate ()
27
26
self .testbed .init_datastore_v3_stub ()
28
27
self .testbed .init_memcache_stub ()
29
28
self .customers_data = load_fixture ('tests/customers.json' , Customer )
30
- self .purchases_data = load_fixture ('tests/purchases .json' , Purchase )
29
+ self .purchases_data = load_fixture ('tests/purchases_key .json' , Purchase )
31
30
32
31
def tearDown (self ):
33
32
self .testbed .deactivate ()
34
33
35
34
def test_loaded_count (self ):
36
- """Make sure we got 2 objects from the JSON file"""
37
35
self .assertEqual (len (self .customers_data ), 2 )
38
36
self .assertEqual (len (self .purchases_data ), 2 )
39
37
40
38
def test_ancestors (self ):
41
- """Check whether the ancestor of keys we imported match the JSON contents"""
39
+ john = Customer .query (Customer .name == 'John' ).get ()
40
+ self .assertEqual (john .name , 'John' )
41
+
42
+ john_purchases = Purchase .query (ancestor = john .key )
43
+ self .assertEqual (john_purchases .count (), 1 )
44
+ self .assertEqual (john_purchases .get ().key .parent (), john .key )
45
+
46
+
47
+ class AncestorParentTests (unittest .TestCase ):
48
+ def setUp (self ):
49
+ self .testbed = testbed .Testbed ()
50
+ self .testbed .activate ()
51
+ self .testbed .init_datastore_v3_stub ()
52
+ self .testbed .init_memcache_stub ()
53
+ self .customers_data = load_fixture ('tests/customers.json' , Customer )
54
+ self .purchases_data = load_fixture ('tests/purchases_parent.json' , Purchase )
55
+
56
+ def tearDown (self ):
57
+ self .testbed .deactivate ()
58
+
59
+ def test_loaded_count (self ):
60
+ self .assertEqual (len (self .customers_data ), 2 )
61
+ self .assertEqual (len (self .purchases_data ), 2 )
62
+
63
+ def test_ancestors (self ):
64
+ john = Customer .query (Customer .name == 'John' ).get ()
65
+ self .assertEqual (john .name , 'John' )
66
+
67
+ john_purchases = Purchase .query (ancestor = john .key )
68
+ self .assertEqual (john_purchases .count (), 1 )
69
+ self .assertEqual (john_purchases .get ().key .parent (), john .key )
70
+
71
+
72
+ class AncestorChildrenTests (unittest .TestCase ):
73
+ def setUp (self ):
74
+ self .testbed = testbed .Testbed ()
75
+ self .testbed .activate ()
76
+ self .testbed .init_datastore_v3_stub ()
77
+ self .testbed .init_memcache_stub ()
78
+ self .data = load_fixture ('tests/customers_purchases.json' ,
79
+ {'Customer' : Customer , 'Purchase' : Purchase })
80
+
81
+ def tearDown (self ):
82
+ self .testbed .deactivate ()
83
+
84
+ def test_loaded_count (self ):
85
+ self .assertEqual (len (self .data ), 2 )
86
+
87
+ def test_ancestors (self ):
88
+ self .assertEqual (Purchase .query ().count (), 2 )
89
+
42
90
john = Customer .query (Customer .name == 'John' ).get ()
43
91
self .assertEqual (john .name , 'John' )
44
92
0 commit comments