Code

Ticket #8134: intermediatefixturestest.diff

File intermediatefixturestest.diff, 3.9 KB (added by kire, 6 years ago)

test cases

Line 
1Index: tests/regressiontests/serializers_regress/models.py
2===================================================================
3--- tests/regressiontests/serializers_regress/models.py (revision 8258)
4+++ tests/regressiontests/serializers_regress/models.py (working copy)
5@@ -132,6 +132,14 @@
6 class FKDataToO2O(models.Model):
7     data = models.ForeignKey(O2OData, null=True)
8 
9+class M2MIntermediateData(models.Model):
10+    data = models.ManyToManyField(Anchor, null=True, through='Intermediate')
11+   
12+class Intermediate(models.Model):
13+    left = models.ForeignKey(M2MIntermediateData)
14+    right = models.ForeignKey(Anchor)
15+    extra = models.CharField(max_length=30, blank=True, default='doesn\'t matter')
16+
17 # The following test classes are for validating the
18 # deserialization of objects that use a user-defined
19 # field as the primary key.
20@@ -243,3 +251,4 @@
21 class ExplicitInheritBaseModel(BaseModel):
22     parent = models.OneToOneField(BaseModel)
23     child_data = models.IntegerField()
24+   
25\ No newline at end of file
26Index: tests/regressiontests/serializers_regress/tests.py
27===================================================================
28--- tests/regressiontests/serializers_regress/tests.py  (revision 8258)
29+++ tests/regressiontests/serializers_regress/tests.py  (working copy)
30@@ -54,6 +54,20 @@
31     instance.data = data
32     return [instance]
33 
34+def im2m_create(pk, klass, data):
35+    instance = klass(id=pk)
36+    models.Model.save_base(instance, raw=True)
37+    return [instance]
38+
39+def im_create(pk, klass, data):
40+    instance = klass(id=pk)
41+    setattr(instance, 'right_id', data['right'])
42+    setattr(instance, 'left_id', data['left'])
43+    if 'extra' in data:
44+        setattr(instance, 'extra', data['extra'])
45+    models.Model.save_base(instance, raw=True)
46+    return [instance]
47+
48 def o2o_create(pk, klass, data):
49     instance = klass()
50     instance.data_id = data
51@@ -99,6 +113,19 @@
52     instance = klass.objects.get(id=pk)
53     testcase.assertEqual(data, [obj.id for obj in instance.data.all()])
54 
55+def im2m_compare(testcase, pk, klass, data):
56+    instance = klass.objects.get(id=pk)
57+    #actually nothing else to check, the instance just should exist
58+
59+def im_compare(testcase, pk, klass, data):
60+    instance = klass.objects.get(id=pk)
61+    testcase.assertEqual(data['left'], instance.left_id)
62+    testcase.assertEqual(data['right'], instance.right_id)
63+    if 'extra' in data:
64+        testcase.assertEqual(data['extra'], instance.extra)
65+    else:
66+        testcase.assertEqual('doesn\'t matter', instance.extra)
67+
68 def o2o_compare(testcase, pk, klass, data):
69     instance = klass.objects.get(data=data)
70     testcase.assertEqual(data, instance.data_id)
71@@ -119,6 +146,8 @@
72 generic_obj = (generic_create, generic_compare)
73 fk_obj = (fk_create, fk_compare)
74 m2m_obj = (m2m_create, m2m_compare)
75+im2m_obj = (im2m_create, im2m_compare)
76+im_obj = (im_create, im_compare)
77 o2o_obj = (o2o_create, o2o_compare)
78 pk_obj = (pk_create, pk_compare)
79 inherited_obj = (inherited_create, inherited_compare)
80@@ -231,6 +260,20 @@
81     (fk_obj, 452, FKDataToField, None),
82 
83     (fk_obj, 460, FKDataToO2O, 300),
84+   
85+    (im2m_obj, 470, M2MIntermediateData, None),
86+   
87+    #testing post- and prereferences and extra fields
88+    (im_obj, 480, Intermediate, {'right': 300, 'left': 470}),
89+    (im_obj, 481, Intermediate, {'right': 300, 'left': 490}),
90+    (im_obj, 482, Intermediate, {'right': 500, 'left': 470}),
91+    (im_obj, 483, Intermediate, {'right': 500, 'left': 490}),
92+    (im_obj, 484, Intermediate, {'right': 300, 'left': 470, 'extra': "extra"}),
93+    (im_obj, 485, Intermediate, {'right': 300, 'left': 490, 'extra': "extra"}),
94+    (im_obj, 486, Intermediate, {'right': 500, 'left': 470, 'extra': "extra"}),
95+    (im_obj, 487, Intermediate, {'right': 500, 'left': 490, 'extra': "extra"}),
96+   
97+    (im2m_obj, 490, M2MIntermediateData, []),
98 
99     (data_obj, 500, Anchor, "Anchor 3"),
100     (data_obj, 501, Anchor, "Anchor 4"),