def test_generate_products(valid_product_for_package):
    """
    XmlGenerator.generate should return the content of Products.xml
    """
    # We add a second product in the package
    valid_product_for_package1 = deepcopy(valid_product_for_package)
    valid_product_for_package1["EanList"] = {
        "ProductEan": [{
            "Ean": "3606918243774"
        }]
    }
    valid_product_for_package1["SellerProductId"] = "120905784"
    valid_product_for_package1["Size"] = "36/34"

    xml_generator = XmlGenerator({
        "Products": [{
            "Product": valid_product_for_package
        }, {
            "Product": valid_product_for_package1
        }]
    })
    xml_generator.add([{"Product": valid_product_for_package}])
    content = xml_generator.generate()

    with open(SAMPLES_DIR.joinpath("products", "Products.xml")) as f:
        expected_content = f.read()

    assert_xml_files_equal(content, expected_content, "Product")
def test_generate_offers_with_offer_publication_list(valid_offer_for_package):
    """
    XmlGenerator.generate should return the content of Offers.xml
    """
    # We add a second offer in the package
    valid_offer_for_package1 = deepcopy(valid_offer_for_package)
    valid_offer_for_package1["Price"] = 20
    valid_offer_for_package1["SellerProductId"] = "MY_SKU2"
    xml_generator = XmlGenerator({
        "OfferCollection": [{
            "Offer": valid_offer_for_package
        }],
        "Name":
        "A good package"
    })
    xml_generator.add([{
        "Offer": valid_offer_for_package
    }, {
        "Offer": valid_offer_for_package1
    }])
    content = xml_generator.generate()

    with open(
            SAMPLES_DIR.joinpath(
                "offers", "Offers_with_offer_publication_list.xml")) as f:
        expected_content = f.read()

    assert_xml_files_equal(content, expected_content, "Offer")
def test_XmlGenerator_constructor(valid_offer_for_package):
    """
    XmlGenerator should raise a ValueError if `data` isn't a dictionary with the key
    'OfferCollection' for Offers.xml and 'Products' for Products.xml
    """
    XmlGenerator({"OfferCollection": [{"Offer": valid_offer_for_package}]})
    XmlGenerator({"Products": []})
    pytest.raises(ValueError, XmlGenerator,
                  {"InvalidKey": [{
                      "Offer": valid_offer_for_package
                  }]})
def test_add_products_with_invalid_product(valid_product_for_package):
    xml_generator = XmlGenerator(
        {"Products": [{
            "Product": valid_product_for_package
        }]})
    invalid_product = {"invalid_product": True}
    pytest.raises(ValidationError, xml_generator.add,
                  [{
                      "Product": valid_product_for_package
                  }, {
                      "Product": invalid_product
                  }])
def test_add_offers(valid_offer_for_package):
    """
    XmlGenerator.add should append the unique valid offers in the
    attribute `XmlGenerator.data`
    """
    xml_generator = XmlGenerator({
        "OfferCollection": [{
            "Offer": valid_offer_for_package
        }],
        "Name":
        "A good package"
    })
    assert len(xml_generator.data) == 1

    xml_generator.add([{"Offer": valid_offer_for_package}])

    # The should be only 1 valid offer (we added 2 times the same offer)
    assert len(xml_generator.data) == 1

    # We create a new valid offer with a DiscountList
    valid_offer_for_package1 = deepcopy(valid_offer_for_package)
    valid_offer_for_package1["DiscountList"] = {
        "DiscountComponent": [discount_component()]
    }
    xml_generator.add([{"Offer": valid_offer_for_package1}])

    # There should be 2 valid offers
    assert len(xml_generator.data) == 2
def test_add_offers_with_invalid_offer(valid_offer_for_package):
    """
    XmlGenerator.add should raise a TypeError when the offer is invalid
    """
    xml_generator = XmlGenerator(
        {"OfferCollection": [{
            "Offer": valid_offer_for_package
        }]})
    invalid_offer_for_package = {"invalid_offer": True}
    pytest.raises(
        ValidationError,
        xml_generator.add,
        [{
            "Offer": valid_offer_for_package
        }, {
            "Offer": invalid_offer_for_package
        }],
    )
def test_add_products(valid_product_for_package):
    xml_generator = XmlGenerator(
        {"Products": [{
            "Product": valid_product_for_package
        }]})
    assert len(xml_generator.data) == 1

    xml_generator.add([{"Product": valid_product_for_package}])

    # The should be only 1 valid offer (we added 2 times the same offer)
    assert len(xml_generator.data) == 1

    # We create a new valid offer with a different DiscountValue
    valid_product1 = deepcopy(valid_product_for_package)
    valid_product1["SellerProductColorName"] = "Bleu Canard"
    xml_generator.add([{"Product": valid_product1}])

    # There should be 2 valid offers
    assert len(xml_generator.data) == 2