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

Dictionary keys by variable is throwing exception #48

Closed
alcalda opened this issue Feb 12, 2019 · 2 comments
Closed

Dictionary keys by variable is throwing exception #48

alcalda opened this issue Feb 12, 2019 · 2 comments

Comments

@alcalda
Copy link

alcalda commented Feb 12, 2019

Trying to assert a dictionary to not contain keys from a list of keys
data | should.not_have.key(key)

is throwing AssertionError
`test_ddt.py:19: in _runtestmodule
matchrsi(cfg.result[0], data)
......\modules\matchrsi.py:32: in matchrsi
data | should.not_have.key(key)
C:\Program Files\Python37\lib\site-packages\grappa\test.py:208: in ror
return self.overload(value)
C:\Program Files\Python37\lib\site-packages\grappa\test.py:196: in overload
return self.call(subject, overload=True)
C:\Program Files\Python37\lib\site-packages\grappa\test.py:73: in call
return self._trigger() if overload else Test(subject)

self = <grappa.test.Test object at 0x0000026784443C88>

def _trigger(self):
    """
    Trigger assertions in the current test engine.

    Raises:
        AssertionError: in case of assertion error.
        Exception: in case of any other assertion error.
    """
    log.debug('[test] trigger with context: {}'.format(self._ctx))

    try:
        err = self._engine.run(self._ctx)
    except Exception as _err:
        err = _err
    finally:
        # Important: reset engine state to defaults
        self._engine.reset()
        self._root._engine.reset()

    # If error is present, raise it!
    if err:
      raise err

E AssertionError: Oops! Something went wrong!
E
E The following assertion was not satisfied
E subject "{'id': 'de83e969-504 ..." should not have key "restrictionReason"
E
E Where
E File "D:\workspace\isswtest\issw-functest\modules\matchrsi.py", line 32, in matchrsi
E
E 24| empty_attr = [k for k, v in rsi.Attributes.items() if v is None]
E 25| nonempty_attr = [k for k, v in rsi.Attributes.items() if v is not None]
E 26| else:
E 27| empty_attr = nonempty_attr = []
E 28|
E 29| if rsi.object_exist:
E 30| is_xobject(data)
E 31| for key in empty_attr:
E 32| > data | should.not_have.key(key)
E 33| list(data.keys()) | should.do_not.contain(key)
E 34|
E 35| for key in nonempty_attr:
E 36| data | should.have.key(key)
E 37| data.get(key) | should.be.equal.to(rsi.Attributes.get(key))
E 38| else:
E 39| # RSI Object does not exist

C:\Program Files\Python37\lib\site-packages\grappa\test.py:113: AssertionError`

Alternatively using
list(data.keys()) | should.do_not.contain(key)
is working though

@h2non
Copy link
Member

h2non commented Feb 12, 2019

What's the type/value of data. I cannot reproduce.
Please, provide an example in repl.it/languages/python3 and share it here.

@zevisert
Copy link

zevisert commented Nov 27, 2019

@h2non I can reproduce. Please take a look, I tried to make this as approachable as possible
https://repl.it/repls/DisastrousVigilantSites


In this case it comes down to some wrong assumptions in operators/keys.py:after_success

def after_success(self, obj, *keys):
if not self.ctx.negate:
self.ctx.subject = [obj[x] for x in obj if x in keys]
if len(keys) == 1 and len(self.ctx.subject):
self.ctx.subject = self.ctx.subject[0]

In my example, the operators' self.ctx.subject is correct ({"some":"thing"}), and the method's *args are also correct (("other",)). But instead it tries to access self.ctx.subject[0] raising a KeyError. However, if we are negating it does convert our subject to a list and this could work, but I think it's better off to keep the context as a dict, that's what the docs suggest .keys is to be used for.

@h2non h2non closed this as completed in 518fed3 Nov 21, 2020
h2non added a commit that referenced this issue Nov 21, 2020
Fixes #53, #55, #48, #59 (partially) and added mock call operators
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

No branches or pull requests

3 participants