def test_simple_objects(self): """ Parsing a collection of simple objects_data :return: """ # First define the function that we want to test # (not useful, but just to show a complete story in the readme...) def exec_op(x: float, y: float, op: str) -> float: if op is '+': return x + y elif op is '-': return x - y else: raise ValueError('Unsupported operation : \'' + op + '\'') # Then define the simple class representing your test case class ExecOpTest(object): def __init__(self, x: float, y: float, op: str, expected_result: float): self.x = x self.y = y self.op = op self.expected_result = expected_result def __str__(self): return self.__repr__() def __repr__(self): return str(self.x) + ' ' + self.op + ' ' + str( self.y) + ' =? ' + str(self.expected_result) # Create the parser and parse a single file # e = parse_item('./test_data/objects_data/test_diff_1', ExecOpTest) # pprint(e) # parse all of them as dicts sf_tests_dct = parse_collection(fix_path('./simple_objects'), Dict) # assert that they are sorted assert list(sf_tests_dct.keys()) == list(sorted(sf_tests_dct.keys())) # parse all of them as objects_data sf_tests = parse_collection(fix_path('./simple_objects'), ExecOpTest) pprint(sf_tests) # RootParser().print_capabilities_for_type(typ=ExecOpTest)
def test_simple_object_with_contract_attrs(self): """ Parsing a collection of simple objects_data where the class is defined with `attrs` :return: """ import attr from attr.validators import instance_of from parsyfiles.plugins_optional.support_for_attrs import chain # custom contract used in the class def validate_io_op(instance, attribute, value): allowed = {'+', '*'} if value not in allowed: raise ValueError('\'op\' has to be a string, in ' + str(allowed) + '!') @attr.s class ExecOpTest(object): x = attr.ib(convert=float, validator=instance_of(float)) y = attr.ib(convert=float, validator=instance_of(float)) op = attr.ib(convert=str, validator=chain(instance_of(str), validate_io_op)) expected_result = attr.ib(convert=float, validator=instance_of(float)) try: sf_tests = parse_collection(fix_path('./simple_objects'), ExecOpTest) except ParsingException as e: self.assertIn( '<class \'ValueError\'> \'op\' has to be a string, in ', e.args[0])
def test_objects_support(self): """ Tests all the supported ways to parse a simple object :return: """ # Then define the simple class representing your test case class ExecOpTest(object): def __init__(self, x: float, y: float, op: str, expected_result: float): self.x = x self.y = y self.op = op self.expected_result = expected_result def __str__(self): return self.__repr__() def __repr__(self): return str(self.x) + ' ' + self.op + ' ' + str( self.y) + ' =? ' + str(self.expected_result) # create the parser and parse a single file e = parse_item(fix_path('./test_data/objects/test_diff_1'), ExecOpTest) pprint(e) # parse all of them e = parse_collection(fix_path('./test_data/objects'), ExecOpTest) pprint(e)
def test_simple_collection_lazy(self): """ Parsing a collection of dataframes in lazy mode :return: """ from pandas import DataFrame dfs = parse_collection(fix_path('./test_data/demo/simple_collection'), DataFrame, lazy_mfcollection_parsing=True) # check len self.assertEquals(len(dfs), 5) print('dfs length : ' + str(len(dfs))) # check keys self.assertEquals(dfs.keys(), {'a', 'b', 'c', 'd', 'e'}) print('dfs keys : ' + str(dfs.keys())) # check contains self.assertTrue('b' in dfs) print('Is b in dfs : ' + str('b' in dfs)) # check iter self.assertEquals({key for key in dfs}, {'a', 'b', 'c', 'd', 'e'}) # check get self.assertIsNotNone(dfs.get('b')) pprint(dfs.get('b')) # check values for value in dfs.values(): print(value) # check items for key, value in dfs.items(): print(value) # finally print pprint(dfs)
def test_simple_collection_dataframe_all(self): """ All the possible ways to parse a dataframe :return: """ from pandas import DataFrame # TODO recreate a t_pickle.pyc example with latest version of pandas (current file is obsolete) dfs = parse_collection( fix_path('./simple_collection_dataframe_inference'), DataFrame) pprint(dfs)
def test_simple_collection_dataframe_all(self): """ All the possible ways to parse a dataframe :return: """ from pandas import DataFrame dfs = parse_collection( fix_path('./test_data/demo/simple_collection_dataframe_inference'), DataFrame) pprint(dfs)
def test_multifile_objects(self): """ Parsing a list of multifile objects_data :return: """ from pandas import Series, DataFrame class AlgoConf(object): def __init__(self, foo_param: str, bar_param: int): self.foo_param = foo_param self.bar_param = bar_param class AlgoResults(object): def __init__(self, score: float, perf: float): self.score = score self.perf = perf def exec_op_series(x: Series, y: AlgoConf) -> AlgoResults: pass class ExecOpSeriesTest(object): def __init__(self, x: Series, y: AlgoConf, expected_results: AlgoResults): self.x = x self.y = y self.expected_results = expected_results # parse all of them mf_tests = parse_collection(fix_path('./complex_objects'), ExecOpSeriesTest) pprint(mf_tests) RootParser().print_capabilities_for_type(typ=ExecOpSeriesTest) from parsyfiles import FlatFileMappingConfiguration dfs = parse_collection( fix_path('./complex_objects_flat'), DataFrame, file_mapping_conf=FlatFileMappingConfiguration()) pprint(dfs)
def test_simple_collection(self): """ Parsing a collection of dataframes as a dictionary :return: """ from pandas import DataFrame dfs = parse_collection(fix_path('./simple_collection'), DataFrame) pprint(dfs) df = parse_item(fix_path('./simple_collection/c'), DataFrame) pprint(df) RootParser().print_capabilities_for_type(typ=DataFrame)
def test_simple_collection_nologs(self): """ parsing a collection of dataframe with a different logger :return: """ from pandas import DataFrame dfs = parse_collection(fix_path('./simple_collection'), DataFrame, logger=getLogger()) pprint(dfs) df = parse_item(fix_path('./simple_collection/c'), DataFrame, logger=getLogger()) pprint(df)
def test_simple_objects_support(): """ Tests all the supported ways to parse a simple object :return: """ # create the parser and parse a single file e = parse_item(get_path('test1', 'test_diff_1'), ExecOpTest) pprint(e) # parse all of them e = parse_collection(get_path('test1'), ExecOpTest) pprint(e) for case_name, case in e.items(): assert exec_op(case.x, case.y, case.op) == case.expected_result
def test_simple_object_with_contract_classtools(self): """ Parsing a collection of simple objects where the class is defined with `autocode-classtools` :return: """ from classtools_autocode import autoprops, autoargs from contracts import contract, new_contract # custom contract used in the class new_contract('allowed_op', lambda x: x in {'+', '*'}) @autoprops class ExecOpTest(object): @autoargs @contract(x='int|float', y='int|float', op='str,allowed_op', expected_result='int|float') def __init__(self, x: float, y: float, op: str, expected_result: float): pass def __str__(self): return self.__repr__() def __repr__(self): return str(self.x) + ' ' + self.op + ' ' + str( self.y) + ' =? ' + str(self.expected_result) try: sf_tests = parse_collection( fix_path('./test_data/demo/simple_objects'), ExecOpTest) except ParsingException as e: self.assertIn( '<class \'contracts.interface.ContractNotRespected\'> ' 'Breach for argument \'op\' to ExecOpTest:generated_setter_fun().\n' 'Value does not pass criteria of <lambda>()() (module: test_parsyfiles).\n' 'checking: callable() for value: Instance of <class \'str\'>: \'-\' \n' 'checking: allowed_op for value: Instance of <class \'str\'>: \'-\' \n' 'checking: str,allowed_op for value: Instance of <class \'str\'>: \'-\' \n' 'Variables bound in inner context:\n! cannot write context\n', e.args[0])
import os from pprint import pprint import pytest from parsyfiles.tests.parsing_capabilities_by_type.test_parse_custom_object import ExecOpTest, exec_op from parsyfiles import parse_collection, get_capabilities_for_type THIS_DIR = os.path.dirname(os.path.abspath(__file__)) # check that we can parse this type pprint(get_capabilities_for_type(ExecOpTest)) # let's parse the collection of tests cases = parse_collection(os.path.join(THIS_DIR, os.pardir, 'parsing_capabilities_by_type', 'objects_data', 'test1'), ExecOpTest, lazy_mfcollection_parsing=True) @pytest.mark.parametrize("case_name", cases.keys()) def test_simple_objects(case_name: str): """ pytest integration tests: reads simple test case data from a folder and executes the corresponding tests """ case = cases[ case_name] # lazy-load case data (so that parsing errors don't fail the whole collection) print(case) assert exec_op(case.x, case.y, case.op) == case.expected_result