def test_output_sql(input_obj, kwargs): data = DataSet(input_obj, **kwargs) assert isinstance(data, DataSet) assert data.db_uri == kwargs.get('db_uri') data.to_output() db = dataset_db.connect(kwargs.get('db_uri')) rows = list(db.query('select * from test;')) assert len(rows) == 2 # removing id assert sorted(list(rows[0].values())[1:]) == sorted( list(input_obj[0].values())) if os.path.exists(kwargs.get('db_uri').replace('sqlite:///', '')): os.remove(kwargs.get('db_uri').replace('sqlite:///', ''))
def test_output(input_obj, output, output_type, kwargs): data = DataSet(input_obj, output=output, **kwargs) if output.startswith('file://'): assert data.output_filename == output.replace('file://', '') to_output = data.to_output() assert isinstance(to_output, output_type) if output_type == list and isinstance(input_obj, list): assert input_obj == to_output elif output_type == list: assert input_obj.tolist() == to_output elif output_type == pd.DataFrame: assert to_output.shape == (2, 3) elif output_type == np.ndarray and not kwargs: assert np.array_equal(to_output[0, :], [1, 2, 5]) elif output_type == np.ndarray: assert to_output[0] == {'a': 1, 'b': 2, 'd': 5} elif output_type == str: assert os.path.exists(to_output)
def fuzz_from_parser(parser): """ Fuzz using parser input. This will generate a `dataset.Dataset` from `parser.input`, apply any defined strategies and call `dataset.to_output`. Arguments: parser (`parsers.StrategyCLIParser` or `parsers.StrategyYAMLParser`): strategy parser Returns: dataset.to_output() """ dataset = DataSet(parser.input, output=parser.output, db_uri=parser.db_uri, query=parser.query, table=parser.table) for strategy in parser.strategies: strategy_obj = build_strategy(strategy, dataset) try: strategy_obj.run_strategy() except Exception: logging.exception('Error running strategy: %s', strategy) return dataset.to_output()
def test_bad_io(input_obj, output, kwargs): with pytest.raises(Exception): if 'data' in input_obj: input_obj = 'file://{}'.format(input_obj) data = DataSet(input_obj, output=output, **kwargs) data.to_output()
def test_output_errors(input_obj, output): with pytest.raises(NotImplementedError): data = DataSet(input_obj, output=output) data.to_output()