summaryrefslogtreecommitdiffstats
path: root/python/tests/import_jalc.py
blob: 4ebc87b45a4c52cfa4ca7d6517e3276f95a6a7d4 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pytest
from bs4 import BeautifulSoup
from fixtures import *

from fatcat_tools.importers import Bs4XmlFilePusher, Bs4XmlLinesPusher, JalcImporter


@pytest.fixture(scope="function")
def jalc_importer(api):
    with open("tests/files/ISSN-to-ISSN-L.snip.txt", "r") as issn_file:
        yield JalcImporter(
            api, issn_file, extid_map_file="tests/files/example_map.sqlite3", bezerk_mode=True
        )


@pytest.fixture(scope="function")
def jalc_importer_existing(api):
    with open("tests/files/ISSN-to-ISSN-L.snip.txt", "r") as issn_file:
        yield JalcImporter(
            api, issn_file, extid_map_file="tests/files/example_map.sqlite3", bezerk_mode=False
        )


def test_jalc_importer(jalc_importer):
    last_index = jalc_importer.api.get_changelog(limit=1)[0].index
    with open("tests/files/jalc_lod_sample.xml", "r") as f:
        jalc_importer.bezerk_mode = True
        counts = Bs4XmlFilePusher(jalc_importer, f, "Description").run()
    assert counts["insert"] == 2
    assert counts["exists"] == 0
    assert counts["skip"] == 0

    # fetch most recent editgroup
    change = jalc_importer.api.get_changelog_entry(index=last_index + 1)
    eg = change.editgroup
    assert eg.description
    assert "jalc" in eg.description.lower()
    assert eg.extra["git_rev"]
    assert "fatcat_tools.JalcImporter" in eg.extra["agent"]

    last_index = jalc_importer.api.get_changelog(limit=1)[0].index
    with open("tests/files/jalc_lod_sample.xml", "r") as f:
        jalc_importer.bezerk_mode = False
        jalc_importer.reset()
        counts = Bs4XmlFilePusher(jalc_importer, f, "Description").run()
    assert counts["insert"] == 0
    assert counts["exists"] == 2
    assert counts["skip"] == 0
    assert last_index == jalc_importer.api.get_changelog(limit=1)[0].index


def test_jalc_importer_lines(jalc_importer):
    last_index = jalc_importer.api.get_changelog(limit=1)[0].index
    with open("tests/files/jalc_rdf_sample_100.xml", "r") as f:
        jalc_importer.bezerk_mode = True
        counts = Bs4XmlLinesPusher(jalc_importer, f, "<rdf:Description").run()
    assert counts["insert"] == 93
    assert counts["exists"] == 0
    assert counts["skip"] == 0

    # fetch most recent editgroup
    change = jalc_importer.api.get_changelog_entry(index=last_index + 1)
    eg = change.editgroup
    assert eg.description
    assert "jalc" in eg.description.lower()
    assert eg.extra["git_rev"]
    assert "fatcat_tools.JalcImporter" in eg.extra["agent"]

    last_index = jalc_importer.api.get_changelog(limit=1)[0].index
    with open("tests/files/jalc_rdf_sample_100.xml", "r") as f:
        jalc_importer.bezerk_mode = False
        jalc_importer.reset()
        counts = Bs4XmlLinesPusher(jalc_importer, f, "<rdf:Description").run()
    assert counts["insert"] == 0
    assert counts["exists"] == 93
    assert counts["skip"] == 0
    assert last_index == jalc_importer.api.get_changelog(limit=1)[0].index


def test_jalc_xml_parse(jalc_importer):
    with open("tests/files/jalc_lod_sample.xml", "r") as f:
        soup = BeautifulSoup(f, "xml")
        r = jalc_importer.parse_record(soup.find_all("Description")[0])

    print(r.extra)
    assert r.title == "New carbides in the Ni-Ti-Mo-C system"
    assert r.subtitle is None
    assert r.original_title == "Ni-Ti-Mo-C系に出現する新炭化物相について"
    assert r.publisher == "Japan Society of Powder and Powder Metallurgy"
    assert r.release_type == "article-journal"
    assert r.release_stage == "published"
    assert r.license_slug is None
    assert r.ext_ids.doi == "10.2497/jjspm.36.898"
    assert r.language == "ja"
    assert r.volume == "36"
    assert r.issue == "8"
    assert r.pages == "898-902"
    assert r.release_year == 1989
    # matched by ISSN, so shouldn't be in there?
    # assert extra['container_name'] == "International Journal of Quantum Chemistry"
    assert len(r.contribs) == 4

    assert r.contribs[0].raw_name == "Yasuhiko Hashimoto"
    assert r.contribs[0].given_name == "Yasuhiko"
    assert r.contribs[0].surname == "Hashimoto"
    assert r.contribs[0].extra["original_name"]["raw_name"] == "橋本 雍彦"
    assert r.contribs[0].extra["original_name"]["given_name"] == "雍彦"
    assert r.contribs[0].extra["original_name"]["surname"] == "橋本"

    assert r.contribs[3].raw_name == "Teruo Takahashi"
    assert r.contribs[3].given_name == "Teruo"
    assert r.contribs[3].surname == "Takahashi"
    assert r.contribs[3].extra["original_name"]["raw_name"] == "高橋 輝男"
    assert r.contribs[3].extra["original_name"]["given_name"] == "輝男"
    assert r.contribs[3].extra["original_name"]["surname"] == "高橋"

    assert not r.refs