Skip to content

Commit

Permalink
Use relative imports for everything scenario.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Oct 10, 2024
1 parent 77ff698 commit b542ef6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 59 deletions.
6 changes: 3 additions & 3 deletions testing/src/scenario/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def test_base():
assert out.unit_status == UnknownStatus()
"""

from scenario.context import CharmEvents, Context, Manager
from scenario.errors import StateValidationError # For backwards compatibility.
from .context import CharmEvents, Context, Manager
from .errors import StateValidationError # For backwards compatibility.
from ops._private.harness import ActionFailed # For backwards compatibility.
from scenario.state import (
from .state import (
ActiveStatus,
Address,
AnyJson,
Expand Down
8 changes: 4 additions & 4 deletions testing/src/scenario/_consistency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
Union,
)

from scenario.errors import InconsistentScenarioError
from scenario.runtime import logger as scenario_logger
from scenario.state import (
from .errors import InconsistentScenarioError
from .runtime import logger as scenario_logger
from .state import (
CharmType,
PeerRelation,
SubordinateRelation,
Expand All @@ -50,7 +50,7 @@
)

if TYPE_CHECKING: # pragma: no cover
from scenario.state import State, _Event
from .state import State, _Event

logger = scenario_logger.getChild("consistency_checker")

Expand Down
12 changes: 6 additions & 6 deletions testing/src/scenario/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import ops
from ops._private.harness import ActionFailed

from scenario.errors import (
from .errors import (
AlreadyEmittedError,
ContextSetupError,
MetadataNotFoundError,
)
from scenario.logger import logger as scenario_logger
from scenario.runtime import Runtime
from scenario.state import (
from .logger import logger as scenario_logger
from .runtime import Runtime
from .state import (
CharmType,
CheckInfo,
Container,
Expand All @@ -50,8 +50,8 @@
except ImportError:
from ops.testing import ExecArgs # type: ignore

from scenario.ops_main_mock import Ops
from scenario.state import (
from .ops_main_mock import Ops
from .state import (
AnyJson,
CharmType,
JujuLogLine,
Expand Down
41 changes: 21 additions & 20 deletions testing/src/scenario/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@
cast,
)

from ops import JujuVersion, pebble
from ops import (
JujuVersion,
pebble,
SecretInfo,
SecretNotFoundError,
RelationNotFoundError,
SecretRotate,
ModelError,
)

# TODO: Remove the line below after the ops compatibility code is removed.
# pyright: reportUnnecessaryTypeIgnoreComment=false
Expand All @@ -31,23 +39,16 @@
except ImportError:
from ops.testing import ExecArgs, _TestingPebbleClient # type: ignore

from ops.model import CloudSpec as CloudSpec_Ops
from ops.model import ModelError
from ops.model import Port as Port_Ops
from ops.model import RelationNotFoundError
from ops.model import Secret as Secret_Ops # lol
from ops.model import (
SecretInfo,
SecretNotFoundError,
SecretRotate,
_format_action_result_dict,
_ModelBackend,
)
from ops import CloudSpec as CloudSpec_Ops
from ops import Port as Port_Ops
from ops import Secret as Secret_Ops # lol
from ops.model import _format_action_result_dict, _ModelBackend

from ops.pebble import Client, ExecError

from scenario.errors import ActionMissingFromContextError
from scenario.logger import logger as scenario_logger
from scenario.state import (
from .errors import ActionMissingFromContextError
from .logger import logger as scenario_logger
from .state import (
CharmType,
JujuLogLine,
Mount,
Expand All @@ -64,9 +65,9 @@
)

if TYPE_CHECKING: # pragma: no cover
from scenario.context import Context
from scenario.state import Container as ContainerSpec
from scenario.state import Exec, Secret, State, _CharmSpec, _Event
from .context import Context
from .state import Container as ContainerSpec
from .state import Exec, Secret, State, _CharmSpec, _Event

logger = scenario_logger.getChild("mocking")

Expand Down Expand Up @@ -402,7 +403,7 @@ def secret_add(
rotate: Optional[SecretRotate] = None,
owner: Optional[Literal["unit", "app"]] = None,
) -> str:
from scenario.state import Secret
from .state import Secret

secret = Secret(
content,
Expand Down
8 changes: 4 additions & 4 deletions testing/src/scenario/ops_main_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
from ops.charm import CharmMeta
from ops.log import setup_root_logging

from scenario.errors import BadOwnerPath, NoObserverError
from .errors import BadOwnerPath, NoObserverError

if TYPE_CHECKING: # pragma: no cover
from scenario.context import Context
from scenario.state import CharmType, State, _CharmSpec, _Event
from .context import Context
from .state import CharmType, State, _CharmSpec, _Event

# pyright: reportPrivateUsage=false
# TODO: Remove the line below after the ops compatibility code is removed.
Expand Down Expand Up @@ -110,7 +110,7 @@ def setup_framework(
context: "Context",
charm_spec: "_CharmSpec[CharmType]",
):
from scenario.mocking import _MockModelBackend
from .mocking import _MockModelBackend

model_backend = _MockModelBackend(
state=state,
Expand Down
23 changes: 12 additions & 11 deletions testing/src/scenario/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@
)

import yaml
from ops import CollectStatusEvent, pebble
from ops.framework import (
from ops import (
CollectStatusEvent,
pebble,
CommitEvent,
EventBase,
Framework,
Handle,
NoTypeError,
PreCommitEvent,
_event_regex,
)
from ops.storage import NoSnapshotError, SQLiteStorage
from ops.framework import _event_regex
from ops._private.harness import ActionFailed

from scenario.errors import NoObserverError, UncaughtCharmError
from scenario.logger import logger as scenario_logger
from scenario.state import (
from .errors import NoObserverError, UncaughtCharmError
from .logger import logger as scenario_logger
from .state import (
DeferredEvent,
PeerRelation,
Relation,
Expand All @@ -49,8 +50,8 @@
)

if TYPE_CHECKING: # pragma: no cover
from scenario.context import Context
from scenario.state import CharmType, State, _CharmSpec, _Event
from .context import Context
from .state import CharmType, State, _CharmSpec, _Event

logger = scenario_logger.getChild("runtime")
STORED_STATE_REGEX = re.compile(
Expand Down Expand Up @@ -435,7 +436,7 @@ def exec(
# todo consider forking out a real subprocess and do the mocking by
# mocking hook tool executables

from scenario._consistency_checker import check_consistency # avoid cycles
from ._consistency_checker import check_consistency # avoid cycles

check_consistency(state, event, self._charm_spec, self._juju_version)

Expand All @@ -459,7 +460,7 @@ def exec(
os.environ.update(env)

logger.info(" - Entering ops.main (mocked).")
from scenario.ops_main_mock import Ops # noqa: F811
from .ops_main_mock import Ops # noqa: F811

try:
ops = Ops(
Expand Down Expand Up @@ -513,7 +514,7 @@ def _capture_events(
Arguments exposed so that you can define your own fixtures if you want to.
Example::
>>> from ops.charm import StartEvent
>>> from ops import StartEvent
>>> from scenario import Event, State
>>> from charm import MyCustomEvent, MyCharm # noqa
>>>
Expand Down
17 changes: 6 additions & 11 deletions testing/src/scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@

import ops
import yaml
from ops import pebble
from ops.charm import CharmBase, CharmEvents
from ops.model import CloudCredential as CloudCredential_Ops
from ops.model import CloudSpec as CloudSpec_Ops
from ops.model import SecretRotate, StatusBase
from ops import pebble, CharmBase, CharmEvents, SecretRotate, StatusBase
from ops import CloudCredential as CloudCredential_Ops
from ops import CloudSpec as CloudSpec_Ops

from scenario.errors import MetadataNotFoundError, StateValidationError
from scenario.logger import logger as scenario_logger
from .errors import MetadataNotFoundError, StateValidationError
from .logger import logger as scenario_logger

if TYPE_CHECKING: # pragma: no cover
from scenario import Context
from . import Context

AnyJson = Union[str, bool, Dict[str, "AnyJson"], int, float, List["AnyJson"]]
RawSecretRevisionContents = RawDataBagContents = Dict[str, str]
Expand Down Expand Up @@ -678,9 +676,6 @@ def _get_databag_for_remote(self, unit_id: "UnitID") -> "RawDataBagContents":


def _random_model_name():
import random
import string

space = string.ascii_letters + string.digits
return "".join(random.choice(space) for _ in range(20))

Expand Down

0 comments on commit b542ef6

Please sign in to comment.