Ticket #8134: intermediatefixturestest.diff

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

test cases

  • tests/regressiontests/serializers_regress/models.py

     
    132132class FKDataToO2O(models.Model):
    133133    data = models.ForeignKey(O2OData, null=True)
    134134
     135class M2MIntermediateData(models.Model):
     136    data = models.ManyToManyField(Anchor, null=True, through='Intermediate')
     137   
     138class Intermediate(models.Model):
     139    left = models.ForeignKey(M2MIntermediateData)
     140    right = models.ForeignKey(Anchor)
     141    extra = models.CharField(max_length=30, blank=True, default='doesn\'t matter')
     142
    135143# The following test classes are for validating the
    136144# deserialization of objects that use a user-defined
    137145# field as the primary key.
     
    243251class ExplicitInheritBaseModel(BaseModel):
    244252    parent = models.OneToOneField(BaseModel)
    245253    child_data = models.IntegerField()
     254   
     255 No newline at end of file
  • tests/regressiontests/serializers_regress/tests.py

     
    5454    instance.data = data
    5555    return [instance]
    5656
     57def im2m_create(pk, klass, data):
     58    instance = klass(id=pk)
     59    models.Model.save_base(instance, raw=True)
     60    return [instance]
     61
     62def im_create(pk, klass, data):
     63    instance = klass(id=pk)
     64    setattr(instance, 'right_id', data['right'])
     65    setattr(instance, 'left_id', data['left'])
     66    if 'extra' in data:
     67        setattr(instance, 'extra', data['extra'])
     68    models.Model.save_base(instance, raw=True)
     69    return [instance]
     70
    5771def o2o_create(pk, klass, data):
    5872    instance = klass()
    5973    instance.data_id = data
     
    99113    instance = klass.objects.get(id=pk)
    100114    testcase.assertEqual(data, [obj.id for obj in instance.data.all()])
    101115
     116def im2m_compare(testcase, pk, klass, data):
     117    instance = klass.objects.get(id=pk)
     118    #actually nothing else to check, the instance just should exist
     119
     120def im_compare(testcase, pk, klass, data):
     121    instance = klass.objects.get(id=pk)
     122    testcase.assertEqual(data['left'], instance.left_id)
     123    testcase.assertEqual(data['right'], instance.right_id)
     124    if 'extra' in data:
     125        testcase.assertEqual(data['extra'], instance.extra)
     126    else:
     127        testcase.assertEqual('doesn\'t matter', instance.extra)
     128
    102129def o2o_compare(testcase, pk, klass, data):
    103130    instance = klass.objects.get(data=data)
    104131    testcase.assertEqual(data, instance.data_id)
     
    119146generic_obj = (generic_create, generic_compare)
    120147fk_obj = (fk_create, fk_compare)
    121148m2m_obj = (m2m_create, m2m_compare)
     149im2m_obj = (im2m_create, im2m_compare)
     150im_obj = (im_create, im_compare)
    122151o2o_obj = (o2o_create, o2o_compare)
    123152pk_obj = (pk_create, pk_compare)
    124153inherited_obj = (inherited_create, inherited_compare)
     
    231260    (fk_obj, 452, FKDataToField, None),
    232261
    233262    (fk_obj, 460, FKDataToO2O, 300),
     263   
     264    (im2m_obj, 470, M2MIntermediateData, None),
     265   
     266    #testing post- and prereferences and extra fields
     267    (im_obj, 480, Intermediate, {'right': 300, 'left': 470}),
     268    (im_obj, 481, Intermediate, {'right': 300, 'left': 490}),
     269    (im_obj, 482, Intermediate, {'right': 500, 'left': 470}),
     270    (im_obj, 483, Intermediate, {'right': 500, 'left': 490}),
     271    (im_obj, 484, Intermediate, {'right': 300, 'left': 470, 'extra': "extra"}),
     272    (im_obj, 485, Intermediate, {'right': 300, 'left': 490, 'extra': "extra"}),
     273    (im_obj, 486, Intermediate, {'right': 500, 'left': 470, 'extra': "extra"}),
     274    (im_obj, 487, Intermediate, {'right': 500, 'left': 490, 'extra': "extra"}),
     275   
     276    (im2m_obj, 490, M2MIntermediateData, []),
    234277
    235278    (data_obj, 500, Anchor, "Anchor 3"),
    236279    (data_obj, 501, Anchor, "Anchor 4"),
Back to Top