コード例 #1
0
def test_orders_detail_resource__on_delete(db_session, client, admin):
    # creates the object to be updated
    amount_of_users = 1
    amount_of_products = 2

    original_mock_data = util.get_random_order_data_model(
        amount_of_products=amount_of_products,
        amount_of_clients=amount_of_users)

    order = Order(**original_mock_data)
    order.save()

    # creates the request
    response = client.simulate_delete(
        '/v1/orders/{order_id}'.format(order_id=order.id),
        headers={'Authorization': 'Basic {}'.format(admin.basic_password)},
    )

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

    assert response.status == falcon.HTTP_200
    assert Order.get_by_id(data.get('id')) is None
    assert len(
        db_session.query(OrderProduct).filter(
            Order.id == data.get('id')).all()) == 0
    assert len(Ingredient.get_all()) > 0
    assert order not in db_session
コード例 #2
0
def test_orders_detail_resource__on_put(db_session, client, admin):
    # creates the object to be updated
    amount_of_users = 1
    amount_of_products = 2

    original_mock_data = util.get_random_order_data_model(
        amount_of_products=amount_of_products,
        amount_of_clients=amount_of_users)

    products = Product.get_all()

    # saves the object to be updated instance
    order = Order(**original_mock_data)
    order.save()

    # the data to be updated
    mock_data = {
        "products": [
            {
                "amount": 2,
                "product_id": products[0].id
            },
        ]
    }

    # creates the request
    response = client.simulate_put(
        '/v1/orders/{order_id}'.format(order_id=order.id),
        headers={'Authorization': 'Basic {}'.format(admin.basic_password)},
        body=json.dumps(mock_data))

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

    # assert that only the name changed
    assert response.status == falcon.HTTP_200
    assert len(data.get('products')) == len(mock_data.get('products'))
    assert data.get('products')[0].get('amount') == 2

    # new mock to be updated
    mock_data = {
        "products": [
            {
                "amount": 10,
                "product_id": products[0].id,
            },
        ]
    }

    # creates a new request to update only the client unit
    response = client.simulate_put(
        '/v1/orders/{order_id}'.format(order_id=order.id),
        headers={'Authorization': 'Basic {}'.format(admin.basic_password)},
        body=json.dumps(mock_data))

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

    assert response.status == falcon.HTTP_200
    assert len(data.get('products')) == len(mock_data.get('products'))
    assert data.get('products')[0].get('amount') == 10
コード例 #3
0
ファイル: test_models.py プロジェクト: fcgomes92/ssms
def test_order_model_delete(db_session, conf_logger):
    orders = Order.get_all()
    order = util.get_random_order()

    order.delete()

    assert order in orders
    assert order not in Order.get_all()
    assert Order.get_by_id(order.id) is None
    assert Order.get_by_code(order.code) is None
コード例 #4
0
ファイル: test_models.py プロジェクト: fcgomes92/ssms
def test_order_model(db_session, conf_logger):
    amount_of_products = 2
    mock_order_data = [util.get_random_order_data_model(2) for idx in range(8)]

    for order_data in mock_order_data:
        o = Order(**order_data)
        o.save()

    all_orders = Order.get_all()

    for idx, order in enumerate(all_orders):
        assert len(order.products) == amount_of_products
        assert mock_order_data[idx].get('client_id') == order.client_id
        assert Counter(mock_order_data[idx].get('products')) == Counter(
            order.products)
コード例 #5
0
ファイル: test_models.py プロジェクト: fcgomes92/ssms
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
コード例 #6
0
def test_orders_list_resource__on_post(db_session, client, admin):
    amount_of_users = 1
    amount_of_products = 2

    mock_data = util.get_random_order_data(
        amount_of_clients=amount_of_users,
        amount_of_products=amount_of_products)

    user = User.get_by_id(mock_data.get('client_id'))

    response = client.simulate_post(
        '/v1/orders/',
        headers={'Authorization': 'Basic {}'.format(admin.basic_password)},
        body=json.dumps(mock_data))

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

    client_data = data.get('client')

    order = Order.get_by_id(data.get('id'))

    assert response.status == falcon.HTTP_OK
    assert data.get('client_id') == order.client_id
    assert len(data.get('products')) == len(list(order.products))
    assert data.get('code') == order.code
    assert client_data.get('id') == user.id
    assert client_data.get('password') is None
    assert client_data.get('seed') is None
コード例 #7
0
ファイル: hooks.py プロジェクト: fcgomes92/ssms
def get_order(req, resp, resource, params):
    order_id = params.get('order_id', None)
    if not order_id:
        raise falcon.HTTPError(falcon.HTTP_400)

    order = Order.get_by_id(order_id)

    if not order:
        raise falcon.HTTPError(falcon.HTTP_404)

    params['order'] = order
コード例 #8
0
def test_orders_detail_resource__on_get(db_session, client, admin):
    amount_of_users = 1
    amount_of_products = 2

    mock_data = util.get_random_order_data_model(
        amount_of_products=amount_of_products,
        amount_of_clients=amount_of_users)

    order = Order(**mock_data)
    order.save()

    response = client.simulate_get(
        '/v1/orders/{order_id}'.format(order_id=order.id),
        headers={'Authorization': 'Basic {}'.format(admin.basic_password)},
    )

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

    assert response.status == falcon.HTTP_200
    assert data.get('client_id') == mock_data.get('client_id')
    assert len(data.get('products')) == amount_of_products
    assert data.get('client_id') == order.client_id
    assert len(data.get('products')) == len(list(order.products))
    assert data.get('code') == order.code
コード例 #9
0
def test_orders_list_resource__on_get(db_session, client, admin):
    response = client.simulate_get(
        '/v1/orders/',
        headers={'Authorization': 'Basic {}'.format(admin.basic_password)})

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

    assert response.status == falcon.HTTP_OK
    assert isinstance(data, list)

    for order_data in data:
        order = Order.get_by_id(order_data.get('id'))
        assert order_data.get('client_id') == order.client_id
        assert len(order_data.get('products')) == len(list(order.products))
        assert order_data.get('code') == order.code
コード例 #10
0
    def on_get(self, req, resp, *args, **kwargs):
        schema = OrderSchema()
        orders = Order.get_all()

        data, errors = schema.dump(orders, many=True)

        if errors:
            logger.error(errors)
            raise falcon.HTTPInternalServerError()

        schema.context['remove_fields'] = ['seed', 'password']

        data = format_response(data)

        resp.status = falcon.HTTP_200
        resp.body = json.dumps(data, ensure_ascii=False)
コード例 #11
0
    def on_get(self, req, resp, *args, **kwargs):
        oi_schema = OrderIngredientsReportSchema()

        data = json.loads(req.stream.read(req.content_length or 0))

        try:
            report_data = [
                dict(
                    ingredient_id=ingredient.id,
                    ingredient=ingredient,
                    total=float(total),
                ) for ingredient, total in Order.report_ingredients(data)
            ]
            report, errors = oi_schema.dump(report_data, many=True)
        except Exception as e:
            logger.error(e)
            resp.status = falcon.HTTP_500
        else:
            resp.status = falcon.HTTP_200
            resp.body = json.dumps(format_response(report), ensure_ascii=False)
コード例 #12
0
ファイル: test_models.py プロジェクト: fcgomes92/ssms
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
コード例 #13
0
ファイル: util.py プロジェクト: fcgomes92/ssms
def get_random_order():
    orders = Order.get_all()
    return random.choice(orders)
コード例 #14
0
ファイル: util.py プロジェクト: fcgomes92/ssms
def create_random_order(amount_of_products=4, amount_of_clients=1):
    data = get_random_order_data_model(amount_of_products, amount_of_clients)
    order = Order(**data)
    order.save()

    return order
コード例 #15
0
 def make_order(self, data):
     from ssms.models import Order
     return Order(**data)