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)
 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)
Beispiel #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)
Beispiel #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]))
 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
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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)
Beispiel #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
 def test_deserialize(self):
     serializer = ChildSideloadSerializer(
         instance=self.child, data=self.payload)
     self.assertTrue(serializer.is_valid())
     serializer.data
 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())
 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)
 def test_deserialize(self):
     serializer = ChildSideloadSerializer(data=self.payload)
     self.assertTrue(serializer.is_valid())
Beispiel #15
0
 def test_deserialize_requires_root_key(self):
     with self.assertRaises(AssertionError):
         ChildSideloadSerializer(data=self.payload['child_model'])
Beispiel #16
0
 def test_many_creates_list(self):
     result = ChildSideloadSerializer(many=True)
     self.assertTrue(isinstance(result, SideloadListSerializer))
Beispiel #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)
Beispiel #18
0
 def test_deserialize(self):
     serializer = ChildSideloadSerializer(data=self.payload)
     self.assertTrue(serializer.is_valid())
Beispiel #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)
Beispiel #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)
Beispiel #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())
Beispiel #22
0
 def test_deserialize(self):
     serializer = ChildSideloadSerializer(
         instance=self.child, data=self.payload)
     self.assertTrue(serializer.is_valid())
     serializer.data