示例#1
0
def test_order_products_report(db_session, conf_logger):
    """
    Test the order products report query
    Given a list of orders ids, the query should return a list of products used by each order and the total
    amount of products used to produce the given order list.
    """
    # create some products, ingredients and orders
    for idx in range(8):
        util.create_random_ingredient()

    for idx in range(8):
        util.create_random_product(3)

    orders = [util.create_random_order() for idx in range(8)]

    orders_ids = list(order.id for order in orders)

    # repose model:
    # [(product, total)...]
    order_products_report = Order.report_products(orders_ids=orders_ids)

    test_report = {}

    for order in orders:
        for op in order.products:
            if test_report.get(op.product_id, None):
                test_report[op.product_id]['total'] += op.amount
            else:
                test_report[op.product_id] = dict(total=op.amount,
                                                  product=op.product)

    for product, total in order_products_report:
        test_data = test_report[product.id]
        assert test_data.get('total') == total
        assert test_data.get('product') == product
示例#2
0
def test_product_ingredients_report(db_session, conf_logger):
    """
    Test the products ingredients report query
    Given a list of products ids, the query should return a list of ingredients used by each product and the total
    amount of ingredients used to produce the given products list.
    """
    # create some products and ingredients
    for idx in range(8):
        util.create_random_ingredient()

    products = [util.create_random_product(3) for idx in range(8)]
    products_ids = list(product.id for product in products)

    # repose model:
    # [(ingredient, total)...]
    products_ingredients_report = Product.report_ingredients(products_ids)

    test_report = {}

    for product in products:
        for pi in product.ingredients:
            if test_report.get(pi.ingredient_id, None):
                test_report[pi.ingredient_id]['total'] += pi.amount
            else:
                test_report[pi.ingredient_id] = dict(total=pi.amount,
                                                     ingredient=pi.ingredient)

    for ingredient, total in products_ingredients_report:
        test_data = test_report[ingredient.id]
        assert test_data.get('total') == total
        assert test_data.get('ingredient') == ingredient
示例#3
0
def test_orders_products_report_resource__on_get(db_session, client, admin):
    # create some products, ingredients and orders
    for idx in range(2):
        util.create_random_product(amount_of_ingredients=2)

    orders = [
        util.create_random_order(amount_of_products=1) for idx in range(2)
    ]

    orders_ids = list(order.id for order in orders)

    # creates the request
    response = client.simulate_get(
        '/v1/orders/reports/products',
        headers={'Authorization': 'Basic {}'.format(admin.basic_password)},
        body=json.dumps(orders_ids),
    )

    test_report = {}

    for order in orders:
        for op in order.products:
            if test_report.get(op.product_id, None):
                test_report[op.product_id]['total'] += op.amount
            else:
                test_report[op.product_id] = dict(total=op.amount,
                                                  product=op.product)

    assert response.status == falcon.HTTP_200

    data = json.loads(response.content).get('data')

    assert isinstance(data, list)

    for report_data in data:
        test_data = test_report[report_data.get('product_id')]
        assert test_data.get('total') == report_data.get('total')
        assert test_data.get('product').name == report_data.get('product').get(
            'name')
        assert test_data.get('product').value == report_data.get(
            'product').get('value')
        assert len(test_data.get('product').ingredients) == len(
            report_data.get('product').get('ingredients'))
示例#4
0
def test_order_ingredients_report(db_session, conf_logger):
    """
    Test the products ingredients report query
    Given a list of products ids, the query should return a list of ingredients used by each product and the total
    amount of ingredients used to produce the given products list.
    """
    # create some products, ingredients and orders
    for idx in range(2):
        util.create_random_product(amount_of_ingredients=2)

    orders = [
        util.create_random_order(amount_of_products=1) for idx in range(2)
    ]

    orders_ids = list(order.id for order in orders)

    # repose model:
    # [(ingredient, total)...]
    order_ingredients_report = Order.report_ingredients(orders_ids=orders_ids)

    test_report = {}

    for order in orders:
        for op in order.products:
            for pi in op.product.ingredients:
                if test_report.get(pi.ingredient_id, None):
                    test_report[
                        pi.ingredient_id]['total'] += pi.amount * op.amount
                else:
                    test_report[pi.ingredient_id] = dict(
                        total=pi.amount * op.amount, ingredient=pi.ingredient)

    for ingredient, total in order_ingredients_report:
        test_data = test_report[ingredient.id]
        assert test_data.get('total') == total
        assert test_data.get('ingredient') == ingredient
示例#5
0
def test_products_ingredients_report_resource__on_get(db_session, client,
                                                      admin):
    # create some products, ingredients and orders
    for idx in range(8):
        util.create_random_ingredient()

    products = [util.create_random_product(3) for idx in range(8)]
    products_ids = list(product.id for product in products)

    # creates the request
    response = client.simulate_get(
        '/v1/products/reports/ingredients',
        headers={'Authorization': 'Basic {}'.format(admin.basic_password)},
        body=json.dumps(products_ids),
    )

    test_report = {}

    for product in products:
        for pi in product.ingredients:
            if test_report.get(pi.ingredient_id, None):
                test_report[pi.ingredient_id]['total'] += pi.amount
            else:
                test_report[pi.ingredient_id] = dict(total=pi.amount,
                                                     ingredient=pi.ingredient)

    assert response.status == falcon.HTTP_200

    data = json.loads(response.content).get('data')

    assert isinstance(data, list)

    for report_data in data:
        test_data = test_report[report_data.get('ingredient_id')]
        assert test_data.get('total') == report_data.get('total')
        assert test_data.get('ingredient').name == report_data.get(
            'ingredient').get('name')
        assert test_data.get('ingredient').unit == report_data.get(
            'ingredient').get('unit')