def test_upload_invalid_choice_field(self): # invalid choice self.data_set['language'] = 'brainfuck' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['ChoiceField']['invalid_choice'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'language') # empty string self.data_set['language'] = '' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['ChoiceField']['invalid_choice'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'language') # no data provided self.data_set.pop('language') request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['ChoiceField']['required'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'language')
def test_upload_invalid_choice_field(self): # invalid choice self.data_set['language'] = 'brainfuck' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['ChoiceField']['invalid_choice'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'language') # empty string self.data_set['language'] = '' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['ChoiceField']['invalid_choice'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'language') # no data provided self.data_set.pop('language') request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['ChoiceField']['required'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'language')
def test_upload_invalid_char_field(self): # Too long string self.data_set['title'] = 'Too Long Title For Defined Serializer' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['CharField']['max_length'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'title') # Empty string self.data_set['title'] = '' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['CharField']['blank'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'title') # No data provided self.data_set.pop('title') request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['CharField']['required'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'title')
def test_upload_invalid_decimal_field(self): # invalid self.data_set['rating'] = 'text instead of float' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['DecimalField']['invalid'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'rating') # decimal places self.data_set['rating'] = 2.99 request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['DecimalField']['max_decimal_places'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'rating') # decimal max digits self.data_set['rating'] = 222.9 request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['DecimalField']['max_digits'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'rating')
def test_upload_invalid_decimal_field(self): # invalid self.data_set['rating'] = 'text instead of float' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['DecimalField']['invalid'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'rating') # decimal places self.data_set['rating'] = 2.99 request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['DecimalField']['max_decimal_places'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'rating') # decimal max digits self.data_set['rating'] = 222.9 request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['DecimalField']['max_digits'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'rating')
def test_upload_invalid_char_field(self): # Too long string self.data_set['title'] = 'Too Long Title For Defined Serializer' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['CharField']['max_length'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'title') # Empty string self.data_set['title'] = '' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['CharField']['blank'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'title') # No data provided self.data_set.pop('title') request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['CharField']['required'] self.assertEqual(int(response.data['errors'][0]['code']), code) self.assertEqual(response.data['errors'][0]['field'], 'title')
def test_update_snippet(self): self.data_set['code'] = 'def foo(bar):\n\treturn bar' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['code'], 'def foo(bar):\n\treturn bar')
def test_update_snippet(self): self.data_set['code'] = 'def foo(bar):\n\treturn bar' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['code'], 'def foo(bar):\n\treturn bar')
def update_invalid_boolean(self): self.data_set['linenos'] = 'A text instead of a bool' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['BooleanField']['invalid'] self.assertEqual(response.data['linenos'][0]['code'], code)
def update_invalid_boolean(self): self.data_set['linenos'] = 'A text instead of a bool' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['BooleanField']['invalid'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'linenos')
def test_datetime_field_error_content(self): # invalid self.data_set['posted_date'] = 'text instead of date' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['DateTimeField']['invalid'] self.assertEqual(int(response.data['posted_date'][0]['code']), code)
def test_upload_invalid_decimal_field(self): # invalid self.data_set['rating'] = 'text instead of float' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['DecimalField']['invalid'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('rating')) self.assertEqual(type(errors['rating']), list) self.assertEqual(errors['rating'][0]['code'], code) # decimal places self.data_set['rating'] = 2.99 request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['DecimalField']['max_decimal_places'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('rating')) self.assertEqual(type(errors['rating']), list) self.assertEqual(errors['rating'][0]['code'], code) # decimal max digits self.data_set['rating'] = 222.9 request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['DecimalField']['max_digits'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('rating')) self.assertEqual(type(errors['rating']), list) self.assertEqual(errors['rating'][0]['code'], code)
def test_upload_invalid_choice_field(self): # invalid choice self.data_set['language'] = 'brainfuck' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['ChoiceField']['invalid_choice'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('language')) self.assertEqual(type(errors['language']), list) self.assertEqual(errors['language'][0]['code'], code) # empty string self.data_set['language'] = '' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['ChoiceField']['invalid_choice'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('language')) self.assertEqual(type(errors['language']), list) self.assertEqual(errors['language'][0]['code'], code) # no data provided self.data_set.pop('language') request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['ChoiceField']['required'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('language')) self.assertEqual(type(errors['language']), list) self.assertEqual(errors['language'][0]['code'], code)
def test_upload_invalid_char_field(self): # Too long string self.data_set['title'] = 'Too Long Title For Defined Serializer' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['CharField']['max_length'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('title')) self.assertEqual(type(errors['title']), list) self.assertEqual(errors['title'][0]['code'], code) # Empty string self.data_set['title'] = '' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['CharField']['blank'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('title')) self.assertEqual(type(errors['title']), list) self.assertEqual(errors['title'][0]['code'], code) # No data provided self.data_set.pop('title') request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['CharField']['required'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('title')) self.assertEqual(type(errors['title']), list) self.assertEqual(errors['title'][0]['code'], code)
def test_cannot_update_to_not_unique_watermark(self): self.data_set['watermark'] = 'TEST2' Snippet.objects.create(**self.data_set) request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_VALIDATOR_ERRORS['UniqueValidator'] self.assertEqual(int(response.data['watermark'][0]['code']), code)
def test_datetime_field_error_content(self): # invalid self.data_set['posted_date'] = 'text instead of date' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_FIELD_ERRORS['DateTimeField']['invalid'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'posted_date')
def test_cannot_update_to_not_unique_watermark(self): self.data_set['watermark'] = 'TEST2' Snippet.objects.create(**self.data_set) request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 400) code = settings.FRIENDLY_VALIDATOR_ERRORS['UniqueValidator'] self.assertEqual(response.data['errors'][0]['code'], code) self.assertEqual(response.data['errors'][0]['field'], 'watermark')
def update_invalid_boolean(self): self.data_set['linenos'] = 'A text instead of a bool' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['BooleanField']['invalid'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('linenos')) self.assertEqual(type(errors['linenos']), list) self.assertEqual(errors['linenos'][0]['code'], code)
def test_datetime_field_error_content(self): # invalid self.data_set['posted_date'] = 'text instead of date' request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_FIELD_ERRORS['DateTimeField']['invalid'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('posted_date')) self.assertEqual(type(errors['posted_date']), list) self.assertEqual(errors['posted_date'][0]['code'], code)
def test_cannot_update_to_not_unique_watermark(self): self.data_set['watermark'] = 'TEST2' Snippet.objects.create(**self.data_set) request = self.factory.put(reverse('snippet-detail', kwargs={'pk': self.snippet.pk}), data=self.data_set) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) code = FRIENDLY_VALIDATOR_ERRORS['UniqueValidator'] errors = response.data.get('errors') self.assertIsNotNone(errors) self.assertIsNotNone(errors.get('watermark')) self.assertEqual(type(errors['watermark']), list) self.assertEqual(errors['watermark'][0]['code'], code)
def test_retrieve_object(self): request = self.factory.get(reverse('snippet-detail', kwargs={'pk': self.snippet.pk})) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_retrieve_object(self): request = self.factory.get(reverse('snippet-detail', kwargs={'pk': self.snippet.pk})) response = SnippetDetail.as_view()(request, pk=self.snippet.pk) self.assertEqual(response.status_code, 200)