summaryrefslogtreecommitdiffstats
path: root/python/tests/transform_csl.py
blob: 2b6fab3109950a2c7aa70231fc665e80491a5b73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
from typing import Any

import pytest
from fatcat_openapi_client import ReleaseEntity
from fixtures import api
from import_crossref import crossref_importer

from fatcat_tools.transforms import citeproc_csl, entity_from_json, release_to_csl


def test_csl_crossref(crossref_importer: Any) -> None:
    with open("tests/files/crossref-works.single.json", "r") as f:
        # not a single line
        raw = json.loads(f.read())
        r = crossref_importer.parse_record(raw)
    csl = release_to_csl(r)
    citeproc_csl(csl, "csl-json")
    citeproc_csl(csl, "bibtex")
    citeproc_csl(csl, "harvard1")
    citeproc_csl(csl, "harvard1", html=True)

    # check that with no author surnames, can't run
    for c in r.contribs:
        c.raw_name = None
        c.surname = None
    with pytest.raises(ValueError):
        release_to_csl(r)
    with pytest.raises(ValueError):
        csl = release_to_csl(r)
        citeproc_csl(csl, "csl-json")


def test_csl_pubmed(crossref_importer: Any) -> None:
    with open("tests/files/example_releases_pubmed19n0972.json", "r") as f:
        # multiple single lines
        for line in f:
            r = entity_from_json(line, ReleaseEntity)
            csl = release_to_csl(r)
            citeproc_csl(csl, "csl-json")
            citeproc_csl(csl, "bibtex")
            citeproc_csl(csl, "harvard1")
            citeproc_csl(csl, "harvard1", html=True)


def test_csl_pubmed_bibtex(crossref_importer: Any) -> None:
    with open("tests/files/example_releases_pubmed19n0972.json", "r") as f:
        r = entity_from_json(f.readline(), ReleaseEntity)
    csl = release_to_csl(r)
    print(citeproc_csl(csl, "bibtex"))
    # TODO: what's with the '`' in volume?
    assert (
        citeproc_csl(csl, "bibtex").strip()
        == """
@article{mędrela-kuder_szymura_2018, 
  title={Selected anti-health behaviours among women with osteoporosis}, 
  volume={69`}, 
  ISSN={0035-7715}, 
  DOI={10.32394/rpzh.2018.0046}, 
  abstractNote={In the prevention of osteoporosis and its treatment, it is important to prevent bone loss by reducing the occurrence of factors determining human health, which reduce the risk of osteoporosis, such as health behaviors.}, 
  number={4}, 
  journal={Roczniki Panstwowego Zakladu Higieny}, 
  author={Mędrela-Kuder and Szymura}, 
  year={2018}
  }
    """.strip()
    )
    assert (
        citeproc_csl(csl, "harvard1", html=True).strip()
        == """
    Mędrela-Kuder and Szymura (2018) ‘Selected anti-health behaviours among women with osteoporosis’, <i>Roczniki Panstwowego Zakladu Higieny</i>, 69`(4). doi: 10.32394/rpzh.2018.0046.
    """.strip()
    )