Skip to content

Commit d5b74b5

Browse files
author
a.pirogov
committed
Merge branch 'main' into pydantic2, parametrize pyproject test
2 parents 594d01b + 37662ac commit d5b74b5

8 files changed

Lines changed: 228 additions & 27 deletions

File tree

CITATION.cff

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ authors:
1515
email: m.soylu@fz-juelich.de
1616
given-names: Mustafa
1717
family-names: Soylu
18+
- email: a.pirogov@fz-juelich.de
19+
orcid: https://orcid.org/0000-0002-5077-7497
20+
family-names: Pirogov
21+
given-names: Anton
1822
- email: v.hofmann@fz-juelich.de
1923
orcid: https://orcid.org/0000-0002-5149-603X
2024
family-names: Hofmann
@@ -23,10 +27,6 @@ authors:
2327
orcid: https://orcid.org/0000-0001-9560-4728
2428
family-names: Sandfeld
2529
given-names: Stefan
26-
- email: a.pirogov@fz-juelich.de
27-
orcid: https://orcid.org/0000-0002-5077-7497
28-
family-names: Pirogov
29-
given-names: Anton
3030
contact:
3131
- email: m.soylu@fz-juelich.de
3232
family-names: Soylu

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ file in the root folder of your repository:
136136
repos:
137137
# ... (your other hooks) ...
138138
- repo: https://github.com/Materials-Data-Science-and-Informatics/somesy
139-
rev: "0.1.0"
139+
rev: "v0.1.0"
140140
hooks:
141141
- id: somesy
142142
```

codemeta.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
"familyName": "Soylu",
2626
"givenName": "Mustafa"
2727
},
28+
{
29+
"@id": "https://orcid.org/0000-0002-5077-7497",
30+
"@type": "Person",
31+
"familyName": "Pirogov",
32+
"givenName": "Anton"
33+
},
2834
{
2935
"@id": "https://orcid.org/0000-0002-5149-603X",
3036
"@type": "Person",
@@ -36,12 +42,6 @@
3642
"@type": "Person",
3743
"familyName": "Sandfeld",
3844
"givenName": "Stefan"
39-
},
40-
{
41-
"@id": "https://orcid.org/0000-0002-5077-7497",
42-
"@type": "Person",
43-
"familyName": "Pirogov",
44-
"givenName": "Anton"
4545
}
4646
],
4747
"codeRepository": "https://github.com/Materials-Data-Science-and-Informatics/somesy",
@@ -68,14 +68,14 @@
6868
"identifier": "'version':",
6969
"name": "'version':",
7070
"runtimePlatform": "Python 3",
71-
"version": "'^2.4.2'}"
71+
"version": "'^0.7.0'}"
7272
},
7373
{
7474
"@type": "SoftwareApplication",
7575
"identifier": "'version':",
7676
"name": "'version':",
7777
"runtimePlatform": "Python 3",
78-
"version": "'^0.7.0'}"
78+
"version": "'^2.4.2'}"
7979
},
8080
{
8181
"@type": "SoftwareApplication",

tests/conftest.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from enum import Enum
22
from pathlib import Path
3-
from typing import Any, Set, Tuple, Type
3+
from typing import Any, Dict, Set, Tuple, Type
44

55
import pytest
66

@@ -59,20 +59,15 @@ def _create_files(files: Set[Tuple[FileTypes, str]]):
5959
read_file_name: Path = None
6060
# set file name as the input, if not given set to default
6161
if file_type == FileTypes.CITATION:
62-
file_name = file_name if file_name else "CITATION.cff"
63-
read_file_name = read_file_path / Path(file_name)
62+
read_file_name = read_file_path / Path("CITATION.cff")
6463
elif file_type == FileTypes.SETUPTOOLS:
65-
file_name = file_name if file_name else "pyproject.setuptools.toml"
66-
read_file_name = read_file_path / Path(file_name)
64+
read_file_name = read_file_path / Path("pyproject.setuptools.toml")
6765
elif file_type == FileTypes.POETRY:
68-
file_name = file_name if file_name else "pyproject.toml"
69-
read_file_name = read_file_path / Path(file_name)
66+
read_file_name = read_file_path / Path("pyproject.toml")
7067
elif file_type == FileTypes.SOMESY:
71-
file_name = file_name if file_name else "somesy.toml"
72-
read_file_name = read_file_path / Path(file_name)
68+
read_file_name = read_file_path / Path("somesy.toml")
7369
elif file_type == FileTypes.PACKAGE_JSON:
74-
file_name = file_name if file_name else "package.json"
75-
read_file_name = read_file_path / Path(file_name)
70+
read_file_name = read_file_path / Path("package.json")
7671

7772
with open(read_file_name, "r") as f:
7873
content = f.read()
@@ -97,7 +92,7 @@ def load_files():
9792
"""
9893

9994
def _load_files(files: Set[FileTypes]):
100-
file_instances: dict[FileTypes, Any] = {}
95+
file_instances: Dict[FileTypes, Any] = {}
10196
for file_type in files:
10297
if not isinstance(file_type, FileTypes):
10398
raise ValueError(f"Invalid file type: {file_type}")

tests/data/pyproject.setuptools.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "test-package"
33
version = "0.1.0"
4-
description = "This is a test package for demonstration purposes"
4+
description = "This is a test package for demonstration purposes."
55
authors = [{"name"="John Doe", "email"="john.doe@example.com"}]
66
license = "MIT"
77
keywords = ["test", "demo", "example"]

tests/data/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22
name = "test-package"
33
version = "0.1.0"
4-
description = "This is a test package for demonstration purposes"
4+
description = "This is a test package for demonstration purposes."
55
authors = ["John Doe <john.doe@example.com>"]
66
license = "MIT"
77
repository = "https://github.com/example/test-package"
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from somesy.core.models import LicenseEnum, Person, ProjectMetadata
6+
from somesy.pyproject.writer import Poetry, SetupTools
7+
8+
9+
@pytest.fixture
10+
def pyproject_poetry(load_files, file_types):
11+
files = load_files([file_types.POETRY])
12+
return files[file_types.POETRY]
13+
14+
15+
@pytest.fixture
16+
def pyproject_poetry_file(create_files, file_types):
17+
folder = create_files([(file_types.POETRY, "pyproject.toml")])
18+
return folder / Path("pyproject.toml")
19+
20+
21+
@pytest.fixture
22+
def pyproject_setuptools(load_files, file_types):
23+
files = load_files([file_types.SETUPTOOLS])
24+
return files[file_types.SETUPTOOLS]
25+
26+
27+
@pytest.fixture
28+
def pyproject_setuptools_file(create_files, file_types):
29+
folder = create_files([(file_types.SETUPTOOLS, "pyproject.toml")])
30+
return folder / Path("pyproject.toml")
31+
32+
33+
def test_content_match(pyproject_poetry, pyproject_setuptools):
34+
# create a function to check both file formats
35+
def assert_content_match(pyproject_file):
36+
assert pyproject_file.name == "test-package"
37+
assert (
38+
pyproject_file.description
39+
== "This is a test package for demonstration purposes."
40+
)
41+
assert pyproject_file.license == "MIT"
42+
assert len(pyproject_file.authors) == 1
43+
44+
# assert for both formats
45+
assert_content_match(pyproject_poetry)
46+
assert_content_match(pyproject_setuptools)
47+
48+
49+
def test_sync(pyproject_poetry, pyproject_setuptools, somesy_input):
50+
def assert_sync(pyproject):
51+
pyproject.sync(somesy_input.project)
52+
assert pyproject.name == "testproject"
53+
assert pyproject.version == "1.0.0"
54+
55+
assert_sync(pyproject_poetry)
56+
assert_sync(pyproject_setuptools)
57+
58+
59+
def test_save(tmp_path, pyproject_poetry, pyproject_setuptools):
60+
def assert_save(pyproject):
61+
custom_path = tmp_path / Path("pyproject.toml")
62+
pyproject.save(custom_path)
63+
assert custom_path.is_file()
64+
custom_path.unlink()
65+
66+
assert_save(pyproject_poetry)
67+
assert_save(pyproject_setuptools)
68+
69+
70+
def test_from_to_person(person):
71+
# test for poetry
72+
assert Poetry._from_person(person) == f"{person.full_name} <{person.email}>"
73+
74+
p = Poetry._to_person(Poetry._from_person(person))
75+
assert p.full_name == person.full_name
76+
assert p.email == person.email
77+
78+
# test for setuptools
79+
assert SetupTools._from_person(person) == {
80+
"name": person.full_name,
81+
"email": person.email,
82+
}
83+
84+
p = SetupTools._to_person(SetupTools._from_person(person))
85+
assert p.full_name == person.full_name
86+
assert p.email == person.email
87+
88+
89+
@pytest.mark.parametrize(
90+
"writer_class, writer_file_fixture",
91+
[(Poetry, "pyproject_poetry_file"), (SetupTools, "pyproject_setuptools_file")],
92+
)
93+
def test_person_merge_pyproject(request, writer_class, writer_file_fixture, person):
94+
# get suitable project file
95+
writer_file = request.getfixturevalue(writer_file_fixture)
96+
pj = writer_class(writer_file)
97+
# update project file with known data
98+
pm = ProjectMetadata(
99+
name="My awesome project",
100+
description="Project description",
101+
license=LicenseEnum.MIT,
102+
version="0.1.0",
103+
people=[person.model_copy(update=dict(author=True, publication_author=True))],
104+
)
105+
pj.sync(pm)
106+
pj.save()
107+
# ----
108+
109+
# jane becomes john -> modified person
110+
person1b = person.model_copy(
111+
update={"given_names": "John", "author": True, "publication_author": True}
112+
)
113+
114+
# different Jane Doe with different orcid -> new person
115+
person2 = person.model_copy(
116+
update={
117+
"orcid": "https://orcid.org/4321-0987-3231",
118+
"email": "i.am.jane@doe.com",
119+
"author": True,
120+
"publication_author": True,
121+
}
122+
)
123+
# use different order, just for some difference
124+
person2.set_key_order(["given_names", "orcid", "family_names", "email"])
125+
126+
# listed in "arbitrary" order in somesy metadata (new person comes first)
127+
pm.people = [person2, person1b] # need to assign like that to keep _key_order
128+
pj.sync(pm)
129+
pj.save()
130+
131+
# existing author info preserved, order not preserved because no orcid
132+
person1b_rep = writer_class._from_person(person1b)
133+
person2_rep = writer_class._from_person(person2)
134+
assert (pj.authors[0] == person1b_rep) or (pj.authors[1] == person1b_rep)
135+
assert (pj.authors[0] == person2_rep) or (pj.authors[1] == person2_rep)
136+
137+
# new person
138+
person3 = Person(
139+
**{
140+
"given_names": "Janice",
141+
"family_names": "Doethan",
142+
"email": "jane93@gmail.com",
143+
"author": True,
144+
"publication_author": True,
145+
}
146+
)
147+
person3_rep = writer_class._from_person(person3)
148+
149+
# john has a new email address
150+
person1c = person1b.model_copy(update={"email": "john.of.us@qualityland.com"})
151+
person1c_rep = writer_class._from_person(person1c)
152+
153+
# jane 2 is removed from authors, but added to maintainers
154+
person2.author = False
155+
person2.publication_author = False
156+
person2.maintainer = True
157+
# reflect in project metadata
158+
pm.people = [person3, person2, person1c]
159+
# sync to CFF file
160+
pj.sync(pm)
161+
pj.save()
162+
163+
assert len(pj.maintainers) == 1
164+
assert pj.authors[0] == person1c_rep
165+
assert pj.authors[1] == person3_rep

tests/processing/test_cli_util.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
from tomlkit import dump, load
5+
6+
from somesy.cli.util import resolved_somesy_input
7+
8+
9+
@pytest.fixture
10+
def somesy_file(create_files, file_types):
11+
folder_path = create_files([(file_types.SOMESY, "somesy.toml")])
12+
file_path = Path(folder_path) / "somesy.toml"
13+
somesy_file = None
14+
with open(file_path, "r") as f:
15+
somesy_file = load(f)
16+
17+
# delete config section and add one property
18+
del somesy_file["config"]
19+
somesy_file["config"] = {"no_sync_package_json": False}
20+
21+
# save to file
22+
with open(file_path, "w") as f:
23+
dump(somesy_file, f)
24+
return file_path
25+
26+
27+
def test_resolved_somesy_input(somesy_file):
28+
# set cli_args for tests
29+
cli_args = {"input_file": somesy_file, "debug": True, "no_sync_pyproject": True}
30+
31+
somesy_input = resolved_somesy_input(**cli_args)
32+
33+
# check default values
34+
assert somesy_input.config.no_sync_cff == False
35+
assert somesy_input.config.pyproject_file == Path("pyproject.toml")
36+
37+
# check overwritten values
38+
assert somesy_input.config.no_sync_pyproject == True
39+
40+
# check newly add value
41+
assert somesy_input.config.debug == True

0 commit comments

Comments
 (0)