예제 #1
0
class SlugBasedResourceTestCase(TestCase):
    def setUp(self):
        super(SlugBasedResourceTestCase, self).setUp()
        self.n1 = SlugBasedNote.objects.get(pk='first-post')
        self.request = HttpRequest()
        self.request.method = 'PUT'
        self.resource = SlugBasedNoteResource()
        self.n1_bundle = self.resource.build_bundle(obj=self.n1)

    def test_bundle_unique_field(self):
        self.assertEqual(self.resource.get_bundle_detail_data(self.n1_bundle),
                         u'first-post')

    def test_obj_update(self):
        bundle = self.resource.build_bundle(obj=self.n1,
                                            data={
                                                'title': 'Foo!',
                                            })
        updated_bundle = self.resource.obj_update(bundle, slug='first-post')
        self.assertEqual(updated_bundle.obj.slug, 'first-post')
        self.assertEqual(updated_bundle.obj.title, 'Foo!')

        # Again, without the PK this time.
        self.n1.slug = None
        bundle = self.resource.build_bundle(obj=self.n1,
                                            data={
                                                'title': 'Bar!',
                                            })
        updated_bundle_2 = self.resource.obj_update(bundle, slug='first-post')
        self.assertEqual(updated_bundle_2.obj.slug, 'first-post')
        self.assertEqual(updated_bundle_2.obj.title, 'Bar!')

    def test_update_in_place(self):
        new_data = {
            'slug': u'foo',
            'title': u'Foo!',
        }
        new_bundle = self.resource.update_in_place(self.request,
                                                   self.n1_bundle, new_data)
        # Check for updated data.
        self.assertEqual(new_bundle.obj.title, u'Foo!')
        self.assertEqual(new_bundle.obj.slug, u'foo')
        # Make sure it looked up the right instance, even though we didn't
        # hand it a PK...
        self.assertEqual(new_bundle.obj.pk, self.n1_bundle.obj.pk)

    def test_rollback(self):
        bundles = [self.n1_bundle]
        self.resource.rollback(bundles)

        # Make sure it's gone.
        self.assertRaises(SlugBasedNote.DoesNotExist,
                          SlugBasedNote.objects.get,
                          pk='first-post')
예제 #2
0
class SlugBasedResourceTestCase(TestCaseWithFixture):
    def setUp(self):
        super(SlugBasedResourceTestCase, self).setUp()
        self.n1 = SlugBasedNote.objects.get(pk='first-post')
        self.request = HttpRequest()
        self.request.method = 'PUT'
        self.resource = SlugBasedNoteResource()
        self.n1_bundle = self.resource.build_bundle(obj=self.n1)

    def test_bundle_unique_field(self):
        self.assertEqual(self.resource.get_bundle_detail_data(self.n1_bundle), u'first-post')

    def test_obj_update(self):
        bundle = self.resource.build_bundle(obj=self.n1, data={
            'title': 'Foo!',
        })
        updated_bundle = self.resource.obj_update(bundle, slug='first-post')
        self.assertEqual(updated_bundle.obj.slug, 'first-post')
        self.assertEqual(updated_bundle.obj.title, 'Foo!')

        # Again, without the PK this time.
        self.n1.slug = None
        bundle = self.resource.build_bundle(obj=self.n1, data={
            'title': 'Bar!',
        })
        updated_bundle_2 = self.resource.obj_update(bundle, slug='first-post')
        self.assertEqual(updated_bundle_2.obj.slug, 'first-post')
        self.assertEqual(updated_bundle_2.obj.title, 'Bar!')

    def test_update_in_place(self):
        new_data = {
            'slug': u'foo',
            'title': u'Foo!',
        }
        new_bundle = self.resource.update_in_place(self.request, self.n1_bundle, new_data)
        # Check for updated data.
        self.assertEqual(new_bundle.obj.title, u'Foo!')
        self.assertEqual(new_bundle.obj.slug, u'foo')
        # Make sure it looked up the right instance, even though we didn't
        # hand it a PK...
        self.assertEqual(new_bundle.obj.pk, self.n1_bundle.obj.pk)

    def test_rollback(self):
        bundles = [
            self.n1_bundle
        ]
        self.resource.rollback(bundles)

        # Make sure it's gone.
        self.assertRaises(SlugBasedNote.DoesNotExist, SlugBasedNote.objects.get, pk='first-post')
예제 #3
0
    def test_correct_slug_detail_uri_bundle(self):
        n1 = SlugBasedNote.objects.get(pk='first-post')

        resource = SlugBasedNoteResource()
        n1_bundle = resource.build_bundle(obj=n1)
        self.assertEqual(resource.detail_uri_kwargs(n1_bundle), {
            'slug': 'first-post',
        })
예제 #4
0
    def test_correct_slug_detail_uri_bundle(self):
        n1 = SlugBasedNote.objects.get(pk='first-post')

        resource = SlugBasedNoteResource()
        n1_bundle = resource.build_bundle(obj=n1)
        self.assertEqual(resource.detail_uri_kwargs(n1_bundle), {
            'slug': 'first-post',
        })