Skip to content

Commit 2a80aeb

Browse files
committed
add tests
1 parent eab8072 commit 2a80aeb

4 files changed

Lines changed: 193 additions & 36 deletions

File tree

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
class FileTypes(Enum):
2525
POETRY = "poetry"
26+
POETRY2 = "poetry2"
2627
SETUPTOOLS = "setuptools"
2728
CITATION = "citation"
2829
SOMESY = "somesy"
@@ -81,6 +82,8 @@ def _create_files(files: Set[Tuple[FileTypes, str]]):
8182
read_file_name = read_file_path / Path("pyproject.setuptools.toml")
8283
elif file_type == FileTypes.POETRY:
8384
read_file_name = read_file_path / Path("pyproject.toml")
85+
elif file_type == FileTypes.POETRY2:
86+
read_file_name = read_file_path / Path("pyproject2.toml")
8487
elif file_type == FileTypes.SOMESY:
8588
read_file_name = read_file_path / Path("somesy.toml")
8689
elif file_type == FileTypes.PACKAGE_JSON:
@@ -134,6 +137,9 @@ def _load_files(files: Set[FileTypes]):
134137
elif file_type == FileTypes.POETRY:
135138
read_file_name = read_file_name / Path("pyproject.toml")
136139
file_instances[file_type] = Pyproject(read_file_name)
140+
elif file_type == FileTypes.POETRY2:
141+
read_file_name = read_file_name / Path("pyproject2.toml")
142+
file_instances[file_type] = Pyproject(read_file_name)
137143
elif file_type == FileTypes.SOMESY:
138144
read_file_name = read_file_name / Path("somesy.toml")
139145
file_instances[file_type] = SomesyInput.from_input_file(read_file_name)

tests/data/pyproject2.toml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
[project]
2+
name = "test-package"
3+
version = "0.1.0"
4+
description = "This is a test package for demonstration purposes."
5+
authors = ["John Doe <john.doe@example.com>"]
6+
license = "MIT"
7+
repository = "https://github.com/example/test-package"
8+
homepage = "https://example.com/test-package"
9+
documentation = "https://example.com/test-package"
10+
keywords = ["test", "demo", "example"]
11+
classifiers = [
12+
"Operating System :: POSIX :: Linux",
13+
"Development Status :: 3 - Alpha",
14+
"Intended Audience :: Developers",
15+
"Intended Audience :: Science/Research",
16+
"Topic :: Software Development :: Libraries :: Application Frameworks",
17+
]
18+
19+
[tool.poetry.dependencies]
20+
python = "^3.8"
21+
pydantic = "^1.10.2"
22+
23+
24+
[build-system]
25+
requires = ["poetry-core>=1.0.0"]
26+
build-backend = "poetry.core.masonry.api"
27+
28+
# project metadata
29+
[tool.somesy.project]
30+
name = "testproject"
31+
version = "1.0.0"
32+
description = "This is a test project for demonstration purposes."
33+
keywords = ["test", "demo", "example"]
34+
license = "MIT"
35+
repository = "https://github.com/example/testproject"
36+
homepage = "https://example.com/testproject"
37+
38+
[[tool.somesy.project.people]]
39+
family-names = "Doe"
40+
given-names = "John"
41+
email = "john.doe@example.com"
42+
orcid = "https://orcid.org/0000-0000-0000-0000"
43+
44+
contribution = "The main developer, maintainer, and tester."
45+
contribution_begin = "2023-01-15"
46+
contribution_types = ["maintenance", "code", "test", "review", "doc"]
47+
48+
author = true
49+
maintainer = true
50+
publication_author = true
51+
52+
[[tool.somesy.project.people]]
53+
family-names = "Doe"
54+
given-names = "Jane"
55+
email = "jane.doe@example.com"
56+
orcid = "https://orcid.org/0000-0000-0000-0001"
57+
58+
contribution = "Initial concepts, tool development and enhancement, documentation."
59+
contribution_begin = "2023-02-20"
60+
contribution_types = ["ideas", "code", "test", "review", "doc"]
61+
62+
author = true
63+
64+
[[tool.somesy.project.people]]
65+
family-names = "Doe"
66+
given-names = "Michael"
67+
email = "michael.doe@example.com"
68+
orcid = "https://orcid.org/0000-0000-0000-0002"
69+
70+
contribution = "Valuable input concerning metadata standards and usability."
71+
contribution_begin = "2023-03-10"
72+
contribution_types = ["ideas"]
73+
publication_author = true
74+
75+
[tool.somesy.config]
76+
no_sync_cff = false
77+
cff_file = "CITATION.cff"
78+
no_sync_pyproject = false
79+
pyproject_file = "pyproject.toml"
80+
no_sync_codemeta = false
81+
codemeta_file = "codemeta.json"
82+
no_sync_package_json = true
83+
show_info = false
84+
verbose = false
85+
debug = false

tests/input/test_pyproject_validate.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,42 @@
77
def test_poetry_validate_accept(load_files, file_types):
88
"""Validate by loading the data pyproject file using the fixture."""
99
load_files([file_types.SETUPTOOLS])
10-
load_files([file_types.POETRY])
10+
load_files([file_types.POETRY]) # Poetry v1
11+
load_files([file_types.POETRY2]) # Poetry v2
1112

1213

1314
def test_poetry_validate(tmp_path):
1415
"""Test validating a pyproject file in both poetry and setuptools formats."""
1516

16-
# create a pyproject file in poetry format but with a invalid values
17-
reject_poetry_object = {
17+
# Test Poetry v1 format with invalid values
18+
reject_poetry_v1_object = {
1819
"tool": {
1920
"poetry": {"name": "somesy", "version": "abc", "authors": ["John Doe <"]}
2021
}
2122
}
2223
invalid_poetry_path = tmp_path / "pyproject.toml"
2324
with open(invalid_poetry_path, "w+") as f:
24-
dump(reject_poetry_object, f)
25+
dump(reject_poetry_v1_object, f)
2526

2627
with pytest.raises(ValueError):
2728
Pyproject(invalid_poetry_path)
2829

29-
# if we pass validation, it should not raise an error
30+
# Test Poetry v2 format with invalid values
31+
reject_poetry_v2_object = {
32+
"tool": {"poetry": {}},
33+
"project": {"name": "somesy", "version": "abc", "authors": ["John Doe <"]},
34+
}
35+
invalid_poetry_path = tmp_path / "pyproject2.toml"
36+
with open(invalid_poetry_path, "w+") as f:
37+
dump(reject_poetry_v2_object, f)
38+
39+
with pytest.raises(ValueError):
40+
Pyproject(invalid_poetry_path)
41+
42+
# if we pass validation, it should not raise an error for either version
3043
Pyproject(invalid_poetry_path, pass_validation=True)
3144

32-
# create a pyproject file in setuptools format but with a invalid values
45+
# Test setuptools format with invalid values
3346
reject_setuptools_object = {
3447
"project": {"name": "somesy", "version": "abc", "authors": ["John Doe <"]}
3548
}

tests/output/test_pyproject_writer.py

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ def pyproject_poetry(load_files, file_types):
1212
return files[file_types.POETRY]
1313

1414

15+
@pytest.fixture
16+
def pyproject_poetry2(load_files, file_types):
17+
files = load_files([file_types.POETRY2])
18+
return files[file_types.POETRY2]
19+
20+
1521
@pytest.fixture
1622
def pyproject_poetry_file(create_files, file_types):
1723
folder = create_files([(file_types.POETRY, "pyproject.toml")])
1824
return folder / Path("pyproject.toml")
1925

2026

27+
@pytest.fixture
28+
def pyproject_poetry2_file(create_files, file_types):
29+
folder = create_files([(file_types.POETRY2, "pyproject2.toml")])
30+
return folder / Path("pyproject2.toml")
31+
32+
2133
@pytest.fixture
2234
def pyproject_setuptools(load_files, file_types):
2335
files = load_files([file_types.SETUPTOOLS])
@@ -30,7 +42,7 @@ def pyproject_setuptools_file(create_files, file_types):
3042
return folder / Path("pyproject.toml")
3143

3244

33-
def test_content_match(pyproject_poetry, pyproject_setuptools):
45+
def test_content_match(pyproject_poetry, pyproject_poetry2, pyproject_setuptools):
3446
# create a function to check both file formats
3547
def assert_content_match(pyproject_file):
3648
assert pyproject_file.name == "test-package"
@@ -43,29 +55,32 @@ def assert_content_match(pyproject_file):
4355
)
4456
assert len(pyproject_file.authors) == 1
4557

46-
# assert for both formats
58+
# assert for all formats
4759
assert_content_match(pyproject_poetry)
60+
assert_content_match(pyproject_poetry2)
4861
assert_content_match(pyproject_setuptools)
4962

5063

51-
def test_sync(pyproject_poetry, pyproject_setuptools, somesy_input):
64+
def test_sync(pyproject_poetry, pyproject_poetry2, pyproject_setuptools, somesy_input):
5265
def assert_sync(pyproject):
5366
pyproject.sync(somesy_input.project)
5467
assert pyproject.name == "testproject"
5568
assert pyproject.version == "1.0.0"
5669

5770
assert_sync(pyproject_poetry)
71+
assert_sync(pyproject_poetry2)
5872
assert_sync(pyproject_setuptools)
5973

6074

61-
def test_save(tmp_path, pyproject_poetry, pyproject_setuptools):
75+
def test_save(tmp_path, pyproject_poetry, pyproject_poetry2, pyproject_setuptools):
6276
def assert_save(pyproject):
6377
custom_path = tmp_path / Path("pyproject.toml")
6478
pyproject.save(custom_path)
6579
assert custom_path.is_file()
6680
custom_path.unlink()
6781

6882
assert_save(pyproject_poetry)
83+
assert_save(pyproject_poetry2)
6984
assert_save(pyproject_setuptools)
7085

7186

@@ -97,13 +112,25 @@ def test_from_to_person(person):
97112

98113

99114
@pytest.mark.parametrize(
100-
"writer_class, writer_file_fixture",
101-
[(Poetry, "pyproject_poetry_file"), (SetupTools, "pyproject_setuptools_file")],
115+
"writer_class, writer_file_fixture, version",
116+
[
117+
(Poetry, "pyproject_poetry_file", 1),
118+
(Poetry, "pyproject_poetry2_file", 2),
119+
(SetupTools, "pyproject_setuptools_file", None),
120+
],
102121
)
103-
def test_person_merge_pyproject(request, writer_class, writer_file_fixture, person):
122+
def test_person_merge_pyproject(
123+
request, writer_class, writer_file_fixture, version, person
124+
):
104125
# get suitable project file
105126
writer_file = request.getfixturevalue(writer_file_fixture)
106-
pj = writer_class(writer_file)
127+
128+
# Initialize with correct version for Poetry
129+
if writer_class == Poetry:
130+
pj = writer_class(writer_file, version=version)
131+
else:
132+
pj = writer_class(writer_file)
133+
107134
# update project file with known data
108135
pm = ProjectMetadata(
109136
name="My awesome project",
@@ -176,7 +203,8 @@ def test_person_merge_pyproject(request, writer_class, writer_file_fixture, pers
176203

177204

178205
def test_without_email(tmp_path, person):
179-
pyproject_str = """
206+
# Test Poetry v1
207+
pyproject_v1_str = """
180208
[tool.poetry]
181209
name = "ttt"
182210
version = "0.1.0"
@@ -187,33 +215,58 @@ def test_without_email(tmp_path, person):
187215
[tool.poetry.dependencies]
188216
python = "^3.10"
189217
190-
191218
[build-system]
192219
requires = ["poetry-core"]
193220
build-backend = "poetry.core.masonry.api"
194221
"""
195222

196-
# save to file
197-
pyproject_file = tmp_path / Path("pyproject.toml")
198-
pyproject_file.write_text(pyproject_str)
223+
# Test Poetry v2
224+
pyproject_v2_str = """
225+
[tool.poetry]
226+
name = "ttt"
227+
version = "0.1.0"
228+
description = "asd"
229+
authors = ["John Doe"]
230+
license = "MIT"
199231
200-
# load and sync
201-
p = Poetry(pyproject_file)
202-
assert len(p.authors) == 1
232+
[project]
233+
name = "ttt"
234+
version = "0.1.0"
235+
description = "asd"
236+
authors = ["John Doe"]
237+
license = "MIT"
203238
204-
pm = ProjectMetadata(
205-
name="My awesome project",
206-
description="Project description",
207-
license=LicenseEnum.MIT,
208-
version="0.1.0",
209-
people=[
210-
person.model_copy(
211-
update=dict(author=True, publication_author=True, maintainer=True)
212-
)
213-
],
214-
)
239+
[tool.poetry.dependencies]
240+
python = "^3.10"
241+
242+
[build-system]
243+
requires = ["poetry-core"]
244+
build-backend = "poetry.core.masonry.api"
245+
"""
246+
247+
# Test both versions
248+
for pyproject_str, version in [(pyproject_v1_str, 1), (pyproject_v2_str, 2)]:
249+
# save to file
250+
pyproject_file = tmp_path / Path(f"pyproject_v{version}.toml")
251+
pyproject_file.write_text(pyproject_str)
252+
253+
# load and sync
254+
p = Poetry(pyproject_file, version=version)
255+
assert len(p.authors) == 1
256+
257+
pm = ProjectMetadata(
258+
name="My awesome project",
259+
description="Project description",
260+
license=LicenseEnum.MIT,
261+
version="0.1.0",
262+
people=[
263+
person.model_copy(
264+
update=dict(author=True, publication_author=True, maintainer=True)
265+
)
266+
],
267+
)
215268

216-
p.sync(pm)
269+
p.sync(pm)
217270

218-
assert len(p.authors) == 1
219-
assert len(p.maintainers) == 1
271+
assert len(p.authors) == 1
272+
assert len(p.maintainers) == 1

0 commit comments

Comments
 (0)