def test_base_data_default_from_item(): item_model = ItemModel() item_model.item_name = Data(query=jp("title")) item_model.item_brand = Data(query=jp("brandwrong"), default_from_item="name") result = item_model.parse_item(data_dict.title) assert result == {"brand": "Easybook Pro 13", "name": "Easybook Pro 13"}
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_data_from_query_processor(): test_dict = {"info": {"title": "EasyBook"}} db = next( processors.DataFromQueryProcessor(jp("info")).parse_data(test_dict)) assert db["main"]["title"] == "EasyBook"
def test_data_variants_processor_multi_values(): # Lets test with multi_values set to True iter_db = processors.DataVariantsProcessor( query=jp("data.variants"), key_query=key("color"), new_source="color_data", ).parse_data(data_dict.variants_data_multi) db_list = list(iter_db) assert len(db_list) == 2 assert list(db_list[0]["color_data"].values())[0] == "Black" expected_values_result = [ { "color": "Black", "size": "13", "stock": True }, { "color": "Black", "size": "15", "stock": True }, ] assert db_list[0]["color_data_variants"] == expected_values_result
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")
def test_data_variants_processor(): iter_db = processors.DataVariantsProcessor( query=jp("variants"), key_query=key("color"), ).parse_data(data_dict.variants_data) db_list = list(iter_db) assert len(db_list) == 2 assert list(db_list[0]["main"].values())[0] == "Black" assert db_list[0]["main"] == {"color": "Black", "stock": True}
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_base_data_field_query_as_first_parameter(): item_data = Data(jp("info.name")) assert item_data.parse(db) == "EasyBook pro 15"
def test_base_data_query(): item_data = Data(query=jp("info.name")) assert item_data.parse(db) == "EasyBook pro 15"
item_model.item_name = Data(query=jp("title")) item_model.item_brand = Data(from_item="name") result = item_model.parse_item(data_dict.title) assert result == {"brand": "Easybook Pro 13", "name": "Easybook Pro 13"} def test_base_data_field_query_as_first_parameter(): item_data = Data(jp("info.name")) assert item_data.parse(db) == "EasyBook pro 15" @pytest.mark.parametrize( "query, default, test_data, result", [ (jp("info.namewrong"), "Easybook Def 13", db, "Easybook Def 13"), (jp("info.name"), "Easybook Def 13", db, "EasyBook pro 15"), ], ) def test_base_data_default(query, default, test_data, result): item_data = Data(query, default=default) assert item_data.parse(test_data) == result def test_base_data_default_from_item(): item_model = ItemModel() item_model.item_name = Data(query=jp("title")) item_model.item_brand = Data(query=jp("brandwrong"), default_from_item="name") result = item_model.parse_item(data_dict.title) assert result == {"brand": "Easybook Pro 13", "name": "Easybook Pro 13"}
def test_jp_query(query, result): assert jp(query).get(data_dict.item_with_options) == result
def test_jp_query_empty_selector(query, test_data, result): assert jp(query).get(test_data) == result
def test_jp_query_params(query, params, result): assert jp(query).get(data_dict.item_with_options, query_params=params) == result
def test_query_search(): test_data = {"brand": "Groove"} assert parse.query_search(query=jp("brand"), data=test_data) == "Groove"
def test_query_search_data_bag(): assert (parse.query_search(query=jp("data.title"), data=dict_db, source="main") == "EasyData Pro")