Skip to content

Commit

Permalink
Add missing unit tests (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturo-seijas authored Nov 21, 2023
1 parent 3546cc1 commit 5c8ab93
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
14 changes: 4 additions & 10 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_unconfigured_charm_reaches_blocked_status():
def test_misconfigured_port_charm_reaches_blocked_status():
"""
arrange: set up a charm.
act: trigger a configuration change missing required configs.
act: trigger a configuration change with an invalid port.
assert: the charm reaches BlockedStatus.
"""
harness = Harness(SmtpIntegratorOperatorCharm)
Expand All @@ -42,7 +42,7 @@ def test_misconfigured_port_charm_reaches_blocked_status():
def test_misconfigured_auth_type_charm_reaches_blocked_status():
"""
arrange: set up a charm.
act: trigger a configuration change missing required configs.
act: trigger a configuration change with an invalid auth type.
assert: the charm reaches BlockedStatus.
"""
harness = Harness(SmtpIntegratorOperatorCharm)
Expand All @@ -60,15 +60,15 @@ def test_misconfigured_auth_type_charm_reaches_blocked_status():
def test_misconfigured_transport_security_charm_reaches_blocked_status():
"""
arrange: set up a charm.
act: trigger a configuration change missing required configs.
act: trigger a configuration change with an invalid transport security.
assert: the charm reaches BlockedStatus.
"""
harness = Harness(SmtpIntegratorOperatorCharm)
harness.update_config(
{
"host": "smtp.example",
"port": 25,
"auth_type": "nonexisting",
"transport_security": "nonexisting",
}
)
harness.begin()
Expand Down Expand Up @@ -113,7 +113,6 @@ def test_legacy_relation_joined_when_leader():
)
harness.begin()
harness.charm.on.config_changed.emit()
assert harness.model.unit.status == ops.ActiveStatus()
harness.add_relation("smtp-legacy", "example")
data = harness.model.get_relation("smtp-legacy").data[harness.model.app]
assert data["host"] == host
Expand Down Expand Up @@ -144,7 +143,6 @@ def test_relation_joined_when_leader_and_secrets(mock_juju_env):
)
harness.begin()
harness.charm.on.config_changed.emit()
assert harness.model.unit.status == ops.ActiveStatus()
harness.add_relation("smtp", "example")
data = harness.model.get_relation("smtp").data[harness.model.app]
assert data["host"] == host
Expand Down Expand Up @@ -175,7 +173,6 @@ def test_relation_joined_when_leader_and_no_secrets(mock_juju_env):
)
harness.begin()
harness.charm.on.config_changed.emit()
assert harness.model.unit.status == ops.ActiveStatus()
harness.add_relation("smtp", "example")
data = harness.model.get_relation("smtp").data[harness.model.app]
assert data == {}
Expand All @@ -201,7 +198,6 @@ def test_relation_joined_when_leader_and_no_password(mock_juju_env):
)
harness.begin()
harness.charm.on.config_changed.emit()
assert harness.model.unit.status == ops.ActiveStatus()
harness.add_relation("smtp", "example")
data = harness.model.get_relation("smtp").data[harness.model.app]
assert data["host"] == host
Expand All @@ -228,7 +224,6 @@ def test_legacy_relation_joined_when_not_leader():
)
harness.begin()
harness.charm.on.config_changed.emit()
assert harness.model.unit.status == ops.ActiveStatus()
harness.add_relation("smtp-legacy", "example")
data = harness.model.get_relation("smtp-legacy").data[harness.model.app]
assert data == {}
Expand All @@ -254,7 +249,6 @@ def test_relation_joined_when_not_leader(mock_juju_env):
)
harness.begin()
harness.charm.on.config_changed.emit()
assert harness.model.unit.status == ops.ActiveStatus()
harness.add_relation("smtp", "example")
data = harness.model.get_relation("smtp").data[harness.model.app]
assert data == {}
73 changes: 46 additions & 27 deletions tests/unit/test_library_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,45 @@ def _record_event(self, event: ops.EventBase) -> None:
self.events.append(event)


def test_smtp_provider_charm_relations():
"""
arrange: instantiate a SmtpProviderCharm and add a relation.
act: obtain the relations.
assert: the relations retrieved match the existing relations.
"""
harness = Harness(SmtpProviderCharm, meta=PROVIDER_METADATA)
harness.begin()
harness.set_leader(True)
harness.add_relation("smtp-legacy", "smtp-provider")
assert len(harness.charm.smtp_legacy.relations) == 1
assert len(harness.charm.smtp.relations) == 0


def test_smtp_provider_update_relation_data():
"""
arrange: instantiate a SmtpProviderCharm object and add a relation.
act: update the relation data.
assert: the relation data is updated.
"""
harness = Harness(SmtpProviderCharm, meta=PROVIDER_METADATA)
harness.begin()
harness.set_leader(True)
harness.add_relation("smtp-legacy", "smtp-provider")
relation = harness.model.get_relation("smtp-legacy")
smtp_data = smtp.SmtpRelationData(
host="example.smtp",
port=25,
auth_type="plain",
transport_security="tls",
)
harness.charm.smtp_legacy.update_relation_data(relation, smtp_data)
data = relation.data[harness.model.app]
assert data["host"] == smtp_data.host
assert data["port"] == str(smtp_data.port)
assert data["auth_type"] == smtp_data.auth_type
assert data["transport_security"] == smtp_data.transport_security


def test_smtp_relation_data_to_relation_data():
"""
arrange: instantiate a SmtpRelationData object.
Expand Down Expand Up @@ -106,7 +145,7 @@ def test_smtp_relation_data_to_relation_data():
assert relation_data == expected_relation_data


def test_legacy_requirer_charm_does_not_emit_event_id_no_data():
def test_legacy_requirer_charm_does_not_emit_event_id_when_no_data():
"""
arrange: set up a charm with no relation data to be populated.
act: trigger a relation changed event.
Expand All @@ -115,14 +154,13 @@ def test_legacy_requirer_charm_does_not_emit_event_id_no_data():
harness = Harness(SmtpRequirerCharm, meta=REQUIRER_METADATA)
harness.begin()
harness.set_leader(True)
relation_id = harness.add_relation("smtp-legacy", "smtp-provider")
harness.add_relation_unit(relation_id, "smtp-provider/0")
harness.add_relation("smtp-legacy", "smtp-provider")
relation = harness.charm.framework.model.get_relation("smtp-legacy", 0)
harness.charm.on.smtp_legacy_relation_changed.emit(relation)
assert len(harness.charm.events) == 0


def test_requirer_charm_does_not_emit_event_id_no_data():
def test_requirer_charm_does_not_emit_event_id_when_no_data():
"""
arrange: set up a charm with no relation data to be populated.
act: trigger a relation changed event.
Expand All @@ -131,8 +169,7 @@ def test_requirer_charm_does_not_emit_event_id_no_data():
harness = Harness(SmtpRequirerCharm, meta=REQUIRER_METADATA)
harness.begin()
harness.set_leader(True)
relation_id = harness.add_relation("smtp", "smtp-provider")
harness.add_relation_unit(relation_id, "smtp-provider/0")
harness.add_relation("smtp", "smtp-provider")
relation = harness.charm.framework.model.get_relation("smtp", 0)
harness.charm.on.smtp_legacy_relation_changed.emit(relation)
assert len(harness.charm.events) == 0
Expand All @@ -158,13 +195,7 @@ def test_legacy_requirer_charm_with_valid_relation_data_emits_event(is_leader):
harness = Harness(SmtpRequirerCharm, meta=REQUIRER_METADATA)
harness.begin()
harness.set_leader(is_leader)
relation_id = harness.add_relation("smtp-legacy", "smtp-provider")
harness.add_relation_unit(relation_id, "smtp-provider/0")
harness.update_relation_data(
relation_id,
"smtp-provider",
relation_data,
)
harness.add_relation("smtp-legacy", "smtp-provider", app_data=relation_data)

assert len(harness.charm.events) == 1
assert harness.charm.events[0].host == relation_data["host"]
Expand Down Expand Up @@ -205,13 +236,7 @@ def test_requirer_charm_with_valid_relation_data_emits_event(is_leader):
harness = Harness(SmtpRequirerCharm, meta=REQUIRER_METADATA)
harness.begin()
harness.set_leader(is_leader)
relation_id = harness.add_relation("smtp", "smtp-provider")
harness.add_relation_unit(relation_id, "smtp-provider/0")
harness.update_relation_data(
relation_id,
"smtp-provider",
relation_data,
)
harness.add_relation("smtp", "smtp-provider", app_data=relation_data)

assert len(harness.charm.events) == 1
assert harness.charm.events[0].host == relation_data["host"]
Expand Down Expand Up @@ -242,12 +267,6 @@ def test_requirer_charm_with_invalid_relation_data_doesnt_emit_event(is_leader):
harness = Harness(SmtpRequirerCharm, meta=REQUIRER_METADATA)
harness.begin()
harness.set_leader(is_leader)
relation_id = harness.add_relation("smtp-legacy", "smtp-provider")
harness.add_relation_unit(relation_id, "smtp-provider/0")
harness.update_relation_data(
relation_id,
"smtp-provider",
relation_data,
)
harness.add_relation("smtp-legacy", "smtp-provider", app_data=relation_data)

assert len(harness.charm.events) == 0

0 comments on commit 5c8ab93

Please sign in to comment.