Exemplo n.º 1
0
class RequestsTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = RequestLoggerMiddleware()

        request = self.factory.get('/')
        self.middleware.process_request(request)

        self.data_valid = {
            'form-0-id': '1',
            'form-0-priority': '123',
            'form-INITIAL_FORMS': 1,
            'form-MAX_NUM_FORMS': 1000,
            'form-TOTAL_FORMS': 1
        }
        self.data_invalid = {
            'form-0-id': '1',
            'form-0-priority': 'abc',
            'form-INITIAL_FORMS': 1,
            'form-MAX_NUM_FORMS': 1000,
            'form-TOTAL_FORMS': 1
        }

    def test_log_view(self):
        response = self.client.get(reverse('requests'))
        self.assertIsNotNone(response.context['formset'])
        self.assertContains(response, response.context['formset'][0].instance.host)
        self.assertIsNotNone(response.context['form'])
        self.assertContains(response, response.context['form'].fields['order'].label)

    def test_ordering(self):
        # add 2 records (id=2, priority=100)
        request = self.factory.get('/')
        self.middleware.process_request(request)
        obj = LogRequest.objects.get(pk=2)
        obj.priority = 100
        obj.save()

        response = self.client.get(reverse('requests'), {'order': LogOrderingForm.DATE})
        self.assertEqual(response.context['formset'][0].instance.id, 1)

        response = self.client.get(reverse('requests'), {'order': LogOrderingForm.PRIORITY})
        self.assertEqual(response.context['formset'][0].instance.id, 2)

    def test_formset_save(self):
        response = self.client.post(reverse('requests'), self.data_valid)
        self.assertEqual(response.status_code, 302)

        response = self.client.get(reverse('requests'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['formset'][0].instance.priority, 123)

    def test_formset_error(self):
        response = self.client.post(reverse('requests'), self.data_invalid)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Enter a whole number')
Exemplo n.º 2
0
class MiddlewareTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = RequestLoggerMiddleware()

    def test_log_write(self):
        request = self.factory.get('/')
        self.middleware.process_request(request)
        self.assertEqual(LogRequest.objects.count(), 1)
        self.assertEqual(LogRequest.objects.get(pk=1).method, 'GET')
        self.assertEqual(LogRequest.objects.get(pk=1).path, '/')

    def test_log_not_write(self):
        request = self.factory.get(reverse('requests'))
        self.middleware.process_request(request)
        self.assertEqual(LogRequest.objects.count(), 0)
Exemplo n.º 3
0
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = RequestLoggerMiddleware()

        request = self.factory.get('/')
        self.middleware.process_request(request)

        self.data_valid = {
            'form-0-id': '1',
            'form-0-priority': '123',
            'form-INITIAL_FORMS': 1,
            'form-MAX_NUM_FORMS': 1000,
            'form-TOTAL_FORMS': 1
        }
        self.data_invalid = {
            'form-0-id': '1',
            'form-0-priority': 'abc',
            'form-INITIAL_FORMS': 1,
            'form-MAX_NUM_FORMS': 1000,
            'form-TOTAL_FORMS': 1
        }
Exemplo n.º 4
0
 def setUp(self):
     self.factory = RequestFactory()
     self.middleware = RequestLoggerMiddleware()