The best conditional logic for match and match each case - karate

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

Related

How to check if a string is IN a text[]?

This is the code I am running in postgres:
SELECT CASE WHEN 'oz' != ANY(('{oz,l,m,g}')::text[]) THEN 'HELOO' ELSE 'WOAH' END;
AFAIK, this should select 'WOAH' but it gives 'HELOO'. Now, with any string on the left side, it always selects 'HELOO' (I have tried 'monkey', and 'mg').
When I run the same code but now without a !:
SELECT CASE WHEN 'oz' = ANY(('{oz,l,m,g}')::text[]) THEN 'HELOO' ELSE 'WOAH' END;
It works as intended i.e. it selects 'HELOO' and when the left side string is 'monkey', it selects 'WOAH'.
How can I check here if the left side string is NOT in the right side text[]?
From the documentation of ANY:
The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ANY is "true" if any true result is obtained.
So when you say 'oz' != ANY(('{oz,l,m,g}')::text[]), Postgres examines in turn 'oz' != 'oz', 'oz' != 'l', 'oz' != 'm', and 'oz' != 'g'. Although the first of those is false, the others are true, so the ANY expression is true.
What you want instead is ALL, where:
The result is “false” if any false result is found.
So when you say 'oz' != ALL(('{oz,l,m,g}')::text[]), Postgres examines the same set of comparisons, but because the first one is false, the whole expression is false.
Effectively, ANY is using an OR between the possibilities, and ALL is using an AND.
This explains why NOT ('oz' = ANY(('{oz,l}')::text[])) also works: it is equivalent to NOT ('oz' = 'oz' OR 'oz' = 'l') and De Morgan's laws tell us that's the same as (NOT 'oz' = 'oz') AND (NOT 'oz' = 'l'); that is ('oz' != 'oz') AND ('oz' != 'l'), which is what we wanted.
You want <> ALL:
SELECT (CASE WHEN 'oz' <> ALL(('{oz,l,m,g}')::text[])
THEN 'HELOO' ELSE 'WOAH'
END);
Note that the important part is ALL; != also works but <> is the traditional SQL operator for not-equals.
I should note that you can also use:
SELECT (CASE WHEN NOT ('oz' = ANY(('{oz,l,m,g}')::text[]))
THEN 'HELOO' ELSE 'WOAH'
END);
demo:db<>fiddle
You can either use the construction: 'oz' = ANY('{...}') IS NOT TRUE
SELECT
CASE
WHEN 'oz' = ANY(('{oz,l,m,g}')::text[]) IS NOT TRUE THEN 'HELOO'
ELSE 'WOAH'
END;
or use a completely different quantifier function: ALL as #GordonLinoff said

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.

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

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

Making SPARQL FILTER conditional

How to make a FILTER() conditional? This is the relevant part of my query:
SELECT *
WHERE {
VALUES (?open) {$U2}
?URI_OPP CSV:id_opportunita ?ID_OPP.
OPTIONAL { ?URI_OPP CSV:data_scadenza ?DATA_S }
FILTER ((NOW() - xsd:datetime(?DATA_S)) > 0)
}
It gets $U2 as a value for ?open. I want to apply the filter if ?open = 1, and not to apply it in all other cases.
While IF() works on the query results, I don't know what to use to switch off parts of the query itself.
Since the above commented never answered, I will:
FILTER(
(?open != 1) ||
((NOW() - xsd:datetime(?DATA_S)) > 0)
)

LINQ to SQL: subfilter in where conditions

This question may have been answered somewhere (and if so I would love to have the link!). But since I don't know what type of linq query I'm trying to do, I haven't been able to find anything to help me.
I already have a very simple query setup to get all records of type DomainSequences.
var query = db.DomainSequences;
There are different types of domain sequences. I want to get all of them. However, for records that have a DomainType attribute equal to 'AT', then I only want to return some of those of 'AT' records (ie. do a where clause on those to filter the 'AT' records but still want to return all non 'AT' records.
More simply put, I want to get all DomainSequence records, but for the records that have a DomainType == "AT", then return those only if they meet certain conditions.
I can think of a way of doing it by doing something like:
query.Where(x => x.DomainType != "AT" || (x.DomainType == "AT" && AT conditions....));
I think this should work but the problem comes when I have to do subfilters on other columns and then it starts to get messier and complicated very quickly.
Ideally, I would like to do something like
query.Where(x => x.DomainType == "AT" || x.DomainType == "KS")
.WhereIf(y => y.DomainType == "AT" then apply filter on those)
.WhereIf(z => z.DomainType == "KS" then apply filter to these);
I'm not sure if there is a way to do this type of subfilter in LINQ or in SQL (though I imagine there is). Any suggestions on how this could be done relatively cleanly?
Thanks!
It's really not all that different than what you might write in SQL. In fact, if you would try the query expression syntax approach, you might find that particular connection easier to make.
var query = from domain in db.DomainSequences
where (domain.DomainType == "AT" && domain.Foo == 42 && ...)
|| (domain.DomainType == "KS" && domain.Foo = 117 && ...)
select domain;
That maps nicely to the SQL you might expect to write
SELECT *
FROM DomainSequences
WHERE
(DomainType = 'AT' AND Foo = 42 AND ...)
OR
(DomainType = 'KS' AND Foo = 117 AND ...)
You could, of course, keep it in the fluent extension method syntax using lambdas, of course, it's just a single .Where(...) invocation, after all.
query.Where(x => x.DomainType == "AT" || x.DomainType == "KS")
.Where(y => y.DomainType != "AT" || other filter)
.Where(z => z.DomainType != "KS" || other filter);