Skip to content

Commit

Permalink
some more meta autoload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Oct 10, 2023
1 parent a3c2cd2 commit 9a7a706
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 26 deletions.
Empty file removed tests/resources/__init__.py
Empty file.
18 changes: 0 additions & 18 deletions tests/resources/demo_decorate_class.py

This file was deleted.

80 changes: 80 additions & 0 deletions tests/test_charm_spec_autoload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import importlib
import sys
import tempfile
from pathlib import Path
from typing import Type

import pytest
import yaml
from ops.testing import CharmType

from scenario import Context, Relation, State
from scenario.context import ContextSetupError

CHARM = """
from ops import CharmBase
class MyCharm(CharmBase): pass
"""


def import_name(name: str, source: Path) -> Type[CharmType]:
pkg_path = str(source.parent)
sys.path.append(pkg_path)
charm = importlib.import_module("charm")
obj = getattr(charm, name)
sys.path.remove(pkg_path)
return obj


def create_tempcharm(
charm: str = CHARM, meta=None, actions=None, config=None, name: str = "MyCharm"
):
root = Path(tempfile.TemporaryDirectory().name)

src = root / "src"
src.mkdir(parents=True)
charmpy = src / "charm.py"
charmpy.write_text(charm)

if meta is not None:
(root / "metadata.yaml").write_text(yaml.safe_dump(meta))

if actions is not None:
(root / "actions.yaml").write_text(yaml.safe_dump(actions))

if config is not None:
(root / "config.yaml").write_text(yaml.safe_dump(config))

return import_name(name, charmpy)


def test_meta_autoload(tmp_path):
charm = create_tempcharm(meta={"name": "foo"})
ctx = Context(charm)
ctx.run("start", State())


def test_no_meta_raises(tmp_path):
charm = create_tempcharm()
with pytest.raises(ContextSetupError):
Context(charm)


def test_relations_ok(tmp_path):
charm = create_tempcharm(
meta={"name": "josh", "requires": {"cuddles": {"interface": "arms"}}}
)
# this would fail if there were no 'cuddles' relation defined in meta
Context(charm).run("start", State(relations=[Relation("cuddles")]))


def test_config_defaults(tmp_path):
charm = create_tempcharm(
meta={"name": "josh"},
config={"options": {"foo": {"type": "bool", "default": True}}},
)
# this would fail if there were no 'cuddles' relation defined in meta
with Context(charm).manager("start", State()) as mgr:
mgr.run()
assert mgr.charm.config["foo"] is True
15 changes: 7 additions & 8 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ package = wheel
wheel_build_env = .pkg
description = unit tests
deps =
coverage[toml]
jsonpatch
pytest
pytest-cov
setenv =
PYTHONPATH = {toxinidir}
commands =
coverage run \
--source={[vars]src_path} \
-m pytest -v --tb native --log-cli-level=INFO -s {posargs} {[vars]tst_path}
coverage html
pytest --cov-report html -v --tb native --log-cli-level=INFO -s {posargs} {[vars]tst_path}

[testenv:lint]
description = Format the code base to adhere to our styles, and complain about what we cannot do automatically.
Expand All @@ -48,7 +47,7 @@ deps =
coverage[toml]
isort
commands =
black --check tests scenario
black --check {[vars]tst_path} {[vars]src_path}
isort --check-only --profile black {[vars]tst_path}

[testenv:fmt]
Expand All @@ -58,5 +57,5 @@ deps =
black
isort
commands =
black tests scenario
isort --profile black tests scenario
black {[vars]tst_path} {[vars]src_path}
isort --profile black {[vars]tst_path} {[vars]src_path}

0 comments on commit 9a7a706

Please sign in to comment.