def single_gridfs_data_stream(self, graphical=None): """Dump data to database sequentially.""" db = Mongo(self.args) db.connect() # loop n many iterations for i in range(self.args["iterations"]) if graphical is None else tqdm( range(self.args["iterations"])): # dump all data associated with a single iteration for data in self.data: print("submitting: ", i) db.dump( db_collection_name=self.args["db_collection_name"], data=( {"iteration": i}, data))
def test_dump(self): """Test/ example of dump and retrieve from a MongoDB database.""" from ezdb.mongo import Mongo db = Mongo({"pylog": null_printer}) self.assertIsInstance(db, Mongo) db.connect() db.dump(db_collection_name="test", data={"success": 1}) cursor = db.getCursor(db_collection_name="test") for batch in db.getBatches(db_data_cursor=cursor): self.assertEqual(len(batch), 1) for doc in batch: self.assertEqual(doc["success"], 1)
def test_gridfs(self): """Test/ example of gridfs dump and retrieve from MongoDB.""" from ezdb.mongo import Mongo db = Mongo({"pylog": null_printer}) self.assertIsInstance(db, Mongo) db.connect() db.dump(db_collection_name="test", data=({"success": 1}, b'success')) cursor = db.getCursor(db_collection_name="test.files") for batch in db.getFiles(db_data_cursor=cursor): for grid in batch: # check ids match self.assertEqual(grid["_id"], grid["metadata"]["_id"]) # read file and check is equal to what we put in self.assertEqual(grid["gridout"].read(), b'success')
def test_delete(self): """Test that we can delete items from db correctly by id.""" from ezdb.mongo import Mongo db = Mongo({"pylog": null_printer}) self.assertIsInstance(db, Mongo) db.connect() db.dump(db_collection_name="test", data={"success": 1}) cursor = db.getCursor(db_collection_name="test") for batch in db.getBatches(db_data_cursor=cursor): self.assertEqual(len(batch), 1) for doc in batch: self.assertEqual(doc["success"], 1) db.deleteId(db_collection_name="test", id=doc["_id"]) cursor = db.getCursor(db_collection_name="test") deleted_collection = list(db.getBatches(db_data_cursor=cursor)) self.assertEqual(deleted_collection, [])
def test_donate(self): """Test/ example of donating data to another collection.""" from ezdb.mongo import Mongo db = Mongo({"pylog": null_printer}) self.assertIsInstance(db, Mongo) db.connect() # insert data in the donor collection db.dump(db_collection_name="donor", data={"success": 1}) # donate data db.getCursor(db_collection_name="rec") db.donate(other=db, other_collection="rec", db_collection_name="donor") # check donated data is correct cursor = db.getCursor(db_collection_name="rec") for batch in db.getBatches(db_data_cursor=cursor): self.assertEqual(len(batch), 1) for doc in batch: self.assertEqual(doc["success"], 1)