-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6426fea
commit 604d9c3
Showing
4 changed files
with
107 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
class FastCSException(Exception): | ||
pass | ||
|
||
|
||
class LaunchError(FastCSException): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import json | ||
from dataclasses import dataclass | ||
|
||
import pytest | ||
from pydantic import create_model | ||
from typer.testing import CliRunner | ||
|
||
from fastcs.controller import Controller | ||
from fastcs.exceptions import LaunchError | ||
from fastcs.main import _launch, launch | ||
from fastcs.transport.epics.options import EpicsOptions | ||
from fastcs.transport.tango.options import TangoOptions | ||
|
||
|
||
@dataclass | ||
class SomeConfig: | ||
name: str | ||
|
||
|
||
class SingleArg(Controller): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
|
||
class NotHinted(Controller): | ||
def __init__(self, arg): | ||
super().__init__() | ||
|
||
|
||
class IsHinted(Controller): | ||
def __init__(self, arg: SomeConfig): | ||
super().__init__() | ||
|
||
|
||
class ManyArgs(Controller): | ||
def __init__(self, arg: SomeConfig, too_many): | ||
super().__init__() | ||
|
||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_is_hinted_schema(): | ||
target_model = create_model( | ||
"IsHinted", | ||
controller=(SomeConfig, ...), | ||
transport=(EpicsOptions | TangoOptions, ...), | ||
__config__={"extra": "forbid"}, | ||
) | ||
target_dict = target_model.model_json_schema() | ||
|
||
app = _launch(IsHinted) | ||
result = runner.invoke(app, ["schema"]) | ||
assert result.exit_code == 0 | ||
result_dict = json.loads(result.stdout) | ||
|
||
assert result_dict == target_dict | ||
|
||
|
||
def test_not_hinted_schema(): | ||
error = ( | ||
"Expected typehinting in 'NotHinted.__init__' but received " | ||
"(self, arg). Add a typehint for `arg`." | ||
) | ||
|
||
with pytest.raises(LaunchError) as exc_info: | ||
launch(NotHinted) | ||
assert str(exc_info.value) == error | ||
|
||
|
||
def test_over_defined_schema(): | ||
error = ( | ||
"" | ||
"Expected no more than 2 arguments for 'ManyArgs.__init__' " | ||
"but received 3 as `(self, arg: test_main.SomeConfig, too_many)`" | ||
) | ||
|
||
with pytest.raises(LaunchError) as exc_info: | ||
launch(ManyArgs) | ||
assert str(exc_info.value) == error |