def test_a_helloworld(self): with self.assertRaises(FileNotFoundError): result = parse_item(fix_path('hello_world'), str) with self.assertRaises(ObjectNotFoundOnFileSystemError): result = parse_item(fix_path('a_helloworld/hello_world.txt'), str) result = parse_item(fix_path('a_helloworld/hello_world'), str) print(result) assert result == 'hello'
def test_simple_collection_set_list_tuple(self): """ Parsing a collection of dataframes as a list or a tuple :return: """ from pandas import DataFrame dfl = parse_item(fix_path('./simple_collection'), List[DataFrame]) pprint(dfl) # dataframe objects_data are not mutable > can't be hashed and therefore no set can be built # dfs = parse_item('./simple_collection', Set[DataFrame], logger=getLogger()) # pprint(dfs) dft = parse_item( fix_path('./simple_collection'), Tuple[DataFrame, DataFrame, DataFrame, DataFrame, DataFrame]) pprint(dft)
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_collections(root_parser): """ Tests all the supported ways to parse collections_data :return: """ l = parse_item( get_path(), Tuple[Dict[str, int], List[int], Set[int], Tuple[str, int, str]]) print(l)
def test_collections(self): """ Tests all the supported ways to parse collections :return: """ l = parse_item( fix_path('./test_data/collections'), Tuple[Dict[str, int], List[int], Set[int], Tuple[str, int, str]]) print(l)
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_autoclass_pycontract(self): """ Parsing a collection of simple objects_data where the class is defined with `autoclass` and PyContracts :return: """ from autoclass 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 __repr__(self): return str(self.x) + ' ' + self.op + ' ' + str( self.y) + ' =? ' + str(self.expected_result) try: sf_tests = parse_item(fix_path('./simple_objects/test_diff_1'), ExecOpTest) except ParsingException as e: self.assertIn( "<class 'contracts.interface.ContractNotRespected'>" + " Breach for argument 'op' to ExecOpTest:autoprops_generated_setter().\n" + "Value does not pass criteria of <lambda>()() (module:", e.args[0]) self.assertIn( "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'>: '-'", e.args[0])
def test_simple_object_with_contract_autoclass_enforce(self): """ Parsing a collection of simple objects_data where the class is defined with `autoclass` and enforce :return: """ from autoclass import autoprops, autoargs from valid8 import validate_io, gt, minlens from enforce import runtime_validation, config from numbers import Real, Integral config(dict(mode='covariant')) # to accept subclasses in validation # this first example is in the index.md @runtime_validation @autoprops class MySimpleObject: @validate_io(age=gt(0), name=minlens(0)) @autoargs def __init__(self, age: Integral, name: str): pass MySimpleObject(0, 'r') @runtime_validation @autoprops class ExecOpTest(object): @autoargs @validate_io(op=is_in({'+', '*'})) def __init__(self, x: Real, y: Real, op: str, expected_result: Real): pass def __repr__(self): return str(self.x) + ' ' + self.op + ' ' + str( self.y) + ' =? ' + str(self.expected_result) try: sf_tests = parse_item(fix_path('./simple_objects/test_diff_1'), ExecOpTest) except ParsingException as e: self.assertIn("InputValidationError[ValueError]", e.args[0])
def parse_with_default_method(): result = parse_item( os.path.join(THIS_DIR, 'test_data/b64pickle-float-1.0=True'), bool) assert result == True