-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add function types tests #161
- Loading branch information
Showing
7 changed files
with
258 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import pytest | ||
|
||
from tackle import tackle | ||
from tackle import exceptions | ||
from tackle.parser import parse_function_type | ||
from tackle.models import Context | ||
|
||
|
||
@pytest.fixture() | ||
def context_fixture(change_dir): | ||
return Context( | ||
hook_dirs=['.hooks'], | ||
calling_file='', | ||
private_hooks={}, | ||
) | ||
|
||
|
||
BASE_ID = "pydantic.main.Base" | ||
TYPE_FIXTURES = [ | ||
('Base', f"<class '{BASE_ID}'>"), | ||
('list[str]', 'list[str]'), | ||
('list[str, int]', 'list[str, int]'), | ||
('list[Base]', f'list[{BASE_ID}]'), | ||
('List[Base]', f'typing.List[{BASE_ID}]'), | ||
('dict[str,Base]', f'dict[str, {BASE_ID}]'), | ||
('Dict[str, Base]', f'typing.Dict[str, {BASE_ID}]'), | ||
('optional[Base]', f'typing.Optional[{BASE_ID}]'), | ||
('Optional[Base]', f'typing.Optional[{BASE_ID}]'), | ||
('union[Base, str]', f'typing.Union[{BASE_ID}, str]'), | ||
('Union[Base, str]', f'typing.Union[{BASE_ID}, str]'), | ||
('Optional[Union[Base, str]]', f'typing.Union[{BASE_ID}, str, NoneType]'), | ||
('list[Base, dict[str, list]]', f'list[{BASE_ID}, dict[str, list]]'), | ||
('list[dict[str, Base], Base]', f'list[dict[str, {BASE_ID}], {BASE_ID}]'), | ||
('list[Base, dict[str, Base]]', f'list[{BASE_ID}, dict[str, {BASE_ID}]]'), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("type_str,expected_repr", TYPE_FIXTURES) | ||
def test_functions_types_parse_function_type(type_str, expected_repr, context_fixture): | ||
""" | ||
Check complex type parsing where a `Base` hook is imported from the current | ||
directory's '.hooks' dir with various typing around it. | ||
""" | ||
output = parse_function_type( | ||
context=context_fixture, | ||
type_str=type_str, | ||
func_name="foo", | ||
) | ||
assert repr(output) == expected_repr | ||
|
||
|
||
BAD_TYPE_FIXTURES = [ | ||
'NotBase', | ||
'Str', | ||
'list[Str]', | ||
'list[NotBase]', | ||
'Optional[Base, str]', | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("type_str", BAD_TYPE_FIXTURES) | ||
def test_functions_types_malformed_field_exceptions(type_str, context_fixture): | ||
"""Check that an error is thrown for bad types.""" | ||
with pytest.raises(exceptions.MalformedFunctionFieldException): | ||
parse_function_type( | ||
context=context_fixture, | ||
type_str=type_str, | ||
func_name="foo", | ||
) | ||
|
||
|
||
@pytest.fixture() | ||
def types_fixtures(chdir): | ||
chdir('types-fixtures') | ||
|
||
|
||
def test_functions_types_base(types_fixtures): | ||
"""Check that we can compose a single hook type.""" | ||
output = tackle('base.yaml') | ||
assert output['check_ok']['bar']['foo'] == 'baz' | ||
assert output['check_false'] == 1 | ||
|
||
|
||
def test_functions_types_base_embed(types_fixtures): | ||
"""Check that we can compose embedded hook types.""" | ||
output = tackle('base-embed.yaml') | ||
assert output['check_ok']['bar']['foo2']['foo1'] == 'baz' | ||
assert output['check_false'] == 1 | ||
|
||
|
||
def test_functions_types_list_base(types_fixtures): | ||
"""Validate a list with hooks.""" | ||
output = tackle('list-base.yaml') | ||
assert output['check_ok']['bar'][0]['foo'] == 'baz' | ||
assert output['check_ok']['bar'][1]['foo'] == 'baz2' | ||
assert output['check_false'] == 1 | ||
|
||
|
||
def test_functions_types_dict_base(types_fixtures): | ||
"""Validate a dict with hooks.""" | ||
output = tackle('dict-base.yaml') | ||
assert output['check_ok']['bar']['bar']['foo'] == 'baz' | ||
assert output['check_false'] == 1 | ||
|
||
|
||
def test_functions_composition_enum_basic(types_fixtures): | ||
""" | ||
Show that we can create enum types, that they fail when we give them some value | ||
out of its members, and that it is properly deserialized. | ||
""" | ||
output = tackle('enum-basic.yaml') | ||
|
||
assert output['failure'] | ||
assert output['failure_default'] | ||
|
||
assert output['success']['color'] == 'blue' | ||
assert output['success']['color_default'] == 'red' | ||
assert output['success_default']['color'] == 'blue' | ||
assert output['success_default']['color_default'] == 'green' |
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,27 @@ | ||
base1<-: | ||
foo1: bar | ||
|
||
base2<-: | ||
foo2: | ||
type: base1 | ||
|
||
obj<-: | ||
stuff: things | ||
bar: | ||
type: base2 | ||
|
||
check_ok: | ||
->: obj | ||
bar: | ||
foo2: | ||
foo1: baz | ||
|
||
thing: | ||
foo2: | ||
foo1: baz | ||
check_ok_render->: obj --bar {{thing}} | ||
|
||
check_false: | ||
->: obj --try --except 1 | ||
bar: | ||
not foo: baz |
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,21 @@ | ||
base<-: | ||
foo: bar | ||
|
||
obj<-: | ||
stuff: things | ||
bar: | ||
type: base | ||
|
||
check_ok: | ||
->: obj | ||
bar: | ||
foo: baz | ||
|
||
thing: | ||
foo: baz | ||
check_ok_render->: obj --bar {{thing}} | ||
|
||
check_false: | ||
->: obj --try --except 1 | ||
bar: | ||
not foo: baz |
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,19 @@ | ||
base<-: | ||
foo: bar | ||
|
||
obj<-: | ||
stuff: things | ||
bar: | ||
type: dict[str, base] | ||
|
||
check_ok: | ||
->: obj | ||
bar: | ||
bar: | ||
foo: baz | ||
|
||
check_false: | ||
->: obj --try --except 1 | ||
bar: | ||
bar: | ||
not foo: baz |
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,27 @@ | ||
|
||
|
||
Schema<-: | ||
foo: | ||
type: str | ||
default: bar | ||
color: | ||
type: string | ||
enum: | ||
- red | ||
- green | ||
- blue | ||
|
||
color_default: | ||
type: string | ||
default: red | ||
enum: | ||
- red | ||
- green | ||
- blue | ||
|
||
|
||
success->: Schema --color blue | ||
success_default->: Schema --color blue --color_default green | ||
|
||
failure->: Schema --color grey --try --except true | ||
failure_default->: Schema --color blue --color_default grey --try --except true |
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,27 @@ | ||
|
||
|
||
Schema<-: | ||
foo: | ||
type: str | ||
default: bar | ||
color: | ||
type: string | ||
enum: | ||
- red | ||
- green | ||
- blue | ||
|
||
color_default: | ||
type: string | ||
default: red | ||
enum: | ||
- red | ||
- green | ||
- blue | ||
|
||
|
||
success->: Schema --color blue | ||
success_default->: Schema --color blue --color_default green | ||
|
||
failure->: Schema --color grey --try --except true | ||
failure_default->: Schema --color blue --color_default grey --try --except true |
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,18 @@ | ||
base<-: | ||
foo: bar | ||
|
||
obj<-: | ||
stuff: things | ||
bar: | ||
type: list[base] | ||
|
||
check_ok: | ||
->: obj | ||
bar: | ||
- foo: baz | ||
- foo: baz2 | ||
|
||
check_false: | ||
->: obj --try --except 1 | ||
bar: | ||
- not foo: baz |