Commit b2c27bf9 by Peter Weidenbach

overwrite file function added

parent e127a30d
from .gridfs import overwrite_file
__all__ = [
'overwrite_file'
]
def overwrite_file(fs, file_name, binary):
"""
overwrite a file by name in gridfs
:param fs: gridFS instance
:type fs: gridFS
:param file_name: file to overwrite
:type file_name: str
:param mongo_curser: MongoDB curser
:type mongo_curser: mongo_db_curser
:return: None
"""
original_file = fs.find_one({'filename': file_name})
if original_file is not None:
fs.delete(original_file._id)
fs.put(binary, filename=file_name)
import unittest
from pymongo import MongoClient
import gridfs
from common_helper_mongo.gridfs import overwrite_file
class TestGridFS(unittest.TestCase):
def setUp(self):
self.mongo_client = MongoClient()
self.db = self.mongo_client["common_code_test"]
self.fs = gridfs.GridFS(self.db)
def tearDown(self):
self.mongo_client.drop_database(self.db)
self.mongo_client.close()
def testOverwriteFile(self):
self.fs.put(b'original', filename="test_file")
original_content = self.fs.find_one({'filename': "test_file"}).read()
self.assertEqual(original_content, b'original', 'original content not correct')
overwrite_file(self.fs, "test_file", b'changed')
self.assertEqual(len(self.fs.list()), 1, "original file not deleted")
changed_content = self.fs.find_one({'filename': "test_file"}).read()
self.assertEqual(changed_content, b'changed', "content not correct")
if __name__ == "__main__":
# import sys;sys.argv = ['', 'Test.testName']
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment