Может ли кто-нибудь помочь мне создать тест в Postman, чтобы проверить, имеет ли ключ ожидаемое значение внутри вложенной группы? - PullRequest
0 голосов
/ 10 июля 2020

Может ли кто-нибудь указать мне правильное направление для написания теста в Postman, чтобы проверить наличие определенного ключа или значения из следующего ответа. В ответе много вложенных объектов. В частности, я хочу проверить следующее:

  1. Я хочу подтвердить, что в output.treatments есть ключ «type» со значением «Diet»
  2. , и я также хочу чтобы убедиться, что в output.treatments.details есть ключ «name» со значением «SULFAMETHOXAZOLE / TRIMETHOPRIM»

Спасибо!

Ответ API выглядит следующим образом:

{
  "additionalInformation": {
    "knownDrugAllergies": [],
    "knownDrugConflicts": [
      {
        "conflict": "OXYBUTYNIN CHLORIDE",
        "potentialDrugs": [
          "item1",
          "item2"
        ]
      }
    ],
    "knownPrecautionConflicts": [
      {
        "conflict": "OXYBUTYNIN CHLORIDE",
        "potentialDrugs": [
          "Drowsy",
          "Gastroesophageal Reflux",
          "Myasthenia Gravis",
          "Parkinsonism",
          "Ulcerative Colitis"
        ]
      }
    ]
  },
  "output": [
    {
      "icdCode": "N39.3",
      "icdDesc": "Stress incontinence (female) (male)",
      "isIllness": true,
      "treatments": [
        {
          "details": [],
          "type": "Physical Exam"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group Placeholder",
              "longName": "Normal Diet",
              "name": "Normal Diet",
              "nameDetails": "The patient has no dietary restrictions",
              "priority": 16,
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ],
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "Diet"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group Placeholder",
              "longName": "No Restrictions",
              "name": "No Restrictions",
              "nameDetails": "The patient has no activity restrictions",
              "priority": 16,
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ],
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "Activity"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group Placeholder",
              "longName": "None Needed ",
              "name": "None Needed",
              "nameDetails": "No physical therapy is needed.",
              "priority": 20,
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ],
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "Physical Therapy"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group Placeholder",
              "longName": "Kegel Exercises",
              "name": "Kegel Exercises",
              "nameDetails": "INCOMPLETE",
              "priority": 20,
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ],
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "Counseling"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group Placeholder",
              "longName": "Patient Can Resume Work/School Immediately",
              "name": "Immediately",
              "nameDetails": "Patient Can Resume Work/School Immediately",
              "priority": 8,
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ],
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "Return to Work/School Status"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group Placeholder",
              "longName": "Wound Care Not Needed",
              "name": "Not Needed",
              "nameDetails": "Wound Care Not Needed",
              "priority": 20,
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ],
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "Wound Care"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group Placeholder",
              "longName": "Urine Analysis",
              "name": "Urine Analysis",
              "nameDetails": "Evaluate the urine for electrolytes and pH.",
              "priority": 20,
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "Labs"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group Placeholder",
              "longName": "Specialist Consultation",
              "name": "Specialist Consult",
              "nameDetails": "The patient should consult with a specialist in the appropriate field.",
              "priority": 29,
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "Discharge Disposition"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Group 2",
              "longName": "placeholder",
              "name": "DULOXETINE HCL",
              "nameDetails": "Name Details Placeholder",
              "priority": 20,
              "protocol": [
                "example"
              ],
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            },
            {
              "directions": [],
              "dosage": "",
              "groupName": "Standard",
              "longName": "placeholder",
              "name": "SULFAMETHOXAZOLE/TRIMETHOPRIM",
              "nameDetails": "Name Details Placeholder",
              "priority": 20,
              "protocol": [
                "example"
              ],
              "rank": 3,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            },
            {
              "directions": [],
              "dosage": "",
              "groupName": "Standard",
              "longName": "placeholder",
              "name": "TOLTERODINE TARTRATE",
              "nameDetails": "Name Details Placeholder",
              "priority": 20,
              "protocol": [
                "example"
              ],
              "rank": 1,
              "reasons": [],
              "sources": [
                [
                  "no sources",
                  "no URL"
                ]
              ]
            }
          ],
          "type": "Prescription Drugs"
        },
        {
          "details": [
            {
              "directions": [],
              "dosage": "",
              "groupName": "Standard",
              "longName": "placeholder",
              "name": "OXYBUTYNIN",
              "nameDetails": "Name Details Placeholder",
              "priority": 20,
              "protocol": [
                "example"
              ],
              "rank": 2,
              "reasons": [],
              "sources": [
                [
                  "TEST",
                  "https://google.com"
                ]
              ]
            }
          ],
          "type": "OTC Drugs"
        }
      ]
    }
  ],
  "status": "Complete",
  "version": 3
} 

1 Ответ

0 голосов
/ 10 июля 2020

Нашел решение:

var jsonData = pm.response.json();

pm.test("OXYBUTYNIN present", function () {
    pm.expect(_isContains(jsonData.output, "name" ,"OXYBUTYNIN")).to.be.true;
});

pm.test("TOLTERODINE TARTRATE present", function () {
    pm.expect(_isContains(jsonData.output, "name" ,"TOLTERODINE TARTRATE")).to.be.true;
});

pm.test("SULFAMETHOXAZOLE/TRIMETHOPRIM present", function () {
    pm.expect(_isContains(jsonData.output, "name" ,"SULFAMETHOXAZOLE/TRIMETHOPRIM")).to.be.true;
});

pm.test("DULOXETINE HCL present", function () {
    pm.expect(_isContains(jsonData.output, "name" ,"DULOXETINE HCL")).to.be.true;
});

function _isContains(json, keyname, value) {
return Object.keys(json).some(key => {
        return typeof json[key] === 'object' ? 
        _isContains(json[key], keyname, value) : key === keyname && json[key] === value;
    });
}
...