python - Understanding Mocking and SideEffects -


i newb python , understand testing, however, cannot wrap head around working mocked objects , side_effects.

here method:

@retry(every=retry_every, until=retry_until) @unique() @sessionized(0) def record_click(session, queue, mailing_id, member_id, link_id, timestamp, user_agent):     message = session.query(message).get((mailing_id, member_id))     mailing = session.query(mailing).get(mailing_id)     # more code here 

here test:

@mock.patch("audience.jobs.eventprovider") @mock.patch("audience.jobs.enqueue_webhook") @mock.patch("logging.exception") @mock.patch("audience.jobs.audience_queues") @mock.patch("audience.jobs.session") @mock.patch("audience.jobs.databaseconnector") def test_track_click_publishes_event_to_sns(self, databaseconnector, session, audience_queues, logger, enqueue_webhook, eventprovider):     message_mock = mock.mock(account_id=77)     message_mock.record_open.return_value = true     mailing_mock = mock.mock(mailing_id=123)     mailing_mock.recipient_groups.return_value = [111]     session_query = session.return_value.query.return_value     session_query.side_effect = lambda arg: message_mock if isinstance(arg, tuple) else mailing_mock      result = jobs.record_click(         888,         9999,         2048,         datetime.datetime(1999, 12, 31, 23, 59, 59, 999999).isoformat(),         "mozilla/5.0")      self.assertisnone(result)     self.assertlistequal(eventprovider.mock_calls, [         mock.call(),         mock.call().publish_link_clicked(             headers={'user-agent': 'mozilla/5.0'},             mailing_id=888,             account_id=77,             contact_id=9999,             link_id=2048,             group_ids=[111]         )     ])     self.assertlistequal(logger.mock_calls, []) 

there error keep receiving is:

instead of
call().publish_link_clicked(group_ids=[111], account_id=77, **etc)

this called in unittest
call().publish_link_clicked(group_ids=<magicmock name='session().query().get().recipient_groups' id='4557662736'>, account_id=<magicmock name='session().query().get().account_id' id='4557652048'>, **etc)

what doing wrong?

don't call session() or query(); use mock.return_value attribute instead traverse call graph:

session.return_value.query.return_value.side_effect = lambda arg: message_mock if isinstance(arg, tuple) else mailing_mock 

i use intermediary names hold return value:

session_query = session.return_value.query.return_value session_query.side_effect = lambda arg: message_mock if isinstance(arg, tuple) else mailing_mock 

you need patch right session class; depends entirely how code produces session argument record_click. see where patch more details. if @sessionized decorator produces argument, , doesn't live in audience.jobs module, not patching right location.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -