grappa-py / grappa

Behavior-oriented, expressive, human-friendly Python assertion library for the 21st century
http://grappa.rtfd.io
MIT License
134 stars 15 forks source link

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

Open SatheeshJM opened 5 years ago

SatheeshJM commented 5 years ago

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.

sgissinger commented 3 years ago

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)