PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/many_to_one_regress/tests.py

https://code.google.com/p/mango-py/
Python | 105 lines | 64 code | 22 blank | 19 comment | 0 complexity | e1868d7f50cd79a1b373b4a833da3b50 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.db import models
  2. from django.test import TestCase
  3. from models import First, Second, Third, Parent, Child, Category, Record, Relation
  4. class ManyToOneRegressionTests(TestCase):
  5. def test_object_creation(self):
  6. Third.objects.create(id='3', name='An example')
  7. parent = Parent(name='fred')
  8. parent.save()
  9. Child.objects.create(name='bam-bam', parent=parent)
  10. def test_fk_assignment_and_related_object_cache(self):
  11. # Tests of ForeignKey assignment and the related-object cache (see #6886).
  12. p = Parent.objects.create(name="Parent")
  13. c = Child.objects.create(name="Child", parent=p)
  14. # Look up the object again so that we get a "fresh" object.
  15. c = Child.objects.get(name="Child")
  16. p = c.parent
  17. # Accessing the related object again returns the exactly same object.
  18. self.assertTrue(c.parent is p)
  19. # But if we kill the cache, we get a new object.
  20. del c._parent_cache
  21. self.assertFalse(c.parent is p)
  22. # Assigning a new object results in that object getting cached immediately.
  23. p2 = Parent.objects.create(name="Parent 2")
  24. c.parent = p2
  25. self.assertTrue(c.parent is p2)
  26. # Assigning None succeeds if field is null=True.
  27. p.bestchild = None
  28. self.assertTrue(p.bestchild is None)
  29. # bestchild should still be None after saving.
  30. p.save()
  31. self.assertTrue(p.bestchild is None)
  32. # bestchild should still be None after fetching the object again.
  33. p = Parent.objects.get(name="Parent")
  34. self.assertTrue(p.bestchild is None)
  35. # Assigning None fails: Child.parent is null=False.
  36. self.assertRaises(ValueError, setattr, c, "parent", None)
  37. # You also can't assign an object of the wrong type here
  38. self.assertRaises(ValueError, setattr, c, "parent", First(id=1, second=1))
  39. # Nor can you explicitly assign None to Child.parent during object
  40. # creation (regression for #9649).
  41. self.assertRaises(ValueError, Child, name='xyzzy', parent=None)
  42. self.assertRaises(ValueError, Child.objects.create, name='xyzzy', parent=None)
  43. # Creation using keyword argument should cache the related object.
  44. p = Parent.objects.get(name="Parent")
  45. c = Child(parent=p)
  46. self.assertTrue(c.parent is p)
  47. # Creation using keyword argument and unsaved related instance (#8070).
  48. p = Parent()
  49. c = Child(parent=p)
  50. self.assertTrue(c.parent is p)
  51. # Creation using attname keyword argument and an id will cause the
  52. # related object to be fetched.
  53. p = Parent.objects.get(name="Parent")
  54. c = Child(parent_id=p.id)
  55. self.assertFalse(c.parent is p)
  56. self.assertEqual(c.parent, p)
  57. def test_multiple_foreignkeys(self):
  58. # Test of multiple ForeignKeys to the same model (bug #7125).
  59. c1 = Category.objects.create(name='First')
  60. c2 = Category.objects.create(name='Second')
  61. c3 = Category.objects.create(name='Third')
  62. r1 = Record.objects.create(category=c1)
  63. r2 = Record.objects.create(category=c1)
  64. r3 = Record.objects.create(category=c2)
  65. r4 = Record.objects.create(category=c2)
  66. r5 = Record.objects.create(category=c3)
  67. r = Relation.objects.create(left=r1, right=r2)
  68. r = Relation.objects.create(left=r3, right=r4)
  69. r = Relation.objects.create(left=r1, right=r3)
  70. r = Relation.objects.create(left=r5, right=r2)
  71. r = Relation.objects.create(left=r3, right=r2)
  72. q1 = Relation.objects.filter(left__category__name__in=['First'], right__category__name__in=['Second'])
  73. self.assertQuerysetEqual(q1, ["<Relation: First - Second>"])
  74. q2 = Category.objects.filter(record__left_set__right__category__name='Second').order_by('name')
  75. self.assertQuerysetEqual(q2, ["<Category: First>", "<Category: Second>"])
  76. p = Parent.objects.create(name="Parent")
  77. c = Child.objects.create(name="Child", parent=p)
  78. self.assertRaises(ValueError, Child.objects.create, name="Grandchild", parent=c)
  79. def test_fk_instantiation_outside_model(self):
  80. # Regression for #12190 -- Should be able to instantiate a FK outside
  81. # of a model, and interrogate its related field.
  82. cat = models.ForeignKey(Category)
  83. self.assertEqual('id', cat.rel.get_related_field().name)