From 9a7a706dd712e7b468e4d602fae13ebe4a9183d1 Mon Sep 17 00:00:00 2001 From: Pietro Pasotti Date: Tue, 10 Oct 2023 16:20:09 +0200 Subject: [PATCH] some more meta autoload tests --- tests/resources/__init__.py | 0 tests/resources/demo_decorate_class.py | 18 ------ tests/test_charm_spec_autoload.py | 80 ++++++++++++++++++++++++++ tox.ini | 15 +++-- 4 files changed, 87 insertions(+), 26 deletions(-) delete mode 100644 tests/resources/__init__.py delete mode 100644 tests/resources/demo_decorate_class.py create mode 100644 tests/test_charm_spec_autoload.py diff --git a/tests/resources/__init__.py b/tests/resources/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/resources/demo_decorate_class.py b/tests/resources/demo_decorate_class.py deleted file mode 100644 index 56e329f8..00000000 --- a/tests/resources/demo_decorate_class.py +++ /dev/null @@ -1,18 +0,0 @@ -class MyDemoClass: - _foo: int = 0 - - def get_foo(self, *args, **kwargs): - return self._foo - - def set_foo(self, foo): - self._foo = foo - - def unpatched(self, *args, **kwargs): - return self._foo - - -class MyOtherClass: - _foo: int = 0 - - def foo(self, *args, **kwargs): - return self._foo diff --git a/tests/test_charm_spec_autoload.py b/tests/test_charm_spec_autoload.py new file mode 100644 index 00000000..536fdab2 --- /dev/null +++ b/tests/test_charm_spec_autoload.py @@ -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 diff --git a/tox.ini b/tox.ini index 23a1d22f..9e052fb5 100644 --- a/tox.ini +++ b/tox.ini @@ -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. @@ -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] @@ -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}