aboutsummaryrefslogtreecommitdiff
path: root/backend/tests/test_gen_mapping_data.py
blob: 9aa99b767fa877d3c6928268be060e2e9137ae8d (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
import unittest
import tempfile, os

from tests.common import createTestFile, createTestGzip, createTestDbTable, readTestDbTable
from tol_data.gen_mapping_data import \
	genData, readTaxonomyFile, readEolIdsFile, readWikidataDb, readPickedMappings, getEnwikiPageIds

class TestReadTaxonomyFile(unittest.TestCase):
	def test_read(self):
		with tempfile.TemporaryDirectory() as tempDir:
			# Create temp taxonomy file
			taxonomyFile = os.path.join(tempDir, 'taxonomy.tsv')
			SEP = '\t|\t'
			createTestFile(taxonomyFile, ''.join([
				SEP.join(['uid', 'parent_uid', 'name', 'rank', 'sourceinfo', 'uniqueName', 'flags', '\n']),
				SEP.join(['1', '2', 'one', 'species', 'ncbi:10', '', '', '\n']),
				SEP.join(['2', '3', 'two', 'genus', 'ncbi:20,gbif:1', 'bananas', '', '\n']),
				SEP.join(['10', '20', 'ten', 'family', 'if:10,if:100', '', '', '\n']),
				SEP.join(['11', '100', 'eleven', '', 'igloo:1,ncbi:?', '', '', '\n'])
			]))
			# Run
			nodeToSrcIds = {}
			usedSrcIds = set()
			readTaxonomyFile(taxonomyFile, nodeToSrcIds, usedSrcIds)
			# Check
			self.assertEqual(nodeToSrcIds, {
				1: {'ncbi': 10},
				2: {'ncbi': 20, 'gbif': 1},
				10: {'if': 10},
			})
			self.assertEqual(usedSrcIds, {
				('ncbi', 10),
				('ncbi', 20),
				('gbif', 1),
				('if', 10)
			})
class TestReadEolIdsFile(unittest.TestCase):
	def test_read(self):
		with tempfile.TemporaryDirectory() as tempDir:
			# Create temp EOL IDs file
			eolIdsFile = os.path.join(tempDir, 'ids.csv.gz')
			createTestGzip(eolIdsFile, (
				'node_id,resource_pk,resource_id,page_id,preferred_canonical_for_page\n'
				'0,10,676,1,rhubarb\n' # EOL ID 1 with ncbi ID 10
				'0,99,767,2,nothing\n' # EOL ID 2 with worms ID 99
				'0,234,459,100,goat\n' # EOL ID 100 with gbif ID 234
				'0,23,676,101,lemon\n' # EOL ID 101 with ncbi ID 23
			))
			# Create input maps
			nodeToSrcIds = {
				10: {'ncbi': 10},
				20: {'ncbi': 23, 'gbif': 234}
			}
			# Run
			usedSrcIds = {('ncbi', 10), ('gbif', 234), ('ncbi', 23)}
			nodeToEolId = {}
			readEolIdsFile(eolIdsFile, nodeToSrcIds, usedSrcIds, nodeToEolId)
			# Check
			self.assertEqual(nodeToEolId, {
				10: 1,
				20: 101,
			})
class TestReadWikidataDb(unittest.TestCase):
	def test_read(self):
		with tempfile.TemporaryDirectory() as tempDir:
			# Create temp wikidata db
			wikidataDb = os.path.join(tempDir, 'taxon_srcs.db')
			createTestDbTable(
				wikidataDb,
				'CREATE TABLE src_id_to_title (src TEXT, id INT, title TEXT, PRIMARY KEY(src, id))',
				'INSERT INTO src_id_to_title VALUES (?, ?, ?)',
				[
					('ncbi', 1, 'one'),
					('ncbi', 11, 'two'),
					('gbif', 21, 'three'),
					('if', 31, 'three'),
					('ncbi', 2, 'four'),
					('gbif', 1, 'five'),
					('eol', 1, 'one'),
					('eol', 2, 'three'),
					('ncbi', 100, 'six'),
				]
			)
			createTestDbTable(
				wikidataDb,
				'CREATE TABLE title_iucn (title TEXT PRIMARY KEY, status TEXT)',
				'INSERT INTO title_iucn VALUES (?, ?)',
				[
					('one', 'least concern'),
					('three', 'vulnerable'),
					('six', 'extinct in the wild'),
				]
			)
			# Create input maps
			nodeToSrcIds = {
				10: {'ncbi': 1},
				20: {'ncbi': 11, 'gbif': 21, 'if': 31},
				30: {'ncbi': 2, 'gbif': 1},
				40: {'ncbi': 99},
			}
			usedSrcIds = {
				('ncbi', 1), ('ncbi', 2), ('gbif', 1), ('ncbi', 11), ('gbif', 21), ('if', 31),
				('eol', 10), ('ncbi', 99)
			}
			nodeToEolId = {
				20: 100,
			}
			# Run
			nodeToWikiTitle = {}
			titleToIucnStatus = {}
			readWikidataDb(wikidataDb, nodeToSrcIds, usedSrcIds, nodeToWikiTitle, titleToIucnStatus, nodeToEolId)
			# Check
			self.assertEqual(nodeToWikiTitle, {
				10: 'one',
				20: 'three',
				30: 'four',
			})
			self.assertEqual(titleToIucnStatus, {
				'one': 'least concern',
				'three': 'vulnerable',
			})
			self.assertEqual(nodeToEolId, {
				10: 1,
				20: 100,
			})
class TestReadPickedMappings(unittest.TestCase):
	def test_read(self):
		with tempfile.TemporaryDirectory() as tempDir:
			# Create temp picked-mappings files
			pickedMappings = {'eol': ['1.txt'], 'enwiki': ['2.txt', '3.txt']}
			pickedMappingsContent = {'eol': [''], 'enwiki': ['', '']}
			pickedMappingsContent['eol'][0] = (
				'10|100\n'
				'20|202\n'
			)
			pickedMappingsContent['enwiki'][0] = (
				'12|abc\n'
				'23|def\n'
			)
			pickedMappingsContent['enwiki'][1] = (
				'15|ghi\n'
				'35|jkl\n'
			)
			for src in pickedMappings:
				for idx in range(len(pickedMappings[src])):
					pickedMappings[src][idx] = os.path.join(tempDir, pickedMappings[src][idx])
					createTestFile(pickedMappings[src][idx], pickedMappingsContent[src][idx])
			# Create input maps
			nodeToEolId = {
				1: 1,
				10: 66,
			}
			nodeToWikiTitle = {
				10: 'one',
				12: 'two',
				35: 'goanna',
			}
			# Run
			readPickedMappings(pickedMappings, nodeToEolId, nodeToWikiTitle)
			# Check
			self.assertEqual(nodeToEolId, {
				1: 1,
				10: 100,
				20: 202,
			})
			self.assertEqual(nodeToWikiTitle, {
				10: 'one',
				12: 'abc',
				23: 'def',
				15: 'ghi',
				35: 'jkl',
			})
class TestReadGetEnwikiPageIds(unittest.TestCase):
	def test_read(self):
		with tempfile.TemporaryDirectory() as tempDir:
			# Create temp dump index
			dumpIndexDb = os.path.join(tempDir, 'dump_index.db')
			createTestDbTable(
				dumpIndexDb,
				'CREATE TABLE offsets (title TEXT PRIMARY KEY, id INT UNIQUE, offset INT, next_offset INT)',
				'INSERT INTO offsets VALUES (?, ?, ?, ?)',
				[
					('one', 1, 10, 100),
					('two', 22, 10, 100),
					('four', 3, 1000, 2000),
				]
			)
			# Create input maps
			nodeToWikiTitle = {
				10: 'one',
				20: 'two',
				30: 'three',
			}
			# Run
			titleToPageId = {}
			getEnwikiPageIds(dumpIndexDb, nodeToWikiTitle, titleToPageId)
			# Check
			self.assertEqual(titleToPageId, {
				'one': 1,
				'two': 22,
			})
class TestGenData(unittest.TestCase):
	def test_mapping(self):
		with tempfile.TemporaryDirectory() as tempDir:
			# Create temp taxonomy file
			taxonomyFile = os.path.join(tempDir, 'taxonomy.tsv')
			SEP = '\t|\t'
			createTestFile(taxonomyFile, ''.join([
				SEP.join(['uid', 'parent_uid', 'name', 'rank', 'sourceinfo', 'uniqueName', 'flags', '\n']),
				SEP.join(['1', '', '', '', 'ncbi:10', '', '', '\n']),
				SEP.join(['2', '', '', '', 'ncbi:20,gbif:1', '', '', '\n']),
				SEP.join(['3', '', '', '', 'ncbi:30,if:2', '', '', '\n']),
			]))
			# Create temp EOL IDs file
			eolIdsFile = os.path.join(tempDir, 'ids.csv.gz')
			createTestGzip(eolIdsFile, (
				'node_id,resource_pk,resource_id,page_id,preferred_canonical_for_page\n'
				'0,10,676,1,\n' # EOL ID 1 with ncbi ID 10
				'0,30,676,2,\n' # EOL ID 2 with ncbi ID 30
			))
			# Create temp wikidata db
			wikidataDb = os.path.join(tempDir, 'taxon_srcs.db')
			createTestDbTable(
				wikidataDb,
				'CREATE TABLE src_id_to_title (src TEXT, id INT, title TEXT, PRIMARY KEY(src, id))',
				'INSERT INTO src_id_to_title VALUES (?, ?, ?)',
				[
					('ncbi', 10, 'one'),
					('gbif', 1, 'two'),
					('eol', 100, 'two'),
					('if', 2, 'three'),
				]
			)
			createTestDbTable(
				wikidataDb,
				'CREATE TABLE title_iucn (title TEXT PRIMARY KEY, status TEXT)',
				'INSERT INTO title_iucn VALUES (?, ?)',
				[
					('one', 'least concern'),
					('three', 'vulnerable'),
				]
			)
			# Create temp picked-mappings files
			pickedMappings = {'eol': [], 'enwiki': ['w_ids.txt']}
			pickedMappingsContent = {'eol': [], 'enwiki': ['']}
			pickedMappingsContent['enwiki'][0] = (
				'3|four\n'
			)
			for src in pickedMappings:
				for idx in range(len(pickedMappings[src])):
					pickedMappings[src][idx] = os.path.join(tempDir, pickedMappings[src][idx])
					createTestFile(pickedMappings[src][idx], pickedMappingsContent[src][idx])
			# Create temp dump index
			dumpIndexDb = os.path.join(tempDir, 'dump_index.db')
			createTestDbTable(
				dumpIndexDb,
				'CREATE TABLE offsets (title TEXT PRIMARY KEY, id INT UNIQUE, offset INT, next_offset INT)',
				'INSERT INTO offsets VALUES (?, ?, ?, ?)',
				[
					('one', 1000, 1, 2),
					('two', 2000, 1, 2),
					('three', 3000, 1, 2),
					('four', 4000, 1, 2),
				]
			)
			# Create temp tree-of-life db
			dbFile = os.path.join(tempDir, 'data.db')
			createTestDbTable(
				dbFile,
				'CREATE TABLE nodes (name TEXT PRIMARY KEY, id TEXT UNIQUE, tips INT)',
				'INSERT INTO nodes VALUES (?, ?, ?)',
				[
					('first', 'ott1', 10),
					('second', 'ott2', 1),
					('third', 'ott3', 2),
				]
			)
			# Run
			genData(taxonomyFile, eolIdsFile, wikidataDb, pickedMappings, dumpIndexDb, dbFile)
			# Check
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT name, id from eol_ids'),
				{
					('first', 1),
					('second', 100),
					('third', 2),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT name, id from wiki_ids'),
				{
					('first', 1000),
					('second', 2000),
					('third', 4000),
				}
			)
			self.assertEqual(
				readTestDbTable(dbFile, 'SELECT name, iucn from node_iucn'),
				{
					('first', 'least concern'),
				}
			)