コード例 #1
0
 def test_update(self):
     serializer = ChildSideloadSerializer(
         instance=self.child, data=self.payload)
     self.assertEqual(
         ChildModel.objects.get(pk=self.child.pk).old_parent, self.old_parent)
     serializer.is_valid()
     serializer.save()
     self.assertEqual(
         ChildModel.objects.get(pk=self.child.pk).old_parent, self.parent)
コード例 #2
0
 def test_errors_response_format(self):
     self.payload['child_model'].pop('parent')
     serializer = ChildSideloadSerializer(
         data=self.payload, instance=self.child)
     try:
         serializer.is_valid(True)
     except ValidationError as e:
         response = exception_handler(e)
         self.assertEqual(response.data,
             {'errors': {'parent': ['This field is required.']}})
         self.assertEqual(response.status_code, 422)
コード例 #3
0
 def test_errors_response_format(self):
     self.payload['child_model'].pop('parent')
     serializer = ChildSideloadSerializer(
         data=self.payload, instance=self.child)
     try:
         serializer.is_valid(True)
     except ValidationError as e:
         response = exception_handler(e, serializer.context)
         self.assertEqual(
             response.data,
             {'errors': {'parent': ['This field is required.']}})
         self.assertEqual(response.status_code, 422)
コード例 #4
0
 def test_get_sideload_ids(self):
     with self.assertNumQueries(1):
         result = ChildSideloadSerializer(many=True).get_sideload_ids(
             ChildModel.objects.all())
     self.assertEqual(len(result), 1)
     self.assertEqual(result['parent_models'],
                      set([p.id for p in self.parents]))
コード例 #5
0
 def test_active_model_json_renderer(self):
     parents = [ParentModel.objects.create() for x in range(3)]
     children = [
         ChildModel.objects.create(parent=parents[1],
                                   old_parent=parents[2]),
         ChildModel.objects.create(parent=parents[1],
                                   old_parent=parents[2]),
         ChildModel.objects.create(parent=parents[0], old_parent=parents[1])
     ]
     obj = ChildSideloadSerializer(children, many=True).data
     expected = {
         'child_models': [
             OrderedDict([('id', c.id), ('parent_id', c.parent.id),
                          ('old_parent_id', c.old_parent.id)])
             for c in children
         ],
         'parent_models': [
             OrderedDict([('id', p.id), ('text', p.text),
                          ('child_ids', p.child_ids),
                          ('old_child_ids', p.old_child_ids)])
             for p in parents
         ]
     }
     result = convert_to_active_model_json(obj)
     assert result == expected
コード例 #6
0
 def test_get_sideload_objects(self):
     result = ChildSideloadSerializer().get_sideload_objects(self.child)
     expected = [
        {'id': p.id, 'text': p.text, 'children': p.child_ids,
         'old_children': p.old_child_ids}
         for p in ParentModel.objects.all()]
     self.assertEqual(len(result), 1)
     self.assertEqual(result['parent_models'], expected)
コード例 #7
0
 def test_update(self):
     serializer = ChildSideloadSerializer(
         instance=self.child, data=self.payload)
     self.assertEqual(
         ChildModel.objects.get(pk=self.child.pk).old_parent,
         self.old_parent)
     serializer.is_valid()
     serializer.save()
     self.assertEqual(
         ChildModel.objects.get(pk=self.child.pk).old_parent, self.parent)
コード例 #8
0
 def test_serialization(self):
     with self.assertNumQueries(3):
         result = ChildSideloadSerializer(self.child).data
     expected = {
         'child_model': {
             'id': self.child.pk,
             'parent': self.parent.pk,
             'old_parent': self.old_parent.pk
         },
         'parent_models': [
             {'id': self.parent.pk, 'text': self.parent.text,
              'children': [self.child.pk], 'old_children': []},
             {'id': self.old_parent.pk, 'text': self.old_parent.text,
              'children': [], 'old_children': [self.child.pk]}
         ]
     }
     self.assertEqual(result, expected)
コード例 #9
0
 def test_serialization(self):
     serializer = ChildSideloadSerializer(ChildModel.objects.all(), many=True)
     assert isinstance(serializer, SideloadListSerializer)
     result = serializer.data
     expected = {
         'child_models': [{
             'id': c.pk,
             'parent': c.parent.pk,
             'old_parent': c.old_parent.pk
         } for c in self.children],
         'parent_models': [
             {'id': p.pk, 'text': p.text,
             'children': p.child_ids,
             'old_children': p.old_child_ids}
             for p in self.parents
         ]
     }
     self.assertEqual(result, expected)
コード例 #10
0
 def test_convert_related_keys_single(self):
     parent = ParentModel.objects.create()
     old_parent = ParentModel.objects.create()
     child = ChildModel.objects.create(parent=parent, old_parent=old_parent)
     obj = ChildSideloadSerializer(instance=child).data
     expected = {
         'child_model':
         OrderedDict([(
             'id',
             child.id,
         ), (
             'parent_id',
             parent.id,
         ), ('old_parent_id', old_parent.id)]),
         'parent_models': [
             OrderedDict([('id', p.id), ('text', p.text),
                          ('child_ids', p.child_ids),
                          ('old_child_ids', p.old_child_ids)])
             for p in [parent, old_parent]
         ]
     }
     result = convert_to_active_model_json(obj)
     assert result == expected
コード例 #11
0
 def test_deserialize(self):
     serializer = ChildSideloadSerializer(
         instance=self.child, data=self.payload)
     self.assertTrue(serializer.is_valid())
     serializer.data
コード例 #12
0
 def test_deserialize_field_validation_works(self):
     self.payload['child_model'].pop('parent')
     serializer = ChildSideloadSerializer(
         data=self.payload, instance=self.child)
     self.assertFalse(serializer.is_valid())
コード例 #13
0
 def test_create(self):
     serializer = ChildSideloadSerializer(data=self.payload)
     self.assertEqual(ChildModel.objects.count(), 0)
     serializer.is_valid()
     serializer.save()
     self.assertEqual(ChildModel.objects.count(), 1)
コード例 #14
0
 def test_deserialize(self):
     serializer = ChildSideloadSerializer(data=self.payload)
     self.assertTrue(serializer.is_valid())
コード例 #15
0
 def test_deserialize_requires_root_key(self):
     with self.assertRaises(AssertionError):
         ChildSideloadSerializer(data=self.payload['child_model'])
コード例 #16
0
 def test_many_creates_list(self):
     result = ChildSideloadSerializer(many=True)
     self.assertTrue(isinstance(result, SideloadListSerializer))
コード例 #17
0
 def test_deserialize_field_validation_works(self):
     self.payload['child_model'].pop('parent')
     with self.assertRaises(ValidationError):
         ChildSideloadSerializer(data=self.payload).is_valid(True)
コード例 #18
0
 def test_deserialize(self):
     serializer = ChildSideloadSerializer(data=self.payload)
     self.assertTrue(serializer.is_valid())
コード例 #19
0
 def test_get_sideload_ids(self):
     result = ChildSideloadSerializer().get_sideload_ids(self.child)
     expected = set([self.parent.id, self.old_parent.id])
     self.assertEqual(len(result), 1)
     self.assertEqual(result['parent_models'], expected)
コード例 #20
0
 def test_create(self):
     serializer = ChildSideloadSerializer(data=self.payload)
     self.assertEqual(ChildModel.objects.count(), 0)
     serializer.is_valid()
     serializer.save()
     self.assertEqual(ChildModel.objects.count(), 1)
コード例 #21
0
 def test_deserialize_field_validation_works(self):
     self.payload['child_model'].pop('parent')
     serializer = ChildSideloadSerializer(
         data=self.payload, instance=self.child)
     self.assertFalse(serializer.is_valid())
コード例 #22
0
 def test_deserialize(self):
     serializer = ChildSideloadSerializer(
         instance=self.child, data=self.payload)
     self.assertTrue(serializer.is_valid())
     serializer.data