aboutsummaryrefslogtreecommitdiffstats
path: root/python/tests/transform_elasticsearch.py
blob: cee37867f082d76363f07ffa673bf1e6a62d85d4 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import datetime
import json

from fatcat_openapi_client import (
    ChangelogEntry,
    ContainerEntity,
    FileEntity,
    FileUrl,
    ReleaseEntity,
    ReleaseExtIds,
    ReleaseRef,
)
from fixtures import api
from import_crossref import crossref_importer
from import_journal_metadata import journal_metadata_importer

from fatcat_tools.transforms import (
    changelog_to_elasticsearch,
    container_to_elasticsearch,
    entity_from_json,
    file_to_elasticsearch,
    release_to_elasticsearch,
)


def test_basic_elasticsearch_convert(crossref_importer):
    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)
    r.state = 'active'
    release_to_elasticsearch(r)

def test_rich_elasticsearch_convert():
    r = ReleaseEntity(
        title="something",
        release_year=1234,
        license_slug="CC-BY-NC",
        ext_ids=ReleaseExtIds(),
        refs=[
            ReleaseRef(),
            ReleaseRef(target_release_id="iznnn644szdwva7khyxqzc73bi"),
        ],
    )
    r.state = 'active'
    r.container = ContainerEntity(
        name="dummy journal",
        extra={
            "ia": {
                "sim": {
                    "year_spans": [[1000, 1100]],
                },
            },
            "kbart": {
                "lockss": {
                    "year_spans": [[1200, 1300]],
                },
                "jstor": {
                    "year_spans": [[1000, 1300], [1950, 1960], [1980, 2005]],
                },
            },
            "sherpa_romeo": {"color": "blue"},
            "doaj": {"as_of": "2010-02-03"},
        },
    )
    r.files = [FileEntity(
        mimetype="application/pdf",
        urls=[
            FileUrl(rel="dweb", url="dat://a954329dlk/thingie"),
            FileUrl(rel="webarchive", url="https://web.archive.org/web/20001122030405/http://example.com"),
            FileUrl(rel="web", url="https://archive.org/details/blah/file.pdf"),
        ],
        extra={
            "shadows": {},
        },
    )]
    es = release_to_elasticsearch(r)
    assert es['release_year'] == r.release_year
    assert es['file_count'] == 1
    assert es['fileset_count'] == 0
    assert es['webcapture_count'] == 0
    assert es['ref_count'] == 2
    assert es['ref_linked_count'] == 1

    assert es['preservation'] == "bright"
    assert es['is_oa'] is True
    assert es['is_longtail_oa'] is False
    assert es['is_preserved'] is True
    assert es['in_web'] is True
    assert es['in_dweb'] is True
    assert es['in_ia'] is True
    assert es['in_ia_sim'] is False
    assert es['in_kbart'] is True
    assert es['in_jstor'] is True

def test_elasticsearch_release_from_json():
    r = entity_from_json(open('./tests/files/release_etodop5banbndg3faecnfm6ozi.json', 'r').read(), ReleaseEntity)
    es = release_to_elasticsearch(r)

    assert es['subtitle'] == "Correpondence"
    assert es['ident'] == "etodop5banbndg3faecnfm6ozi"
    assert es['container_name'] == "BJOG: an International Journal of Obstetrics and Gynaecology"
    assert es['first_page'] == "1404"
    assert es['issue'] == "11"
    assert es['volume'] == "118"
    assert es['number'] is None

    assert es['preservation'] == "dark"
    assert es['is_oa'] is False
    assert es['is_longtail_oa'] is False
    assert es['is_preserved'] is True
    assert es['in_web'] is False
    assert es['in_dweb'] is False
    assert es['in_ia'] is False
    assert es['in_ia_sim'] is True
    assert es['in_kbart'] is True
    assert es['in_jstor'] is False

    # this release has a fileset, and no file
    r = entity_from_json(open('./tests/files/release_3mssw2qnlnblbk7oqyv2dafgey.json', 'r').read(), ReleaseEntity)
    es = release_to_elasticsearch(r)

    assert es['title'] == "Jakobshavn Glacier Bed Elevation"
    assert es['ident'] == "3mssw2qnlnblbk7oqyv2dafgey"
    assert es['file_count'] == 0
    assert es['fileset_count'] == 1
    assert es['webcapture_count'] == 0

    assert es['preservation'] == "dark"
    assert es['is_oa'] is True
    assert es['is_longtail_oa'] is False
    assert es['is_preserved'] is True
    assert es['in_web'] is True
    assert es['in_dweb'] is True
    assert es['in_ia'] is False
    assert es['in_ia_sim'] is False
    assert es['in_kbart'] is False
    assert es['in_jstor'] is False

    # this release has a web capture, and no file (edited the JSON to remove file)
    r = entity_from_json(open('./tests/files/release_mjtqtuyhwfdr7j2c3l36uor7uy.json', 'r').read(), ReleaseEntity)
    es = release_to_elasticsearch(r)

    assert es['title'] == "Rethinking Personal Digital Archiving, Part 1"
    assert es['ident'] == "mjtqtuyhwfdr7j2c3l36uor7uy"
    assert es['file_count'] == 0
    assert es['fileset_count'] == 0
    assert es['webcapture_count'] == 1

    assert es['preservation'] == "bright"
    assert es['is_oa'] is True
    assert es['is_longtail_oa'] is False
    assert es['is_preserved'] is True
    assert es['in_web'] is True
    assert es['in_dweb'] is False
    assert es['in_ia'] is True
    assert es['in_ia_sim'] is False
    assert es['in_kbart'] is False
    assert es['in_jstor'] is False

def test_elasticsearch_container_transform(journal_metadata_importer):
    with open('tests/files/journal_metadata.sample.json', 'r') as f:
        raw1 = json.loads(f.readline())
        raw2 = json.loads(f.readline())
        c1 = journal_metadata_importer.parse_record(raw1)
        c1.state = 'active'
        c2 = journal_metadata_importer.parse_record(raw2)
        c2.state = 'active'

    c1.extra['publisher_type'] = "big5"
    c1.extra['discipline'] = "history"
    es = container_to_elasticsearch(c1)
    assert es['publisher'] == c1.publisher
    assert es['discipline'] == c1.extra['discipline']
    assert es['publisher_type'] == c1.extra['publisher_type']
    assert es['keepers'] == []

    stats = {
        "ident": "en4qj5ijrbf5djxx7p5zzpjyoq",
        "in_kbart": 11136,
        "in_web": 9501,
        "is_preserved": 11136,
        "issnl": "2050-084X",
        "preservation": {
            "bright": 9501,
            "dark": 1635,
            "none": 0,
            "shadows_only": 0,
            "total": 11136
        },
        "release_type": {
            "_unknown": 9,
            "article-journal": 11124,
            "editorial": 2,
            "letter": 1
        },
        "total": 11136
    }
    es = container_to_elasticsearch(c2, stats=stats)
    assert es['name'] == c2.name
    assert es['publisher'] == c2.publisher
    assert es['keepers'] == list(c2.extra['kbart'].keys()) == ["portico"]
    assert es['any_kbart'] is True


def test_elasticsearch_file_transform():

    with open('./tests/files/file_bcah4zp5tvdhjl5bqci2c2lgfa.json', 'r') as f:
        json_str = f.read()

    fe = entity_from_json(json_str, FileEntity)

    fe.state = 'active'
    es = file_to_elasticsearch(fe)

    # pylint infers type of 'fe' incorrectly for some reason (as str/bytes)
    assert es['sha1'] == fe.sha1  # pylint: disable=no-member
    assert es['sha256'] == fe.sha256  # pylint: disable=no-member
    assert es['md5'] == fe.md5  # pylint: disable=no-member
    assert es['size_bytes'] == fe.size  # pylint: disable=no-member
    assert es['mimetype'] == fe.mimetype  # pylint: disable=no-member
    assert es['in_ia'] is True

    assert 'web' in es['rels']
    assert 'www.zhros.ru' in es['hosts']
    assert 'zhros.ru' in es['domains']
    assert 'archive.org' in (es['hosts'] + es['domains'])
    assert 'web.archive.org' in (es['hosts'] + es['domains'])
    # old regression
    assert '.archive.org' not in (es['hosts'] + es['domains'])

def test_elasticsearch_changelog_transform():
    ce = entity_from_json(open('./tests/files/changelog_3469683.json', 'r').read(), ChangelogEntry)

    es = changelog_to_elasticsearch(ce)
    assert es['index'] == 3469683
    # len("2020-01-30T05:04:39") => 19
    assert es['timestamp'][:19] == "2020-01-30T05:04:39.738601Z"[:19]
    assert es['editor_id'] == "scmbogxw25evtcesfcab5qaboa"
    assert es['username'] == "crawl-bot"
    assert es['is_bot'] is True
    assert es['is_admin'] is True
    assert es['agent'] == "fatcat_tools.IngestFileResultImporter"

    assert es['total'] == 50
    assert es['files'] == 50
    assert es['new_files'] == 50
    assert es['created'] == 50

    assert es['releases'] == 0
    assert es['new_releases'] == 0
    assert es['updated'] == 0
    assert es['deleted'] == 0

def test_elasticsearch_release_kbart_year():
    this_year = datetime.date.today().year
    r = ReleaseEntity(
        title="something",
        release_year=this_year,
        license_slug="CC-BY-NC",
        ext_ids=ReleaseExtIds(),
        refs=[
            ReleaseRef(),
            ReleaseRef(target_release_id="iznnn644szdwva7khyxqzc73bi"),
        ],
    )
    r.state = 'active'
    r.container = ContainerEntity(
        name="dummy journal",
        extra={
            "kbart": {
                "lockss": {
                    "year_spans": [[1900, this_year - 2]],
                },
            },
        },
    )
    es = release_to_elasticsearch(r)
    assert es['release_year'] == this_year

    assert es['preservation'] == "none"
    assert es['is_oa'] is True
    assert es['is_longtail_oa'] is False
    assert es['is_preserved'] is False
    assert es['in_web'] is False
    assert es['in_dweb'] is False
    assert es['in_ia'] is False
    assert es['in_ia_sim'] is False
    assert es['in_kbart'] is False
    assert es['in_jstor'] is False

    r.container = ContainerEntity(
        name="dummy journal",
        extra={
            "kbart": {
                "lockss": {
                    "year_spans": [[1900, this_year - 1]],
                },
            },
        },
    )
    es = release_to_elasticsearch(r)
    assert es['release_year'] == this_year

    assert es['preservation'] == "dark"
    assert es['is_oa'] is True
    assert es['is_longtail_oa'] is False
    assert es['is_preserved'] is True
    assert es['in_web'] is False
    assert es['in_dweb'] is False
    assert es['in_ia'] is False
    assert es['in_ia_sim'] is False
    assert es['in_kbart'] is True
    assert es['in_jstor'] is False