2

I have Karate feature with a long list of operations. I need to manually have some test data setup before running these tests which is essentially one sql query. Is there a way we can have this query run in "background" in Karate? I'm trying to update all the values in status which aren't "ready_to_test" to "ready_to_test".

Say my query is
update my_table set status = 'ready_to_test' where status != 'ready_to_test';

EDIT: I am trying to run the update query as follows

use JDBC to setup test data

* def config = {username: 'postgres', password: 'postgres', url: 'jdbc:postgresql://localhost:5432/postgres', driverClassName: 'org.postgresql.Driver'}
* def DbUtil = Java.type('com.utils.DbUtils')
* def db = new DbUtil(config)
* def correctStatus = 'ready_to_test'
* def testData = db.cleanRows('UPDATE MY_TABLE M SET M.STATUS = ' 'WHERE M.STATUS != ' + correctStatus)

Also tried * def testData = db.cleanRows('UPDATE MY_TABLE SET STATUS = 'ready_to_test' WHERE STATUS != 'ready_to_test)

Niv
  • 271
  • 1
  • 7
  • 16
  • Look at [this question](https://stackoverflow.com/questions/46225745/in-the-karate-dsl-framework-how-can-we-add-custom-step-definitions-to-expand-it) – Alex D Aug 23 '18 at 20:13
  • The example Alex suggested shows a very helpful demonstration of using JDBC but however is there a way to use the cleanRows and update as well? – Niv Aug 23 '18 at 23:07

2 Answers2

1

Please see if the callSingle API solves your need.

https://github.com/intuit/karate#karate-callsingle

var result = karate.callSingle('classpath:common.feature', { some: 'config' });

Also see hooks: https://github.com/intuit/karate#hooks

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Would it be possible to add an update example similar to the select ones shown in dogs.feature? I am trying to use cleanRows from DbUtils and execute a simple update in my background but not able to come up with the right syntax. I have posted what I'm trying with in edited question. – Niv Aug 27 '18 at 03:33
  • @Niv sounds like you want SQL help, sorry this is "out of scope" for me or Karate. ask a different question if needed. – Peter Thomas Aug 27 '18 at 03:35
  • no, I'm trying to find the right way to have that query in my background executed. It's a basic update query as mentioned in edit and I am able to run it on my table through the SQL editor. It'll be helpful if I can find a Karate feature that is using an update statement. Thank you! – Niv Aug 27 '18 at 03:58
  • 1
    @Niv in my opinion ALL the info you need are in the answer. look at the documentation on hooks. I really don't understand what more you need. – Peter Thomas Aug 27 '18 at 04:07
  • in dogs.feature there are examples of several methods from DbUtils for various usages of select statements. Similarly, is there an example of an update in any feature which uses the cleanRows method in DbUtils? (That's what I'm trying to do) I wasn't able to find a usage of an update calling the cleanRows method from DbUtils. – Niv Aug 27 '18 at 21:31
  • 1
    @Niv DbUtils is just part of a demo example and is not a core capability of Karate. you are supposed to use it as a reference and create a function using SQL on your own. all the best. – Peter Thomas Aug 27 '18 at 23:10
0

I also wanted to update particular row in database and I tried add the function below to DbUtils then use it feature file , It works.

public void insertRows(final String sql) {
    System.out.println("Inserting data to database...");
    jdbc.batchUpdate(new String[]{sql});
}

For more information you can watch this video

Peter Csala
  • 17,736
  • 16
  • 35
  • 75