def test_csvdb_all_reports(clean_csv_files): """ Test create/save/read all kind of reports """ all_reports = [(PowerModel(), gen_power_report), (HWPCModel(), gen_hwpc_report)] for model, generator in all_reports: # Load DB csvdb = CsvDB(current_path=PATH_TO_SAVE) csvdb.connect() # Create report report = generator() # Save report csvdb.save(report, model) # Read report for r, d, f in os.walk(PATH_TO_SAVE + report.sensor + "-" + report.target + "/"): for file in f: if '.csv' in file: csvdb.add_file(os.path.join(r, file)) csvdb_iter = csvdb.iter(model, False) read_report = next(csvdb_iter) # Compare assert read_report == report
def __init__(self, component_group_name): Generator.__init__(self, component_group_name) self.model_factory = { 'HWPCReport': HWPCModel(), 'PowerReport': PowerModel(), 'FormulaReport': FormulaModel(), 'ControlReport': ControlModel(), } self.db_factory = { 'mongodb': lambda db_config: MongoDB(db_config['uri'], db_config['db'], db_config['collection']), 'socket': lambda db_config: SocketDB(db_config['port']), 'csv': lambda db_config: CsvDB( current_path=os.getcwd() if 'directory' not in db_config else db_config['directory'], files=[] if 'files' not in db_config else db_config['files']), 'influxdb': lambda db_config: InfluxDB(db_config['uri'], db_config['port'], db_config['db']), 'opentsdb': lambda db_config: OpenTSDB(db_config['uri'], db_config['port'], db_config['metric_name']), }
def test_csvdb_bad_filepath(self): """ Test with bad filepath """ with pytest.raises(CsvBadFilePathError) as pytest_wrapped: CsvDB(HWPCModel(), ["/tmp/unknowfile"]).connect() assert pytest_wrapped.type == CsvBadFilePathError
def test_csvdb_second_primary_missing(self): """ Create one full HWPCReport (the first), then return None """ csvdb = CsvDB(HWPCModel(), SECOND_PRIMARY_MISSING) csvdb.connect() group_name = [path.split('/')[-1] for path in SECOND_PRIMARY_MISSING] csvdb_iter = iter(csvdb) report = next(csvdb_iter) for key in KEYS_COMMON: assert key in report for group in group_name: assert group in report['groups'] assert report['timestamp'] == "1539260664189" with pytest.raises(StopIteration) as pytest_wrapped: next(csvdb_iter) assert pytest_wrapped.type == StopIteration
def test_csvdb_bad_common(self): """ Test when file miss some common column """ csv_files = BAD_COMMON while csv_files: with pytest.raises(CsvBadCommonKeysError) as pytest_wrapped: CsvDB(HWPCModel(), csv_files).connect() assert pytest_wrapped.type == CsvBadCommonKeysError csv_files = csv_files[1:]
def test_csvdb_two_reports(self): """ Create two full HWPCReport, then return None """ csvdb = CsvDB(HWPCModel(), BASIC_FILES) csvdb.connect() group_name = [path.split('/')[-1] for path in BASIC_FILES] csvdb_iter = iter(csvdb) for _ in range(2): report = next(csvdb_iter) for key in KEYS_COMMON: assert key in report for group in group_name: assert group in report['groups'] with pytest.raises(StopIteration) as pytest_wrapped: next(csvdb_iter) assert pytest_wrapped.type == StopIteration
def test_csvdb_second_rapl_missing(self): """ Create two reports, one is full, second without rapl, then return None """ csvdb = CsvDB(HWPCModel(), SECOND_RAPL_MISSING) csvdb.connect() group_name = [path.split('/')[-1] for path in SECOND_RAPL_MISSING] csvdb_iter = iter(csvdb) for i in range(2): report = next(csvdb_iter) for key in KEYS_COMMON: assert key in report for group in group_name: if i == 1 and "rapl" in group: assert group not in report['groups'] else: assert group in report['groups'] with pytest.raises(StopIteration) as pytest_wrapped: next(csvdb_iter) assert pytest_wrapped.type == StopIteration
def test_csvdb_save_header_error(self, corrupted_csvdb): """ Try to save a PowerReport with an existent file and a corrupted header """ csvdb = CsvDB(PowerReport, ['mdt_socket'], current_path=PATH_TO_SAVE) csvdb.connect() # Try to save one PowerReport power_report = gen_power_report() with pytest.raises(HeaderAreNotTheSameError) as pytest_wrapped: csvdb.save(power_report)
def __init__(self, component_group_name): Generator.__init__(self, component_group_name) self.model_factory = { 'HWPCReport': HWPCModel(), 'PowerReport': PowerModel(), 'FormulaReport': FormulaModel(), 'ControlReport': ControlModel(), } self.db_factory = { 'mongodb': lambda db_config, _: MongoDB(db_config['uri'], db_config['db'], db_config['collection']), 'socket': lambda db_config, _: SocketDB(db_config['port']), 'csv': lambda db_config, _: CsvDB( current_path=os.getcwd() if 'directory' not in db_config else db_config['directory'], files=[] if 'files' not in db_config else db_config['files']), 'influxdb': lambda db_config, _: InfluxDB(db_config['uri'], db_config['port'], db_config['db']), 'opentsdb': lambda db_config, _: OpenTSDB(db_config['uri'], db_config['port'], db_config['metric_name']), 'prom': lambda db_config, _: PrometheusDB( db_config['port'], db_config['addr'], db_config['metric_name'], db_config['metric_description'], self.model_factory[db_config[ 'model']], db_config['aggregation_period']), 'tcp': lambda db_config, group_name: IOTcpDB(db_config['uri'], db_config['port'], input=(group_name == "input") ) }
def test_csvdb_save_on(self, clean_csv_files): """ Save a PowerReport from an basic object """ csvdb = CsvDB(current_path=PATH_TO_SAVE) csvdb.connect() power_reports = list() # Save one time with a file that doesn't exist power_reports.append(gen_power_report()) csvdb.save(power_reports[0], PowerModel()) # Save three time for _ in range(3): power_reports.append(gen_power_report()) csvdb.save(power_reports[-1], PowerModel()) # Read the the csvdb and compare the data reading_power_reports = [] csvdb_read = CsvDB(current_path=PATH_TO_SAVE) csvdb_read.add_file(PATH_TO_SAVE + SENSOR + "-" + TARGET + "/PowerReport.csv") csvdb_read.connect() csvdb_read_iter = csvdb_read.iter(PowerModel(), False) for _ in range(4): reading_power_reports.append(next(csvdb_read_iter)) with pytest.raises(StopIteration) as pytest_wrapped: next(csvdb_read_iter) assert pytest_wrapped.type == StopIteration for i in range(4): assert power_reports[i] == reading_power_reports[i]
def csvdb(): return CsvDB(current_path=os.getcwd())
def hwpc_csvdb(): return CsvDB(HWPCReport, ['socket'], current_path=os.getcwd())
def power_csvdb(): return CsvDB(PowerReport, ['socket'], current_path=os.getcwd())