-
Notifications
You must be signed in to change notification settings - Fork 60
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
Hironori Yamamoto
committed
Nov 27, 2024
1 parent
5138294
commit 4d5e42e
Showing
3 changed files
with
116 additions
and
1 deletion.
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
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,89 @@ | ||
import json | ||
import tempfile | ||
from dataclasses import asdict, dataclass | ||
|
||
import luigi | ||
import pytest | ||
from luigi.cmdline_parser import CmdlineParser | ||
from mypy import api | ||
|
||
from gokart import SerializableObjectParameter, TaskOnKart, build | ||
from test.config import PYPROJECT_TOML | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Config: | ||
foo: int | ||
bar: str | ||
|
||
def serialize(self) -> str: | ||
# dict is ordered in Python 3.7+ | ||
return json.dumps(asdict(self)) | ||
|
||
@classmethod | ||
def deserialize(cls, s: str) -> 'Config': | ||
return cls(**json.loads(s)) | ||
|
||
|
||
class WithOutDefault(TaskOnKart): | ||
task_namespace = __name__ | ||
config: Config = SerializableObjectParameter(object_type=Config) | ||
|
||
def run(self): | ||
self.dump(self.config) | ||
|
||
|
||
class WithDefault(TaskOnKart): | ||
task_namespace = __name__ | ||
config: Config = SerializableObjectParameter(object_type=Config, default=Config(foo=1, bar='bar')) | ||
|
||
def run(self): | ||
self.dump(self.config) | ||
|
||
|
||
class TestSerializableObjectParameter: | ||
def test_default(self): | ||
with CmdlineParser.global_instance([f'{__name__}.WithDefault']) as cp: | ||
assert cp.get_task_obj().config == Config(foo=1, bar='bar') | ||
|
||
def test_parse_param(self): | ||
with CmdlineParser.global_instance([f'{__name__}.WithOutDefault', '--config', '{"foo": 100, "bar": "val"}']) as cp: | ||
assert cp.get_task_obj().config == Config(foo=100, bar='val') | ||
|
||
def test_missing_parameter(self): | ||
with pytest.raises(luigi.parameter.MissingParameterException): | ||
with CmdlineParser.global_instance([f'{__name__}.WithOutDefault']) as cp: | ||
cp.get_task_obj() | ||
|
||
def test_value_error(self): | ||
with pytest.raises(ValueError): | ||
with CmdlineParser.global_instance([f'{__name__}.WithOutDefault', '--config', 'Foo']) as cp: | ||
cp.get_task_obj() | ||
|
||
def test_expected_one_argment_error(self): | ||
with pytest.raises(SystemExit): | ||
with CmdlineParser.global_instance([f'{__name__}.WithOutDefault', '--config']) as cp: | ||
cp.get_task_obj() | ||
|
||
def test_build(self): | ||
WithOutDefault.clear_instance_cache() | ||
config = Config(foo=100, bar='val') | ||
actual = build(WithOutDefault(config=config)) | ||
assert actual == config | ||
|
||
def test_mypy(self): | ||
"""check invalid object cannot used for SerializableObjectParameter""" | ||
|
||
test_code = """ | ||
import gokart | ||
class InvalidClass: | ||
... | ||
gokart.SerializableObjectParameter(object_type=InvalidClass) | ||
""" | ||
with tempfile.NamedTemporaryFile(suffix='.py') as test_file: | ||
test_file.write(test_code.encode('utf-8')) | ||
test_file.flush() | ||
result = api.run(['--no-incremental', '--cache-dir=/dev/null', '--config-file', str(PYPROJECT_TOML), test_file.name]) | ||
assert 'Value of type variable "S" of "SerializableObjectParameter" cannot be "InvalidClass" [type-var]' in result[0] |