Skip to content

Commit 96a8fd8

Browse files
authored
✨ Drastically simplified the test-suite (#7)
1 parent f50f9e7 commit 96a8fd8

File tree

4 files changed

+199
-137
lines changed

4 files changed

+199
-137
lines changed

poetry.lock

Lines changed: 40 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pytest-cov = "^2.12.1"
3030
codecov = "^2.1.12"
3131
mkdocs = "^1.2.2"
3232
mkdocs-material = "^7.2.6"
33+
PyYAML = "^5.4.1"
34+
pydantic = "^1.8.2"
3335

3436
[tool.coverage.paths]
3537
source = ["src", "*/site-packages"]

test/simple-tests.yaml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
tests:
2+
- name: test_simple_change
3+
before: |
4+
def bla(foo: str = None):
5+
...
6+
after: |
7+
from typing import Optional
8+
def bla(foo: Optional[str] = None):
9+
...
10+
11+
- name: test_simple_change_already_imported
12+
before: |
13+
from typing import Optional
14+
def bla(foo:
15+
str = None):
16+
...
17+
after: |
18+
from typing import Optional
19+
def bla(foo: Optional[str] = None):
20+
...
21+
22+
- name: test_simple_change_star_import
23+
before: |
24+
from typing import *
25+
def bla(foo:
26+
str = None):
27+
...
28+
after: |
29+
from typing import *
30+
def bla(foo: Optional[str] = None):
31+
...
32+
33+
- name: test_simple_change_already_imported_optional_as
34+
before: |
35+
from typing import Optional as O
36+
def bla(foo: str = None):
37+
...
38+
after: |
39+
from typing import Optional as O
40+
def bla(foo: O[str] = None):
41+
...
42+
43+
- name: test_nested_change
44+
before: |
45+
def bla(foo: Tuple[str] = None):
46+
...
47+
after: |
48+
from typing import Optional
49+
def bla(foo: Optional[Tuple[str]] = None):
50+
...
51+
52+
- name: test_not_optional
53+
unchanged: |
54+
def bla(foo:
55+
str = 'a'):
56+
...
57+
58+
- name: test_already_optional
59+
unchanged: |
60+
from typing import Optional
61+
def bla(foo:
62+
Optional[str] = None):
63+
...
64+
65+
- name: test_legal_no_type
66+
unchanged: |
67+
class Bloep:
68+
def bla(self):
69+
...
70+
71+
- name: test_nochange_typing_import
72+
unchanged: |
73+
from typing import Optional
74+
def bla(foo:
75+
Optional[str] = None):
76+
...
77+
78+
- name: test_nochange_renamed_typing
79+
unchanged: |
80+
import typing
81+
import typing as t
82+
def bla(foo: t.Optional[str] = None):
83+
...
84+
85+
- name: test_prioritise_from_import
86+
before: |
87+
import typing
88+
from typing import Optional
89+
def bla(foo: str = None):
90+
...
91+
after: |
92+
import typing
93+
from typing import Optional
94+
def bla(foo: Optional[str] = None):
95+
...
96+
97+
- name: test_nochange_typing_as_import
98+
unchanged: |
99+
import typing as t
100+
def bla(foo: t.Optional[str] = None):
101+
...

test/test_all.py

Lines changed: 56 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,59 @@
1+
from pathlib import Path
2+
from typing import List, Union
3+
4+
import pytest
5+
from pydantic import BaseModel
6+
from yaml import load
7+
18
from auto_optional.convert import convert_file
29

10+
try:
11+
from yaml import CLoader as YamlLoader
12+
except ImportError:
13+
from yaml import YamlLoader
14+
15+
16+
class BaseTestConfig(BaseModel):
17+
name: str
18+
19+
20+
class BeforeAndAfterTest(BaseTestConfig):
21+
before: str
22+
after: str
23+
24+
25+
class UnchangedTest(BaseTestConfig):
26+
unchanged: str
27+
28+
29+
class SimpleTestConfig(BaseModel):
30+
tests: List[Union[BeforeAndAfterTest, UnchangedTest]]
31+
32+
33+
def get_singe_file_tests() -> List:
34+
with (Path(__file__).parent / "simple-tests.yaml").open() as file:
35+
return SimpleTestConfig(
36+
**load(
37+
file,
38+
Loader=YamlLoader,
39+
)
40+
).tests
41+
42+
43+
SINGLE_FILE_TESTS = get_singe_file_tests()
44+
345

4-
def test_simple_change() -> None:
5-
before = """
6-
def bla(foo: str = None):
7-
...
8-
"""
9-
after = """
10-
from typing import Optional
11-
def bla(foo: Optional[str] = None):
12-
...
13-
"""
14-
assert convert_file(before) == after
15-
16-
17-
def test_simple_change_already_imported() -> None:
18-
before = """
19-
from typing import Optional
20-
def bla(foo: str = None):
21-
...
22-
"""
23-
after = """
24-
from typing import Optional
25-
def bla(foo: Optional[str] = None):
26-
...
27-
"""
28-
assert convert_file(before) == after
29-
30-
31-
def test_simple_change_star_import() -> None:
32-
before = """
33-
from typing import *
34-
def bla(foo: str = None):
35-
...
36-
"""
37-
after = """
38-
from typing import *
39-
def bla(foo: Optional[str] = None):
40-
...
41-
"""
42-
assert convert_file(before) == after
43-
44-
45-
def test_simple_change_already_imported_optional_as() -> None:
46-
before = """\
47-
from typing import Optional as O
48-
def bla(foo: str = None):
49-
...
50-
"""
51-
after = """\
52-
from typing import Optional as O
53-
def bla(foo: O[str] = None):
54-
...
55-
"""
56-
assert convert_file(before) == after
57-
58-
59-
def test_nested_change() -> None:
60-
before = """\
61-
def bla(foo: Tuple[str] = None):
62-
...
63-
"""
64-
after = """\
65-
from typing import Optional
66-
def bla(foo: Optional[Tuple[str]] = None):
67-
...
68-
"""
69-
assert convert_file(before) == after
70-
71-
72-
def test_not_optional() -> None:
73-
before = """\
74-
def bla(foo: str = 'a'):
75-
...
76-
"""
77-
assert convert_file(before) == before
78-
79-
80-
def test_nochange() -> None:
81-
before = """\
82-
from typing import Optional
83-
def bla(foo: Optional[str] = None):
84-
...
85-
"""
86-
assert convert_file(before) == before
87-
88-
89-
def test_legal_no_type() -> None:
90-
before = """
91-
class Bloep:
92-
def bla(self):
93-
...
94-
"""
95-
assert convert_file(before) == before
96-
97-
98-
def test_nochange_typing_import() -> None:
99-
before = """
100-
from typing import Optional
101-
def bla(foo: Optional[str] = None):
102-
...
103-
"""
104-
assert convert_file(before) == before
105-
106-
107-
def test_nochange_renamed_typing() -> None:
108-
before = """\
109-
import typing
110-
import typing as t
111-
def bla(foo: t.Optional[str] = None):
112-
...
113-
"""
114-
assert convert_file(before) == before
115-
116-
117-
def test_prioritise_from_import() -> None:
118-
before = """\
119-
import typing
120-
from typing import Optional
121-
def bla(foo: str = None):
122-
...
123-
"""
124-
after = """\
125-
import typing
126-
from typing import Optional
127-
def bla(foo: Optional[str] = None):
128-
...
129-
"""
130-
assert convert_file(before) == after
131-
132-
133-
def test_nochange_typing_as_import() -> None:
134-
before = """\
135-
import typing as t
136-
def bla(foo: t.Optional[str] = None):
137-
...
138-
"""
139-
assert convert_file(before) == before
46+
@pytest.mark.parametrize(
47+
"test_config",
48+
SINGLE_FILE_TESTS,
49+
ids=[test.name for test in SINGLE_FILE_TESTS],
50+
)
51+
def test_single_files_from_yaml(test_config):
52+
if isinstance(test_config, BeforeAndAfterTest):
53+
assert convert_file(test_config.before) == test_config.after
54+
elif isinstance(test_config, UnchangedTest):
55+
assert convert_file(test_config.unchanged) == test_config.unchanged
56+
else:
57+
raise NotImplementedError(
58+
f"tests of type {type(test_config)} are not yet supported"
59+
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy