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_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_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)
####################### # JPEG DOWNLOAD FROM DB ####################### db_connect_args = db_config projection = [{'$project': {'_id': 1}}] db = Mongo(db_connect_args) db.connect() db.getCursor(db_collection_name=db_connect_args["db_collection_name"], db_pipeline=db["db_pipeline"].extend(projection)) # db.debug() every_id = [] # check verify if files exist already for batch in db.getBatches(): every_id.extend(list(map(lambda doc: str(doc["_id"]), batch))) print(every_id) file_names = [f for f in glob.glob("*.jpeg")] print(file_names) # get data that does not already exist from database print(db_connect_args["db_pipeline"]) db.getCursor(db_collection_name=db_connect_args["db_collection_name"], db_pipeline=db_connect_args["db_pipeline"]) print("begin stream... (* files that exist in filesystem will be skipped)") for batch in db.getFiles(db_collection_name=top_level_collection_name): for grid in batch: image = grid["gridout"].read() image = io.BytesIO(image)