grappa icon indicating copy to clipboard operation
grappa copied to clipboard

"From a list of objects, assert on how many objects contain a specific key/value pair"

Open SatheeshJM opened this issue 5 years ago • 1 comments

Say I have a list

[{"foo" : "bar"}, {"foo: "bar2"}, {"foo" : "bar"},{"random":"1"}]

Is there a way of asserting that the list contains exactly 2 occurrences of {"foo":"bar"}

Can't seem to find anything in the docs.

SatheeshJM avatar Apr 13 '19 22:04 SatheeshJM

I would do this

items = [{"foo" : "bar"}, {"foo": "bar2"}, {"foo" : "bar"},{"random":"1"}]
search = {"foo" : "bar"}

matches = [x for x in items if x == search]
matches | should.have.length(2)

sgissinger avatar Nov 23 '20 23:11 sgissinger