1

I have a scenario where I'm retrieving rows of data from a Postgres DB table and matching the corresponding column output against a separate .json file. The db.readRow() Java Util method returns a Map<String, Object>. The column in question that's causing a data type mismatch is defined as 'json' datatype in Postgres DB. This problem is similar to a recent post that my colleague submitted, and I looked at the information Peter T. provided (thanks Peter!).

Below is the feature file code and supporting file and attached is the corresponding Karate report showing the match error on line 15. Line 16 is a workaround I/we found where we have a helper function that converts the results of that column to a JSON object. We then are able to match the data correctly against the specific column on line 18. The ideal would be having line 15 work as normally there will be other column data in the embeddedJsonDb.json included in the same table. For the other non-json columns, #regex, #notnull, or other matching types are working as expected.

Thanks in advance for any help or tips.

embeddedJson.feature:

  Feature: Karate Embedded Json Example

  Background:
    * def dbExpectedResults = read('embeddedJsonDb.json')
    * def DBUtils = Java.type('utils.DBUtils')
    * def db = new DBUtils()
    * def helpers = call karate.read('classpath:helpers2.feature')
    
  @embeddedJson
  Scenario Outline: Karate Embedded Json Example
    * def dbResult = db.readRow("select * from embedded_json_table")
    * json embedded_json_msg = embeddedJsonMsg
    * print "dbResult: ", dbResult
    * print "dbResult.embedded_json_message: ", dbResult.embedded_json_message
    * match dbResult == dbExpectedResults.karateJsonIssue.embedded_json_table
    * def embeddedJsonObject = helpers.jsonStringToJsonObject(dbResult.embedded_json_message.toString())
    * print "embeddedJsonObject", embeddedJsonObject
    * match embeddedJsonObject == embedded_json_msg
    Examples:
      | testDescription | embeddedJsonMsg                                                            |
      | embedded json   | {"name":"Luke Skywalker","info":"{\"occupation\":{\"jobTitle\":\"Jedi\"}"} |

embeddedJsonDb.json:

{
  "karateJsonIssue": {
    "embedded_json_table": {
      "embedded_json_message": "#(embedded_json_msg)"
    }
  }  
}

Karate report output

  • sorry this is too hard for me to understand and your image doesn't match. the rules are simple. ensure everything is a java map. clearly embeddedJsonObject is not. maybe what you missed is that the "outer" map may be fine, but the values inside may still be something else. I don't recommend using JSON.parse(), instead use `karate.fromString()` - note that there are ways to convert table colums into JSON: https://github.com/karatelabs/karate#scenario-outline-enhancements – Peter Thomas Jun 23 '22 at 15:21
  • Thanks Peter. Yes, I believe that's the issue as well. The "outer" map seems to be correct, but for some reason dbResult.embedded_json_message is not even though the output looks the same in the match error (line 15 in the attached image). When line 15 is commented out, the workaround in lines 16-18 is used and passes. I'll check out karate.fromString() and the outline enhancements. I'm also wondering whether the embedded_json_message db column defined as "json" datatype in Postgres is contributing to it as well. I'll do some more digging and tweaking. Thanks again. – Ed Mendonca Jun 23 '22 at 21:17

0 Answers0