def test_GridFsObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test yield self._drop_gridfs(db) gfs = GridFS(db) # Default collection yield gfs.delete(u"test") _ = gfs.new_file(filename="test_1", contentType="text/plain", chunk_size=65536) yield conn.disconnect() conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test yield self._drop_gridfs(db) gfs = GridFS(db) # Default collection _ = yield gfs.put(b"0xDEADBEEF", filename="test_2", contentType="text/plain", chunk_size=65536) # disconnect yield conn.disconnect() conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test gfs = GridFS(db) # Default collection # Missing file raises error yield self.assertFailure(gfs.get("test_3"), NoFile) # disconnect yield conn.disconnect()
def test_GridFsObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test db.fs.files.remove({}) # drop all objects there first db.fs.chunks.remove({}) gfs = GridFS(db) # Default collection yield gfs.delete(u"test") _ = gfs.new_file(filename="test_1", contentType="text/plain", chunk_size=65536) yield conn.disconnect() conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test db.fs.files.remove({}) # drop all objects there first gfs = GridFS(db) # Default collection _ = yield gfs.put(b"0xDEADBEEF", filename="test_2", contentType="text/plain", chunk_size=65536) # disconnect yield conn.disconnect() conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test gfs = GridFS(db) # Default collection _ = yield gfs.get("test_3") # disconnect yield conn.disconnect()
def test_GridFsOperations(self): """ Tests gridfs operations """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test # Drop files first TODO: iterate through files and delete them yield self._drop_gridfs(db) # Don't forget to disconnect self.addCleanup(self._disconnect, conn) try: in_file = StringIO(b"Test input string") out_file = StringIO() except Exception as 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(db) # Default collection if twisted_version.major >= 15: with self.assertRaises(NoFile): yield gfs.get_last_version("optest") 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: 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() if g_out: 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)
def test_GridFsOperations(self): """ Tests gridfs operations """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test # Drop files first TODO: iterate through files and delete them yield self._drop_gridfs(db) # Don't forget to disconnect self.addCleanup(self._disconnect, conn) try: in_file = StringIO(b"Test input string") out_file = StringIO() except Exception as 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(db) # Default collection if twisted_version.major >= 15: with self.assertRaises(NoFile): yield gfs.get_last_version("optest") 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: 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() if g_out: 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)