If else statement in Spock test framework - testing

I am trying to write a test code coverage for the below scanner method i have in groovy. I am using Spock test Framework to write my test case.
I am having issues trying to write the case for the if else part. It complains that i am still missing invocations for
1 * script.sh(N_COMMAND)
1 * script.sh(Node2_COMMAND + b + XYZ_NODE_COMMAND)
And
!PROJECT_TYPE.equals('nodejs')
1 * script.sh(a + b + XYZ_GRADLE_COMMAND)
I am new to Spock. Any help is appreciated.
Groovy Code:
void scanner() {
if (obj.projectType.equals("nodejs")){
script.sh N_COMMAND
script.sh Node2_COMMAND + b + XYZ_NODE_COMMAND
} else{
script.sh a + b + XYZ_GRADLE_COMMAND
}
script.sh a + b + c
}
Spock test framework code:
def 'should scanner'() {
given:
config.projectType >> PROJECT_TYPE
when:
scanner()
then:
PROJECT_TYPE.equals('nodejs')
1 * script.sh(N_COMMAND)
1 * script.sh(Node2_COMMAND + b + XYZ_NODE_COMMAND)
then:
!PROJECT_TYPE.equals('nodejs')
1 * script.sh(a + b + XYZ_GRADLE_COMMAND)
then:
1 * script.sh(a + b + c)
where:
PROJECT_TYPE << ['nodejs', 'java']
}

Related

Issues observed in " karate-junit5 " library when migrated from version 1.2.0 to 1.3.1

Recently I upgraded the version of karate-junit5 library from 1.2.0 to 1.3.1.
I have a common feature file which contains some common functions used all other scenarios like below sample.
common.feature
#Common
Scenario: Common scenario
* def someFunction =
"""
function(){
karate.log('In some function..');
var textArray = driver.locateAll("//*[#role='button']").map( x => x.text)
karate.log('Button text is: ', textArray);
}
"""
* def getDateInFormat =
"""
function(date){
var curr_date = date.getDate();
if(curr_date < 10)
curr_date = '0' + curr_date;
var curr_month = date.getMonth() + 1;
if(curr_month < 10)
curr_month = '0' + curr_month;
var curr_year = date.getFullYear();
var mod_date = curr_date + '/' + curr_month + '/' + curr_year;
return mod_date;
}
"""
* def sortFunction =
"""
function(sortArr, sortType){
if(sortType == 'asc')
sortArr.sort();
else
sortArr.reverse();
return sortArr;
}
"""
I call above common scenarios from other feature files whenever required. A sample calling feature file would be as below.
Test.feature
Feature: Test feature
Background:
* callonce read('common.feature#Common')
Scenario: Test 1
* print "Today's date is: ", getDateInFormat(new Date())
Scenario: Test 2
* print "Sorted data by A to Z is: ", sortFunction(['Dog', 'Cat', 'Horse'], 'asc')
Scenario: Test 3
* configure driver = { type: 'chrome', addOptions: ['--ignore-certificate-errors', '--incognito', '--disable-gpu', '--window-size=1920,1080', '--no-sandbox'], headless: false }
* configure retry = { count: 10, interval: 2000 }
* driver 'https://www.google.com/'
* someFunction()
All the scenarios 1,2 and 3 are executed properly for 1.2.0 version but throw below errors for when library version is upgraded to 1.3.1 :
For Scenario 1:
" org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (getDate) on java.util.LinkedHashMap#6a969fb8 failed due to: Unknown identifier: getDate "
For Scenario 2:
" org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (sort) on java.util.ArrayList#ff6077 failed due to: Arity error - expected: 1 actual: 0 "
For Scenario 3:
" org.graalvm.polyglot.PolyglotException: ReferenceError: "driver" is not defined "
Can somebody explain what has changed in karate-junit5 library version 1.3.1 from 1.2.0 ?
For Scenario 2, sortArr.reverse() is working properly even in 1.3.1 karate version. Above error is observed for sortArr.sort().
For Scenario 3, if I change the someFunction() to accept driver , like someFunction(driver), then it is executed properly on 1.3.1 version as well.

How to fix the issue of output file not appearing, after having launched a run configuration?

I work on CVRP in combinatorial optimization, where the goal is to compare several models on several instances, using the OPL language. For this to e done, I first wrote and tested the said models; then I wrote an main flow control supposed to run each instance for a 2 hours duration.
My problem is that after 2 hours, and this for each model, the output file appears for some instances, containing the desired information; but for other instances, the output files do not appear, as if these instances do not exist. This problem persists even when you launch one or more of these instances in isolation.
below is the main flow control code, where BPF designates the name of the .mod file, which is simply modified in the code, when one wishes to switch to another model.
{string} datFiles=...;
main {
var source = new IloOplModelSource("BPF.mod");
var cplex = new IloCplex();
var def = new IloOplModelDefinition(source);
cplex.tilim=2*60*60;
cplex.threads=1;
for(var datFile in thisOplModel.datFiles)
{
var opl = new IloOplModel(def,cplex);
var data2= new IloOplDataSource(datFile);
opl.addDataSource(data2);
opl.generate();
var o=new IloOplOutputFile("BPF "+datFile+".txt");
if (cplex.solve()) {
if(cplex.getCplexStatus()==11){
o.writeln("instance : " + datFile
+" / opt = "
+ " / UB = " + cplex.getObjValue()
+ " / LB = " + cplex.getBestObjValue()
+" / time = " + " - "
+" / Gap = "+cplex.getMIPRelativeGap()*100+"%");
}
else {
o.writeln("instance : " + datFile
+ " / opt = " + cplex.getObjValue()
+ " / UB = " + cplex.getBestObjValue()+"*"
+" / time = " + cplex.getSolvedTime()
+ " / Gap : -");
}
o.close();
}
opl.end();
}
}

Extract and comparing data from json array not working in karate

I am trying to search a certain value in a JSON array using the value stored in a variable and then comparing somehow this is not working for me. Can you please help. BillerId1 is always returning a blank value
Given url buyerApi
Given url paymentHub
Then path '/BPAY/v' + version + '/billers'
And header Authorization = 'Bearer ' + token
When method get
Then status 200
* def id = response[0].savedBillerId
Then url paymentHub
Then path '/BPAY/v' + version + '/billers/' +id
And header Authorization = 'Bearer ' + token
And request {billerCode:<billerCode>, billerCRN:'<billerCRN>'}
When method put
Then status 200
Then url paymentHub
Then path '/BPAY/v' + version + '/billers'
And header Authorization = 'Bearer ' + token
When method get
Then status 200
* print id
* def billId1 = get[0] response[?(#.savedBillerId==**'#id'**)].savedBillerId
* print billId1
And match billId1 == id
Examples:
| billerCode | billerCRN |
| 65284 | 65112345675 |
Array looks like this
[
{
"savedBillerId": "ebfa2b9f-f49c-4b0c-c6ee-08d7e671944a",
"billerId": "26c67edb-b3dc-44ea-aa74-08d7d6890798",
"billerName": "test case 21c",
"billerCode": 65284,
"crn": "65112345675"
},
{
"savedBillerId": "500dfde7-e31c-408d-c6ef-08d7e671944a",
"billerId": "26c67edb-b3dc-44ea-aa74-08d7d6890798",
"billerName": "test case 21c",
"billerCode": 65284,
"crn": "65112345672"
}
]
#ptrthomas
Please read the docs: https://github.com/intuit/karate#jsonpath-filters
* def id = '500dfde7-e31c-408d-c6ef-08d7e671944a'
* def billId1 = karate.jsonPath(response, "$[?(#.savedBillerId=='" + id + "')].savedBillerId")[0]
* match billId1 == id
Also I feel this is a worthless check. You find by id and again check that id ?

how to obtain version of app?

Is there a way to obtain a version number of a Titanium app?
e.g when installed on an iOS device then perhaps obtain the CFBundleVersion and when installed on Android then obtain its version number.
UPDATED: I was looking in the Titanium.Platform documentation for the app version number. The app version number is under Titanium.App.
Here is my code for sending the version number of my app to the server, so that the server can provide different content depending on the app-version-number.
function obtain_request_header() {
var caps = Titanium.Platform.displayCaps;
var s0 = 'appversion=' + Titanium.App.version;
var s1 = 'osname=' + Titanium.Platform.osname + ',name=' + Titanium.Platform.name + ',version=' + Titanium.Platform.version + ',model=' + Titanium.Platform.model;
var s2 = 'width=' + caps.platformWidth + ',height=' + caps.platformHeight + ',dpi=' + caps.dpi;
return s0 + ',' + s1 + ',' + s2;
}
To obtain the version of your application regardless of OS, simply use:
Titanium.App.version

Export custom formatted expressions from Mathematica

How can I get Mathematica to export/save/write a text file with proper Fortan77 formatting, that is, 72 columns and a continuation marker on the sixth column?
I am using Mathematica to generate large and complex analytic expressions, which I then need to insert into pre-existing Fortran77 code. I have everything working correctly in the front end of Mathematica with FortranForm[] and
SetOptions[$Output, PageWidth -> 72]
However, I can't figure out how to get Mathematica to output correctly to a text file. I want something like this:
MM11 = mH1**2 + (g2**2*v1**2)/2. -
- (g2**2*(v1**2/2. -
- ((v2*Cos(phi2) - (0,1)*v2*Sin(phi2))*
- (v2*Cos(phi2) + (0,1)*v2*Sin(phi2)))/2.))/2.
...
but get either this:
MM11 = FortranForm[mH1^2 + (g2^2*v1^2)/2 - ...
or this:
MM11 = mH1**2 + (g2**2*v1**2)/2. - (g2**2*
(v1**2/2. - ((v2*Cos(phi2) - (0,1)*v2*Sin(phi2))*
...
This is a job for the surprisingly little-known Splice function. First, you make a template file, with the extension ".mf", like so:
file = "test.mf";
out = OpenWrite[file];
WriteString[out, "MH1 = <* form *>"];
Close[out];
Now when you use Splice, Mathematica will automatically replace everything between the <* and *> delimiters with its evaluated form. So if you set
form = 4 + b9^2 + c1^5 + c4^5 + h10^4 + j2 + k10^4 + p10^4 + q5^5 +
q8 + s3^3 + s7^2 + t6^3 + u3^2 + u9^3 + x8^4 + z2^3;
and call
Splice["test.mf", PageWidth -> 72];
which will automatically infer you want FortranForm output from the file extension, and which allows you to set PageWidth as an option, you will get a pretty decent result in the automatically generated file "test.f" (note the new extension):
MH1 = 4 + b9**2 + c1**5 + c4**5 + h10**4 + j2 + k10**4 + p10**4 +
- q5**5 + q8 + s3**3 + s7**2 + t6**3 + u3**2 + u9**3 + x8**4 +
- z2**3
Look at the docs for Splice for more options (changing the name of the output file and the like).