Skip to content

Commit

Permalink
Fix mock usage
Browse files Browse the repository at this point in the history
Some tests used `assert mock_object.called_once_with()`. The return
value of `called_once_with()` is a mock object which always evaluates to
true which means it always passes the assertion.
  • Loading branch information
cklein committed Dec 28, 2022
1 parent 3c1741d commit 651d1a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
8 changes: 5 additions & 3 deletions tests/autoscaling/test_autoscaling_service_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,13 +1357,15 @@ def test_get_new_instance_count():
num_healthy_instances=4,
persist_data=False,
)
assert mock_decision_policy.called_with(
mock_decision_policy.assert_called_with(
error=0.1,
min_instances=mock_marathon_service_config.get_min_instances(),
max_instances=mock_marathon_service_config.get_max_instances(),
current_instances=4,
zookeeper_path=mock_compose_autoscaling_zookeeper_root.return_value,
decision_policy="mock_dp",
utilization=0.7,
mock_param=2,
num_healthy_instances=4,
persist_data=False,
)
mock_marathon_service_config.limit_instance_count.assert_called_with(5)
assert ret == mock_marathon_service_config.limit_instance_count.return_value
Expand Down
2 changes: 1 addition & 1 deletion tests/test_marathon_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ def test_app_has_tasks_more(self):
def test_add_leading_slash(self):
fake_client = mock.Mock(list_tasks=mock.Mock(return_value=[{}, {}, {}, {}]))
marathon_tools.app_has_tasks(fake_client, "fake_app", 4)
assert fake_client.list_tasks.called_with("/fake_app")
fake_client.list_tasks.assert_called_with(app_id="/fake_app")

def test_get_config_hash(self):
test_input = {"foo": "bar"}
Expand Down
31 changes: 15 additions & 16 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import mock
import pytest
from freezegun import freeze_time
from pytest import raises

from paasta_tools import utils
Expand Down Expand Up @@ -219,17 +220,14 @@ def test_format_audit_log_line_with_details():


try:
from utils import ScribeLogWriter
from paasta_tools.utils import ScribeLogWriter

def test_ScribeLogWriter_log_raise_on_unknown_level():
with raises(utils.NoSuchLogLevel):
ScribeLogWriter().log("fake_service", "fake_line", "build", "BOGUS_LEVEL")

@freeze_time("2022-12-28")
def test_ScribeLogWriter_logs_audit_messages():
slw = ScribeLogWriter(scribe_disable=True)
mock_clog = mock.Mock()
slw.clog = mock_clog

user = "fake_user"
host = "fake_hostname"
action = "mark-for-deployment"
Expand All @@ -249,18 +247,19 @@ def test_ScribeLogWriter_logs_audit_messages():
instance=instance,
)

slw.log_audit(
user=user,
host=host,
action=action,
action_details=action_details,
service=service,
cluster=cluster,
instance=instance,
)
with mock.patch("paasta_tools.utils.clog") as mock_clog:
slw = ScribeLogWriter(scribe_disable=True)
slw.log_audit(
user=user,
host=host,
action=action,
action_details=action_details,
service=service,
cluster=cluster,
instance=instance,
)

assert mock_clog.log_line.call_count == 1
assert mock_clog.log_line.called_once_with(expected_log_name, expected_line)
mock_clog.log_line.assert_called_once_with(expected_log_name, expected_line)

except ImportError:
warnings.warn("ScribeLogWriter is unavailable")
Expand Down

0 comments on commit 651d1a3

Please sign in to comment.