MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / test_file_disk_space

Method test_file_disk_space

tests/fields/test_file_field.py:310–374  ·  view source on GitHub ↗

Test disk space usage when we delete/replace a file

(self)

Source from the content-addressed store, hash-verified

308 assert test_file.the_file not in [{"test": 1}]
309
310 def test_file_disk_space(self):
311 """Test disk space usage when we delete/replace a file"""
312
313 class TestFile(Document):
314 the_file = FileField()
315
316 text = b"Hello, World!"
317 content_type = "text/plain"
318
319 testfile = TestFile()
320 testfile.the_file.put(text, content_type=content_type, filename="hello")
321 testfile.save()
322
323 # Now check fs.files and fs.chunks
324 db = TestFile._get_db()
325
326 files = db.fs.files.find()
327 chunks = db.fs.chunks.find()
328 assert len(list(files)) == 1
329 assert len(list(chunks)) == 1
330
331 # Deleting the document should delete the files
332 testfile.delete()
333
334 files = db.fs.files.find()
335 chunks = db.fs.chunks.find()
336 assert len(list(files)) == 0
337 assert len(list(chunks)) == 0
338
339 # Test case where we don't store a file in the first place
340 testfile = TestFile()
341 testfile.save()
342
343 files = db.fs.files.find()
344 chunks = db.fs.chunks.find()
345 assert len(list(files)) == 0
346 assert len(list(chunks)) == 0
347
348 testfile.delete()
349
350 files = db.fs.files.find()
351 chunks = db.fs.chunks.find()
352 assert len(list(files)) == 0
353 assert len(list(chunks)) == 0
354
355 # Test case where we overwrite the file
356 testfile = TestFile()
357 testfile.the_file.put(text, content_type=content_type, filename="hello")
358 testfile.save()
359
360 text = b"Bonjour, World!"
361 testfile.the_file.replace(text, content_type=content_type, filename="hello")
362 testfile.save()
363
364 files = db.fs.files.find()
365 chunks = db.fs.chunks.find()
366 assert len(list(files)) == 1
367 assert len(list(chunks)) == 1

Callers

nothing calls this directly

Calls 6

TestFileClass · 0.85
_get_dbMethod · 0.80
replaceMethod · 0.80
putMethod · 0.45
saveMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected