Skip to content

Commit

Permalink
Fix misspecified tests using MagicMock().called_with() (#1072)
Browse files Browse the repository at this point in the history
* Fix misspecified twilio tests

Prior implementation ran `assert ...MagicMock.called_with(...)`

`called_with` is not a real test method on MagicMock, so it returns a
MagicMock instance by default, which is truthy and evaluates to true
by assert. This test wasn't checking for anything and would pass no
matter what actually happened in the code.

These new tests check for the intended methods being called.

* Fix misspecified salesforce tests

* Fix misspecified test guidelines in docs
  • Loading branch information
austinweisgrau authored Jun 25, 2024
1 parent 061bb89 commit c49f631
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/write_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Now, we can test our Salesforce Parsons Connector's query method:
# Call the query method with a fake value
response = self.sf.query('FAKESOQL')
# Check that our mock client's query_all method was also called with the fake value
assert self.sf._client.query_all.called_with('FAKESOQL')
self.sf._client.query_all.assert_called_with('FAKESOQL')
# Check that the response from our query method is what we expect
self.assertEqual(response[0]['value'], 'FAKE')
Expand Down
10 changes: 5 additions & 5 deletions test/test_salesforce/test_salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ def test_describe_fields(self):
def test_query(self):
fake_soql = "FAKESOQL"
response = self.sf.query(fake_soql)
assert self.sf.client.query_all.called_with(fake_soql)
self.sf.client.query_all.assert_called_with(fake_soql)
self.assertEqual(response["records"][0]["Id"], "1234567890AaBbC")

def test_insert(self):
fake_data = Table([{"firstname": "Chrisjen", "lastname": "Avasarala"}])
response = self.sf.insert_record("Contact", fake_data)
assert self.sf.client.bulk.Contact.insert.called_with(fake_data)
self.sf.client.bulk.Contact.insert.assert_called_with(fake_data.to_dicts())
assert response[0]["created"]

def test_update(self):
Expand All @@ -69,7 +69,7 @@ def test_update(self):
]
)
response = self.sf.update_record("Contact", fake_data)
assert self.sf.client.bulk.Contact.update.called_with(fake_data)
self.sf.client.bulk.Contact.update.assert_called_with(fake_data.to_dicts())
assert not response[0]["created"]

def test_upsert(self):
Expand All @@ -84,13 +84,13 @@ def test_upsert(self):
]
)
response = self.sf.upsert_record("Contact", fake_data, "id")
assert self.sf.client.bulk.Contact.update.called_with(fake_data)
self.sf.client.bulk.Contact.upsert.assert_called_with(fake_data.to_dicts(), "id")
print(response)
assert not response[0]["created"]
assert response[1]["created"]

def test_delete(self):
fake_data = Table([{"id": "1234567890AaBbC"}])
response = self.sf.delete_record("Contact", fake_data)
assert self.sf.client.bulk.Contact.update.called_with(fake_data)
self.sf.client.bulk.Contact.delete.assert_called_with(fake_data.to_dicts())
assert not response[0]["created"]
48 changes: 31 additions & 17 deletions test/test_twilio/test_twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,54 @@ def test_get_account(self):

fake_sid = "FAKESID"
self.twilio.get_account(fake_sid)
assert self.twilio.client.api.accounts.called_with(fake_sid)
self.twilio.client.api.accounts.assert_called_with(fake_sid)

def test_get_accounts(self):

self.twilio.get_accounts(name="MyOrg", status="active")
assert self.twilio.client.api.accounts.list.called_with(
self.twilio.client.api.accounts.list.assert_called_with(
friendly_name="MyOrg", status="active"
)

def test_get_messages(self):

self.twilio.get_messages(date_sent="2019-10-29")
assert self.twilio.client.messages.list.called_with(date_sent="2019-10-29")
self.twilio.client.messages.list.assert_called_with(
date_sent="2019-10-29",
to=None,
from_=None,
date_sent_before=None,
date_sent_after=None,
)

def test_get_account_usage(self):

# Make sure that it is calling the correct Twilio methods
self.twilio.client.usage.records.today.list.assert_not_called()
self.twilio.get_account_usage(time_period="today")
assert self.twilio.client.usage.records.today.list.called_with(time_period="today")
self.twilio.client.usage.records.today.list.assert_called()

self.twilio.client.usage.records.last_month.list.assert_not_called()
self.twilio.get_account_usage(time_period="last_month")
assert self.twilio.client.usage.records.last_month.list.called_with(
time_period="last_month"
)
self.twilio.client.usage.records.last_month.list.assert_called()

self.twilio.client.usage.records.this_month.list.assert_not_called()
self.twilio.get_account_usage(time_period="this_month")
assert self.twilio.client.usage.records.this_month.list.called_with(
time_period="this_month"
)
self.twilio.client.usage.records.this_month.list.assert_called()

self.twilio.client.usage.records.yesterday.list.assert_not_called()
self.twilio.get_account_usage(time_period="yesterday")
assert self.twilio.client.usage.records.today.list.called_with(time_period="yesterday")
self.twilio.client.usage.records.yesterday.list.assert_called()

# Make sure that it is calling the correct Twilio methods
self.twilio.get_account_usage(time_period="daily", start_date="10-19-2019")
assert self.twilio.client.usage.records.daily.list.called_with(start_date="10-19-2019")
self.twilio.get_account_usage(time_period="monthly", start_date="10-19-2019")
assert self.twilio.client.usage.records.monthly.list.called_with(start_date="10-19-2019")
self.twilio.get_account_usage(time_period="yearly", start_date="10-19-2019")
assert self.twilio.client.usage.records.yearly.list.called_with(start_date="10-19-2019")
self.twilio.client.usage.records.daily.list.assert_not_called()
self.twilio.get_account_usage(group_by="daily", start_date="10-19-2019")
self.twilio.client.usage.records.daily.list.assert_called_with(start_date="10-19-2019")

self.twilio.client.usage.records.monthly.list.assert_not_called()
self.twilio.get_account_usage(group_by="monthly", start_date="10-19-2019")
self.twilio.client.usage.records.monthly.list.assert_called_with(start_date="10-19-2019")

self.twilio.client.usage.records.yearly.list.assert_not_called()
self.twilio.get_account_usage(group_by="yearly", start_date="10-19-2019")
self.twilio.client.usage.records.yearly.list.assert_called_with(start_date="10-19-2019")

0 comments on commit c49f631

Please sign in to comment.