Skip to content

Commit 7fa8707

Browse files
committed
add julia tests
1 parent ab114d7 commit 7fa8707

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

tests/input/test_julia_validate.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from tomlkit import dump
3+
4+
from somesy.julia import Julia
5+
6+
7+
def test_julia_validate_accept(load_files, file_types):
8+
"""Validate by loading the data Project.toml file using the fixture."""
9+
load_files([file_types.JULIA])
10+
11+
12+
def test_julia_validate(tmp_path):
13+
"""Test validating a Project.toml file."""
14+
15+
# create a Project.toml file but with a invalid values
16+
reject_julia_object = {
17+
"name": "somesy",
18+
"version": "abc",
19+
"authors": ["John Doe <"],
20+
}
21+
22+
invalid_julia_path = tmp_path / "Project.toml"
23+
with open(invalid_julia_path, "w+") as f:
24+
dump(reject_julia_object, f)
25+
26+
with pytest.raises(ValueError):
27+
Julia(invalid_julia_path)

tests/output/test_julia_writer.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from somesy.core.models import LicenseEnum, Person, ProjectMetadata
6+
from somesy.julia.writer import Julia
7+
8+
9+
@pytest.fixture
10+
def julia(load_files, file_types):
11+
files = load_files([file_types.JULIA])
12+
return files[file_types.JULIA]
13+
14+
15+
@pytest.fixture
16+
def julia_file(create_files, file_types):
17+
folder = create_files([(file_types.JULIA, "Project.toml")])
18+
return folder / Path("Project.toml")
19+
20+
21+
def test_content_match(julia):
22+
assert julia.name == "test-package"
23+
assert len(julia.authors) == 1
24+
25+
26+
def test_sync(julia, somesy_input):
27+
julia.sync(somesy_input.project)
28+
assert julia.name == "testproject"
29+
assert julia.version == "1.0.0"
30+
31+
32+
def test_save(tmp_path, julia):
33+
custom_path = tmp_path / Path("Project.toml")
34+
julia.save(custom_path)
35+
assert custom_path.is_file()
36+
custom_path.unlink()
37+
38+
39+
def test_from_to_person(person):
40+
assert Julia._from_person(person) == f"{person.full_name} <{person.email}>"
41+
42+
p = Julia._to_person(Julia._from_person(person))
43+
assert p.full_name == person.full_name
44+
assert p.email == person.email
45+
46+
47+
def test_person_merge(request, julia_file, person):
48+
pj = Julia(julia_file)
49+
# update project file with known data
50+
pm = ProjectMetadata(
51+
name="My awesome project",
52+
description="Project description",
53+
license=LicenseEnum.MIT,
54+
version="0.1.0",
55+
people=[person.model_copy(update=dict(author=True, publication_author=True))],
56+
)
57+
pj.sync(pm)
58+
pj.save()
59+
# ----
60+
61+
# jane becomes john -> modified person
62+
person1b = person.model_copy(
63+
update={"given_names": "John", "author": True, "publication_author": True}
64+
)
65+
66+
# different Jane Doe with different orcid -> new person
67+
person2 = person.model_copy(
68+
update={
69+
"orcid": "https://orcid.org/4321-0987-3231",
70+
"email": "i.am.jane@doe.com",
71+
"author": True,
72+
"publication_author": True,
73+
}
74+
)
75+
# use different order, just for some difference
76+
person2.set_key_order(["given_names", "orcid", "family_names", "email"])
77+
78+
# listed in "arbitrary" order in somesy metadata (new person comes first)
79+
pm.people = [person2, person1b] # need to assign like that to keep _key_order
80+
pj.sync(pm)
81+
pj.save()
82+
83+
# existing author info preserved, order not preserved because no orcid
84+
person1b_rep = Julia._from_person(person1b)
85+
person2_rep = Julia._from_person(person2)
86+
assert (pj.authors[0] == person1b_rep) or (pj.authors[1] == person1b_rep)
87+
assert (pj.authors[0] == person2_rep) or (pj.authors[1] == person2_rep)
88+
89+
# new person
90+
person3 = Person(
91+
**{
92+
"given_names": "Janice",
93+
"family_names": "Doethan",
94+
"email": "jane93@gmail.com",
95+
"author": True,
96+
"publication_author": True,
97+
}
98+
)
99+
person3_rep = Julia._from_person(person3)
100+
101+
# john has a new email address
102+
person1c = person1b.model_copy(update={"email": "john.of.us@qualityland.com"})
103+
person1c_rep = Julia._from_person(person1c)
104+
105+
# jane 2 is removed from authors, but added to maintainers
106+
person2.author = False
107+
person2.publication_author = False
108+
person2.maintainer = True
109+
# reflect in project metadata
110+
pm.people = [person3, person2, person1c]
111+
# sync to CFF file
112+
pj.sync(pm)
113+
pj.save()
114+
115+
assert len(pj.authors) == 2
116+
assert pj.authors[0] == person1c_rep
117+
assert pj.authors[1] == person3_rep

0 commit comments

Comments
 (0)