Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] Support for #notpresent keyword #270

Closed
avpavlov opened this issue Dec 22, 2017 · 20 comments
Closed

[feature request] Support for #notpresent keyword #270

avpavlov opened this issue Dec 22, 2017 · 20 comments
Assignees
Milestone

Comments

@avpavlov
Copy link

avpavlov commented Dec 22, 2017

Use case: I'm writing tests for Spring Boot Repository. It supports "projections" so user can request different detalization, e.g.

  • "default" projection: Fields A,B,C
  • "more-data" projection: Fields A,B,C and array D (it is loaded from separate table)
  • "complete-data" projection: Fields A,B,C and arrays D,E,F,G (each is loaded from separate table)

I cannot check contains/!contains with single JSON so right now my tests look like

Scenario "default"
 <request>
 then match response contains 
  """
  {
    A : '#string', 
    B : '#string' , 
    C : '#string'
  }
  """
  and match response !contains 
  """
  {
    D : '#array', 
    E : '#array', 
    F : '#array', 
    G : '#array' 
  }
  """

Scenario "more-data"
 <request>
 then match response contains 
  """
  {
    A : '#string', 
    B : '#string' , 
    C : '#string',
    D : '#array' 
  }
  """
  and match response !contains 
  """
  {
    E : '#array', 
    F : '#array', 
    G : '#array' 
  }
  """

Scenario "complete-data"
 <request>
 then match response contains 
  """
  {
    A : '#string', 
    B : '#string' , 
    C : '#string',
    D : '#array', 
    E : '#array', 
    F : '#array', 
    G : '#array' 
  }
  """

With new keyword added my scenarios will be cleaner and easier to read

Scenario "default"
 <request>
 then match response contains 
  """
  {
    A : '#string', 
    B : '#string' , 
    C : '#string'
    D : '#undefined', 
    E : '#undefined', 
    F : '#undefined, 
    G : '#undefined' 
  }
  """

Scenario "more-data"
 <request>
 then match response contains 
  """
  {
    A : '#string', 
    B : '#string' , 
    C : '#string',
    D : '#array', 
    E : '#undefined', 
    F : '#undefined, 
    G : '#undefined' 
  }
  """

Scenario "complete-data"
 <request>
 then match response contains 
  """
  {
    A : '#string', 
    B : '#string' , 
    C : '#string',
    D : '#array', 
    E : '#array', 
    F : '#array', 
    G : '#array' 
  }
  """
@ptrthomas
Copy link
Member

@avpavlov actually there is an #ignore marker already, can you confirm that it is what works for you: https://github.com/intuit/karate#fuzzy-matching

@avpavlov
Copy link
Author

#ignore excludes field from verification. I do not want to exclude, I want to ensure this field is not included.

@ptrthomas ptrthomas reopened this Dec 22, 2017
@avpavlov
Copy link
Author

In other words, #undefined is like local !contains for single field

@ptrthomas
Copy link
Member

would #notnull work ?

@ptrthomas
Copy link
Member

I meant #null

@avpavlov
Copy link
Author

Thank you, both work - #null and ##null.

However, I would say working #null is a bug in this case, because if I expect some key with null value then missing key should fail scenario.

##null is right fit in this case - any chances you expand "contains / !contains" readme sections to propose it as way to verify key is not presented in JSON?

@ptrthomas
Copy link
Member

However, I would say working #null is a bug in this case, because if I expect some key with null value then missing key should fail scenario.

Yes. Or how about this, to check for a null just use null itself. So this should fail (as of now it passes, but I'm proposing to change this):

* def foo = { }
* match foo == { a: null }

In my experience, most teams assume a null value and the key missing to be the same. Typically people set the Json marshaller config to 'strip nulls' for example, to reduce payload bloat.

Can you help by suggesting what change you'd like to see in the contains readme section, I'll be happy to add.

@avpavlov
Copy link
Author

avpavlov commented Dec 22, 2017

(as of now it passes, but I'm proposing to change this):

Sounds good

most teams assume a null value and the key missing to be the same.

That's true. That's why #undefined could help ;)

Can you help by suggesting what change you'd like to see in the contains readme section,

Let me think

@ptrthomas
Copy link
Member

@avpavlov cool, I'm beginning to agree. see I'm quite reasonable :P

how about #notpresent - which could be more clear ? undefined has a certain meaning in JS also ?

@avpavlov
Copy link
Author

notpresent sounds better than undefined. I tried to invent something like na or notavailable or missed but all of these were not enough clear so finally I borrowed keyword from JS

@ptrthomas
Copy link
Member

great ! looking at it now. are you able to build from source or do you prefer a release.

@ptrthomas ptrthomas self-assigned this Dec 22, 2017
@ptrthomas ptrthomas added this to the v0.7.0 milestone Dec 22, 2017
@avpavlov
Copy link
Author

avpavlov commented Dec 22, 2017

Created PR for ##null in README #271

@avpavlov
Copy link
Author

I can build from sources

ptrthomas added a commit that referenced this issue Dec 22, 2017
the behavior of match for #null and even the 'null' value has changed, the key is expected to be present in both cases
so upgrading may need to use #ignore instead of #null in some rare cases
@ptrthomas
Copy link
Member

@avpavlov yes, is now in the develop branch. I actually decided that both #null and the null value will expect the JSON key to be present. Just felt that this is consistent and reduces confusion. Teams may need to use #ignore or #notpresent which is more clear.

@avpavlov
Copy link
Author

I agree, Karate tests output/protocol not how this output could be interpreted by consumer

@ptrthomas ptrthomas changed the title [feature request] Support for #undefined keyword [feature request] Support for #notpresent keyword Dec 22, 2017
@avpavlov
Copy link
Author

Just tested project with develop branch - it works! Thank you!
Is there any way in the Github to subscribe to the notifications about releases?

@ptrthomas
Copy link
Member

@avpavlov not sure, but you can watch the project on GiHub. there's a twitter account if you are in to that kind of thing: https://twitter.com/KarateDSL

ptrthomas added a commit that referenced this issue Dec 23, 2017
@ptrthomas
Copy link
Member

@avpavlov well. after thinking about it, it made sense to implement #present as well ! thanks for triggering this, I think Karate has become a little better as a result. So now here is what is possible. I recommend that folks don't use != not-equals because it gets really confusing, but hey.

    * def foo = { }
    * match foo != { a: '#present' }
    * match foo == { a: '#notpresent' }
    * match foo == { a: '#ignore' }
    * match foo == { a: '##null' }
    * match foo != { a: '#null' }
    * match foo != { a: '#notnull' }
    * match foo == { a: '##notnull' }
    * match foo != { a: null }

    * def foo = { a: null }
    * match foo == { a: '#null' }    
    * match foo == { a: '##null' }
    * match foo != { a: '#notnull' }
    * match foo != { a: '##notnull' }
    * match foo == { a: '#present' }
    * match foo == { a: '#ignore' }
    * match foo != { a: '#notpresent' }

    * def foo = { a: 1 }
    * match foo == { a: '#notnull' }
    * match foo == { a: '##notnull' }
    * match foo != { a: '#null' }    
    * match foo != { a: '##null' }
    * match foo == { a: '#present' }
    * match foo == { a: '#ignore' }
    * match foo != { a: '#notpresent' }

@Shaileshz204
Copy link

@ptrthomas When I am validating schema using Karate one of the field as per schema is defined as String but its returning null but it returns string value also in some cases so when its returning null its failing my test as its string. How to handle this. I want it to pass if its null or string when its string in schema.

@ptrthomas
Copy link
Member

@Shaileshz204 use stack overflow for questions like this please: https://stackoverflow.com/a/71522605/143475

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants