def test_not_null_validator(): func = StringParser(quoted=0).not_null().build() assert func("SOME_VALUE") == "SOME_VALUE" with pytest.raises(NullValueInNotNullFieldException): assert func(None) assert func("") == "" p = StringParser().not_null(allow_white_space=True) func = p.build() assert func("") == "" # Default value assignment check func = StringParser(quoted=0).not_null(default_value="NA").build() assert func(None) == "NA" assert func("") == "NA"
# Import required parser class import logging from parseval.parser import StringParser logging.basicConfig(format='%(levelname)s:%(asctime)s:: %(message)s', level=logging.DEBUG) basic_parser = StringParser() # Create basic parser object basic_parse_func = basic_parser.build() # Build the parser function input_data = 'Any String' # Input Data basic_parsed_output = basic_parse_func(input_data) # Parse data logging.info(('#' * 50) + " STRING PARSING " + ('#' * 50)) logging.info("====> Simple Data Parsing example:") logging.info("Input 1: {}".format(input_data)) logging.info("Output 1: {}".format(basic_parsed_output)) logging.info('\n') # Now let's see some available validators, to get the idea of how to use those # Note, we will not go through all available validators, because all validators work in same fashion # Syntax and description of all validators are available in documentation validation_parser = StringParser()\ .not_null(default_value="NA")\ .value_set(["Apple", "Google", "NA"]) # null check validation and allowed values validation is added validation_parse_func = validation_parser( ) # Yes, you can directly call the object to build the parser valid_input_data = 'Apple' output_for_valid_input_data = validation_parse_func( valid_input_data) # Parse data logging.info("====> Data Validation example:")
def _first_three_char_check(data: str): list_of_allowed_value: list = ['ABC', 'DEF'] if not data: return data if str(data)[:3].upper() in [ word.upper() for word in list_of_allowed_value ]: return data else: raise Exception("Bad prefix in the value.") # Create and build the parser parser = StringParser().add_func(_first_three_char_check) parser_func = parser.build() input_data = "ABC2344" logging.info(('#' * 50) + " ADDING CUSTOM VALIDATOR " + ('#' * 50)) logging.info("====> Valid Input:") logging.info("Input: {}".format(input_data)) logging.info("Output: {}".format(parser_func(input_data))) logging.info('\n') input_data = "PQR12344" logging.info("====> Invalid Input:") try: logging.info("Invalid Input: {}".format(input_data)) parser_func(input_data) except Exception as e: logging.error("Raised Exception: {}".format(str(e)))