From be6845edf77901f202d8c25de2ebf94c6806aa6b Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Fri, 2 Feb 2024 16:26:16 +0100 Subject: [PATCH] Use pytest instead of unittest --- .github/workflows/ci.yml | 2 +- environment.yaml | 1 + tests/test_GbibClean.py | 380 +++++++++++++++++++-------------------- tests/test_bibtex.py | 129 +++++++------ tests/test_journals.py | 352 ++++++++++++++++++------------------ tests/test_recognise.py | 65 +++---- tests/test_reformat.py | 143 +++++---------- tests/test_tex.py | 19 +- 8 files changed, 498 insertions(+), 593 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1fa5e82..629b234 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,7 +46,7 @@ jobs: run: python -m pip install . -v --no-build-isolation --no-deps - name: Run tests - run: python -m unittest discover tests -v + run: pytest -sv - name: Build docs working-directory: docs diff --git a/environment.yaml b/environment.yaml index e5efe52..5945bab 100644 --- a/environment.yaml +++ b/environment.yaml @@ -7,6 +7,7 @@ dependencies: - docopt - gitpython - numpy +- pytest - python - pyyaml - requests diff --git a/tests/test_GbibClean.py b/tests/test_GbibClean.py index 9c10f9e..68ad5e5 100644 --- a/tests/test_GbibClean.py +++ b/tests/test_GbibClean.py @@ -2,7 +2,6 @@ import re import shutil import subprocess -import unittest import bibtexparser import yaml @@ -12,166 +11,228 @@ dirname = os.path.dirname(__file__) -class Test_GooseBib(unittest.TestCase): - """ - GooseBib - """ +def test_inplace(): + source = os.path.join(dirname, "library_mendeley.bib") + output = os.path.join(dirname, "output.bib") + shutil.copy2(source, output) + data = os.path.join(dirname, "library.yaml") + gbib.bibtex.GbibClean(["--in-place", output]) - def test_inplace(self): - source = os.path.join(dirname, "library_mendeley.bib") - output = os.path.join(dirname, "output.bib") - shutil.copy2(source, output) - data = os.path.join(dirname, "library.yaml") - gbib.bibtex.GbibClean(["--in-place", output]) + with open(output) as file: + bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) - with open(output) as file: - bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) + with open(data) as file: + data = yaml.load(file.read(), Loader=yaml.FullLoader) - with open(data) as file: - data = yaml.load(file.read(), Loader=yaml.FullLoader) + for entry in bib.entries: + d = data[entry["ID"]] - for entry in bib.entries: - d = data[entry["ID"]] + for key in d: + if entry[key][0] == "{": + assert "{" + str(d[key]) + "}" == entry[key] + else: + assert str(d[key]) == entry[key] - for key in d: - if entry[key][0] == "{": - self.assertEqual("{" + str(d[key]) + "}", entry[key]) - else: - self.assertEqual(str(d[key]), entry[key]) + os.remove(output) - os.remove(output) - def test_mendeley(self): - source = os.path.join(dirname, "library_mendeley.bib") - output = os.path.join(dirname, "output.bib") - data = os.path.join(dirname, "library.yaml") - gbib.bibtex.GbibClean(["-f", "-o", output, source]) +def test_mendeley(): + source = os.path.join(dirname, "library_mendeley.bib") + output = os.path.join(dirname, "output.bib") + data = os.path.join(dirname, "library.yaml") + gbib.bibtex.GbibClean(["-f", "-o", output, source]) - with open(output) as file: - bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) + with open(output) as file: + bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) - with open(data) as file: - data = yaml.load(file.read(), Loader=yaml.FullLoader) + with open(data) as file: + data = yaml.load(file.read(), Loader=yaml.FullLoader) - for entry in bib.entries: - d = data[entry["ID"]] + for entry in bib.entries: + d = data[entry["ID"]] - for key in d: - if entry[key][0] == "{": - self.assertEqual("{" + str(d[key]) + "}", entry[key]) - else: - self.assertEqual(str(d[key]), entry[key]) + for key in d: + if entry[key][0] == "{": + assert "{" + str(d[key]) + "}" == entry[key] + else: + assert str(d[key]) == entry[key] - os.remove(output) + os.remove(output) - def test_hidden_doi_arxiv(self): - source = os.path.join(dirname, "library_hidden_doi_arxiv.bib") - output = os.path.join(dirname, "output.bib") - data = os.path.join(dirname, "library.yaml") - gbib.bibtex.GbibClean(["-f", "-o", output, source]) - with open(output) as file: - bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) +def test_hidden_doi_arxiv(): + source = os.path.join(dirname, "library_hidden_doi_arxiv.bib") + output = os.path.join(dirname, "output.bib") + data = os.path.join(dirname, "library.yaml") + gbib.bibtex.GbibClean(["-f", "-o", output, source]) - with open(data) as file: - data = yaml.load(file.read(), Loader=yaml.FullLoader) + with open(output) as file: + bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) - for entry in bib.entries: - d = data[entry["ID"]] + with open(data) as file: + data = yaml.load(file.read(), Loader=yaml.FullLoader) - for key in d: - if entry[key][0] == "{": - self.assertEqual("{" + str(d[key]) + "}", entry[key]) - else: - self.assertEqual(str(d[key]), entry[key]) + for entry in bib.entries: + d = data[entry["ID"]] - os.remove(output) + for key in d: + if entry[key][0] == "{": + assert "{" + str(d[key]) + "}" == entry[key] + else: + assert str(d[key]) == entry[key] - def test_rename_field(self): - source = os.path.join(dirname, "library_hidden_doi_arxiv.bib") - output = os.path.join(dirname, "output.bib") - data = os.path.join(dirname, "library.yaml") - gbib.bibtex.GbibClean(["-f", "-o", output, source, "--rename-field", "arxivid", "eprint"]) + os.remove(output) - with open(output) as file: - bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) - with open(data) as file: - data = yaml.load(file.read(), Loader=yaml.FullLoader) +def test_rename_field(): + source = os.path.join(dirname, "library_hidden_doi_arxiv.bib") + output = os.path.join(dirname, "output.bib") + data = os.path.join(dirname, "library.yaml") + gbib.bibtex.GbibClean(["-f", "-o", output, source, "--rename-field", "arxivid", "eprint"]) - for key in data: - data[key]["eprint"] = data[key].pop("arxivid") + with open(output) as file: + bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) - for entry in bib.entries: - d = data[entry["ID"]] + with open(data) as file: + data = yaml.load(file.read(), Loader=yaml.FullLoader) - for key in d: - if entry[key][0] == "{": - self.assertEqual("{" + str(d[key]) + "}", entry[key]) - else: - self.assertEqual(str(d[key]), entry[key]) + for key in data: + data[key]["eprint"] = data[key].pop("arxivid") - os.remove(output) + for entry in bib.entries: + d = data[entry["ID"]] - def test_missing_doi_arxiv(self): - source = os.path.join(dirname, "library_missing_doi_arxiv.bib") - output = os.path.join(dirname, "output.yaml") - data = os.path.join(dirname, "library.yaml") - subprocess.check_output(["GbibDiscover", "--arxiv", "-s", "-f", "-o", output, source]) + for key in d: + if entry[key][0] == "{": + assert "{" + str(d[key]) + "}" == entry[key] + else: + assert str(d[key]) == entry[key] - with open(output) as file: - discover = yaml.load(file.read(), Loader=yaml.FullLoader) + os.remove(output) - with open(data) as file: - data = yaml.load(file.read(), Loader=yaml.FullLoader) - self.assertTrue("DeGeus2015a" in discover) - self.assertTrue("DeGeus2019" in discover) - self.assertEqual(len(discover), 2) - self.assertEqual(len(discover["DeGeus2015a"]), 1) - self.assertEqual(len(discover["DeGeus2019"]), 1) - self.assertEqual(len(discover["DeGeus2015a"]["doi"]), 1) - self.assertEqual(len(discover["DeGeus2019"]["arxivid"]), 1) - self.assertEqual(data["DeGeus2015a"]["doi"], discover["DeGeus2015a"]["doi"][0]) - self.assertTrue( - re.match( - "(" + str(data["DeGeus2019"]["arxivid"]) + ")([v]?)([0-9]*)", - discover["DeGeus2019"]["arxivid"][0], - ) - ) +def test_missing_doi_arxiv(): + source = os.path.join(dirname, "library_missing_doi_arxiv.bib") + output = os.path.join(dirname, "output.yaml") + data = os.path.join(dirname, "library.yaml") + subprocess.check_output(["GbibDiscover", "--arxiv", "-s", "-f", "-o", output, source]) - os.remove(output) + with open(output) as file: + discover = yaml.load(file.read(), Loader=yaml.FullLoader) - def test_arxiv_preprint(self): - source = os.path.join(dirname, "library_arxiv_preprint.bib") - output = os.path.join(dirname, "output.bib") - data = os.path.join(dirname, "library_arxiv_preprint.yaml") - subprocess.check_output( - ["GbibClean", "-f", "--arxiv", "arXiv preprint: {}", "-o", output, source] - ) + with open(data) as file: + data = yaml.load(file.read(), Loader=yaml.FullLoader) - with open(output) as file: - bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) + assert "DeGeus2015a" in discover + assert "DeGeus2019" in discover + assert len(discover) == 2 + assert len(discover["DeGeus2015a"]) == 1 + assert len(discover["DeGeus2019"]) == 1 + assert len(discover["DeGeus2015a"]["doi"]) == 1 + assert len(discover["DeGeus2019"]["arxivid"]) == 1 + assert data["DeGeus2015a"]["doi"] == discover["DeGeus2015a"]["doi"][0] + assert re.match( + "(" + str(data["DeGeus2019"]["arxivid"]) + ")([v]?)([0-9]*)", + discover["DeGeus2019"]["arxivid"][0], + ) - with open(data) as file: - data = yaml.load(file.read(), Loader=yaml.FullLoader) + os.remove(output) - for entry in bib.entries: - d = data[entry["ID"]] - for key in d: - if entry[key][0] == "{": - self.assertEqual("{" + str(d[key]) + "}", entry[key]) - else: - self.assertEqual(str(d[key]), entry[key]) +def test_arxiv_preprint(): + source = os.path.join(dirname, "library_arxiv_preprint.bib") + output = os.path.join(dirname, "output.bib") + data = os.path.join(dirname, "library_arxiv_preprint.yaml") + subprocess.check_output( + ["GbibClean", "-f", "--arxiv", "arXiv preprint: {}", "-o", output, source] + ) - os.remove(output) + with open(output) as file: + bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) + + with open(data) as file: + data = yaml.load(file.read(), Loader=yaml.FullLoader) - def test_authorsep(self): + for entry in bib.entries: + d = data[entry["ID"]] + + for key in d: + if entry[key][0] == "{": + assert "{" + str(d[key]) + "}" == entry[key] + else: + assert str(d[key]) == entry[key] + + os.remove(output) + + +def test_authorsep(): + source = os.path.join(dirname, "library_mendeley.bib") + output = os.path.join(dirname, "output.bib") + data = os.path.join(dirname, "library.yaml") + gbib.bibtex.GbibClean(["-f", "--author-sep", " ", "-o", output, source]) + + with open(output) as file: + bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) + + with open(data) as file: + data = yaml.load(file.read(), Loader=yaml.FullLoader) + + for key in data: + data[key]["author"] = data[key]["author"].replace("T.W.J.", "T. W. J.") + data[key]["author"] = data[key]["author"].replace("R.H.J.", "R. H. J.") + data[key]["author"] = data[key]["author"].replace("M.G.D.", "M. G. D.") + data[key]["author"] = data[key]["author"].replace("C.B.", "C. B.") + + for entry in bib.entries: + d = data[entry["ID"]] + + for key in d: + if entry[key][0] == "{": + assert "{" + str(d[key]) + "}" == entry[key] + else: + assert str(d[key]) == entry[key] + + os.remove(output) + + +def test_no_title(): + source = os.path.join(dirname, "library_mendeley.bib") + output = os.path.join(dirname, "output.bib") + gbib.bibtex.GbibClean(["-f", "--no-title", "-o", output, source]) + + with open(output) as file: + bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) + + for entry in bib.entries: + assert "title" not in entry + + os.remove(output) + + +def test_journalrename(): + lookup = dict( + official={ + "IJSS": "International Journal of Solids and Structures", + "PNAS": "Proceedings of the National Academy of Sciences", + "MRS": "Mechanics Research Communications", + }, + abbreviation={ + "IJSS": "Int. J. Solids Struct.", + "PNAS": "Proc. Natl. Acad. Sci.", + "MRS": "Mech. Res. Commun.", + }, + acronym={ + "IJSS": "IJSS", + "PNAS": "PNAS", + "MRS": "Mech. Res. Commun.", + }, + ) + + for key in lookup: source = os.path.join(dirname, "library_mendeley.bib") output = os.path.join(dirname, "output.bib") data = os.path.join(dirname, "library.yaml") - gbib.bibtex.GbibClean(["-f", "--author-sep", " ", "-o", output, source]) + gbib.bibtex.GbibClean(["-f", "-j", key, "-o", output, source]) with open(output) as file: bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) @@ -179,83 +240,18 @@ def test_authorsep(self): with open(data) as file: data = yaml.load(file.read(), Loader=yaml.FullLoader) - for key in data: - data[key]["author"] = data[key]["author"].replace("T.W.J.", "T. W. J.") - data[key]["author"] = data[key]["author"].replace("R.H.J.", "R. H. J.") - data[key]["author"] = data[key]["author"].replace("M.G.D.", "M. G. D.") - data[key]["author"] = data[key]["author"].replace("C.B.", "C. B.") + data["DeGeus2015a"]["journal"] = lookup[key]["IJSS"] + data["DeGeus2015b"]["journal"] = lookup[key]["IJSS"] + data["DeGeus2019"]["journal"] = lookup[key]["PNAS"] + data["DeGeus2013"]["journal"] = lookup[key]["MRS"] for entry in bib.entries: d = data[entry["ID"]] for key in d: if entry[key][0] == "{": - self.assertEqual("{" + str(d[key]) + "}", entry[key]) + assert "{" + str(d[key]) + "}" == entry[key] else: - self.assertEqual(str(d[key]), entry[key]) + assert str(d[key]) == entry[key] os.remove(output) - - def test_no_title(self): - source = os.path.join(dirname, "library_mendeley.bib") - output = os.path.join(dirname, "output.bib") - gbib.bibtex.GbibClean(["-f", "--no-title", "-o", output, source]) - - with open(output) as file: - bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) - - for entry in bib.entries: - self.assertFalse("title" in entry) - - os.remove(output) - - def test_journalrename(self): - lookup = dict( - official={ - "IJSS": "International Journal of Solids and Structures", - "PNAS": "Proceedings of the National Academy of Sciences", - "MRS": "Mechanics Research Communications", - }, - abbreviation={ - "IJSS": "Int. J. Solids Struct.", - "PNAS": "Proc. Natl. Acad. Sci.", - "MRS": "Mech. Res. Commun.", - }, - acronym={ - "IJSS": "IJSS", - "PNAS": "PNAS", - "MRS": "Mech. Res. Commun.", - }, - ) - - for key in lookup: - source = os.path.join(dirname, "library_mendeley.bib") - output = os.path.join(dirname, "output.bib") - data = os.path.join(dirname, "library.yaml") - gbib.bibtex.GbibClean(["-f", "-j", key, "-o", output, source]) - - with open(output) as file: - bib = bibtexparser.load(file, parser=bibtexparser.bparser.BibTexParser()) - - with open(data) as file: - data = yaml.load(file.read(), Loader=yaml.FullLoader) - - data["DeGeus2015a"]["journal"] = lookup[key]["IJSS"] - data["DeGeus2015b"]["journal"] = lookup[key]["IJSS"] - data["DeGeus2019"]["journal"] = lookup[key]["PNAS"] - data["DeGeus2013"]["journal"] = lookup[key]["MRS"] - - for entry in bib.entries: - d = data[entry["ID"]] - - for key in d: - if entry[key][0] == "{": - self.assertEqual("{" + str(d[key]) + "}", entry[key]) - else: - self.assertEqual(str(d[key]), entry[key]) - - os.remove(output) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_bibtex.py b/tests/test_bibtex.py index 72b1401..deaa8f7 100644 --- a/tests/test_bibtex.py +++ b/tests/test_bibtex.py @@ -1,81 +1,72 @@ -import unittest +import pytest import GooseBib as bib -class Test_bibtex(unittest.TestCase): - """ - GooseBib.bibtex - """ - - def test_clever_merge(self): - text = """ - @article{DeGeus2021, - author = {De Geus, T.W.J}, - title = {My new hello world}, - journal = {Journal of GooseBib}, - year = {2021}, - volume = {1}, - number = {1}, - pages = {1--6} - } - - @article{DeGeus2019, - author = {De Geus, T.W.J}, - title = {My new hello world}, - journal = {arXiv preprint: arXiv:1901.00001}, - year = {2019} - } +def test_clever_merge(): + text = """ + @article{DeGeus2021, + author = {De Geus, T.W.J}, + title = {My new hello world}, + journal = {Journal of GooseBib}, + year = {2021}, + volume = {1}, + number = {1}, + pages = {1--6} + } - @article{DeGeus21, - author = {De Geus, T.W.J}, - title = {My new hello world}, - journal = {Journal of GooseBib}, - year = {2021}, - volume = {1}, - number = {1}, - pages = {1--6}, - doi = {10.1234/1234567890} - } - """ + @article{DeGeus2019, + author = {De Geus, T.W.J}, + title = {My new hello world}, + journal = {arXiv preprint: arXiv:1901.00001}, + year = {2019} + } - out = """ - @article{DeGeus2021, - author = {De Geus, T.W.J}, - title = {My new hello world}, - journal = {Journal of GooseBib}, - year = {2021}, - volume = {1}, - number = {1}, - pages = {1--6}, - doi = {10.1234/1234567890} - } - - @article{DeGeus2019, - author = {De Geus, T.W.J}, - title = {My new hello world}, - journal = {arXiv preprint: arXiv:1901.00001}, - year = {2019} - } - """ + @article{DeGeus21, + author = {De Geus, T.W.J}, + title = {My new hello world}, + journal = {Journal of GooseBib}, + year = {2021}, + volume = {1}, + number = {1}, + pages = {1--6}, + doi = {10.1234/1234567890} + } + """ - out = out.split("\n") - out[1] = out[1].strip(" ") - out[10] = out[10].strip(" ") - out[12] = out[12].strip(" ") - out[-2] = out[-2].strip(" ") - out[-1] = out[-1].strip(" ") - out = "\n".join(out[1:]) + out = """ + @article{DeGeus2021, + author = {De Geus, T.W.J}, + title = {My new hello world}, + journal = {Journal of GooseBib}, + year = {2021}, + volume = {1}, + number = {1}, + pages = {1--6}, + doi = {10.1234/1234567890} + } - self.assertEqual(bib.bibtex.clever_merge(text)[0], out) + @article{DeGeus2019, + author = {De Geus, T.W.J}, + title = {My new hello world}, + journal = {arXiv preprint: arXiv:1901.00001}, + year = {2019} + } + """ - text = text.split("\n") - text[-5] = " number = {100}," - text = "\n".join(text) + out = out.split("\n") + out[1] = out[1].strip(" ") + out[10] = out[10].strip(" ") + out[12] = out[12].strip(" ") + out[-2] = out[-2].strip(" ") + out[-1] = out[-1].strip(" ") + out = "\n".join(out[1:]) - with self.assertWarns(Warning): - self.assertEqual(bib.bibtex.clever_merge(text)[0], out) + assert bib.bibtex.clever_merge(text)[0] == out + text = text.split("\n") + text[-5] = " number = {100}," + text = "\n".join(text) -if __name__ == "__main__": - unittest.main() + with pytest.warns(Warning): + assert bib.bibtex.clever_merge(text)[0] == out diff --git a/tests/test_journals.py b/tests/test_journals.py index 423b7d5..8a12ba6 100644 --- a/tests/test_journals.py +++ b/tests/test_journals.py @@ -1,209 +1,203 @@ -import unittest - import numpy as np import GooseBib as bib -class Test_journals(unittest.TestCase): - """ - GooseBib.journals - """ +def test_Journal(): + name = "name" + abbreviation = "abbr" + acronym = "acro" + entry = bib.journals.Journal( + name=name, abbreviation=abbreviation, variations=[name, abbreviation] + ) + entry = entry.unique() + + assume = dict( + name=name, + abbreviation=abbreviation, + ) + + assert assume == dict(entry) + + # overwrite abbreviation + + entry.set_abbreviation(name) + entry = entry.unique() + + assume = dict( + name=name, + variations=[abbreviation], + ) + + assert assume == dict(entry) + + entry.set_abbreviation(abbreviation) + entry = entry.unique() + + assume = dict( + name=name, + abbreviation=abbreviation, + ) + + assert assume == dict(entry) + + # add acronym + + entry.set_acronym(acronym) + entry = entry.unique() + + assume = dict( + name=name, + abbreviation=abbreviation, + acronym=acronym, + ) + + assert assume == dict(entry) + + # overwrite name + + entry.set_name(abbreviation) + entry = entry.unique() + + assume = dict( + name=abbreviation, + acronym=acronym, + variations=[name], + ) + + assert assume == dict(entry) + + entry.set_name(name) + entry.set_abbreviation(abbreviation) + entry = entry.unique() + + # overwrite variations - def test_Journal(self): - name = "name" - abbreviation = "abbr" - acronym = "acro" - entry = bib.journals.Journal( - name=name, abbreviation=abbreviation, variations=[name, abbreviation] - ) - entry = entry.unique() + entry.add_variation(name) + entry.add_variations([abbreviation, acronym]) + entry = entry.unique() - assume = dict( - name=name, - abbreviation=abbreviation, - ) + assume = dict( + name=name, + abbreviation=abbreviation, + acronym=acronym, + ) - self.assertEqual(assume, dict(entry)) + assert assume == dict(entry) - # overwrite abbreviation - entry.set_abbreviation(name) - entry = entry.unique() +def test_JournalList(): + lst = bib.journals.JournalList() + lst.names = np.array(["a", "b", "c", "a", "d"]) + lst.index = np.array([0, 0, 3, 2, 2]) + lst.name = -1 * np.array([1, 0, 0, 1, 0]) + lst.abbr = -1 * np.array([0, 1, 0, 0, 0]) + lst.acro = -1 * np.array([0, 0, 0, 0, 1]) + lst._renum() - assume = dict( - name=name, - variations=[abbreviation], - ) + m = [] + for i in lst: + m += [dict(i)] - self.assertEqual(assume, dict(entry)) + n = [] + for i in lst: + n += [dict(i)] - entry.set_abbreviation(abbreviation) - entry = entry.unique() + mylist = [dict(i) for i in lst.tolist()] - assume = dict( - name=name, - abbreviation=abbreviation, - ) + assert m == n + assert m == mylist - self.assertEqual(assume, dict(entry)) + lst.unique() - # add acronym + assume = [dict(name="a", abbreviation="b", variations=["d"]), dict(name="c")] - entry.set_acronym(acronym) - entry = entry.unique() + assert assume == [dict(i) for i in lst.tolist()] - assume = dict( - name=name, - abbreviation=abbreviation, - acronym=acronym, - ) - self.assertEqual(assume, dict(entry)) +def test_JournalList_no_force_first(): + lst = bib.journals.JournalList() + lst.names = np.array(["a", "b", "c", "a", "d"]) + lst.index = np.array([0, 0, 3, 2, 2]) + lst.name = -1 * np.array([1, 0, 0, 1, 0]) + lst.abbr = -1 * np.array([0, 1, 0, 0, 0]) + lst.acro = -1 * np.array([0, 0, 0, 0, 1]) + lst._renum() - # overwrite name + m = [] + for i in lst: + m += [dict(i)] - entry.set_name(abbreviation) - entry = entry.unique() + n = [] + for i in lst: + n += [dict(i)] - assume = dict( - name=abbreviation, - acronym=acronym, - variations=[name], - ) + mylist = [dict(i) for i in lst.tolist()] - self.assertEqual(assume, dict(entry)) + assert m == n + assert m == mylist - entry.set_name(name) - entry.set_abbreviation(abbreviation) - entry = entry.unique() + lst.unique(force_first=False) - # overwrite variations + assume = [dict(name="a", abbreviation="b", acronym="d"), dict(name="c")] - entry.add_variation(name) - entry.add_variations([abbreviation, acronym]) - entry = entry.unique() + assert assume == [dict(i) for i in lst.tolist()] - assume = dict( - name=name, - abbreviation=abbreviation, - acronym=acronym, - ) - self.assertEqual(assume, dict(entry)) +def test_JournalList_map(): + lst = bib.journals.JournalList() + lst.names = np.array(["a", "b", "c", "a", "d"]) + lst.index = np.array([0, 0, 3, 2, 2]) + lst.name = -1 * np.array([1, 0, 0, 1, 0]) + lst.abbr = -1 * np.array([0, 1, 0, 0, 0]) + lst.acro = -1 * np.array([0, 0, 0, 0, 1]) + lst._renum() + lst.unique() + + assert lst.map2name(["b", "foo", "d"]) == ["a", "foo", "a"] + assert lst.map2abbreviation(["b", "foo", "d"]) == ["b", "foo", "b"] + assert lst.map2acronym(["b", "foo", "d"]) == ["a", "foo", "a"] - def test_JournalList(self): - lst = bib.journals.JournalList() - lst.names = np.array(["a", "b", "c", "a", "d"]) - lst.index = np.array([0, 0, 3, 2, 2]) - lst.name = -1 * np.array([1, 0, 0, 1, 0]) - lst.abbr = -1 * np.array([0, 1, 0, 0, 0]) - lst.acro = -1 * np.array([0, 0, 0, 0, 1]) - lst._renum() - - m = [] - for i in lst: - m += [dict(i)] - - n = [] - for i in lst: - n += [dict(i)] - - mylist = [dict(i) for i in lst.tolist()] - - self.assertEqual(m, n) - self.assertEqual(m, mylist) - - lst.unique() - - assume = [dict(name="a", abbreviation="b", variations=["d"]), dict(name="c")] - self.assertEqual(assume, [dict(i) for i in lst.tolist()]) - - def test_JournalList_no_force_first(self): - lst = bib.journals.JournalList() - lst.names = np.array(["a", "b", "c", "a", "d"]) - lst.index = np.array([0, 0, 3, 2, 2]) - lst.name = -1 * np.array([1, 0, 0, 1, 0]) - lst.abbr = -1 * np.array([0, 1, 0, 0, 0]) - lst.acro = -1 * np.array([0, 0, 0, 0, 1]) - lst._renum() - - m = [] - for i in lst: - m += [dict(i)] - - n = [] - for i in lst: - n += [dict(i)] - - mylist = [dict(i) for i in lst.tolist()] - - self.assertEqual(m, n) - self.assertEqual(m, mylist) - - lst.unique(force_first=False) - - assume = [dict(name="a", abbreviation="b", acronym="d"), dict(name="c")] - - self.assertEqual(assume, [dict(i) for i in lst.tolist()]) - - def test_JournalList_map(self): - lst = bib.journals.JournalList() - lst.names = np.array(["a", "b", "c", "a", "d"]) - lst.index = np.array([0, 0, 3, 2, 2]) - lst.name = -1 * np.array([1, 0, 0, 1, 0]) - lst.abbr = -1 * np.array([0, 1, 0, 0, 0]) - lst.acro = -1 * np.array([0, 0, 0, 0, 1]) - lst._renum() - lst.unique() - - self.assertEqual(lst.map2name(["b", "foo", "d"]), ["a", "foo", "a"]) - self.assertEqual(lst.map2abbreviation(["b", "foo", "d"]), ["b", "foo", "b"]) - self.assertEqual(lst.map2acronym(["b", "foo", "d"]), ["a", "foo", "a"]) - - def test_JournalList_map_no_force_first(self): - lst = bib.journals.JournalList() - lst.names = np.array(["a", "b", "c", "a", "d"]) - lst.index = np.array([0, 0, 3, 2, 2]) - lst.name = -1 * np.array([1, 0, 0, 1, 0]) - lst.abbr = -1 * np.array([0, 1, 0, 0, 0]) - lst.acro = -1 * np.array([0, 0, 0, 0, 1]) - lst._renum() - lst.unique(force_first=False) - - self.assertEqual(lst.map2name(["b", "foo", "d"]), ["a", "foo", "a"]) - self.assertEqual(lst.map2abbreviation(["b", "foo", "d"]), ["b", "foo", "b"]) - self.assertEqual(lst.map2acronym(["b", "foo", "d"]), ["d", "foo", "d"]) - - def test_load(self): - variations = [ - "Proc. Nat. Acad. Sci.", - "Proc. Nat. Acad. Sci. U. S. A", - "Proc. Nat. Acad. Sci. U.S.A", - "Proc. Nat. Acad. Sci. U.S.A.", - "Proc. Nat. Acad. Sci. USA", - "Proc. Natl. Acad. Sci. U. S. A", - "Proc. Natl. Acad. Sci. U.S.A", - "Proc. Natl. Acad. Sci. U.S.A.", - "Proc. Natl. Acad. Sci. USA", - "Proceedings of the National Academy of Sciences of the United States of America", - "Proceedings of the National academy of Sciences of the United States of America", - ] - - expect = [ - { - "name": "Proceedings of the National Academy of Sciences", - "abbreviation": "Proc. Natl. Acad. Sci.", - "acronym": "PNAS", - "variations": variations, - } - ] - read = bib.journals.load("pnas") - read = [dict(r) for r in read.tolist()] - - self.assertEqual(expect, read) - - -if __name__ == "__main__": - unittest.main() +def test_JournalList_map_no_force_first(): + lst = bib.journals.JournalList() + lst.names = np.array(["a", "b", "c", "a", "d"]) + lst.index = np.array([0, 0, 3, 2, 2]) + lst.name = -1 * np.array([1, 0, 0, 1, 0]) + lst.abbr = -1 * np.array([0, 1, 0, 0, 0]) + lst.acro = -1 * np.array([0, 0, 0, 0, 1]) + lst._renum() + lst.unique(force_first=False) + + assert lst.map2name(["b", "foo", "d"]) == ["a", "foo", "a"] + assert lst.map2abbreviation(["b", "foo", "d"]) == ["b", "foo", "b"] + assert lst.map2acronym(["b", "foo", "d"]) == ["d", "foo", "d"] + + +def test_load(): + variations = [ + "Proc. Nat. Acad. Sci.", + "Proc. Nat. Acad. Sci. U. S. A", + "Proc. Nat. Acad. Sci. U.S.A", + "Proc. Nat. Acad. Sci. U.S.A.", + "Proc. Nat. Acad. Sci. USA", + "Proc. Natl. Acad. Sci. U. S. A", + "Proc. Natl. Acad. Sci. U.S.A", + "Proc. Natl. Acad. Sci. U.S.A.", + "Proc. Natl. Acad. Sci. USA", + "Proceedings of the National Academy of Sciences of the United States of America", + "Proceedings of the National academy of Sciences of the United States of America", + ] + + expect = [ + { + "name": "Proceedings of the National Academy of Sciences", + "abbreviation": "Proc. Natl. Acad. Sci.", + "acronym": "PNAS", + "variations": variations, + } + ] + read = bib.journals.load("pnas") + read = [dict(r) for r in read.tolist()] + + assert expect == read diff --git a/tests/test_recognise.py b/tests/test_recognise.py index 2fcb4c3..aa6c4b0 100644 --- a/tests/test_recognise.py +++ b/tests/test_recognise.py @@ -1,45 +1,26 @@ -import unittest - import GooseBib as bib -class Test_recognise(unittest.TestCase): - """ - GooseBib.recognise - """ - - def test_doi(self): - doi = "10.1073/pnas.1906551116" - self.assertEqual(doi, bib.recognise.doi(f"https://doi.org/{doi}")) - self.assertEqual(doi, bib.recognise.doi(f"https://dx.doi.org/{doi}")) - self.assertEqual(doi, bib.recognise.doi(*[f"https://doi.org/{doi}"])) - self.assertEqual(doi, bib.recognise.doi(dict(doi=f"https://doi.org/{doi}"))) - self.assertEqual( - doi, bib.recognise.doi(dict(doi=f"https://journals.aps.org/pre/abstract/{doi}")) - ) - self.assertEqual(doi, bib.recognise.doi(dict(doi=f"https://link.aps.org/doi/{doi}"))) - - self.assertEqual(doi, bib.recognise.doi(f"doi: {doi}")) - self.assertEqual(doi, bib.recognise.doi(f"doi:{doi}")) - - def test_arxiv(self): - arxivid = "1904.07635" - self.assertEqual(arxivid, bib.recognise.arxivid(f"https://arxiv.org/abs/{arxivid}")) - self.assertEqual(arxivid, bib.recognise.arxivid(*[f"https://arxiv.org/abs/{arxivid}"])) - self.assertEqual( - arxivid, - bib.recognise.arxivid(dict(eprint=f"https://arxiv.org/abs/{arxivid}")), - ) - self.assertEqual( - arxivid, - bib.recognise.arxivid(dict(arxivid=f"https://arxiv.org/abs/{arxivid}")), - ) - self.assertEqual(arxivid, bib.recognise.arxivid(f"arXiv preprint: {arxivid}")) - self.assertEqual(arxivid, bib.recognise.arxivid(f"arXiv prep. {arxivid}")) - self.assertEqual(arxivid, bib.recognise.arxivid(f"arXiv Prep. {arxivid}")) - self.assertEqual(arxivid, bib.recognise.arxivid(f"arXiv: {arxivid}")) - self.assertEqual(arxivid, bib.recognise.arxivid(f"arXiv:{arxivid}")) - - -if __name__ == "__main__": - unittest.main() +def test_doi(): + doi = "10.1073/pnas.1906551116" + assert doi == bib.recognise.doi(f"https://doi.org/{doi}") + assert doi == bib.recognise.doi(f"https://dx.doi.org/{doi}") + assert doi == bib.recognise.doi(*[f"https://doi.org/{doi}"]) + assert doi == bib.recognise.doi(dict(doi=f"https://doi.org/{doi}")) + assert doi == bib.recognise.doi(dict(doi=f"https://journals.aps.org/pre/abstract/{doi}")) + assert doi == bib.recognise.doi(dict(doi=f"https://link.aps.org/doi/{doi}")) + assert doi == bib.recognise.doi(f"doi: {doi}") + assert doi == bib.recognise.doi(f"doi:{doi}") + + +def test_arxiv(): + arxivid = "1904.07635" + assert arxivid == bib.recognise.arxivid(f"https://arxiv.org/abs/{arxivid}") + assert arxivid == bib.recognise.arxivid(*[f"https://arxiv.org/abs/{arxivid}"]) + assert arxivid == bib.recognise.arxivid(dict(eprint=f"https://arxiv.org/abs/{arxivid}")) + assert arxivid == bib.recognise.arxivid(dict(arxivid=f"https://arxiv.org/abs/{arxivid}")) + assert arxivid == bib.recognise.arxivid(f"arXiv preprint: {arxivid}") + assert arxivid == bib.recognise.arxivid(f"arXiv prep. {arxivid}") + assert arxivid == bib.recognise.arxivid(f"arXiv Prep. {arxivid}") + assert arxivid == bib.recognise.arxivid(f"arXiv: {arxivid}") + assert arxivid == bib.recognise.arxivid(f"arXiv:{arxivid}") diff --git a/tests/test_reformat.py b/tests/test_reformat.py index d248471..d84c98c 100644 --- a/tests/test_reformat.py +++ b/tests/test_reformat.py @@ -1,106 +1,59 @@ -import unittest - import GooseBib as bib -class Test_reformat(unittest.TestCase): - """ - GooseBib.reformat - """ - - def test_abbreviate_firstname(self): - self.assertEqual( - bib.reformat.abbreviate_firstname("de Geus, Thomas Willem Jan"), - "de Geus, T. W. J.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname("de Geus, Thomas Willem Jan", sep=" "), - "de Geus, T. W. J.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname("de Geus, Thomas W. J.", sep=" "), - "de Geus, T. W. J.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname("de Geus, Thomas W. J.\\", sep=" "), - "de Geus, T. W. J.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname("de Geus, Thomas W.J.", sep=" "), - "de Geus, T. W. J.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname(r"Molinari, Jean-Fran{\c c}ois", sep=" "), - "Molinari, J.- F.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname(r"Temizer, \.{I}.", sep=" "), - r"Temizer, \.{I}.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname(r"Temizer, \.{I}.\.{I}.", sep=" "), - r"Temizer, \.{I}. \.{I}.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname(r"Temizer, \.{I}.\.{I}zemer", sep=" "), - r"Temizer, \.{I}. \.{I}.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname(r"{Ben Arous}, {G{\'{e}}rard}", sep=" "), - r"{Ben Arous}, G.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname(r"Lema{\^{i}}tre, Ana{\"{e}}l", sep=" "), - r"Lema{\^{i}}tre, A.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname(r"Lema{\^i}tre, Ana{\"e}l", sep=" "), - r"Lema{\^i}tre, A.", - ) - - self.assertEqual( - bib.reformat.abbreviate_firstname(r"Manneville, S{\' e}bastien", sep=" "), - r"Manneville, S.", - ) - - self.assertEqual( - bib.reformat.autoformat_names( - r"Chattoraj, Joyjit and Caroli, Christiane and Lemaitre, Ana{\" e}l" - ), +def test_abbreviate_firstname(): + names = [ + ["de Geus, Thomas Willem Jan", " ", "de Geus, T. W. J."], + ["de Geus, Thomas Willem Jan", " ", "de Geus, T. W. J."], + ["de Geus, Thomas W. J.", " ", "de Geus, T. W. J."], + ["de Geus, Thomas W. J.\\", " ", "de Geus, T. W. J."], + ["de Geus, Thomas W.J.", " ", "de Geus, T. W. J."], + [r"Molinari, Jean-Fran{\c c}ois", " ", "Molinari, J.- F."], + [r"Temizer, \.{I}.", " ", r"Temizer, \.{I}."], + [r"Temizer, \.{I}.\.{I}.", " ", r"Temizer, \.{I}. \.{I}."], + [r"Temizer, \.{I}.\.{I}zemer", " ", r"Temizer, \.{I}. \.{I}."], + [r"{Ben Arous}, {G{\'{e}}rard}", " ", r"{Ben Arous}, G."], + [r"Lema{\^{i}}tre, Ana{\"{e}}l", " ", r"Lema{\^{i}}tre, A."], + [r"Lema{\^i}tre, Ana{\"e}l", " ", r"Lema{\^i}tre, A."], + [r"Manneville, S{\' e}bastien", " ", r"Manneville, S."], + ] + + for oldname, sep, newname in names: + assert bib.reformat.abbreviate_firstname(oldname, sep=sep) == newname + + +def test_autoformat_names(): + names = [ + [ + r"Chattoraj, Joyjit and Caroli, Christiane and Lemaitre, Ana{\" e}l", r"Chattoraj, J. and Caroli, C. and Lemaitre, A.", - ) + ] + ] + + for oldname, newname in names: + assert bib.reformat.autoformat_names(oldname) == newname + + +def test_protect_math(): + simple = r"$\tau$" + assert bib.reformat.protect_math(simple) == simple + - def test_protect_math(self): - simple = r"$\tau$" - self.assertEqual(bib.reformat.protect_math(simple), simple) +def test_rm_unicode(): + simple = r"$de Geus, Tom$" + assert bib.reformat.rm_unicode(simple) == simple - def test_rm_unicode(self): - simple = r"$de Geus, Tom$" - self.assertEqual(bib.reformat.rm_unicode(simple), simple) - def test_rm_accents(self): - self.assertEqual(bib.reformat.rm_accents("école"), "ecole") - self.assertEqual(bib.reformat.rm_accents("École"), "Ecole") +def test_rm_accents(): + assert bib.reformat.rm_accents("école") == "ecole" + assert bib.reformat.rm_accents("École") == "Ecole" - def test_name2key(self): - self.assertEqual(bib.reformat.name2key("de Geus, Tom"), "DeGeus") - def test_page_range(self): - self.assertEqual(bib.reformat.number_range("1-6"), "1--6") - self.assertEqual(bib.reformat.number_range("47–58"), "47--58") - self.assertEqual(bib.reformat.number_range("100"), "100") +def test_name2key(): + assert bib.reformat.name2key("de Geus, Tom") == "DeGeus" -if __name__ == "__main__": - unittest.main() +def test_page_range(): + assert bib.reformat.number_range("1-6") == "1--6" + assert bib.reformat.number_range("47–58") == "47--58" + assert bib.reformat.number_range("100") == "100" diff --git a/tests/test_tex.py b/tests/test_tex.py index 497ddd4..661a683 100644 --- a/tests/test_tex.py +++ b/tests/test_tex.py @@ -1,18 +1,7 @@ -import unittest - import GooseBib as bib -class Test_tex(unittest.TestCase): - """ - GooseBib.tex - """ - - def test_list_cise(self): - key = "DeGeus2021" - mytext = r"The authors of \cite{DeGeus2021} claim that ..." - self.assertEqual(bib.tex.list_cite(mytext), [key]) - - -if __name__ == "__main__": - unittest.main() +def test_list_cite(): + key = "DeGeus2021" + mytext = r"The authors of \cite{DeGeus2021} claim that ..." + assert bib.tex.list_cite(mytext) == [key]