Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_name_from_extension(self):
for extension, mimetype in self.mimetypes:
filename = 'doc' + extension
document_type = DocumentType.type_for_file(filename)
# for example: 'DOC Document'
expected_name = extension[1:].upper() + ' Document'
self.assertEqual(document_type.name, expected_name)
def test_type_for_file_returns_the_same_type_object_after_editing(self):
u"""
We have to avoid creating multiple DocumentType objects for the same extension.
This test checks that after some kind of a modification to one type object,
another one will not be created when calling type_for_file with the same file type.
"""
# retrieve a DocumentType for some arbitrary file
document_type = DocumentType.type_for_file("test.txt")
first_id = document_type.id
# now let's change the name just like the administrator would do
document_type.name = u'Text file'
document_type.save()
# and now find the type for another file
another_type = DocumentType.type_for_file("another.txt")
second_id = another_type.id
self.assertEqual(first_id, second_id)
def test_type_for_file_returns_the_same_type_object_after_editing(self):
u"""
We have to avoid creating multiple DocumentType objects for the same extension.
This test checks that after some kind of a modification to one type object,
another one will not be created when calling type_for_file with the same file type.
"""
# retrieve a DocumentType for some arbitrary file
document_type = DocumentType.type_for_file("test.txt")
first_id = document_type.id
# now let's change the name just like the administrator would do
document_type.name = u'Text file'
document_type.save()
# and now find the type for another file
another_type = DocumentType.type_for_file("another.txt")
second_id = another_type.id
self.assertEqual(first_id, second_id)
def save(self, *args, **kwargs):
u"""
Attaches a guessed DocumentType to the Document object.
The check for id is a standard way to determine whether the object
is created (no row in the database yet, hence no id) or updated.
"""
if not self.id:
self.type = DocumentType.type_for_file(self.file)
super(Document, self).save(*args, **kwargs)