def setUp(self): self.conn = connection.ConnectionPool("mongodb://127.0.0.1/dbname") self.db = self.conn['dbname'] self.gfs = gridfs.GridFS(self.db) for n in range(0, 10): data = "Hello" + str(n) yield self.gfs.put(data.encode('utf-8'), filename="world")
class TestGridFsObjects(unittest.TestCase): """ Test the GridFS operations from txmongo._gridfs """ @defer.inlineCallbacks def _disconnect(self, conn): """ Disconnect the connection """ yield conn.disconnect() @defer.inlineCallbacks def test_GridFsObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test collection = db.fs gfs = gridfs.GridFS(db) # Default collection gridin = GridIn(collection, filename='test', contentType="text/plain", chunk_size=2**2**2**2) new_file = gfs.new_file(filename='test2', contentType="text/plain", chunk_size=2**2**2**2) # disconnect yield conn.disconnect() @defer.inlineCallbacks def test_GridFsOperations(self): """ Tests gridfs operations """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test collection = db.fs # Don't forget to disconnect self.addCleanup(self._disconnect, conn) try: in_file = StringIO("Test input string") out_file = StringIO() except Exception, e: self.fail("Failed to create memory files for testing: %s" % e) g_out = None try: # Tests writing to a new gridfs file gfs = gridfs.GridFS(db) # Default collection g_in = gfs.new_file(filename='optest', contentType="text/plain", chunk_size=2**2**2**2) # non-default chunk size used # yielding to ensure writes complete before we close and close before we try to read yield g_in.write(in_file.read()) yield g_in.close() # Tests reading from an existing gridfs file g_out = yield gfs.get_last_version('optest') data = yield g_out.read() out_file.write(data) _id = g_out._id except Exception,e: self.fail("Failed to communicate with the GridFS. " + "Is MongoDB running? %s" % e)
def test_GridFsObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test collection = db.fs gfs = gridfs.GridFS(db) # Default collection gridin = GridIn(collection, filename='test', contentType="text/plain", chunk_size=2**2**2**2) new_file = gfs.new_file(filename='test2', contentType="text/plain", chunk_size=2**2**2**2) # disconnect yield conn.disconnect()
def test_GridFsObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection( ['%s:%d' % (mongo_host, mongo_port)]) db = conn.test collection = db.fs gfs = gridfs.GridFS(db) # Default collection gridin = GridIn(collection, filename='test', contentType="text/plain", chunk_size=65536) new_file = gfs.new_file(filename='test2', contentType="text/plain", chunk_size=65536) yield gfs._GridFS__index_create_deferred # Wait for index to be created # disconnect yield conn.disconnect()
class TestGridFsObjects(unittest.TestCase): """ Test the GridFS operations from txmongo._gridfs """ @defer.inlineCallbacks def _disconnect(self, conn): """ Disconnect the connection """ yield conn.disconnect() @defer.inlineCallbacks def test_GridFsObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection( ['%s:%d' % (mongo_host, mongo_port)]) db = conn.test collection = db.fs gfs = gridfs.GridFS(db) # Default collection gridin = GridIn(collection, filename='test', contentType="text/plain", chunk_size=65536) new_file = gfs.new_file(filename='test2', contentType="text/plain", chunk_size=65536) yield gfs._GridFS__index_create_deferred # Wait for index to be created # disconnect yield conn.disconnect() @defer.inlineCallbacks def test_GridFsOperations(self): """ Tests gridfs operations """ conn = yield txmongo.MongoConnection( ['%s:%d' % (mongo_host, mongo_port)]) db = conn.test # Don't forget to disconnect self.addCleanup(self._disconnect, conn) try: in_file = StringIO("Test input string") out_file = StringIO() except Exception, e: self.fail("Failed to create memory files for testing: %s" % e) try: # Tests writing to a new gridfs file gfs = gridfs.GridFS(db) # Default collection g_in = gfs.new_file(filename='optest', contentType="text/plain", chunk_size=65536) # non-default chunk size used # yielding to ensure writes complete before we close and close before we try to read yield g_in.write(in_file.read()) yield g_in.close() # Tests reading from an existing gridfs file g_out = yield gfs.get_last_version('optest') data = yield g_out.read() out_file.write(data) _id = g_out._id except Exception as e: print "Error:", e self.fail("Failed to communicate with the GridFS. " + "Is MongoDB running? %s" % e) else: self.assertEqual(in_file.getvalue(), out_file.getvalue(), "Could not read the value from writing an input") finally: in_file.close() out_file.close() g_out.close() listed_files = yield gfs.list() self.assertEqual( ['optest'], listed_files, "'optest' is the only expected file and we received %s" % listed_files) yield gfs.delete(_id)