示例#1
0
 def test_text_file_wrong_file_type(self):
     """Tests that a ValidationError is thrown by text_file field when the file is not text/plain"""
     upload_file = open('manage.py', 'rb')
     post_dict = {'name': 'test'}
     file_dict = {
         'text_file':
         InMemoryUploadedFile(upload_file, 'text_file', upload_file.name,
                              '', os.path.getsize('manage.py'), None)
     }
     message = "Uploaded file is not a plain text file."
     form = WordSetCreateForm(post_dict, file_dict, self.form_kwargs)
     self.assertFalse(form.is_valid())
     self.assertIn(message, form.errors['text_file'])
示例#2
0
 def test_text_file_upload(self):
     with open("text.txt", "w") as upload_file:
         upload_file.write("Some text")
     with open("text.txt", "rb") as upload_file:
         post_dict = {'name': 'test'}
         file_dict = {
             'text_file':
             InMemoryUploadedFile(upload_file, 'text_file',
                                  upload_file.name, '',
                                  os.path.getsize('text.txt'), None)
         }
         form = WordSetCreateForm(post_dict, file_dict, self.form_kwargs)
         self.assertTrue(form.is_valid())
示例#3
0
 def test_text_file_em_dash_stripped(self):
     """Tests that the em dash, '--' in plain text, is removed from the text of an uploaded file."""
     with open("text.txt", "w") as upload_file:
         upload_file.write(f"--Some text--with-- -- em -- dashes--")
     with open("text.txt", "rb") as upload_file:
         post_dict = {'name': 'test'}
         file_dict = {
             'text_file':
             InMemoryUploadedFile(upload_file, 'text_file',
                                  upload_file.name, '',
                                  os.path.getsize('text.txt'), None)
         }
         form = WordSetCreateForm(post_dict, file_dict, self.form_kwargs)
         self.assertTrue(form.is_valid())
         expected = ['Some', 'text', 'with', 'em', 'dashes']
         self.assertListEqual(form.cleaned_data['text_file'], expected)
示例#4
0
 def test_text_file_field_help_text(self):
     form = WordSetCreateForm(self.form_kwargs)
     field = form.fields['text_file']
     text = "(Optional) Upload a text file containing words (multiple words per line) " \
            "to include in the set. The text is split into individual words (no " \
            "phrases will be detected). Punctuation (apart from hyphens) will be ignored."
     self.assertEqual(field.help_text, text)
示例#5
0
 def test_text_file_size_limit(self):
     """Tests that a ValidationError is thrown by text_file field if the file is greater than 10mb"""
     size = 10000001
     with open("text.txt", "w") as upload_file:
         upload_file.truncate(size)
     with open("text.txt", "rb") as upload_file:
         post_dict = {'name': 'test'}
         file_dict = {
             'text_file':
             InMemoryUploadedFile(upload_file, 'text_file',
                                  upload_file.name, '',
                                  os.path.getsize('text.txt'), None)
         }
         form = WordSetCreateForm(post_dict, file_dict, self.form_kwargs)
         message = "Uploaded file is to large; file size cannot exceed 10 mb."
         self.assertFalse(form.is_valid())
         self.assertIn(message, form.errors['text_file'])
示例#6
0
 def test_text_file_punctuation_next_to_words_stripped(self):
     """Test for an uploaded text file to confirm punctuation at the beginning or end of each word is stripped."""
     with open("text.txt", "w") as upload_file:
         upload_file.write("?Some text\" !and some" + punctuation +
                           " punctuation.")
     with open("text.txt", "rb") as upload_file:
         post_dict = {'name': 'test'}
         file_dict = {
             'text_file':
             InMemoryUploadedFile(upload_file, 'text_file',
                                  upload_file.name, '',
                                  os.path.getsize('text.txt'), None)
         }
         form = WordSetCreateForm(post_dict, file_dict, self.form_kwargs)
         self.assertTrue(form.is_valid())
         expected = ['Some', 'text', 'and', 'some', 'punctuation']
         self.assertListEqual(form.cleaned_data['text_file'], expected)
示例#7
0
 def test_text_file_all_puncuation_except_hyphens_stripped_from_words(self):
     """Tests that, except for the hyphen '-', punctuation is stripped from the middle of words in uploaded text."""
     with open("text.txt", "w") as upload_file:
         # string.punctuation[12] is the hyphen character
         upload_file.write(f"Some text" + punctuation[:12] +
                           "with punctuation" + punctuation[13:] +
                           "and a hyphenated-word")
     with open("text.txt", "rb") as upload_file:
         post_dict = {'name': 'test'}
         file_dict = {
             'text_file':
             InMemoryUploadedFile(upload_file, 'text_file',
                                  upload_file.name, '',
                                  os.path.getsize('text.txt'), None)
         }
         form = WordSetCreateForm(post_dict, file_dict, self.form_kwargs)
         self.assertTrue(form.is_valid())
         expected = [
             'Some', 'text', 'with', 'punctuation', 'and', 'a',
             'hyphenated-word'
         ]
         self.assertListEqual(form.cleaned_data['text_file'], expected)
示例#8
0
 def test_text_file_field(self):
     """Tests that the field text_file is of the correct class and is not required."""
     form = WordSetCreateForm(self.form_kwargs)
     field = form.fields['text_file']
     self.assertIsInstance(field, WordFileField)
     self.assertFalse(field.required)
示例#9
0
 def test_words_field(self):
     """Test that the words field is of the correct class and strip attribute is false"""
     form = WordSetCreateForm(self.form_kwargs)
     field = form.fields['words']
     self.assertIsInstance(field, WordCharField)
     self.assertFalse(field.strip)
示例#10
0
 def test_words_field_help_text(self):
     form = WordSetCreateForm(self.form_kwargs)
     text = "(Optional) Type the words to include in the set (one word or phrase per line)"
     self.assertEqual(text, form.fields['words'].help_text)
示例#11
0
 def test_creator_field_hidden(self):
     form = WordSetCreateForm(self.form_kwargs)
     self.assertIsInstance(form.fields['creator'].widget,
                           forms.widgets.HiddenInput)
示例#12
0
 def test_correct_fields_present(self):
     form = WordSetCreateForm(self.form_kwargs)
     self.assertIn('name', form.fields)
     self.assertIn('description', form.fields)
     self.assertIn('creator', form.fields)
     self.assertIn('words', form.fields)