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

Value mismatch error when values seem to be the 'same' #20

Closed
casahome2000 opened this issue Jan 24, 2018 · 5 comments · Fixed by #50
Closed

Value mismatch error when values seem to be the 'same' #20

casahome2000 opened this issue Jan 24, 2018 · 5 comments · Fixed by #50
Assignees

Comments

@casahome2000
Copy link

Command

$ tavern-ci --debug some_file.tavern.yaml

Response in Terminal

Value mismatch: '159295' vs '159295'
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tavern/response.py", line 210, in _validate_block
    assert actual_val == expected_val
AssertionError

Test to save user_id from response

  - name: login
    request:
      url: some_test_url.com/login
      json:
        username: someUser
        password: somePassword
        grant_type: password
      method: POST
      headers:
        content-type: application/json
        accept: application/json
    response:
      status_code: 200
      body:
        token_type: bearer
        scope: user
        expires_in: 604800
      headers:
        content-type: application/json; charset=utf-8
      save:
        body:
          access_token: access_token
          refresh_token: refresh_token
          user_id: user_id

Test to use user_id in subsequent test

  - name: create_book
    request:
      url: some_test_url.com/books
      json:
        title: Some title of a book
      method: POST
      headers:
        content-type: application/json
        accept: application/json
        Authorization: "Bearer {access_token:s}" #working to reuse variable saved
    response:
      status_code: 201
      body:
        user_id: "{user_id}"
      headers:
        content-type: application/json; charset=utf-8
@michaelboulton michaelboulton self-assigned this Jan 25, 2018
@michaelboulton
Copy link
Member

I'm guessing this is because it's an integer and user_id is being implicitly converted to an int at some point.

Because it might be desirable to actually have the distinction between an integer and a string being returned I'm not sure what the best thing to do is to fix this - it should at least print something more useful like

Value mismatch: '159295' (type: int) vs '159295' (type: str)

@benhowes
Copy link
Member

@michaelboulton This has been released right?

@Birne94
Copy link
Contributor

Birne94 commented Mar 5, 2018

Currently, values from variables can only be passed by using string formatting (user_id: "{user_id}"). This loses any information about the original data type.

We should come up with another way of passing variables as they are. This might also be useful for passing whole json-blocks as data. String-formatting should stay as it has its own use case (e.g. Authorization header formatting).

Maybe a special token might be appropriate here. I am not sure which tokens are reserved in the yaml spec, so the symbol might need to be changed.

user_id: $user_id

@michaelboulton
Copy link
Member

michaelboulton commented Mar 5, 2018

This can be done using the yaml 'tags' (specs here http://www.yaml.org/spec/1.2/spec.html#id2803311) - for example:

request:
  json:
    user_id: !!int "{user_id_as_int:d}"

and custom tags can also be added (this is used for !include and !uuid).

It should be possible to make this 'pluggable' in future because it is just registering a function (eg

cls.add_constructor('!include', cls.construct_include)
cls.add_constructor('!uuid', makeuuid)
)

@Birne94
Copy link
Contributor

Birne94 commented Mar 5, 2018

Using the default yaml tags does not work, unfortunately, because they are constructed before any string formatting is done.

I propose a solution involving an intermediate token which gets converted after string formatting is done. See #50 for my approach.

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

Successfully merging a pull request may close this issue.

4 participants