def test_with_two_filter(self, database): """ Test filter with two filter - 2 first report return first dispatcher - 2 next report return first and second dispatcher - 2 next report return None """ mongodb = MongoDB(URI, "test_filter", "test_filter1") mongodb.connect() hwpc_filter = Filter() hwpc_filter.filter(lambda msg: True if "sensor" in msg.sensor else False, 1) hwpc_filter.filter(lambda msg: True if "test1" in msg.sensor else False, 2) hwpc_filter.filter(lambda msg: True if msg.sensor == "sensor_test2" else False, 3) mongodb_it = mongodb.iter(HWPCModel(), False) for _ in range(2): hwpc_report = next(mongodb_it) assert hwpc_filter.route(hwpc_report) == [1, 2] for _ in range(2): hwpc_report = next(mongodb_it) assert hwpc_filter.route(hwpc_report) == [1, 3] for _ in range(2): hwpc_report = next(mongodb_it) assert hwpc_filter.route(hwpc_report) == [1]
def test_mongodb_all_reports(database): """ Test create/save/read all kind of reports """ all_reports = [(HWPCModel(), gen_hwpc_report), (PowerModel(), gen_power_report)] for model, generator in all_reports: # Load DB mongodb = MongoDB(URI, "test_reports" + model.get_type().__name__, "test_reports" + model.get_type().__name__) mongodb.connect() # Create report report = generator() # Save report mongodb.save(report, model) # Read report mongodb_iter = mongodb.iter(model, False) read_report = next(mongodb_iter) # Compare assert read_report == report
def test_mongodb_read_capped_db(database): """ Test read mongodb capped collection """ # Load DB mongodb = MongoDB(URI, "test_mongodb", "test_mongodb2") # Check if we can read one time mongodb.connect() mongodb_iter = mongodb.iter(HWPCModel(), False) report = None for _ in range(mongodb.collection.count_documents({})): report = next(mongodb_iter) assert report is not None # Check if there is nothing after with pytest.raises(StopIteration) as pytest_wrapped: next(mongodb_iter) assert pytest_wrapped.type == StopIteration # Add data in the collection for _ in range(1): mongodb.save(report, HWPCModel()) # Check if there is nothing after with pytest.raises(StopIteration) as pytest_wrapped: next(mongodb_iter) assert pytest_wrapped.type == StopIteration
def test_mongodb_read_capped_db(mongo_database): """ Test read mongodb capped collection """ # Load DB mongodb = MongoDB(HWPCReport, MONGO_URI, MONGO_DATABASE_NAME, MONGO_INPUT_COLLECTION_NAME) # Check if we can read one time mongodb.connect() mongodb_iter = mongodb.iter(False) report = None for _ in range(mongodb.collection.count_documents({})): report = next(mongodb_iter) assert report is not None # Check if there is nothing after with pytest.raises(StopIteration) as pytest_wrapped: next(mongodb_iter) assert pytest_wrapped.type == StopIteration # Add data in the collection for _ in range(1): mongodb.save(report) # Check if there is nothing after with pytest.raises(StopIteration) as pytest_wrapped: next(mongodb_iter) assert pytest_wrapped.type == StopIteration
def test_mongodb_read_basic_db(database): """ Test read mongodb collection """ # Load DB mongodb = MongoDB(URI, "test_mongodb", "test_mongodb1") # Check if we can reload after reading mongodb.connect() for _ in range(2): mongodb_iter = mongodb.iter(HWPCModel(), False) for _ in range(10): assert next(mongodb_iter) is not None with pytest.raises(StopIteration) as pytest_wrapped: next(mongodb_iter) assert pytest_wrapped.type == StopIteration
def test_mongodb_read_basic_db(mongo_database): """ Test read mongodb collection """ # Load DB mongodb = MongoDB(HWPCReport, MONGO_URI, MONGO_DATABASE_NAME, MONGO_INPUT_COLLECTION_NAME) # Check if we can reload after reading mongodb.connect() for _ in range(2): mongodb_iter = mongodb.iter(False) for _ in range(10): assert next(mongodb_iter) is not None with pytest.raises(StopIteration) as pytest_wrapped: next(mongodb_iter) assert pytest_wrapped.type == StopIteration