How to put "OR" operator in Karate API assertion statement - karate

I would like to use "OR" operator for assertion statement in Karate API.
This is the first time I am trying to OR operator in Karate API:
match response.items[0].type == 'Purchase'
I would like to use an OR operator like:
match response.items[0].type == 'Purchase' or 'Freeplay'
I have not been able to successfully execute with or statement

Sometimes assert is the simplest option:
* def itemType = response.items[0].type
* assert (itemType == 'Purchase') || (itemType == 'Freeplay')
There are other ways, see: https://stackoverflow.com/a/57377284/143475

Related

typeorm: how to properly use IsNotNull/IsNull?

We created a helper function to create wheres easier. It works fine with eq, neq, lt and gt. Now we're trying to add is null/is not null (for a date column, not sure if that matters).
The critical part of the function looks like this:
// This is ran in a loop for every attribute
const query = `${attribute}` ${comparator} :value${index}`;
// if the checked 'value' is NULL then use IsNull(), same for NOT NULL, otherwise simply use value
const params = { [`value${index}`]: value == 'NULL' ? IsNull() : value === 'NOT NULL' ? Not(IsNull()) : value};
// Add this (sub)query to the qb
qb.andWhere(query, params);
Now we get an error saying this:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ’_type = ‘not’, _value = ‘[object Object]‘, _useParameter =
true, `_multipl’ at line 1"
Value is [object Object] - which kind of makes sense if we use IsNotNull(), right?
As far as I understand from this comment, IsNull() and Not(IsNull()) should work like we are trying to.
We use #nestjs/typeorm 7.1.5.
To check for NULL you need
qb.andWhere(`${attribute} IS NULL`)
To check for NOT NULL you need
qb.andWhere(`${attribute} IS NOT NULL`)
(Note: Omit the second argument, parameters, for these cases).
From your code seems you are using string values 'NULL' and 'NOT NULL' as the value arguments and checking these as special cases. Your code will now look like this:
if ((value == 'NULL' && comparator == '=') || (value == 'NOT NULL' && comparator == '<>'))
qb.andWhere(`${attribute} IS NULL`);
if ((value == 'NOT NULL' && comparator == '=') || (value == 'NULL' && comparator == '<>'))
qb.andWhere(`${attribute} IS NOT NULL`);
else
qb.andWhere(`${attribute} ${comparator} :value${index}`, { [`value${index}`]: value});
(In the code above I check for '=' and '<>' which are standard SQL comparison operators. If your SQL dialect uses 'eq' and 'ne' in place of '=' and '<>', which you mention in your question, you will need to change the code above. If so please update your question and add the appropriate tag to say which SQL database you are using).
When you test this, I recommend that you turn on TypeOrm full logging so you can see the actual generated SQL and you be able to quickly solve any problems. See TypeOrm logging.

Karate: parametric json path expressions [duplicate]

I'm attempting to use a variable in the RHS of an JsonPath filter expression in a Karate test, similar to this:
* def name = 'A Name'
* def ids = $response[?(#.name == '#(name)')].id
If I use the literal string 'A Name' in the RHS of the expression it works as expected.
I've tried various ways to get the variable to evaluate:
'<name>', "#(name)", etc.
I suspect it is because I'm mixing JsonPath parsing with Karate parsing perhaps?
Read this first: https://github.com/intuit/karate#rules-for-embedded-expressions
And what you are looking for is this: https://github.com/intuit/karate#jsonpath-filters
* def ids = karate.jsonPath(response, "$.kittens[?(#.name=='" + name + "')].id")

How to use a variable in JsonPath filter

I'm attempting to use a variable in the RHS of an JsonPath filter expression in a Karate test, similar to this:
* def name = 'A Name'
* def ids = $response[?(#.name == '#(name)')].id
If I use the literal string 'A Name' in the RHS of the expression it works as expected.
I've tried various ways to get the variable to evaluate:
'<name>', "#(name)", etc.
I suspect it is because I'm mixing JsonPath parsing with Karate parsing perhaps?
Read this first: https://github.com/intuit/karate#rules-for-embedded-expressions
And what you are looking for is this: https://github.com/intuit/karate#jsonpath-filters
* def ids = karate.jsonPath(response, "$.kittens[?(#.name=='" + name + "')].id")

The best conditional logic for match and match each case

so, I want to use conditional logic in my code, which the condition is when I got response.response_code == '00' so it will run
And match response == res_3[0]
And match each response.data.bills == res_3[1]
And if response.response_code != '00' , it will run
And match response == res_3
And match each response.data.bills == res_3
so, what is the best conditional logic for this case ??
Read the docs please: https://github.com/intuit/karate#conditional-logic
Use a second feature file:
* eval if (response.response_code != '00') karate.call('it-will-run.feature')
Note: you can't use match where JavaScript is expected, refer: https://stackoverflow.com/a/53961806/143475

Certain expressions to validate json don't work

I am trying to print the following:
* print response.requests[?(#.friendlyId == '#(ORID)')]
where ORID is:
* def temp2 = response.teams[?(#.name == '<Name>')].requestedResources[0].resourceRequestFriendlyId
* def ORID = temp2[0]
The expression is giving value as null where as if i use JSON evaluator, I am getting the correct json.
Only JavaScript is supported on the right-hand-side of the print keyword. And JsonPath is not supported.
I will update the documentation to make this clear.