class TestUpdateOrCreateModelMixin(TestMixinsBase):

    def setUp(self):
        super(TestUpdateOrCreateModelMixin, self).setUp()
        self.dummy = DummyModel.objects.create(name='dummy1')

        class DummyResource(ModelResource):
            model = DummyModel
        self.mixin = UpdateOrCreateModelMixin()
        self.mixin.resource = DummyResource

    def test_update_model(self):
        """
        Making sure update still working fine
        """
        dummy = DummyModel.objects.get(id=1)

        update_data = {'name': 'updated_name'}
        request = self.req.put('/dummy/1', data=update_data)
        self.mixin.CONTENT = update_data

        response = self.mixin.put(request, id=dummy.id)

        self.assertEqual(dummy.name, 'dummy1')
        content = response.raw_content
        self.assertEqual(content.name, 'updated_name')
        self.assertEqual(content.id, dummy.id)

        #get dummy again now it should have the new name
        dummy = DummyModel.objects.get(id=dummy.id)
        self.assertEqual(dummy.name, content.name)

    def test_creation_on_put_not_exist_model(self):
        """
        When make a put request to non-existing object in update or create mixin model, it should create new object
        and return 201
        """

        with self.assertRaises(DummyModel.DoesNotExist):
            DummyModel.objects.get(id=999)

        self.assertEqual(DummyModel.objects.count(), 1)
        update_data = {'name': 'new_dummy'}
        request = self.req.put('/dummy/999', data=update_data)
        self.mixin.CONTENT = update_data

        response = self.mixin.put(request, id=999)

        self.assertEqual(response.status, 201)
        new_dummy = DummyModel.objects.get(id=999)
        self.assertEqual(new_dummy.name, 'new_dummy')
        self.assertEqual(DummyModel.objects.count(), 2)
示例#2
0
    def setUp(self):
        super(TestUpdateOrCreateModelMixin, self).setUp()
        self.dummy = DummyModel.objects.create(name='dummy1')

        class DummyResource(ModelResource):
            model = DummyModel
        self.mixin = UpdateOrCreateModelMixin()
        self.mixin.resource = DummyResource