Using arrays in tests in Postman



  • I am sending a request to purchase an item. In the body is transmitted for example

        "products":[
                {
                    "id":"1",
                    "quantity":3
                },
                {
                    "id":"5",
                    "quantity":2
                }
            ]
    

    In the body of the response:

    "order": {
                "id": "",
                "user": {
                    "id": ,
                    "email": ""
                },
                "products": [
                    {
                        "product": {
                            "id": "1",
                            "description": "",
                            "name": "",
                            "price": 100,
                            "iamgeUrl": ""
                        },
                        "quantity": 3
                    },
                    {
                        "product": {
                            "id": "5",
                            "description": "",
                            "name": "",
                            "price": 500,
                            "iamgeUrl": ""
                        },
                        "quantity": 2
                    }
                ],
                "totalQuantity": 5,
                "status": "paid",
                "totalSum": 1170
            }
    

    Objective: using Postman to test that the discount (10% for more than 3 product units) is considered correct. The problem is that the product can be 1, or maybe more and also with quantity. How to write this logic, please tell me?



  • These are various tests. You must write a Postman request for the no-discount case and the discount case and compare the results.



  • I didn't understand your idea. I just need to check that if there are more than three products, a discount is added, right? Why two tests? While pm.test ("The amount is calculated correctly", function () {var jsonData = pm.response.json (); let sum = 0; jsonData.transaction.order.products.forEach (item => {sum + = item.quantity * item.product.price;}); if (jsonData.transaction.order.totalQuantity> = 3) {if (jsonData.transaction.order.totalSum === sum * 0.9) {return true;}} return false;});



  • The idea is simple, I don't know your formats. To find out what is changing, you need to save test results from a purely empirical approach. But if you look at the sky about 100, you won't understand that something is changing



  • so the goods and their number may be different. I am trying to write a generic function. that is, we know the products, quantity and prices, parse it from the answer and know that the discount should be 10% for> 3, I'm trying to write a function that would return true or false. just don't know the syntax



  • Never try to unify something without having a unified thing. Refactoring or optimization is done only if there is working code to compare the results. In your case, write a few examples by hand and then try to find a common denominator. The transition should always be from simple to complex.


Log in to reply
 

Suggested Topics