Skip to content

Commit e53aa35

Browse files
committed
Fix unit tests
Signed-off-by: Marcelo Henrique Neppel <marcelo.neppel@canonical.com>
1 parent 7fe482a commit e53aa35

File tree

6 files changed

+12
-29
lines changed

6 files changed

+12
-29
lines changed

src/charm.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
from typing import Literal, get_args
2121
from urllib.parse import urlparse
2222

23-
import charm_refresh
24-
2523
from authorisation_rules_observer import (
2624
AuthorisationRulesChangeCharmEvents,
2725
AuthorisationRulesObserver,
2826
)
29-
from refresh import PostgreSQLRefresh
3027

3128
# First platform-specific import, will fail on wrong architecture
3229
try:
@@ -43,6 +40,7 @@
4340
main(WrongArchitectureWarningCharm, use_juju_for_storage=True)
4441
raise
4542

43+
import charm_refresh
4644
from charms.data_platform_libs.v0.data_interfaces import DataPeerData, DataPeerUnitData
4745
from charms.data_platform_libs.v1.data_models import TypedCharmBase
4846
from charms.grafana_k8s.v0.grafana_dashboard import GrafanaDashboardProvider
@@ -146,6 +144,7 @@
146144
)
147145
from ldap import PostgreSQLLDAP
148146
from patroni import NotReadyError, Patroni, SwitchoverFailedError, SwitchoverNotSyncError
147+
from refresh import PostgreSQLRefresh
149148
from relations.async_replication import (
150149
REPLICATION_CONSUMER_RELATION,
151150
REPLICATION_OFFER_RELATION,

tests/unit/conftest.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,22 @@ def disable_charm_tracing():
1919
yield
2020

2121

22-
# class _MockRefresh:
23-
# in_progress = False
24-
# next_unit_allowed_to_refresh = True
25-
# workload_allowed_to_start = True
26-
# app_status_higher_priority = None
27-
# unit_status_higher_priority = None
28-
#
29-
# def __init__(self, _, /):
30-
# pass
31-
#
32-
# def unit_status_lower_priority(self, *, workload_is_running=True):
33-
# return None
34-
35-
36-
# @pytest.fixture(autouse=True)
37-
# def patch(monkeypatch):
38-
# monkeypatch.setattr("charm_refresh.Kubernetes", _MockRefresh)
39-
40-
4122
@pytest.fixture(autouse=True)
4223
def mock_refresh():
4324
"""Fixture to shunt refresh logic and events."""
4425
refresh_mock = Mock()
4526
refresh_mock.in_progress = False
27+
refresh_mock.app_status_higher_priority = None
28+
refresh_mock.app_status_lower_priority.return_value = None
4629
refresh_mock.unit_status_higher_priority = None
4730
refresh_mock.unit_status_lower_priority.return_value = None
4831
refresh_mock.next_unit_allowed_to_refresh = True
4932
refresh_mock.workload_allowed_to_start = True
5033

5134
# Mock the _RefreshVersions class to avoid KeyError when charm key is missing
5235
versions_mock = Mock()
53-
versions_mock.charm = "v1/4.0.0"
54-
versions_mock.workload = "4.0.0"
36+
versions_mock.charm = "v1/16.0.0"
37+
versions_mock.workload = "16.10"
5538

5639
with (
5740
patch("charm_refresh.Kubernetes", Mock(return_value=refresh_mock)),

tests/unit/test_arch_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_on_module_not_found_error(monkeypatch):
2525
# If psycopg2 not there, charm should check architecture
2626
monkeypatch.delitem(sys.modules, "psycopg2", raising=False)
2727
monkeypatch.delitem(sys.modules, "charm", raising=False)
28+
monkeypatch.delitem(sys.modules, "charm_refresh", raising=False)
2829
monkeypatch.setattr(builtins, "__import__", psycopg2_not_found)
2930
with pytest.raises(ModuleNotFoundError):
3031
import charm

tests/unit/test_async_replication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_on_async_relation_broken(harness, is_leader, relation_name):
4949
with (
5050
patch("charm.PostgresqlOperatorCharm.update_config") as _update_config,
5151
patch(
52-
"relations.async_replication.PostgreSQLAsyncReplication._set_app_status"
52+
"relations.async_replication.PostgreSQLAsyncReplication.set_app_status"
5353
) as _set_app_status,
5454
patch("charm.Patroni.get_standby_leader") as _get_standby_leader,
5555
patch(

tests/unit/test_tls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def harness():
1717
harness = Harness(PostgresqlOperatorCharm)
1818

1919
# Set up the initial relation and hooks.
20-
peer_rel_id = harness.add_relation(PEER, "postgresql")
21-
harness.add_relation_unit(peer_rel_id, "postgresql/0")
20+
peer_rel_id = harness.add_relation(PEER, "postgresql-k8s")
21+
harness.add_relation_unit(peer_rel_id, "postgresql-k8s/0")
2222
harness.begin()
2323
yield harness
2424
harness.cleanup()

tests/unit/test_tls_transfer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def harness():
1818
harness = Harness(PostgresqlOperatorCharm)
1919

2020
# Set up the initial relation and hooks.
21-
peer_rel_id = harness.add_relation(PEER, "postgresql")
22-
harness.add_relation_unit(peer_rel_id, "postgresql/0")
21+
peer_rel_id = harness.add_relation(PEER, "postgresql-k8s")
22+
harness.add_relation_unit(peer_rel_id, "postgresql-k8s/0")
2323
harness.begin()
2424
yield harness
2525
harness.cleanup()

0 commit comments

Comments
 (0)