How can I iterate through normal array in karate? - karate

Output of one of my tasks is coming as ['a','b','c']
Now I have to use these values in a DB query
* def queryToGetHotelIdForAvailableCsId = "select id from hotels_hotel where cs_id in ('a','b','c');"
How can I do this in karate ?

It may be better to use a Java helper, but here you go:
* def json = ['a', 'b', 'c']
* def fun =
"""
function(array) {
var temp = '';
for (var i = 0; i < array.length; i++) {
if (i > 0) temp = temp + ',';
temp = temp + "'" + array[i] + "'"
}
return '(' + temp + ')';
}
"""
* def result = fun(json)
* match result == "('a','b','c')"

Related

Karate : Select 'n' number of random values from an Array

To get 'n' number of random values from the below array whenever i execute the test script. How i can i achieve this into Karate in a feature file.
[
"2972029540",
"2972033041",
"2972030914",
"2972028446",
"2972030851",
"2972026534",
"2972029484"
]
Here you go:
* def random = function(max){ return Math.floor(Math.random() * max) + 1 }
* def data = [ "2972029540", "2972033041", "2972030914", "2972028446", "2972030851", "2972026534", "2972029484" ]
* def count = random(data.length)
* print 'random count is', count
* def temp = data.slice(0, count)
* print temp
Read this for more info: https://stackoverflow.com/a/53975071/143475

Why doesn't this return the tuple and instead returns nonetype?

Why doesn't this return the tuple and instead returns nonetype? I've tried removing everything I could think of to get it to return the tuple. I either have mistyped something which i have spent an hour or so checking, or i am missing something bigger..
Thanks!!
import time
def BONGO_BONGO(item, num=1, wait_time=4):
time.sleep(.99)
list_of_positions = []
if item in list_of_positions:
result = (1, 333)
return result
elif num >= wait_time:
print(str(wait_time) + " seconds have passed.. ")
print("test")
result = (2, 9)
print(result)
return result
else:
print(str(item) + " execution checked: " + str(num))
num += 1
BONGO_BONGO(item=item, num=num)
y = BONGO_BONGO("ITEM444")
if __name__ == "__main__":
print(y)
Okay the answer is that the nested bongo_bongo function under the "else" is the function that in the end will return the desired tuple from the "elif". So you need to return that last bongo_bongo function under the "else".
import time
def BONGO_BONGO(item, num=1, wait_time=4):
time.sleep(.99)
list_of_positions = []
if item in list_of_positions:
result = (1, 333)
return result
elif num >= wait_time:
print(str(wait_time) + " seconds have passed.. ")
print("test")
result = (2, 9)
print(result)
return result
else:
print(str(item) + " execution checked: " + str(num))
num += 1
result = BONGO_BONGO(item=item, num=num)
return result
y = BONGO_BONGO("ITEM444")
if __name__ == "__main__":
print("Y=", y)

as for "Rules for Embedded Expressions", the string concatenation does not work

I am fetching the data from database with SQL, I need pass a variable to the where clause, however, I find that the string concatenation doesn't work, even the official example
* def batchnum = "112344552"
* def getBatchIDSQL = '#("select id from sr_sendreceive where batchnum = " + batchnum)'
* print getBatchIDSQL
* def sendReceiveBatchid = db.readValue('#(getBatchIDSQL)')
Then, I tried the official example:
# wrong !
* def foo = 'hello #(name)'
# right !
* def foo1 = '#("hello " + name)'
* print foo1
* def name = 'test name'
* def temp = 'hello ' + name
* def foo2 = '#(temp)'
* print foo2
The result is :
#("select id from sr_sendreceive where batchnum =" + batchnum)
#("hello " + name)
#(temp)
Sorry, the docs are wrong. This works for non-JSON only for match. Like this:
* def batchnum = "112344552"
* def actual = 'select id from sr_sendreceive where batchnum = 112344552'
* match actual == '#("select id from sr_sendreceive where batchnum = " + batchnum)'
Inside JSON it will work:
* def foo = { bar: '#("select id from sr_sendreceive where batchnum = " + batchnum)' }
* print foo
Thanks for pointing this out, I will update the docs.

Find missing number in sorted array

Whats wrong with this code ? Not able able to search missing number in a consecutive array using binary search.
a = [1,2,3,4,5,7,8]
lent = len(a)
beg =0
end = lent-1
while beg < end:
mid = (beg + end) / 2
if (a[mid]-a[beg])==(mid - beg):
beg = mid + 1
else:
end = mid -1
if(beg == end):
mid = (beg + end) / 2
print "missing"
print a[0]+ beg
Update #1: Yes, there was another one mistake. You're right. Here's updated version
Try this variant:
a = [1,2,3,4,5,7,8]
lent = len(a)
beg =0
end = lent-1
while beg < end:
mid = (beg + end) / 2
if (a[mid]-a[beg])==(mid - beg):
beg = mid
else:
end = mid
if abs(beg-end) <= 1:
print "missing: %s" % (a[0] + max(beg, mid),)
Result:
missing: 6
Also, try use functions, so you could easily test and debug your code on different lists:
def find_missing(a):
lent = len(a)
beg =0
end = lent-1
while beg < end:
mid = (beg + end) / 2
if (a[mid]-a[beg])==(mid - beg):
beg = mid
else:
end = mid
if abs(beg-end) <= 1:
return a[0] + max(beg, mid)
a = [1,2,3,4,5,7,8]
print find_missing(a)
a = [1,3,4,5,6]
print find_missing(a)
a = [1,2,3,4,5,7,8,9,10]
print find_missing(a)
Result:
6
2
6
//Implementation of the algorithm using java.
static int missingNumber(int [] nums) {
int i=0;
while(i < nums.length) {
int correct = nums[i];
if (nums[i] < nums.length && nums[i] != nums[correct]) {
swap(nums, i, correct);
} else {
i++;
}
}
for( int index=0; index<nums.length; index++){
if(index != nums[index]) {
return index;
}
}
return nums.length;
}
static void swap(int[] nums, int first, int second) {
int temp = nums[first];
nums[first] = nums[second];
nums[second] = temp;
}
int findMiss(int arr[], int low, int high)
{
if(low>high)
return -1;
if(arr[low]-1 != low)
return arr[low]-1;
int mid = (low + high) / 2;
if(arr[mid]-1 != mid)
return findMiss(arr,low,mid);
else
return findMiss(arr,mid+1,high);
}

google-bigquery UDF for jaro_winkle_distance

I am the following UDF code that calculate jaro_winkle_distance
It seems to work when test it with json test data but when I try to call it in the google-bigquery UI, it keeps consistently gives me a score of zero.
even with self join e.g.
input:
[
{a: "Liu",b:"Lau"},
{a: "John",b:"Jone"}]
Output:
[
{
"scr": 80
},
{
"scr": 87
}
]
SQL:
CREATE TEMP FUNCTION
jwd(a STRING,
b STRING)
RETURNS INT64
LANGUAGE js AS """
// Assumes 'doInterestingStuff' is defined in one of the library files.
//return doInterestingStuff(a, b);
return jaro_winkler_distance(a,b);
""" OPTIONS ( library="gs://kayama808/javascript/jaro_winkler_google_UDF.js" );
SELECT
x.name name1,
jwd(x.name,
x.name) scr
FROM
babynames.usa_1910_2013_copy x
WHERE
x.gender = 'F' and x.number >= 1000 and x.state = 'CA'
ORDER BY
scr DESC;
http://storage.googleapis.com/bigquery-udf-test-tool/testtool.html
https://storage.cloud.google.com/kayama808/javascript/jaro_winkler_google_UDF.js?_ga=1.184402278.1320598031.1475534357
Try below. It works as expected with result as
name1 name2 scr
Liu Liu 100
John Jone 87
Liu Lau 80
Hopefully, you will be able to put it back to your external lib file :o)
CREATE TEMP FUNCTION jwd(a STRING, b STRING)
RETURNS INT64
LANGUAGE js AS """
/* JS implementation of the strcmp95 C function written by
Bill Winkler, George McLaughlin, Matt Jaro and Maureen Lynch,
released in 1994 (http://web.archive.org/web/20100227020019/http://www.census.gov/geo/msb/stand/strcmp.c).
a and b should be strings. Always performs case-insensitive comparisons
and always adjusts for long strings. */
var jaro_winkler_adjustments = {
'A': 'E',
'A': 'I',
'A': 'O',
'A': 'U',
'B': 'V',
'E': 'I',
'E': 'O',
'E': 'U',
'I': 'O',
'I': 'U',
'O': 'U',
'I': 'Y',
'E': 'Y',
'C': 'G',
'E': 'F',
'W': 'U',
'W': 'V',
'X': 'K',
'S': 'Z',
'X': 'S',
'Q': 'C',
'U': 'V',
'M': 'N',
'L': 'I',
'Q': 'O',
'P': 'R',
'I': 'J',
'2': 'Z',
'5': 'S',
'8': 'B',
'1': 'I',
'1': 'L',
'0': 'O',
'0': 'Q',
'C': 'K',
'G': 'J',
'E': ' ',
'Y': ' ',
'S': ' '
};
if (!a || !b) { return 0.0; }
a = a.trim().toUpperCase();
b = b.trim().toUpperCase();
var a_len = a.length;
var b_len = b.length;
var a_flag = []; var b_flag = [];
var search_range = Math.floor(Math.max(a_len, b_len) / 2) - 1;
var minv = Math.min(a_len, b_len);
// Looking only within the search range, count and flag the matched pairs.
var Num_com = 0;
var yl1 = b_len - 1;
for (var i = 0; i < a_len; i++) {
var lowlim = (i >= search_range) ? i - search_range : 0;
var hilim = ((i + search_range) <= yl1) ? (i + search_range) : yl1;
for (var j = lowlim; j <= hilim; j++) {
if (b_flag[j] !== 1 && a[j] === b[i]) {
a_flag[j] = 1;
b_flag[i] = 1;
Num_com++;
break;
}
}
}
// Return if no characters in common
if (Num_com === 0) { return 0.0; }
// Count the number of transpositions
var k = 0; var N_trans = 0;
for (var i = 0; i < a_len; i++) {
if (a_flag[i] === 1) {
var j;
for (j = k; j < b_len; j++) {
if (b_flag[j] === 1) {
k = j + 1;
break;
}
}
if (a[i] !== b[j]) { N_trans++; }
}
}
N_trans = Math.floor(N_trans / 2);
// Adjust for similarities in nonmatched characters
var N_simi = 0; var adjwt = jaro_winkler_adjustments;
if (minv > Num_com) {
for (var i = 0; i < a_len; i++) {
if (!a_flag[i]) {
for (var j = 0; j < b_len; j++) {
if (!b_flag[j]) {
if (adjwt[a[i]] === b[j]) {
N_simi += 3;
b_flag[j] = 2;
break;
}
}
}
}
}
}
var Num_sim = (N_simi / 10.0) + Num_com;
// Main weight computation
var weight = Num_sim / a_len + Num_sim / b_len + (Num_com - N_trans) / Num_com;
weight = weight / 3;
// Continue to boost the weight if the strings are similar
if (weight > 0.7) {
// Adjust for having up to the first 4 characters in common
var j = (minv >= 4) ? 4 : minv;
var i;
for (i = 0; (i < j) && a[i] === b[i]; i++) { }
if (i) { weight += i * 0.1 * (1.0 - weight) };
// Adjust for long strings.
// After agreeing beginning chars, at least two more must agree
// and the agreeing characters must be more than half of the
// remaining characters.
if (minv > 4 && Num_com > i + 1 && 2 * Num_com >= minv + i) {
weight += (1 - weight) * ((Num_com - i - 1) / (a_len * b_len - i*2 + 2));
}
}
return Math.round(weight*100);
""";
SELECT
name1, name2,
jwd(name1, name2) scr
FROM -- babynames.usa_1910_2013_copy x
(
select "Liu" as name1, "Lau" as name2 union all
select "Liu" as name1, "Liu" as name2 union all
select "John" as name1, "Jone" as name2
) x
ORDER BY scr DESC
In addition: I've just double checked your jaro_winkler_google_UDF2.js file and clearly see that issue is in this file.
Fix this file using code in my answer
Or, just remove below lines in it
var a = r.a;
var b = r.b;
and uncomment
//jaro_winkler.distance = function(a, b) {
//return Math.round(weight*100)
and comment all with emit in it
jaro_winkler_distance=function(r, emit) {
emit(weight);
You should be ok then!