Пример #1
0
def test_validate_products_fail(lambda_module, order):
    """
    Test validate_products() failing
    """

    url = "mock://PRODUCTS_API_URL/backend/validate"

    with requests_mock.Mocker() as m:
        m.post(url,
               text=json.dumps({
                   "message": "Something is wrong",
                   "products": order["products"]
               }),
               status_code=200)

        valid, error_msg = asyncio.run(lambda_module.validate_products(order))

    print(valid, error_msg)

    assert m.called
    assert m.call_count == 1
    assert m.request_history[0].method == "POST"
    assert m.request_history[0].url == url
    assert valid == False
    assert error_msg == "Something is wrong"
def test_validate_products(lambda_module, product):
    """
    Test validate_products() against a correct product
    """


    # Stub boto3
    dynamodb = stub.Stubber(lambda_module.dynamodb)
    response = {
        "Responses": {
            lambda_module.TABLE_NAME: [{k: TypeSerializer().serialize(v) for k, v in product.items()}]
        }
    }
    expected_params = {
        "RequestItems": {
            lambda_module.TABLE_NAME: {
                "Keys": [{"productId": {"S": product["productId"]}}],
                "ProjectionExpression": stub.ANY,
                "ExpressionAttributeNames": stub.ANY
            }
        }
    }
    dynamodb.add_response("batch_get_item", response, expected_params)
    dynamodb.activate()

    # Run command
    retval = lambda_module.validate_products([product])
    print(retval)
    assert len(retval) == 2
    assert len(retval[0]) == 0
    assert isinstance(retval[1], str)

    dynamodb.deactivate()
Пример #3
0
def test_validate_products(lambda_module, product):
    """
    Test validate_products() against an incorrect product
    """

    product_incorrect = copy.deepcopy(product)
    product_incorrect["price"] += 200

    # Stub boto3
    table = stub.Stubber(lambda_module.table.meta.client)
    response = {
        "Item": {k: TypeSerializer().serialize(v) for k, v in product.items()}
    }
    expected_params = {
        "Key": {"productId": product["productId"]},
        "ProjectionExpression": stub.ANY,
        "ExpressionAttributeNames": stub.ANY,
        "TableName": lambda_module.TABLE_NAME
    }
    table.add_response("get_item", response, expected_params)
    table.activate()

    # Run command
    retval = lambda_module.validate_products([product_incorrect])
    assert len(retval) == 2
    assert len(retval[0]) == 1
    assert isinstance(retval[1], str)

    table.deactivate()
def test_validate_products_multiple(lambda_module, product):
    """
    Test validate_products() with multiple DynamoDB calls
    """

    products = []
    for i in range(0, 105):
        product_temp = copy.deepcopy(product)
        product_temp["productId"] += str(i)
        products.append(product_temp)

    # Stub boto3
    dynamodb = stub.Stubber(lambda_module.dynamodb)
    response = {
        "Responses": {
            lambda_module.TABLE_NAME: [{k: TypeSerializer().serialize(v) for k, v in p.items()} for p in products[0:100]]
        }
    }
    expected_params = {
        "RequestItems": {
            lambda_module.TABLE_NAME: {
                "Keys": [{"productId": {"S": p["productId"]}} for p in products[0:100]],
                "ProjectionExpression": stub.ANY,
                "ExpressionAttributeNames": stub.ANY
            }
        }
    }
    dynamodb.add_response("batch_get_item", response, expected_params)
    response = {
        "Responses": {
            lambda_module.TABLE_NAME: [{k: TypeSerializer().serialize(v) for k, v in p.items()} for p in products[100:]]
        }
    }
    expected_params = {
        "RequestItems": {
            lambda_module.TABLE_NAME: {
                "Keys": [{"productId": {"S": p["productId"]}} for p in products[100:]],
                "ProjectionExpression": stub.ANY,
                "ExpressionAttributeNames": stub.ANY
            }
        }
    }
    dynamodb.add_response("batch_get_item", response, expected_params)
    dynamodb.activate()

    # Run command
    retval = lambda_module.validate_products(products)
    print(retval)
    assert len(retval) == 2
    assert len(retval[0]) == 0
    assert isinstance(retval[1], str)

    dynamodb.deactivate()
def test_validate_products(lambda_module, order):
    """
    Test validate_products()
    """

    url = "mock://PRODUCTS_API_URL/backend/validate"

    with requests_mock.Mocker() as m:
        m.post(url, text=json.dumps({"message": "All products are valid"}))

        valid, error_msg = lambda_module.validate_products(order)

    print(valid, error_msg)

    assert m.called
    assert m.call_count == 1
    assert m.request_history[0].method == "POST"
    assert m.request_history[0].url == url
    assert valid == True
def test_validate_products_paginated(lambda_module, product):
    """
    Test validate_products() with pagination
    """


    # Stub boto3
    dynamodb = stub.Stubber(lambda_module.dynamodb)
    response = {
        "Responses": {
            lambda_module.TABLE_NAME: [{k: TypeSerializer().serialize(v) for k, v in product.items()}]
        },
        "UnprocessedKeys": {
            lambda_module.TABLE_NAME: {
                "Keys": [{"productId": {"S": product["productId"]}}],
                "ProjectionExpression": "#productId, #name, #package, #price",
                "ExpressionAttributeNames": {
                    "#productId": "productId",
                    "#name": "name",
                    "#package": "package",
                    "#price": "price"
                }
            }
        }
    }
    expected_params = {
        "RequestItems": {
            lambda_module.TABLE_NAME: {
                "Keys": [{"productId": {"S": product["productId"]}}],
                "ProjectionExpression": stub.ANY,
                "ExpressionAttributeNames": stub.ANY
            }
        }
    }
    dynamodb.add_response("batch_get_item", response, expected_params)

    # Stub a second answer
    response = {
        "Responses": {
            lambda_module.TABLE_NAME: [{k: TypeSerializer().serialize(v) for k, v in product.items()}]
        }
    }
    expected_params = {
        "RequestItems": {
            lambda_module.TABLE_NAME: {
                "Keys": [{"productId": {"S": product["productId"]}}],
                "ProjectionExpression": stub.ANY,
                "ExpressionAttributeNames": stub.ANY
            }
        }
    }
    dynamodb.add_response("batch_get_item", response, expected_params)
    dynamodb.activate()

    # Run command
    retval = lambda_module.validate_products([product])
    print(retval)
    assert len(retval) == 2
    assert len(retval[0]) == 0
    assert isinstance(retval[1], str)

    dynamodb.deactivate()