Skip to content

Commit

Permalink
Merge pull request #8715 from cfpb/fix/mock-called-with-tests
Browse files Browse the repository at this point in the history
Fix incorrect Python tests using mock.called_with
  • Loading branch information
chosak authored Jan 13, 2025
2 parents bbd061f + 13f1452 commit e2f151a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
12 changes: 6 additions & 6 deletions cfgov/paying_for_college/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ def test_api_command_calls_update(self, mock_update):
self.assertTrue(mock_update.call_count == 1)
call_command("update_via_api", "--school_id", "999999")
self.assertTrue(mock_update.call_count == 2)
self.assertTrue(mock_update.called_with(single_school=999999))
mock_update.assert_called_with(single_school="999999")
call_command(
"update_via_api", "--school_id", "999999", "--save_programs"
)
self.assertTrue(mock_update.call_count == 3)
self.assertTrue(
mock_update.called_with(single_school=999999, store_programs=True)
mock_update.assert_called_with(
single_school="999999", store_programs=True
)
call_command("update_via_api", "--save_programs")
self.assertTrue(mock_update.call_count == 4)
self.assertTrue(mock_update.called_with(store_programs=True))
mock_update.assert_called_with(store_programs=True)

@mock.patch(
"paying_for_college.management.commands."
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_retry_notifications(self, mock_retry):
self.assertEqual(mock_retry.call_count, 1)
call_command("retry_notifications", "--days", "2")
self.assertEqual(mock_retry.call_count, 2)
self.assertTrue(mock_retry.called_with(days=2))
mock_retry.assert_called_with(days=2)

@mock.patch(
"paying_for_college.management.commands."
Expand All @@ -125,4 +125,4 @@ def test_send_stale_notifications(self, mock_send):
"send_stale_notifications", "--add-email", "[email protected]"
)
self.assertEqual(mock_send.call_count, 2)
self.assertTrue(mock_send.called_with(add_email=["[email protected]"]))
mock_send.assert_called_with(add_email=["[email protected]"])
11 changes: 7 additions & 4 deletions cfgov/paying_for_college/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ def test_api_school_query(self, mock_requests):
mock_requests.return_value = mock_response
api_utils.api_school_query(123456, "school.name")
self.assertEqual(mock_requests.call_count, 1)
self.assertTrue(mock_requests.called_with((123456, "school.name")))
mock_requests.assert_called_with(
"https://api.data.gov/ed/collegescorecard/v1/schools.json?id=123456&api_key=&fields=school.name"
)

@unittest.skipUnless(
connection.vendor == "postgresql", "PostgreSQL-dependent"
Expand All @@ -338,8 +340,9 @@ def test_single_school_request(self, mock_get_data):
mock_get_data.return_value = self.mock_results.get("results")[0]
update_colleges.update(single_school=408039)
self.assertEqual(mock_get_data.call_count, 1)
self.assertTrue(
mock_get_data.called_with(408039, api_utils.build_field_string())
mock_get_data.assert_called_with(
f"https://api.data.gov/ed/collegescorecard/v1/schools.json?"
f"api_key=&id=408039&fields={api_utils.build_field_string()}"
)

@patch(
Expand Down Expand Up @@ -434,7 +437,7 @@ def test_get_scorecard_data(self, mock_requests):
mock_requests.return_value = mock_response
data = update_colleges.get_scorecard_data("example.com")
self.assertEqual(mock_requests.call_count, 1)
self.assertTrue(mock_requests.called_with("example.com"))
mock_requests.assert_called_with("example.com")
self.assertEqual(type(data), dict)

@patch(
Expand Down
68 changes: 32 additions & 36 deletions cfgov/paying_for_college/tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,40 @@ class SchoolSearchTest(TestCase):
fixtures = ["test_fixture.json", "test_school.json"]

@mock.patch.object(SchoolDocument, "search")
def test_school_autocomplete(self, mock_autocomplete):
term = "Kansas"
school = School.objects.get(pk=155317)
school.save()
mock_return = mock.Mock()
mock_return.text = school.primary_alias
mock_return.school_id = school.school_id
mock_return.city = school.city
mock_return.state = school.state
mock_return.zip5 = school.zip5
mock_return.nicknames = "Jayhawks"
# mock_autocomplete.return_value = [mock_return]
mock_queryset = mock.Mock()
mock_queryset.__iter__ = mock.Mock(return_value=iter([mock_return]))
mock_queryset.count.return_value = 1
# mock_autocomplete.return_value = mock_queryset
mock_autocomplete().query().filter().sort().__getitem__().execute.return_value = [ # noqa: E501
mock_return
def test_school_autocomplete(self, mock_search):
# Mock what Opensearch would return in response to a query.
mock_search.return_value.query.return_value.execute.return_value = [
SchoolDocument.from_opensearch(
{
"_source": SchoolDocument().prepare(
School.objects.get(pk=155317)
)
}
)
]
mock_count = mock.Mock(return_value=1)
mock_autocomplete().query().count = mock_count
mock_autocomplete().query().filter().sort().__getitem__().count = (
mock_count
)
url = "{}?q=Kansas".format(
reverse("paying_for_college:disclosures:school_search")
)
response = school_autocomplete(RequestFactory().get(url))
# output = json.loads(response.content)
# self.assertEqual(sorted(output[0].keys()), ["id", "schoolname"])

autocomplete = reverse("paying_for_college:disclosures:school_search")
request = RequestFactory().get(f"{autocomplete}?q=Kansas")
response = school_autocomplete(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(mock_autocomplete.call_count, 4)
self.assertTrue(mock_autocomplete.called_with(search_term=term))
self.assertTrue("Kansas" in mock_return.text)
self.assertEqual(155317, mock_return.school_id)
self.assertTrue("Jayhawks" in mock_return.nicknames)
self.assertTrue("Lawrence" in mock_return.city)
self.assertTrue("KS" in mock_return.state)

self.assertEqual(
json.loads(response.content),
[
{
"schoolname": "University of Kansas",
"id": 155317,
"city": "Lawrence",
"nicknames": "Jayhawks",
"state": "KS",
"zip5": "",
"url": reverse(
"paying_for_college:disclosures:school-json",
args=(155317,),
),
}
],
)

@mock.patch.object(SchoolDocument, "search")
def test_autocomplete_school_id(self, mock_search):
Expand Down
2 changes: 1 addition & 1 deletion cfgov/regulations3k/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_run_no_args(self, mock_inserter):
@mock.patch("regulations3k.scripts.insert_section_links.insert_links")
def test_run_with_args(self, mock_inserter):
run("1002")
self.assertTrue(mock_inserter.called_with, "1002")
mock_inserter.assert_called_with(reg="1002")

def test_insert_links_skips_processed_file(self):
regdown_with_link = (
Expand Down

0 comments on commit e2f151a

Please sign in to comment.