Skip to content

Commit

Permalink
use fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Sep 8, 2023
1 parent 3bd6a44 commit 62456f3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 87 deletions.
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

import shutil
from pathlib import Path

import pytest


@pytest.fixture
def root_test_dir() -> Path:
return Path(__file__).parent


@pytest.fixture
def bids_dir(root_test_dir, tmp_path) -> Path:
# copy the bids directory to a temporary location
tmp_bids_dir = tmp_path / "bids"
shutil.copytree(root_test_dir / "bids", tmp_bids_dir)
return tmp_bids_dir


@pytest.fixture
def license_file(bids_dir) -> Path:
return bids_dir / "derivatives" / "bids2cite" / "LICENSE"
9 changes: 4 additions & 5 deletions tests/test_authors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

import pytest

from .utils import get_test_dir
from bids2cite._authors import choose_from_new_authors
from bids2cite._authors import display_new_authors
from bids2cite._authors import get_author_info_from_orcid
from bids2cite._authors import parse_author


def test_display_new_authors():
authors_file = get_test_dir().parent.joinpath("inputs", "authors.tsv")
def test_display_new_authors(root_test_dir):
authors_file = root_test_dir.parent / "inputs" / "authors.tsv"
display_new_authors(authors_file)


def test_choose_from_new_authors():
authors_file = get_test_dir().parent.joinpath("inputs", "authors.tsv")
def test_choose_from_new_authors(root_test_dir):
authors_file = root_test_dir.parent / "inputs" / "authors.tsv"
author_info = choose_from_new_authors(authors_file, author_idx=1)
assert author_info == {
"affiliation": "UCLouvain",
Expand Down
103 changes: 45 additions & 58 deletions tests/test_bids2cite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,52 @@
import json
from pathlib import Path

from .utils import get_test_dir
from .utils import license_file
import pytest

from bids2cite.bids2cite import _update_bidsignore
from bids2cite.bids2cite import bids2cite


def bids_dir() -> Path:
return get_test_dir().joinpath("bids")


def bidsignore() -> Path:
return bids_dir().joinpath(".bidsignore")

@pytest.fixture
def bidsignore(bids_dir) -> Path:
return bids_dir / ".bidsignore"

def datacite() -> Path:
return bids_dir().joinpath("derivatives", "bids2cite", "datacite.yml")

@pytest.fixture
def datacite(bids_dir) -> Path:
return bids_dir / "derivatives" / "bids2cite" / "datacite.yml"

def citation() -> Path:
return bids_dir().joinpath("derivatives", "bids2cite", "CITATION.cff")

@pytest.fixture
def citation(bids_dir) -> Path:
return bids_dir / "derivatives" / "bids2cite" / "CITATION.cff"

def dataset_description() -> Path:
return bids_dir().joinpath("derivatives", "bids2cite", "dataset_description.json")

@pytest.fixture
def dataset_description(bids_dir) -> Path:
return bids_dir / "derivatives" / "bids2cite" / "dataset_description.json"

def cleanup() -> None:
bidsignore().unlink(missing_ok=True)
datacite().unlink(missing_ok=True)
citation().unlink(missing_ok=True)
dataset_description().unlink(missing_ok=True)


def test_update_bidsignore() -> None:
cleanup()

_update_bidsignore(bids_dir=bids_dir())
assert bidsignore().exists()
with bidsignore().open("r") as f:
def test_update_bidsignore(bids_dir, bidsignore) -> None:
_update_bidsignore(bids_dir=bids_dir)
assert bidsignore.exists()
with bidsignore.open("r") as f:
content = f.read()
assert "datacite.yml" in content

cleanup()
with bidsignore().open("w") as f:

def test_update_bidsignore_2(bids_dir, bidsignore) -> None:
with bidsignore.open("w") as f:
f.write("foo")
_update_bidsignore(bids_dir=bids_dir())
with bidsignore().open("r") as f:
_update_bidsignore(bids_dir=bids_dir)
with bidsignore.open("r") as f:
content = f.read()
assert "datacite.yml" in content

cleanup()


def test_bids2cite_datacite() -> None:
cleanup()

bids_dir = get_test_dir().joinpath("bids")

def test_bids2cite_datacite(
bids_dir, license_file, bidsignore, datacite, citation, dataset_description
) -> None:
bids2cite(
bids_dir=bids_dir,
output_format="datacite",
Expand All @@ -70,13 +58,13 @@ def test_bids2cite_datacite() -> None:
license="PDDL-1.0",
)

assert bidsignore().exists()
assert license_file().exists()
assert dataset_description().exists()
assert datacite().exists()
assert not citation().exists()
assert bidsignore.exists()
assert license_file.exists()
assert dataset_description.exists()
assert datacite.exists()
assert not citation.exists()

with dataset_description().open("r") as f:
with dataset_description.open("r") as f:
content = json.load(f)

assert content.get("authors") is None
Expand All @@ -89,14 +77,15 @@ def test_bids2cite_datacite() -> None:
assert x not in ("")
assert not x.isspace()

cleanup()


def test_bids2cite_citation() -> None:
bids_dir = get_test_dir().joinpath("bids")

cleanup()

def test_bids2cite_citation(
bids_dir,
license_file,
bidsignore,
dataset_description,
datacite,
citation,
) -> None:
bids2cite(
bids_dir=bids_dir,
output_format="citation",
Expand All @@ -106,10 +95,8 @@ def test_bids2cite_citation() -> None:
license="PDDL-1.0",
)

assert bidsignore().exists()
assert license_file().exists()
assert dataset_description().exists()
assert not datacite().exists()
assert citation().exists()

cleanup()
assert bidsignore.exists()
assert license_file.exists()
assert dataset_description.exists()
assert not datacite.exists()
assert citation.exists()
23 changes: 10 additions & 13 deletions tests/test_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@

import pytest

from .utils import get_test_dir
from .utils import license_file
from bids2cite._license import add_license_file
from bids2cite._license import identify_license
from bids2cite._license import update_license


def test_add_license_file():
output_dir = get_test_dir().joinpath("bids", "derivatives", "bids2cite")
def test_add_license_file(bids_dir, license_file):
output_dir = bids_dir / "derivatives" / "bids2cite"

add_license_file("PDDL-1.0", output_dir)

assert license_file().exists()
license_file().unlink(missing_ok=True)
assert license_file.exists()
license_file.unlink(missing_ok=True)

add_license_file("None", output_dir)
assert not license_file().exists()
assert not license_file.exists()


def test_update_license():
bids_dir = get_test_dir().joinpath("bids")
output_dir = get_test_dir().joinpath("bids", "derivatives", "bids2cite")
def test_update_license(root_test_dir, license_file):
bids_dir = root_test_dir.joinpath("bids")
output_dir = root_test_dir.joinpath("bids", "derivatives", "bids2cite")

ds_desc = {"License": "PDDL"}

Expand All @@ -34,8 +32,7 @@ def test_update_license():
assert license_name == "PDDL-1.0"
assert license_url == "https://opendatacommons.org/licenses/pddl/1-0/"

assert license_file().exists()
license_file().unlink(missing_ok=True)
assert license_file.exists()


@pytest.mark.parametrize(
Expand All @@ -50,6 +47,6 @@ def test_update_license():
def test_identify_license(input, expected):
ds_desc = {"License": input}

(name, url) = identify_license(ds_desc)
(name, _) = identify_license(ds_desc)

assert name == expected
11 changes: 0 additions & 11 deletions tests/utils.py

This file was deleted.

0 comments on commit 62456f3

Please sign in to comment.