aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/db/rdf/test_wiki.py
blob: 5f3603402bf86177a3dca2105a2796deec78764d (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""Tests for gn3/db/rdf/wiki.py

NOTE:

Some errors like W0611, W0613, and W0621 are due to Pytest's usage of
fixtures.  This errors are Ignored for now.  The alternative would be
to install pylint-pytest:

    https://github.com/pylint-dev/pylint-pytest

More discussions:

    https://github.com/PyCQA/meta/issues/56

"""
from unittest import TestCase
import pytest

# pylint: disable=W0611
from tests.fixtures.rdf import (
    rdf_setup,
    get_sparql_auth_conf
)

from gn3.db.rdf.wiki import (
    __sanitize_result,
    get_wiki_entries_by_symbol,
    get_comment_history,
    update_wiki_comment,
)


SPARQL_CONF = get_sparql_auth_conf()
GRAPH = "<http://cd-test.genenetwork.org>"


@pytest.mark.unit_test
@pytest.mark.parametrize(
    ("result", "expected"),
    (
        (
            {
                "categories": "",
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": "",
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
            {
                "categories": [],
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": [],
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
        ),
        (
            {
                "categories": "some-category",
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": "",
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
            {
                "categories": ["some-category"],
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": [],
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
        ),
        (
            {
                "categories": "",
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": "21",
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
            {
                "categories": [],
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": [21],
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
        ),
        (
            {
                "categories": "",
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": 21,
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
            {
                "categories": [],
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": [21],
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
        ),
        (
            {
                "categories": "",
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": [21, 22],
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
            {
                "categories": [],
                "comment": "comment",
                "created": "2006-12-23 17:19:14",
                "email": "XXX@XXX.edu",
                "initial": "KMz",
                "pubmed_ids": [21, 22],
                "reason": "add",
                "species": "mouse",
                "symbol": "Sfrs3",
                "version": 0,
                "web_url": "",
            },
        ),
        ({}, {}),
    ),
)
def test_sanitize_result(result, expected):
    """Test that the JSON-LD result is sanitized correctly"""
    assert __sanitize_result(result) == expected


@pytest.mark.rdf
def test_get_wiki_entries_by_symbol(rdf_setup):  # pylint: disable=W0613,W0621
    """Test that wiki entries are fetched correctly by symbol"""
    result = get_wiki_entries_by_symbol(
        symbol="ckb",
        sparql_uri=SPARQL_CONF["sparql_endpoint"],
        graph=GRAPH,
    )
    expected = {
        "@context": {
            "categories": "gnt:belongsToCategory",
            "comment": "rdfs:comment",
            "created": "dct:created",
            "data": "@graph",
            "dct": "http://purl.org/dc/terms/",
            "email": "foaf:mbox",
            "foaf": "http://xmlns.com/foaf/0.1/",
            "gn": "http://genenetwork.org/id/",
            "gnc": "http://genenetwork.org/category/",
            "gnt": "http://genenetwork.org/term/",
            "id": "dct:identifier",
            "initial": "gnt:initial",
            "pubmed_ids": "dct:references",
            "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
            "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
            "reason": "gnt:reason",
            "species": "gnt:species",
            "symbol": "rdfs:label",
            "type": "@type",
            "version": "gnt:hasVersion",
            "web_url": "foaf:homepage",
        },
        "data": [
            {
                "@id": "gn:wiki-1106-0",
                "categories": [],
                "comment": "high similarities on four locations on three different chromosomes",
                "created": "2007-01-16 10:06:34",
                "email": "XXX@XXX.com",
                "id": 1106,
                "initial": "dcc",
                "pubmed_ids": [],
                "reason": "",
                "species": "mouse",
                "symbol": "Ckb",
                "version": 0,
                "web_url": "",
            },
            {
                "@id": "gn:wiki-5006-0",
                "categories": [
                    "Genetic variation and alleles",
                    "Interactions: mRNA, proteins, other molecules",
                ],
                "comment": "Strong cis eQTL in brain RNA-seq data \
(LRS of 29 high B allele). Highly expressed in almost all neurons \
(not just interneurons). Trans targets of CKB include striatin (STRN) \
and C1QL3 (CTRP13).",
                "created": "2012-11-11 10:37:11",
                "email": "XXX@XXX.com",
                "id": 5006,
                "initial": "",
                "pubmed_ids": [],
                "reason": "",
                "species": "mouse",
                "symbol": "Ckb",
                "version": 0,
                "web_url": "",
            },
        ],
    }
    TestCase().assertDictEqual(result, expected)


@pytest.mark.rdf
def test_get_comment_history(rdf_setup):  # pylint: disable=W0613,W0621
    """Test fetching a comment's history from RDF"""
    result = get_comment_history(
        comment_id=1158,
        sparql_uri=SPARQL_CONF["sparql_endpoint"],
        graph=GRAPH,
    )
    expected = {
        "@context": {
            "categories": "gnt:belongsToCategory",
            "comment": "rdfs:comment",
            "created": "dct:created",
            "data": "@graph",
            "dct": "http://purl.org/dc/terms/",
            "email": "foaf:mbox",
            "foaf": "http://xmlns.com/foaf/0.1/",
            "gn": "http://genenetwork.org/id/",
            "gnc": "http://genenetwork.org/category/",
            "gnt": "http://genenetwork.org/term/",
            "id": "dct:identifier",
            "initial": "gnt:initial",
            "pubmed_ids": "dct:references",
            "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
            "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
            "reason": "gnt:reason",
            "species": "gnt:species",
            "symbol": "rdfs:label",
            "type": "@type",
            "version": "gnt:hasVersion",
            "web_url": "foaf:homepage",
        },
        "data": [
            {
                "@id": "gn:wiki-1158-2",
                "categories": [
                    "Probes and probe sets",
                    "Transcriptional and translation control",
                ],
                "comment": "Validated strong cis-QTL in CNS using allele-specific \
expression (ASE) SNaPshot assay. Possible 3' UTR variants.",
                "created": "2007-04-05 09:11:05",
                "email": "XXX@XXX.com",
                "initial": "dcc",
                "pubmed_ids": [],
                "reason": "new data",
                "species": "mouse",
                "symbol": "Lpl",
                "version": 2,
                "web_url": "",
            },
            {
                "@id": "gn:wiki-1158-1",
                "categories": [
                    "Probes and probe sets",
                    "Transcriptional and translation control",
                ],
                "comment": "putative 3' UTR variants",
                "created": "2007-02-09 09:23:40",
                "email": "XXX@XXX.com",
                "initial": "dcc",
                "reason": "",
                "pubmed_ids": [],
                "species": "mouse",
                "symbol": "Lpl",
                "version": 1,
                "web_url": "",
            },
            {
                "@id": "gn:wiki-1158-0",
                "categories": [
                    "Probes and probe sets",
                    "Transcriptional and translation control",
                ],
                "comment": "Validated strong cis-QTL in CNS using allele-specific \
expression (ASE) SNaPshot assay (Daniel Ciobanu and al., 2007). \
Possible 3' UTR variants.",
                "created": "2007-05-08 14:46:07",
                "email": "XXX@XXX.com",
                "initial": "dcc",
                "pubmed_ids": [],
                "reason": "addition",
                "species": "mouse",
                "symbol": "Lpl",
                "version": 0,
                "web_url": "",
            },
        ],
    }
    TestCase().assertDictEqual(result, expected)


@pytest.mark.rdf
def test_update_wiki_comment(rdf_setup):  # pylint: disable=W0613,W0621
    """Test that a comment is updated correctly"""
    update_wiki_comment(
        insert_dict={
            "Id": 230,
            "symbol": "Shh",
            "PubMed_ID": "2 3 4",
            "email": "test@test.com",
            "comment": "Updated comment",
            "createtime": "2007-01-16 11:06:34",
            "weburl": "http://some-website.com",
            "initial": "BMK",
            "reason": "Testing",
            "versionId": 3,
            "species": "mouse",
            "categories": [],
        },
        sparql_user=SPARQL_CONF["sparql_user"],
        sparql_password=SPARQL_CONF["sparql_password"],
        sparql_auth_uri=SPARQL_CONF["sparql_auth_uri"],
        graph=GRAPH,
    )
    updated_history = get_comment_history(
        comment_id=230,
        sparql_uri=SPARQL_CONF["sparql_endpoint"],
        graph=GRAPH,
    )["data"]
    assert len(updated_history) == 4
    TestCase().assertDictEqual(updated_history[0], {
        "@id": "gn:wiki-230-3",
        "categories": [],
        "comment": "Updated comment",
        "created": "2007-01-16 11:06:34",
        "email": "test@test.com",
        "initial": "BMK",
        "pubmed_ids": [2, 3, 4],
        "reason": "Testing",
        "species": "mouse",
        "symbol": "Shh",
        "version": 3,
        "web_url": "http://some-website.com",
    })