

@override_settings(DEBUG=True)
def test_api_accepts_decorator_debug(self):
    '\n        Test the @api_accepts decorator (with DEBUG=True).\n        '
    rf = RequestFactory()

    @api_accepts({
        'im_required': forms.IntegerField(),
    })
    def my_post_view(request, *args, **kwargs):
        self.assertIsInstance(request.POST['im_required'], int)
        return JsonResponseWithStatus('post called')
    request = rf.post('/post_success', data={
        'im_required': '10',
    })
    response = my_post_view(request)
    self.assertEquals(response.status_code, 200)
    response_json = json.loads(response.content)
    self.assertEquals(response_json['error_message'], 'post called')

    @api_accepts({
        'im_required': forms.IntegerField(),
    })
    def my_get_view(request, *args, **kwargs):
        self.assertIsInstance(request.GET['im_required'], int)
        return JsonResponseWithStatus('get called')
    request = rf.get('/get_success', data={
        'im_required': '10',
    })
    response = my_get_view(request)
    self.assertEquals(response.status_code, 200)
    response_json = json.loads(response.content)
    self.assertEquals(response_json['error_message'], 'get called')

    @api_accepts({
        'im_required': forms.IntegerField(),
    })
    def my_failed_post_view(request, *args, **kwargs):
        return JsonResponseWithStatus('not called on failure')
    request = rf.post('/post_failure', data={
        'hello': 'world',
    })
    response = my_failed_post_view(request)
    self.assertEquals(response.status_code, 400)
    user = User.objects.create()

    @api_accepts({
        'im_required': forms.IntegerField(),
        'user': User(),
    })
    def model_view(request, *args, **kwargs):
        self.assertTrue((request.GET['user'].id == user.id))
        return JsonResponse()
    request = rf.get('/model_failure', data={
        'im_required': 10,
        'user-id': 99999,
    })
    response = model_view(request)
    self.assertEquals(response.status_code, 404)
    request = rf.get('/model_success', data={
        'im_required': 10,
        'user-id': user.id,
    })
    response = model_view(request)
    self.assertEquals(response.status_code, 200)

    @api_accepts({
        'date_param': forms.DateField(),
        'datetime_param': forms.DateTimeField(input_formats=['%Y-%m-%dT%H:%M:%S']),
        'time_param': forms.TimeField(),
    })
    def datetime_view(request, *args, **kwargs):
        self.assertTrue(request.GET['date_param'], datetime.date(2010, 2, 17))
        self.assertTrue(request.GET['datetime_param'], datetime.datetime(2006, 11, 21, 16, 30))
        self.assertTrue(request.GET['time_param'], datetime.time(16, 30))
        return JsonResponse()
    request = rf.get('/datetime_success', data={
        'date_param': '2010-02-17',
        'datetime_param': '2006-11-21T16:30:00',
        'time_param': '16:30:00',
    })
    response = datetime_view(request)
    self.assertEquals(response.status_code, 200)

dataset/ETHPy150Open bipsandbytes/django-api/django_api/tests.py SimpleTest.test_api_accepts_decorator_debug/original