class ProductModel(ItemModel): ED_DATA_VARIANTS_NAME = "variants_test_override" item_language = "en" item_tags = ["phones", "ecommerce"] _item_brand = parsers.Text(jp("brand"), source="json_data") item_designer = parsers.Text(from_item="brand")
class ProductModel(models.ItemModel): _item_category = parsers.Text(pq("#accessory .name::text")) _item_name = parsers.Text(pq("#accessory .type::text")) item_type = parsers.Choice( lookups=["name", "category"], choices=[ ("phone", ["mobile"]), ("accessory", ["phone case"]), ], )
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 test_choice_lookup_queries_choice_bool_parser_source( pq_query, bool_query, 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) data_bag = DataBag(main=data_html.categories, json_data=data_dict.name) # Test lookup queries choice_parser = generate_choice_parser(lookups=[pq(pq_query)]) assert choice_parser.parse(data_bag) == result # Test lookup parsers choice_parser = generate_choice_parser( lookups=[parsers.Text(pq(pq_query))]) assert choice_parser.parse(data_bag) == result
def test_data_variants_processor_html(): # Lets test with HTML data and pq selector iter_db = processors.DataVariantsProcessor( query=pq("#color-variants .color::items"), key_parser=parsers.Text(pq("::text"), uppercase=True), new_source="color_data", ).parse_data(data_html.prices_and_variants) db_list = list(iter_db) assert len(db_list) == 2 assert db_list[0]["color_data"].text() == "Black" assert db_list[0]["color_data_key"] == "BLACK" assert db_list[0]["color_data_variants_len"] == 2