N
N
nurzhannogerbek2017-09-13 12:27:37
Django
nurzhannogerbek, 2017-09-13 12:27:37

What else to add to the unit test for the sort function?

Hello! Please help me figure it out.
The page displays a list of article titles. The user can drag and drop them and sort them as they wish. Used jQuery UI. This behavior is implemented by this view (see below).
The question is the following. Could someone help to make a unit test for this view. I had little to do with unit testing. At the moment, I only checked the status_code. It would be interesting to know your opinions. I will be grateful for any help!
views.py:

from braces.views import CsrfExemptMixin, JsonRequestResponseMixin

class ArticleSortingView(CsrfExemptMixin, JsonRequestResponseMixin, FormView):
    def post(self, request, *args, **kwargs):
        for pk, idx in self.request_json.items():
            Slide.objects.filter(pk=pk).update(idx=idx)
        return self.render_json_response({'saved': 'OK'})

tests.py:
class ArticleViewTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        self.credentials = {'username': 'user', 'password': 'password'}
        self.user = User.objects.create_user(**self.credentials)

    def test_sorting_article(self):
        self.assertTrue(self.user)

        logged_in = self.client.login(**self.credentials)
        self.assertTrue(logged_in)

        response = self.client.post(reverse("article:article_sorting"), follow=True)
        self.assertEqual(response.status_code, 200)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Kuzmichev, 2017-09-13
@nurzhannogerbek

  • in setUpcreate multiple Slide objects
  • add to call
    self.client.post(reverse("article:article_sorting"), follow=True)
    json, which will change the sorting of these objects
  • add one more check besides the status (and possibly the response json) that the Slide objects have changed their sorting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question