quickbrownfox
quickbrownfox

Reputation: 161

Python: Assert that all objects in a list have a specific attribute values

I am working for a web-app using django-1.6 where there will be application(s) coming in and going through various stages of scrutiny. On satisfying criteria(s), they get approved and promoted to the next stage or get deferred. I was testing the approval transition. And landed up in situation where I need to assert if approvable application(s) have their status changed to pending for the next stage. So, this is how I do it right now:

self.assertEqual('stage2.pending', stage1_approved_mentor_applications[0].status)

what I am looking for is something like

self.assertEqual('stage2.pending', stage1_approved_mentor_applications.status)

which would assure that all the objects in the list stage1_approved_mentor_applications have their status as 'stage2.pending'. One way would be to pass it to a function taking a list and returning True on all status being 'stage2.pending' and False if otherwise. This function will be called in assertTrue. Wondering if there's already a workaround for this that would save me from reinventing the wheel.

Can anyone help me with that? Thanks in advance.

Upvotes: 0

Views: 2423

Answers (3)

Boklucius
Boklucius

Reputation: 1926

what about this:

for s in stage1_approved_mentor_applications:
   self.assertEqual('stage2.pending', s.status)

so you know what status is different

Upvotes: 4

freakish
freakish

Reputation: 56467

How about this?

self.assertFalse(
    any(obj.status != 'stage2.pending' for obj in stage1_approved_mentor_applications)
)

Upvotes: 2

kharandziuk
kharandziuk

Reputation: 12890

self.assertTrue( all([x is 'stage2.pending' for x in stage1_approved_mentor_applications] ))

Upvotes: 0

Related Questions