Ensure that file fields can be written to and their data retrieved
(self)
| 48 | DemoFile.objects.create() |
| 49 | |
| 50 | def test_file_fields(self): |
| 51 | """Ensure that file fields can be written to and their data retrieved""" |
| 52 | |
| 53 | class PutFile(Document): |
| 54 | the_file = FileField() |
| 55 | |
| 56 | PutFile.drop_collection() |
| 57 | |
| 58 | text = b"Hello, World!" |
| 59 | content_type = "text/plain" |
| 60 | |
| 61 | putfile = PutFile() |
| 62 | putfile.the_file.put(text, content_type=content_type, filename="hello") |
| 63 | putfile.save() |
| 64 | |
| 65 | result = PutFile.objects.first() |
| 66 | assert putfile == result |
| 67 | assert ( |
| 68 | "%s" % result.the_file |
| 69 | == "<GridFSProxy: hello (%s)>" % result.the_file.grid_id |
| 70 | ) |
| 71 | assert result.the_file.read() == text |
| 72 | assert result.the_file.content_type == content_type |
| 73 | result.the_file.delete() # Remove file from GridFS |
| 74 | PutFile.objects.delete() |
| 75 | |
| 76 | # Ensure file-like objects are stored |
| 77 | PutFile.drop_collection() |
| 78 | |
| 79 | putfile = PutFile() |
| 80 | putstring = BytesIO() |
| 81 | putstring.write(text) |
| 82 | putstring.seek(0) |
| 83 | putfile.the_file.put(putstring, content_type=content_type) |
| 84 | putfile.save() |
| 85 | |
| 86 | result = PutFile.objects.first() |
| 87 | assert putfile == result |
| 88 | assert result.the_file.read() == text |
| 89 | assert result.the_file.content_type == content_type |
| 90 | result.the_file.delete() |
| 91 | |
| 92 | def test_file_fields_stream(self): |
| 93 | """Ensure that file fields can be written to and their data retrieved""" |