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'])
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())
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)
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'])
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)
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)