2

We come from here: https://github.com/intuit/karate#match-contains-deep

As it states:

This modifies the behavior of match contains so that nested lists or objects are processed for a "deep contains" match, ..., you only want to check for some values in the various "trees" of data

So let's try to play a bit, and find some {e: 5} that will be somewhere deep in the tree, when original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }

Feature: Match contains deep

 # match contains deep test (works ok, but this is not "deep" by any stretch of imagination, this is guiding the whole path in the search)
 Scenario: match contains deep test modified original
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { d: { e: 5 } }
   * match original contains deep expected

 #Thats was not deep, this is deep (fails, and this is what anyone would understand by "deep")
 Scenario: match contains deep
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { e: 5 }
   * match original contains deep expected

 #So you dont need deep (fails)
 Scenario: match contains test modified original without deep
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { d: { e: 5 } }
   * match original contains expected

 #So maybe it works with any (fails)
 Scenario: match contains test modified original
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = { e: 5 }
   * match original contains any expected

 #Maybe I'm tripping with syntax (fails)
 Scenario: match contains deep test my test #2
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = e: 5
   * match original contains deep expected

 #So maybe I'm tripping with syntax and semantics, and its any (fails)
 Scenario: match contains deep test my test #2
   * def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
   * def expected = e: 5
   * match original contains any expected
 

So, either I'm not really getting the thing, or it doesn't work as one would expect, i.e. I would like to check an existing key-value pair placed anywhere in the tree.

If someone can throw some light at it, that would be great. As I can see @PeterThomas is answering most of the questions tagged with Karate, I want to thank him for the great effort on putting this tool in the hands of the community.

Greco
  • 172
  • 1
  • 11

2 Answers2

3

Well as the creator of the tool, that's how I have defined "deep" so I guess you have to deal with it ;) You are the first ever to find it misleading, by the way - and in my honest opinion, it does work the way you expect. You have "subsets" of JSON - but you still do want to "fix" the path. And this is what most people want, because when you deal with JSON you never want "surprises" like some values jump around and appear somewhere else.

That said, what you seem to be looking for is JsonPath:

* def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: 5 } }
* match original..* contains deep { e: 5 }

I leave it as an exercise to you to figure out how it works. But feel free to ask for hints :P

EDIT: note that newer versions of Karate support a match contains only deep: https://github.com/karatelabs/karate#match-contains-only-deep

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
1

@Greco, I'm sorry, late to the party You're are right, initially I also thought that deep would be able to find anywhere a key value pair, but sadly its not intended that way. The way I make sense of these terms is:

contains - find mentioned keys in the root object, but with complete match of their value. contains deep - find mentioned keys in the root object, and in turn these keys can be allowed for partial match of their value, just like contains allowed root object to have partial match.

Like @Peter said, you have to form a path but it can go to any level.

* def original = { a: 1, b: 2, c: 3, d: { a: 1, b: 2, e: {f:5,g:6} } }
* def expected = { d: { e: {f:5} } }
* match original contains deep expected
pragun
  • 471
  • 1
  • 6
  • 9
  • 1
    hint: we are always looking for people who can contribute code ;) – Peter Thomas Oct 14 '21 at 12:03
  • 1
    @PeterThomas hehehe. I wish I could. I am not a core programmer/developer. Someone who just uses tool to get the job done with some amount of knowledge to use code that suffices for testing purpose. – pragun Oct 14 '21 at 13:50