def test_dict(): dict_parser = parsers.Dict( pq("#size-variants li::items"), key_parser=parsers.Text(pq("::text")), val_parser=parsers.Bool(pq("::attr(size-stock)"), contains=["true"]), ) expected_result = {"l": True, "xl": False, "xxl": True} assert dict_parser.parse(data_html.sizes) == expected_result dict_parser = parsers.Dict( pq("#size-variants li::items"), key_query=pq("::text"), val_parser=parsers.Bool(pq("::attr(size-stock)"), contains=["true"]), ) assert dict_parser.parse(data_html.sizes) == expected_result dict_parser = parsers.Dict( pq("#size-variants li::items"), key_query=pq("::text"), val_query=pq("::attr(size-stock)"), ) expected_text_result = {"l": "true", "xl": "false", "xxl": "true"} assert dict_parser.parse(data_html.sizes) == expected_text_result dict_parser = parsers.Dict(jp("sizes"), key_parser=parsers.Text(), val_parser=parsers.Bool()) assert dict_parser.parse(data_dict.sizes) == expected_result dict_parser = parsers.Dict(jp("sizes")) assert dict_parser.parse(data_dict.sizes) == expected_result dict_parser = parsers.Dict(jp("sizes"), key_parser=parsers.Text()) assert dict_parser.parse(data_dict.sizes) == expected_result dict_parser = parsers.Dict(jp("sizes"), val_parser=parsers.Bool()) assert dict_parser.parse(data_dict.sizes) == expected_result dict_parser = parsers.Dict(jp("sizes"), key_parser=parsers.Text(), val_parser=parsers.Text()) expected_result = {"l": "True", "xl": "False", "xxl": "True"} assert dict_parser.parse(data_dict.sizes) == expected_result
def generate_choice_parser(**kwargs): return parsers.Choice(choices=[ ( "phone", parsers.Bool( query=jp(bool_query), ccontains=["phone", "CELL"], source="json_data", ), ), ("accessory", ["phone"]), ], **kwargs)
def test_choice_choices(test_data, result): choice_parser = parsers.Choice( choices=[ ("accessory", ["phone case"]), ("watch", "watch"), ("monitor", parsers.Bool(ccontains=["LCD"])), ("notebook", [r"book\b"]), ( "phone", ( "mobile", "phone", ), ), ("monitor", ["oled", "monitor"]), ], default_choice="software", ) assert choice_parser.parse(test_data) == result
def test_bool_contains_query(query, contains_query, test_data, result): bool_parser = parsers.Bool(query, contains_query=contains_query) assert bool_parser.parse(test_data) is result
def test_bool_contains_case(ccontains_keys, test_data, result): bool_parser = parsers.Bool(ccontains=ccontains_keys) assert bool_parser.parse(test_data) is result
def test_bool_various_types(test_data, result): assert parsers.Bool().parse(test_data) is result
def test_bool(query, test_data, result): bool_parser = parsers.Bool(query) assert bool_parser.parse(test_data) is result