-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_crossref_xml.py
More file actions
215 lines (175 loc) · 7.5 KB
/
test_crossref_xml.py
File metadata and controls
215 lines (175 loc) · 7.5 KB
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
"""Tests for crossref_xml parsing and validation logic."""
import pandas as pd
import pytest
from crossref_xml import (
parse_contributors,
parse_references,
validate_csv,
_extract_date,
_extract_authors,
)
class TestParseContributors:
"""Test bracket notation parsing for author metadata."""
def test_single_author_basic(self):
result = parse_contributors("Chen, Maria")
assert len(result) == 1
assert result[0]['surname'] == 'Chen'
assert result[0]['given_name'] == 'Maria'
assert result[0]['orcid'] is None
assert result[0]['affiliations'] == []
def test_single_author_with_orcid(self):
result = parse_contributors("Chen, Maria ORCID[https://orcid.org/0000-0001-2345-6789]")
assert result[0]['orcid'] == 'https://orcid.org/0000-0001-2345-6789'
def test_single_author_with_affiliation(self):
result = parse_contributors("Chen, Maria ORG[Riverside University]")
assert result[0]['affiliations'] == ['Riverside University']
def test_single_author_with_ror(self):
result = parse_contributors("Chen, Maria ROR[https://ror.org/abc123]")
assert result[0]['rors'] == ['https://ror.org/abc123']
def test_multiple_affiliations_with_rors(self):
result = parse_contributors(
"Larsson, Erik ORG[Northern College; Marine Biology Center] "
"ROR[https://ror.org/def456; https://ror.org/ghi789]"
)
assert result[0]['affiliations'] == ['Northern College', 'Marine Biology Center']
assert result[0]['rors'] == ['https://ror.org/def456', 'https://ror.org/ghi789']
def test_multiple_authors(self):
result = parse_contributors("Chen, Maria; Okonkwo, David; Larsson, Erik")
assert len(result) == 3
assert result[0]['surname'] == 'Chen'
assert result[1]['surname'] == 'Okonkwo'
assert result[2]['surname'] == 'Larsson'
def test_semicolon_inside_brackets_preserved(self):
result = parse_contributors("Author, Test ORG[Dept A; Dept B]")
assert result[0]['affiliations'] == ['Dept A', 'Dept B']
def test_empty_string(self):
assert parse_contributors("") == []
assert parse_contributors(None) == []
class TestParseReferences:
"""Test reference parsing from JSON and Python literal formats."""
def test_valid_json_array(self):
refs_str = '[{"key": "ref1", "DOI": "10.1234/test", "doi-asserted-by": "publisher"}]'
result = parse_references(refs_str)
assert len(result) == 1
assert result[0]['key'] == 'ref1'
assert result[0]['DOI'] == '10.1234/test'
def test_python_literal_format(self):
refs_str = "[{'key': 'ref1', 'unstructured': 'Author (2020). Title.'}]"
result = parse_references(refs_str)
assert len(result) == 1
assert result[0]['unstructured'] == 'Author (2020). Title.'
def test_malformed_json_returns_empty_list(self):
assert parse_references('not valid json') == []
assert parse_references('[{broken}]') == []
def test_empty_or_none_input(self):
assert parse_references('') == []
assert parse_references(None) == []
assert parse_references('[]') == []
class TestExtractDate:
"""Test Crossref date-parts extraction."""
def test_full_date(self):
date_field = {'date-parts': [[2024, 3, 15]]}
assert _extract_date(date_field) == '2024-03-15'
def test_year_month_only(self):
date_field = {'date-parts': [[2024, 3]]}
assert _extract_date(date_field) == '2024-03'
def test_year_only(self):
date_field = {'date-parts': [[2024]]}
assert _extract_date(date_field) == '2024'
def test_single_digit_month_and_day_padded(self):
date_field = {'date-parts': [[2024, 1, 5]]}
assert _extract_date(date_field) == '2024-01-05'
def test_empty_or_invalid_input(self):
assert _extract_date(None) == ''
assert _extract_date({}) == ''
assert _extract_date({'date-parts': []}) == ''
assert _extract_date({'date-parts': [[]]}) == ''
class TestExtractAuthors:
"""Test Crossref API author format to bracket notation conversion."""
def test_basic_author_with_family_and_given(self):
author_list = [{'family': 'Chen', 'given': 'Maria'}]
result = _extract_authors(author_list)
assert result == 'Chen, Maria'
def test_author_with_orcid(self):
author_list = [{
'family': 'Chen',
'given': 'Maria',
'ORCID': 'https://orcid.org/0000-0001-2345-6789'
}]
result = _extract_authors(author_list)
assert 'ORCID[https://orcid.org/0000-0001-2345-6789]' in result
def test_author_with_affiliation(self):
author_list = [{
'family': 'Chen',
'given': 'Maria',
'affiliation': [{'name': 'Riverside University'}]
}]
result = _extract_authors(author_list)
assert 'ORG[Riverside University]' in result
def test_author_with_ror(self):
author_list = [{
'family': 'Chen',
'given': 'Maria',
'affiliation': [{
'name': 'Riverside University',
'id': [{'id-type': 'ROR', 'id': 'https://ror.org/abc123'}]
}]
}]
result = _extract_authors(author_list)
assert 'ROR[https://ror.org/abc123]' in result
def test_multiple_authors(self):
author_list = [
{'family': 'Chen', 'given': 'Maria'},
{'family': 'Okonkwo', 'given': 'David'}
]
result = _extract_authors(author_list)
assert result == 'Chen, Maria; Okonkwo, David'
def test_empty_or_invalid_input(self):
assert _extract_authors(None) == ''
assert _extract_authors([]) == ''
assert _extract_authors([{'name': 'Some Org'}]) == 'ORG[Some Org]'
class TestValidateCsv:
"""Test CSV validation for required columns."""
def test_valid_csv_with_all_required_columns(self):
df = pd.DataFrame({
'doi': ['10.1234/test'],
'title': ['Test Title'],
'publication': ['Test Journal'],
'authors': ['Chen, Maria']
})
errors = validate_csv(df)
assert errors == []
def test_missing_required_column(self):
df = pd.DataFrame({
'doi': ['10.1234/test'],
'title': ['Test Title'],
'authors': ['Chen, Maria']
})
errors = validate_csv(df)
assert len(errors) == 1
assert 'publication' in errors[0]
def test_missing_multiple_required_columns(self):
df = pd.DataFrame({'doi': ['10.1234/test']})
errors = validate_csv(df)
assert len(errors) == 1
assert 'title' in errors[0]
assert 'publication' in errors[0]
assert 'authors' in errors[0]
def test_empty_doi_values(self):
df = pd.DataFrame({
'doi': ['10.1234/test', None, ''],
'title': ['Title 1', 'Title 2', 'Title 3'],
'publication': ['Pub', 'Pub', 'Pub'],
'authors': ['Author', 'Author', 'Author']
})
errors = validate_csv(df)
assert any('doi' in error.lower() for error in errors)
def test_empty_title_values(self):
df = pd.DataFrame({
'doi': ['10.1234/test', '10.1234/test2'],
'title': ['Title 1', None],
'publication': ['Pub', 'Pub'],
'authors': ['Author', 'Author']
})
errors = validate_csv(df)
assert any('title' in error.lower() for error in errors)