Skip to content

Commit

Permalink
Refactor charm state (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturo-seijas authored Nov 29, 2023
1 parent b62e655 commit a3e0867
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 31 deletions.
9 changes: 3 additions & 6 deletions src-docs/charm_state.py.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
# <kbd>module</kbd> `charm_state.py`
Module defining the CharmState class which represents the state of the SMTP Integrator charm.

**Global Variables**
---------------
- **KNOWN_CHARM_CONFIG**


---
Expand All @@ -21,7 +18,7 @@ Exception raised when a charm configuration is found to be invalid.

- <b>`msg`</b> (str): Explanation of the error.

<a href="../src/charm_state.py#L56"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L46"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `__init__`

Expand Down Expand Up @@ -59,7 +56,7 @@ Represents the state of the SMTP Integrator charm.
- <b>`transport_security`</b>: The security protocol to use for the outgoing SMTP relay.
- <b>`domain`</b>: The domain used by the sent emails from SMTP relay.

<a href="../src/charm_state.py#L89"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L79"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>function</kbd> `__init__`

Expand All @@ -80,7 +77,7 @@ Initialize a new instance of the CharmState class.

---

<a href="../src/charm_state.py#L104"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
<a href="../src/charm_state.py#L94"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>

### <kbd>classmethod</kbd> `from_charm`

Expand Down
13 changes: 1 addition & 12 deletions src/charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
from charms.smtp_integrator.v0 import smtp
from pydantic import BaseModel, Field, ValidationError

KNOWN_CHARM_CONFIG = (
"host",
"port",
"user",
"password",
"auth_type",
"transport_security",
"domain",
)


class SmtpIntegratorConfig(BaseModel):
"""Represent charm builtin configuration values.
Expand Down Expand Up @@ -114,10 +104,9 @@ def from_charm(cls, charm: "ops.CharmBase") -> "CharmState":
Raises:
CharmConfigInvalidError: if the charm configuration is invalid.
"""
config = {k: v for k, v in charm.config.items() if k in KNOWN_CHARM_CONFIG}
try:
# Incompatible with pydantic.AnyHttpUrl
valid_config = SmtpIntegratorConfig(**config) # type: ignore
valid_config = SmtpIntegratorConfig(**dict(charm.config.items())) # type: ignore
except ValidationError as exc:
error_fields = set(
itertools.chain.from_iterable(error["loc"] for error in exc.errors())
Expand Down
14 changes: 1 addition & 13 deletions tests/unit/test_charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,8 @@
from unittest.mock import MagicMock

import pytest
import yaml

from charm_state import KNOWN_CHARM_CONFIG, CharmConfigInvalidError, CharmState


def test_known_charm_config():
"""
arrange: none
act: none
assert: KNOWN_CHARM_CONFIG in the consts module matches the content of config.yaml file.
"""
with open("config.yaml", encoding="utf-8") as config_file:
config = yaml.safe_load(config_file)
assert sorted(config["options"].keys()) == sorted(KNOWN_CHARM_CONFIG)
from charm_state import CharmConfigInvalidError, CharmState


def test_charm_state_from_charm():
Expand Down

0 comments on commit a3e0867

Please sign in to comment.