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