Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

dsl - Cannot use "==" and "contains" in same line of scenario using conditional logic in Karate

This is a follow up of a question noted here

Lets says our implemented server v1 and v2 response looks as follows

v1Response = { id: "1", name: "awesome" }
v2Response = { id: "2", name: "awesome", value: "karate" }

Similarly we define the client schema for v1 and v2 like as follows

v1Schema = { id: "#string", name: "#string }
v2Schema = { id: "#string", name: "#string, value: "#string" }

We implement schema validation in our generic scenario as follows. We can easily set the "response" with either v1Response/v2Response AND "schema" with either v1Schema/v2Schema depending on our the environment.

* match response == schema

Above generic script works perfectly fine as long as we are testing v1 server against v1 client / v2 server against v2 client. However we cannot re-use the same scenario when we want to test backward compatibility for example v2 server against v1 client. In this case

* match response (actually v2Response) == schema (actually v1Schema) <--- will fail

So in order to make it work and do backward compatibility testing, I also wanted to use karate "contains" feature like

* match response (actually v2Response) contains schema (actually v1Schema) <--- will pass

However in the quest to keep my scenarios generic it is currently not possible to do either

  1. Use both ==/contains in the same line of script like as follows
    • serverVersion == clientVersion ? (match response == schema) : (match response contains schema)

OR

  1. Using some flag as follows

    • match response SOMEFLAG schema

whereas SOMEFLAG can be set to either "==" or "contains" in karate-config.js depending on environment we are testing.

EDIT

From the above example, all I want is to test following cases that should pass

* match v1Response == v1Schema
* match v2Response == v2Schema 
* match v2Response contains v1Schema

using a generic line as following

* match response == schema <--- can it possibly be solved using above suggested solutions ?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For some reason you feel that hacking the match clause is the only way to solve this problem. Please keep an open mind and, here you go:

* def schemas =
"""
{
  v1: { id: "#string", name: "#string" },
  v2: { id: "#string", name: "#string", value: "#string" }
}
"""

* def env = 'v1'
* def response = { id: "1", name: "awesome" }
* match response == schemas[env]

* def env = 'v2'
* def response = { id: "2", name: "awesome", value: "karate" }
* match response == schemas[env]

* def response = { id: "1", name: "awesome" }
* match response == karate.filterKeys(schemas[env], response)

The last line is as generic as you can get.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...