Search code examples
karateintuit-partner-platform

How to match DB query result and API response directly in karate


My code is as follows => @UserValidationEmail

Feature: Verify User Details 

  Scenario: Verify User Details api

    Given url 'http://127.0.0.1/user/?id=123'
    When method get
    Then status 200

    * def DbUtils = Java.type('com.intuit.karate.treebo.util.DbUtils')
    * def db = new DbUtils()

    * def query = "SELECT * FROM user_table where id = 123"
    * def dbResult = db.readRows(query)


    * match dbResult[0] contains response.data

The response of the API call http://127.0.0.1/user/?id=123 is =>

{"status":"success","data":{"id":123,"created_at":"2018-01-18T10:27:24.631959","modified_at":"2018-01 18T10:27:24.641493","first_name":"Foo","last_name":"Bar","email":"foo@bar.com","phone_number":null}}

The response of the query on the DB is =>

[print] dbResult: [{id=123, password=bZeGU3h66cp, created_at=2018-01-18 15:57:24.631959, modified_at=2018-01-18 15:57:24.641493, first_name=Foo, last_name=Bar, email=foo@bar.com, phone_number=null}]

On trying to match the above two results using contains, I get the following error:

Tests in error: 
  * match dbResult[0] contains response.data(Scenario: Verify User Details api): path: $[0].created_at, actual: 2018-01-18 15:57:24.631959, expected: '2018-01-18T10:27:24.631959', reason: actual value is not a string

Ho can I match the result of the DB query directly to the API response?


Solution

  • Let's take a look at the message:

    path: $[0].created_at, actual: 2018-01-18 15:57:24.631959, expected: '2018-01-18T10:27:24.631959'
    reason: actual value is not a string
    

    Clearly the result-row returned has a java.util.Date or similar, instead of a string. even if it was a string, note that the values are not the same.

    Either convert that field to a string and compare. Or most likely this will work for you - just do this before the comparison:

    * set response.data.created_at = '#ignore'
    

    Read the docs on fuzzy-matching if you need to understand this more: https://github.com/intuit/karate#fuzzy-matching