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