Skip to content

Commit 71f86ac

Browse files
committed
Improve unit test
1 parent 0d7ea40 commit 71f86ac

File tree

1 file changed

+53
-52
lines changed

1 file changed

+53
-52
lines changed

tests/unit/test_charm.py

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -140,69 +140,70 @@ def test_on_get_password(self):
140140
@patch("charm.Patroni.reload_patroni_configuration")
141141
@patch("charm.Patroni.render_patroni_yml_file")
142142
@patch("charm.PostgresqlOperatorCharm._set_secret")
143+
@patch("charm.PostgresqlOperatorCharm.postgresql")
143144
@patch("charm.Patroni.are_all_members_ready")
144145
@patch("charm.PostgresqlOperatorCharm._on_leader_elected")
145146
def test_on_set_password(
146147
self,
147148
_,
148149
_are_all_members_ready,
150+
_postgresql,
149151
_set_secret,
150152
_render_patroni_yml_file,
151153
_reload_patroni_configuration,
152154
):
153-
with patch.object(PostgresqlOperatorCharm, "postgresql", Mock()) as postgresql_mock:
154-
# Create a mock event.
155-
mock_event = MagicMock(params={})
156-
157-
# Set some values for the other mocks.
158-
_are_all_members_ready.side_effect = [False, True, True, True, True]
159-
postgresql_mock.update_user_password = PropertyMock(
160-
side_effect=[PostgreSQLUpdateUserPasswordError, None, None, None]
161-
)
155+
# Create a mock event.
156+
mock_event = MagicMock(params={})
162157

163-
# Test trying to set a password through a non leader unit.
164-
self.charm._on_set_password(mock_event)
165-
mock_event.fail.assert_called_once()
166-
_set_secret.assert_not_called()
167-
168-
# Test providing an invalid username.
169-
self.harness.set_leader()
170-
mock_event.reset_mock()
171-
mock_event.params["username"] = "user"
172-
self.charm._on_set_password(mock_event)
173-
mock_event.fail.assert_called_once()
174-
_set_secret.assert_not_called()
175-
176-
# Test without providing the username option but without all cluster members ready.
177-
mock_event.reset_mock()
178-
del mock_event.params["username"]
179-
self.charm._on_set_password(mock_event)
180-
mock_event.fail.assert_called_once()
181-
_set_secret.assert_not_called()
182-
183-
# Test for an error updating when updating the user password in the database.
184-
mock_event.reset_mock()
185-
self.charm._on_set_password(mock_event)
186-
mock_event.fail.assert_called_once()
187-
_set_secret.assert_not_called()
188-
189-
# Test without providing the username option.
190-
self.charm._on_set_password(mock_event)
191-
self.assertEqual(_set_secret.call_args_list[0][0][1], "operator-password")
192-
193-
# Also test providing the username option.
194-
_set_secret.reset_mock()
195-
mock_event.params["username"] = "replication"
196-
self.charm._on_set_password(mock_event)
197-
self.assertEqual(_set_secret.call_args_list[0][0][1], "replication-password")
198-
199-
# And test providing both the username and password options.
200-
_set_secret.reset_mock()
201-
mock_event.params["password"] = "replication-test-password"
202-
self.charm._on_set_password(mock_event)
203-
_set_secret.assert_called_once_with(
204-
"app", "replication-password", "replication-test-password"
205-
)
158+
# Set some values for the other mocks.
159+
_are_all_members_ready.side_effect = [False, True, True, True, True]
160+
_postgresql.update_user_password = PropertyMock(
161+
side_effect=[PostgreSQLUpdateUserPasswordError, None, None, None]
162+
)
163+
164+
# Test trying to set a password through a non leader unit.
165+
self.charm._on_set_password(mock_event)
166+
mock_event.fail.assert_called_once()
167+
_set_secret.assert_not_called()
168+
169+
# Test providing an invalid username.
170+
self.harness.set_leader()
171+
mock_event.reset_mock()
172+
mock_event.params["username"] = "user"
173+
self.charm._on_set_password(mock_event)
174+
mock_event.fail.assert_called_once()
175+
_set_secret.assert_not_called()
176+
177+
# Test without providing the username option but without all cluster members ready.
178+
mock_event.reset_mock()
179+
del mock_event.params["username"]
180+
self.charm._on_set_password(mock_event)
181+
mock_event.fail.assert_called_once()
182+
_set_secret.assert_not_called()
183+
184+
# Test for an error updating when updating the user password in the database.
185+
mock_event.reset_mock()
186+
self.charm._on_set_password(mock_event)
187+
mock_event.fail.assert_called_once()
188+
_set_secret.assert_not_called()
189+
190+
# Test without providing the username option.
191+
self.charm._on_set_password(mock_event)
192+
self.assertEqual(_set_secret.call_args_list[0][0][1], "operator-password")
193+
194+
# Also test providing the username option.
195+
_set_secret.reset_mock()
196+
mock_event.params["username"] = "replication"
197+
self.charm._on_set_password(mock_event)
198+
self.assertEqual(_set_secret.call_args_list[0][0][1], "replication-password")
199+
200+
# And test providing both the username and password options.
201+
_set_secret.reset_mock()
202+
mock_event.params["password"] = "replication-test-password"
203+
self.charm._on_set_password(mock_event)
204+
_set_secret.assert_called_once_with(
205+
"app", "replication-password", "replication-test-password"
206+
)
206207

207208
@patch_network_get(private_address="1.1.1.1")
208209
@patch("charm.Patroni.get_primary")

0 commit comments

Comments
 (0)