PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/m2m_through_regress/tests.py

https://code.google.com/p/mango-py/
Python | 148 lines | 130 code | 18 blank | 0 comment | 2 complexity | d54098b32ced693fbb15bb893aec3dd2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. try:
  2. from cStringIO import StringIO
  3. except ImportError:
  4. from StringIO import StringIO
  5. from django.core import management
  6. from django.contrib.auth.models import User
  7. from django.test import TestCase
  8. from models import (Person, Group, Membership, UserMembership,
  9. Car, Driver, CarDriver)
  10. class M2MThroughTestCase(TestCase):
  11. def test_everything(self):
  12. bob = Person.objects.create(name="Bob")
  13. jim = Person.objects.create(name="Jim")
  14. rock = Group.objects.create(name="Rock")
  15. roll = Group.objects.create(name="Roll")
  16. frank = User.objects.create_user("frank", "frank@example.com", "password")
  17. jane = User.objects.create_user("jane", "jane@example.com", "password")
  18. Membership.objects.create(person=bob, group=rock)
  19. Membership.objects.create(person=bob, group=roll)
  20. Membership.objects.create(person=jim, group=rock)
  21. self.assertQuerysetEqual(
  22. bob.group_set.all(), [
  23. "<Group: Rock>",
  24. "<Group: Roll>",
  25. ]
  26. )
  27. self.assertQuerysetEqual(
  28. roll.members.all(), [
  29. "<Person: Bob>",
  30. ]
  31. )
  32. self.assertRaises(AttributeError, setattr, bob, "group_set", [])
  33. self.assertRaises(AttributeError, setattr, roll, "members", [])
  34. self.assertRaises(AttributeError, rock.members.create, name="Anne")
  35. self.assertRaises(AttributeError, bob.group_set.create, name="Funk")
  36. UserMembership.objects.create(user=frank, group=rock)
  37. UserMembership.objects.create(user=frank, group=roll)
  38. UserMembership.objects.create(user=jane, group=rock)
  39. self.assertQuerysetEqual(
  40. frank.group_set.all(), [
  41. "<Group: Rock>",
  42. "<Group: Roll>",
  43. ]
  44. )
  45. self.assertQuerysetEqual(
  46. roll.user_members.all(), [
  47. "<User: frank>",
  48. ]
  49. )
  50. def test_serialization(self):
  51. "m2m-through models aren't serialized as m2m fields. Refs #8134"
  52. p = Person.objects.create(name="Bob")
  53. g = Group.objects.create(name="Roll")
  54. m =Membership.objects.create(person=p, group=g)
  55. pks = {"p_pk": p.pk, "g_pk": g.pk, "m_pk": m.pk}
  56. out = StringIO()
  57. management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
  58. self.assertEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks)
  59. out = StringIO()
  60. management.call_command("dumpdata", "m2m_through_regress", format="xml",
  61. indent=2, stdout=out)
  62. self.assertEqual(out.getvalue().strip(), """
  63. <?xml version="1.0" encoding="utf-8"?>
  64. <django-objects version="1.0">
  65. <object pk="%(m_pk)s" model="m2m_through_regress.membership">
  66. <field to="m2m_through_regress.person" name="person" rel="ManyToOneRel">%(p_pk)s</field>
  67. <field to="m2m_through_regress.group" name="group" rel="ManyToOneRel">%(g_pk)s</field>
  68. <field type="IntegerField" name="price">100</field>
  69. </object>
  70. <object pk="%(p_pk)s" model="m2m_through_regress.person">
  71. <field type="CharField" name="name">Bob</field>
  72. </object>
  73. <object pk="%(g_pk)s" model="m2m_through_regress.group">
  74. <field type="CharField" name="name">Roll</field>
  75. </object>
  76. </django-objects>
  77. """.strip() % pks)
  78. def test_join_trimming(self):
  79. "Check that we don't involve too many copies of the intermediate table when doing a join. Refs #8046, #8254"
  80. bob = Person.objects.create(name="Bob")
  81. jim = Person.objects.create(name="Jim")
  82. rock = Group.objects.create(name="Rock")
  83. roll = Group.objects.create(name="Roll")
  84. Membership.objects.create(person=bob, group=rock)
  85. Membership.objects.create(person=jim, group=rock, price=50)
  86. Membership.objects.create(person=bob, group=roll, price=50)
  87. self.assertQuerysetEqual(
  88. rock.members.filter(membership__price=50), [
  89. "<Person: Jim>",
  90. ]
  91. )
  92. self.assertQuerysetEqual(
  93. bob.group_set.filter(membership__price=50), [
  94. "<Group: Roll>",
  95. ]
  96. )
  97. class ToFieldThroughTests(TestCase):
  98. def setUp(self):
  99. self.car = Car.objects.create(make="Toyota")
  100. self.driver = Driver.objects.create(name="Ryan Briscoe")
  101. CarDriver.objects.create(car=self.car, driver=self.driver)
  102. def test_to_field(self):
  103. self.assertQuerysetEqual(
  104. self.car.drivers.all(),
  105. ["<Driver: Ryan Briscoe>"]
  106. )
  107. def test_to_field_reverse(self):
  108. self.assertQuerysetEqual(
  109. self.driver.car_set.all(),
  110. ["<Car: Toyota>"]
  111. )
  112. class ThroughLoadDataTestCase(TestCase):
  113. fixtures = ["m2m_through"]
  114. def test_sequence_creation(self):
  115. "Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107"
  116. out = StringIO()
  117. management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
  118. self.assertEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""")