Alex Burgel avatar Alex Burgel committed d296262

test cases for ancestor queries and GAEKeys

Comments (0)

Files changed (2)

tests/__init__.py

 from .not_return_sets import NonReturnSetsTest
 from .decimals import DecimalTest
 from .transactions import TransactionTest
+from .keys import KeysTest
+from django.db import models
+from django.test import TestCase
+from django.db.utils import DatabaseError
+
+from google.appengine.api.datastore import Key
+
+from ..fields import GAEKeyField
+from ..models import GAEKey
+
+class ParentModel(models.Model):
+    key = GAEKeyField(primary_key=True)
+
+class NonGAEParentModel(models.Model):
+    id = models.AutoField(primary_key=True)
+
+class ChildModel(models.Model):
+    key = GAEKeyField(primary_key=True, parent_key_name='parent_key')
+
+class AnotherChildModel(models.Model):
+    key = GAEKeyField(primary_key=True, parent_key_name='also_parent_key')
+
+class KeysTest(TestCase):
+    def testUnsavedParent(self):
+        parent = ParentModel()
+
+        with self.assertRaises(ValueError):
+            child = ChildModel(parent_key=parent.pk)
+
+    def testNonGAEParent(self):
+        parent = NonGAEParentModel()
+        parent.save()
+
+        with self.assertRaises(ValueError):
+            child = ChildModel(parent_key=parent.pk)
+
+    def testParentChildSave(self):
+        parent = ParentModel()
+        orig_parent_pk = parent.pk
+        parent.save()
+        
+        child = ChildModel(parent_key=parent.pk)
+        orig_child_pk = child.pk
+        child.save()
+        
+        self.assertNotEquals(parent.pk, orig_parent_pk)
+        self.assertNotEquals(child.pk, orig_child_pk)
+        self.assertEquals(child.pk.parent_key, parent.pk)
+        self.assertEquals(child.pk.parent_key.real_key, parent.pk.real_key)
+    
+    def testAncestorFilterQuery(self):
+        parent = ParentModel()
+        parent.save()
+        
+        child = ChildModel(parent_key=parent.pk)
+        child.save()
+        
+        results = list(ChildModel.objects.filter(pk=parent.pk.as_ancestor()))
+        
+        self.assertEquals(1, len(results))
+        self.assertEquals(results[0].pk, child.pk)
+
+    def testAncestorGetQuery(self):
+        parent = ParentModel()
+        parent.save()
+        
+        child = ChildModel(parent_key=parent.pk)
+        child.save()
+
+        result = ChildModel.objects.get(pk=parent.pk.as_ancestor())
+
+        self.assertEquals(result.pk, child.pk)
+
+    def testEmptyAncestorQuery(self):
+        parent = ParentModel()
+        parent.save()
+
+        results = list(ChildModel.objects.filter(pk=parent.pk.as_ancestor()))
+
+        self.assertEquals(0, len(results))
+
+    def testEmptyAncestorQueryWithUnsavedChild(self):
+        parent = ParentModel()
+        parent.save()
+
+        child = ChildModel(parent_key=parent.pk)
+
+        results = list(ChildModel.objects.filter(pk=parent.pk.as_ancestor()))
+
+        self.assertEquals(0, len(results))
+
+    def testUnsavedAncestorQuery(self):
+        parent = ParentModel()
+
+        with self.assertRaises(AttributeError):
+            results = list(ChildModel.objects.filter(pk=parent.pk.as_ancestor()))
+
+    def testDifferentChildrenAncestorQuery(self):
+        parent = ParentModel()
+        parent.save()
+
+        child1 = ChildModel(parent_key=parent.pk)
+        child1.save()
+        child2 = AnotherChildModel(also_parent_key=parent.pk)
+        child2.save()
+
+        results = list(ChildModel.objects.filter(pk=parent.pk.as_ancestor()))
+
+        self.assertEquals(1, len(results))
+        self.assertEquals(results[0].pk, child1.pk)
+
+        results = list(AnotherChildModel.objects.filter(pk=parent.pk.as_ancestor()))
+        self.assertEquals(1, len(results))
+        self.assertEquals(results[0].pk, child2.pk)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.