Skip to content

test_ncbi_taxonomy

python.tests.ncbi_taxonomy.test_ncbi_taxonomy

Unit testing of :mod:ncbi_taxonomy module.

Typical usage example::

$ pytest test_ncbi_taxonomy.py

TestNCBITaxonomyUtils

Tests :class:~ensembl.ncbi_taxonomy.api.utils.Taxonomy in utils.py

Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
@pytest.mark.parametrize("test_dbs", [[{"src": "ncbi_db"}]], indirect=True)
class TestNCBITaxonomyUtils:
    """Tests :class:`~ensembl.ncbi_taxonomy.api.utils.Taxonomy` in utils.py"""

    dbc: UnitTestDB = None

    @pytest.fixture(scope="class", autouse=True)
    def setup(self, test_dbs: dict[str, UnitTestDB]) -> None:
        """Loads the required fixtures and values as class attributes.

        Args:
            db: Generator of unit test database (fixture).
        """
        type(self).dbc = test_dbs["ncbi_db"].dbc

    result_dict = {
        "taxon_id": 9615,
        "name": "beagle dog",
        "name_class": "includes",
        "parent_id": 9612,
        "rank": "subspecies",
        "genbank_hidden_flag": 1,
        "left_index": 595,
        "right_index": 596,
        "root_id": 1,
    }

    @pytest.mark.parametrize("taxon_id, expectation", [(9615, result_dict)])
    def test_fetch_node_by_id(self, taxon_id: int, expectation: NCBITaxonomy) -> None:
        """Tests :func:`fetch_node_by_id()`

        Args:
            taxon_id: An existing taxon_id as in ncbi_taxonomy.
            expectation: NCBITaxonomy object.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.fetch_node_by_id(session, taxon_id)
            result = result.__dict__
            result.pop("_sa_instance_state")
            assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(9616, raises(NoResultFound))])
    def test_fetch_node_by_id_false_id(self, taxon_id: int, expectation: ContextManager) -> None:
        """Tests :func:`fetch_node_by_id()` with a non-existant taxon_id.

        Args:
            taxon_id: A taxon_id that is not in ncbi_taxonomy.
            expectation: NoResultFound() exception.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                result = Taxonomy.fetch_node_by_id(session, taxon_id)
                assert result == expectation

    result_dict2 = {
        "taxon_id": 9615,
        "name": "Canis lupus familiaris",
        "name_class": "scientific name",
        "parent_id": 9612,
        "rank": "subspecies",
        "genbank_hidden_flag": 1,
        "left_index": 595,
        "right_index": 596,
        "root_id": 1,
    }

    @pytest.mark.parametrize("name, expectation", [("Canis lupus familiaris", result_dict2)])
    def test_fetch_taxon_by_species_name(self, name: int, expectation: NCBITaxonomy) -> None:
        """Tests :func:`fetch_taxon_by_species_name()`.

        Args:
            name: An existing scientific name as in ncbi_taxonomy.
            expectation: Class NCBITaxonomy object.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.fetch_taxon_by_species_name(session, name)
            result = result.__dict__
            result.pop("_sa_instance_state")
            assert result == expectation

    @pytest.mark.parametrize("name, expectation", [("Canis loopy familiaris", raises(NoResultFound))])
    def test_fetch_taxon_by_species_name_false_name(self, name: int, expectation: ContextManager) -> None:
        """Tests :func:`fetch_taxon_by_species_name()` with a non-existant name.

        Args:
            name: A false scientific name not in ncbi_taxonomy.
            expectation: NoResultFound() exception.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                result = Taxonomy.fetch_taxon_by_species_name(session, name)
                assert result == expectation

    result_dict3 = {
        "taxon_id": 9612,
        "name": "Canis lupus",
        "name_class": "scientific name",
        "parent_id": 9611,
        "rank": "species",
        "genbank_hidden_flag": 1,
        "left_index": 594,
        "right_index": 597,
        "root_id": 1,
    }

    @pytest.mark.parametrize("taxon_id, expectation", [(9615, result_dict3)])
    def test_parent(self, taxon_id: int, expectation: NCBITaxonomy) -> None:
        """Tests :func:`parent()`.

        Args:
            taxon_id: An existing taxon_id as in ncbi_taxonomy.
            expectation: Class NCBITaxonomy object.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.parent(session, taxon_id)
            result = result.__dict__
            result.pop("_sa_instance_state")
            assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(9616, raises(NoResultFound))])
    def test_parent_false_id(self, taxon_id: int, expectation: ContextManager) -> None:
        """Tests :func:`parent()` with non-existant taxon_id.

        Args:
            taxon_id: A false taxon_id not in ncbi_taxonomy.
            expectation: NoResultFound() exception.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                result = Taxonomy.parent(session, taxon_id)
                assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(1, raises(NoResultFound))])
    def test_parent_as_root_id(self, taxon_id: int, expectation: ContextManager) -> None:
        """Tests :func:`parent()` with root taxon_id, no parent expected of course.

        Args:
            taxon_id: The root taxon_id not in ncbi_taxonomy.
            expectation: NoResultFound() exception.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                result = Taxonomy.parent(session, taxon_id)
                assert result == expectation

    result_tuple = (
        {
            "genbank_hidden_flag": 0,
            "left_index": 356,
            "name": "Hystricognathi",
            "name_class": "scientific name",
            "parent_id": 9989,
            "rank": "suborder",
            "right_index": 363,
            "root_id": 1,
            "taxon_id": 33550,
        },
        {
            "genbank_hidden_flag": 0,
            "left_index": 364,
            "name": "Sciurognathi",
            "name_class": "scientific name",
            "parent_id": 9989,
            "rank": "suborder",
            "right_index": 399,
            "root_id": 1,
            "taxon_id": 33553,
        },
    )

    @pytest.mark.parametrize("taxon_id, expectation", [(9989, result_tuple)])
    def test_children(self, taxon_id: int, expectation: tuple) -> None:
        """Tests :func:`children()`.

        Args:
            taxon_id: An existing taxon_id as in ncbi_taxonomy.
            expectation: A tuple of Class NCBITaxonomy objects.
        """
        with self.dbc.session_scope() as session:
            results = Taxonomy.children(session, taxon_id)
            rows = list(results)
            for row in rows:
                row.pop("_sa_instance_state")
            results = tuple(rows)
            assert results == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(9615, raises(NoResultFound))])
    def test_children_none(self, taxon_id: int, expectation: ContextManager) -> None:
        """Tests :func:`children()` with extant leaf taxon_id, no children expected of course.

        Args:
            taxon_id: The root taxon_id not in ncbi_taxonomy.
            expectation: NoResultFound() exception.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                results = Taxonomy.children(session, taxon_id)
                assert results == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(1, True)])
    def test_is_root(self, taxon_id: int, expectation: bool) -> None:
        """Tests :func:`is_root()`.

        Args:
            taxon_id: Root taxon_id as in ncbi_taxonomy.
            expectation: True.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.is_root(session, taxon_id)
            assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(9615, False)])
    def test_is_root_not_root(self, taxon_id: int, expectation: bool) -> None:
        """Tests :func:`is_root()` with extant leaf taxon_id, not a root.

        Args:
            taxon_id: A leaf taxon_id in ncbi_taxonomy.
            expectation: False.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.is_root(session, taxon_id)
            assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(9612, 1)])
    def test_num_descendants(self, taxon_id: int, expectation: int) -> None:
        """Tests :func:`num_descendants()`.

        Args:
            taxon_id: An internal node taxon_id in ncbi_taxonomy.
            expectation: Correct number of descendants.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.num_descendants(session, taxon_id)
            assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(1, 381)])
    def test_num_descendants_large(self, taxon_id: int, expectation: int) -> None:
        """Tests :func:`num_descendants()`.

        Args:
            taxon_id: The root node taxon_id in ncbi_taxonomy.
            expectation: The total number taxon entries in db - 1.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.num_descendants(session, taxon_id)
            assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(9615, 0)])
    def test_num_descendants_leaf_taxon(self, taxon_id: int, expectation: int) -> None:
        """Tests :func:`num_descendants()` with leaf taxon_id.

        Args:
            taxon_id: Leaf taxon_id not in ncbi_taxonomy.
            expectation: Correct number of descendants.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.num_descendants(session, taxon_id)
            assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(0, raises(NoResultFound))])
    def test_num_descendants_false_taxon(self, taxon_id: int, expectation: ContextManager) -> None:
        """Tests :func:`num_descendants()` with leaf taxon_id.

        Args:
            taxon_id: Leaf taxon_id not in ncbi_taxonomy.
            expectation: Correct number of descendants.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                result = Taxonomy.num_descendants(session, taxon_id)
                assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(9615, True)])
    def test_is_leaf(self, taxon_id: int, expectation: bool) -> None:
        """Tests :func:`is_leaf()`.

        Args:
            taxon_id: leaf taxon_id as in ncbi_taxonomy.
            expectation: True.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.is_leaf(session, taxon_id)
            assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(1, False)])
    def test_is_leaf_not_leaf(self, taxon_id: int, expectation: bool) -> None:
        """Tests :func:`is_leaf()` with root taxon_id - so not a leaf.

        Args:
            taxon_id: The root taxon_id in ncbi_taxonomy.
            expectation: False.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.is_leaf(session, taxon_id)
            assert result == expectation

    result_tuple2 = (
        {
            "genbank_hidden_flag": 0,
            "left_index": 1,
            "parent_id": 0,
            "rank": "no rank",
            "right_index": 764,
            "root_id": 1,
            "taxon_id": 1,
        },
        {
            "genbank_hidden_flag": 0,
            "left_index": 3,
            "parent_id": 131567,
            "rank": "superkingdom",
            "right_index": 762,
            "root_id": 1,
            "taxon_id": 2759,
        },
        {
            "genbank_hidden_flag": 1,
            "left_index": 4,
            "parent_id": 2759,
            "rank": "no rank",
            "right_index": 761,
            "root_id": 1,
            "taxon_id": 33154,
        },
        {
            "genbank_hidden_flag": 1,
            "left_index": 2,
            "parent_id": 1,
            "rank": "no rank",
            "right_index": 763,
            "root_id": 1,
            "taxon_id": 131567,
        },
    )

    @pytest.mark.parametrize("taxon_id, expectation", [(33208, result_tuple2)])
    def test_fetch_ancestors(self, taxon_id: int, expectation: tuple) -> None:
        """Tests :func:`fetch_ancestors()`.

        Args:
            taxon_id: An existing taxon_id as in ncbi_taxonomy.
            expectation: A tuple of dictionary from Taxonomy objects.
        """
        with self.dbc.session_scope() as session:
            results = Taxonomy.fetch_ancestors(session, taxon_id)
            rows = list(results)
            for row in rows:
                row.pop("_sa_instance_state")
            results = tuple(rows)
            assert results == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(1, raises(NoResultFound))])
    def test_fetch_ancestors_root(self, taxon_id: int, expectation: ContextManager) -> None:
        """Tests :func:`fetch_ancestors()` with root taxon_id.

        Args:
            taxon_id: The root taxon_id as in ncbi_taxonomy.
            expectation: NoResultFound() exception.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                result = Taxonomy.fetch_ancestors(session, taxon_id)
                assert result == expectation

    @pytest.mark.parametrize("taxon_id, expectation", [(0, raises(NoResultFound))])
    def test_fetch_ancestors_false_taxon(self, taxon_id: int, expectation: ContextManager) -> None:
        """Tests :func:`fetch_ancestors()` with non-existant taxon_id.

        Args:
            taxon_id: False taxon_id not in ncbi_taxonomy.
            expectation: NoResultFound() exception.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                result = Taxonomy.fetch_ancestors(session, taxon_id)
                assert result == expectation

    result_tuple3 = (
        {
            "genbank_hidden_flag": 0,
            "left_index": 1,
            "name": "all",
            "name_class": "synonym",
            "parent_id": 0,
            "rank": "no rank",
            "right_index": 764,
            "root_id": 1,
            "taxon_id": 1,
        },
        {
            "genbank_hidden_flag": 1,
            "left_index": 2,
            "name": "biota",
            "name_class": "synonym",
            "parent_id": 1,
            "rank": "no rank",
            "right_index": 763,
            "root_id": 1,
            "taxon_id": 131567,
        },
        {
            "genbank_hidden_flag": 0,
            "left_index": 3,
            "name": "Eucarya",
            "name_class": "synonym",
            "parent_id": 131567,
            "rank": "superkingdom",
            "right_index": 762,
            "root_id": 1,
            "taxon_id": 2759,
        },
        {
            "genbank_hidden_flag": 1,
            "left_index": 4,
            "name": "1500",
            "name_class": "ensembl timetree mya",
            "parent_id": 2759,
            "rank": "no rank",
            "right_index": 761,
            "root_id": 1,
            "taxon_id": 33154,
        },
    )

    @pytest.mark.parametrize("taxon_id1, taxon_id2, expectation", [(33208, 4751, result_tuple3)])
    def test_all_common_ancestors(self, taxon_id1: int, taxon_id2: int, expectation: tuple) -> None:
        """Tests :func:`all_common_ancestors()`.

        Args:
            taxon_id1: An existing taxon_id as in ncbi_taxonomy.
            taxon_id2: An existing taxon_id as in ncbi_taxonomy.
            expectation: A tuple of Class NCBITaxonomy objects.
        """
        with self.dbc.session_scope() as session:
            results = Taxonomy.all_common_ancestors(session, taxon_id1, taxon_id2)
            rows = list(results)
            result = []
            for row in rows:
                row = row.__dict__
                row.pop("_sa_instance_state")
                result.append(row)
            results = tuple(result)
            assert results == expectation

    @pytest.mark.parametrize("taxon_id1, taxon_id2, expectation", [(1, 9615, raises(NoResultFound))])
    def test_all_common_ancestors_root(
        self, taxon_id1: int, taxon_id2: int, expectation: ContextManager
    ) -> None:
        """Tests :func:`all_common_ancestors()` with leaf taxon_id.

        Args:
            taxon_id1: A root node taxon_id as in ncbi_taxonomy.
            taxon_id2: A taxon_id as in ncbi_taxonomy.
            expectation: NoResultFound() exception.
        """
        with expectation:
            with self.dbc.session_scope() as session:
                result = Taxonomy.all_common_ancestors(session, taxon_id1, taxon_id2)
                assert result == expectation

    result_dict4 = {
        "genbank_hidden_flag": 0,
        "left_index": 1,
        "name": "all",
        "name_class": "synonym",
        "parent_id": 0,
        "rank": "no rank",
        "right_index": 764,
        "root_id": 1,
        "taxon_id": 1,
    }

    @pytest.mark.parametrize("taxon_id1, taxon_id2, expectation", [(33154, 131567, result_dict4)])
    def test_last_common_ancestors(self, taxon_id1: int, taxon_id2: int, expectation: tuple) -> None:
        """Tests :func:`all_common_ancestors()`.

        Args:
            taxon_id1: An existing taxon_id as in ncbi_taxonomy.
            taxon_id2: An existing taxon_id as in ncbi_taxonomy.
            expectation: A Class NCBITaxonomy objects.
        """
        with self.dbc.session_scope() as session:
            result = Taxonomy.last_common_ancestor(session, taxon_id1, taxon_id2)
            result = result.__dict__
            result.pop("_sa_instance_state")
            assert result == expectation

dbc: UnitTestDB = None class-attribute instance-attribute

result_dict = {'taxon_id': 9615, 'name': 'beagle dog', 'name_class': 'includes', 'parent_id': 9612, 'rank': 'subspecies', 'genbank_hidden_flag': 1, 'left_index': 595, 'right_index': 596, 'root_id': 1} class-attribute instance-attribute

result_dict2 = {'taxon_id': 9615, 'name': 'Canis lupus familiaris', 'name_class': 'scientific name', 'parent_id': 9612, 'rank': 'subspecies', 'genbank_hidden_flag': 1, 'left_index': 595, 'right_index': 596, 'root_id': 1} class-attribute instance-attribute

result_dict3 = {'taxon_id': 9612, 'name': 'Canis lupus', 'name_class': 'scientific name', 'parent_id': 9611, 'rank': 'species', 'genbank_hidden_flag': 1, 'left_index': 594, 'right_index': 597, 'root_id': 1} class-attribute instance-attribute

result_dict4 = {'genbank_hidden_flag': 0, 'left_index': 1, 'name': 'all', 'name_class': 'synonym', 'parent_id': 0, 'rank': 'no rank', 'right_index': 764, 'root_id': 1, 'taxon_id': 1} class-attribute instance-attribute

result_tuple = ({'genbank_hidden_flag': 0, 'left_index': 356, 'name': 'Hystricognathi', 'name_class': 'scientific name', 'parent_id': 9989, 'rank': 'suborder', 'right_index': 363, 'root_id': 1, 'taxon_id': 33550}, {'genbank_hidden_flag': 0, 'left_index': 364, 'name': 'Sciurognathi', 'name_class': 'scientific name', 'parent_id': 9989, 'rank': 'suborder', 'right_index': 399, 'root_id': 1, 'taxon_id': 33553}) class-attribute instance-attribute

result_tuple2 = ({'genbank_hidden_flag': 0, 'left_index': 1, 'parent_id': 0, 'rank': 'no rank', 'right_index': 764, 'root_id': 1, 'taxon_id': 1}, {'genbank_hidden_flag': 0, 'left_index': 3, 'parent_id': 131567, 'rank': 'superkingdom', 'right_index': 762, 'root_id': 1, 'taxon_id': 2759}, {'genbank_hidden_flag': 1, 'left_index': 4, 'parent_id': 2759, 'rank': 'no rank', 'right_index': 761, 'root_id': 1, 'taxon_id': 33154}, {'genbank_hidden_flag': 1, 'left_index': 2, 'parent_id': 1, 'rank': 'no rank', 'right_index': 763, 'root_id': 1, 'taxon_id': 131567}) class-attribute instance-attribute

result_tuple3 = ({'genbank_hidden_flag': 0, 'left_index': 1, 'name': 'all', 'name_class': 'synonym', 'parent_id': 0, 'rank': 'no rank', 'right_index': 764, 'root_id': 1, 'taxon_id': 1}, {'genbank_hidden_flag': 1, 'left_index': 2, 'name': 'biota', 'name_class': 'synonym', 'parent_id': 1, 'rank': 'no rank', 'right_index': 763, 'root_id': 1, 'taxon_id': 131567}, {'genbank_hidden_flag': 0, 'left_index': 3, 'name': 'Eucarya', 'name_class': 'synonym', 'parent_id': 131567, 'rank': 'superkingdom', 'right_index': 762, 'root_id': 1, 'taxon_id': 2759}, {'genbank_hidden_flag': 1, 'left_index': 4, 'name': '1500', 'name_class': 'ensembl timetree mya', 'parent_id': 2759, 'rank': 'no rank', 'right_index': 761, 'root_id': 1, 'taxon_id': 33154}) class-attribute instance-attribute

setup(test_dbs)

Loads the required fixtures and values as class attributes.

Parameters:

Name Type Description Default
db

Generator of unit test database (fixture).

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
40
41
42
43
44
45
46
47
@pytest.fixture(scope="class", autouse=True)
def setup(self, test_dbs: dict[str, UnitTestDB]) -> None:
    """Loads the required fixtures and values as class attributes.

    Args:
        db: Generator of unit test database (fixture).
    """
    type(self).dbc = test_dbs["ncbi_db"].dbc

test_all_common_ancestors(taxon_id1, taxon_id2, expectation)

Tests :func:all_common_ancestors().

Parameters:

Name Type Description Default
taxon_id1 int

An existing taxon_id as in ncbi_taxonomy.

required
taxon_id2 int

An existing taxon_id as in ncbi_taxonomy.

required
expectation tuple

A tuple of Class NCBITaxonomy objects.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
@pytest.mark.parametrize("taxon_id1, taxon_id2, expectation", [(33208, 4751, result_tuple3)])
def test_all_common_ancestors(self, taxon_id1: int, taxon_id2: int, expectation: tuple) -> None:
    """Tests :func:`all_common_ancestors()`.

    Args:
        taxon_id1: An existing taxon_id as in ncbi_taxonomy.
        taxon_id2: An existing taxon_id as in ncbi_taxonomy.
        expectation: A tuple of Class NCBITaxonomy objects.
    """
    with self.dbc.session_scope() as session:
        results = Taxonomy.all_common_ancestors(session, taxon_id1, taxon_id2)
        rows = list(results)
        result = []
        for row in rows:
            row = row.__dict__
            row.pop("_sa_instance_state")
            result.append(row)
        results = tuple(result)
        assert results == expectation

test_all_common_ancestors_root(taxon_id1, taxon_id2, expectation)

Tests :func:all_common_ancestors() with leaf taxon_id.

Parameters:

Name Type Description Default
taxon_id1 int

A root node taxon_id as in ncbi_taxonomy.

required
taxon_id2 int

A taxon_id as in ncbi_taxonomy.

required
expectation ContextManager

NoResultFound() exception.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
@pytest.mark.parametrize("taxon_id1, taxon_id2, expectation", [(1, 9615, raises(NoResultFound))])
def test_all_common_ancestors_root(
    self, taxon_id1: int, taxon_id2: int, expectation: ContextManager
) -> None:
    """Tests :func:`all_common_ancestors()` with leaf taxon_id.

    Args:
        taxon_id1: A root node taxon_id as in ncbi_taxonomy.
        taxon_id2: A taxon_id as in ncbi_taxonomy.
        expectation: NoResultFound() exception.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            result = Taxonomy.all_common_ancestors(session, taxon_id1, taxon_id2)
            assert result == expectation

test_children(taxon_id, expectation)

Tests :func:children().

Parameters:

Name Type Description Default
taxon_id int

An existing taxon_id as in ncbi_taxonomy.

required
expectation tuple

A tuple of Class NCBITaxonomy objects.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
@pytest.mark.parametrize("taxon_id, expectation", [(9989, result_tuple)])
def test_children(self, taxon_id: int, expectation: tuple) -> None:
    """Tests :func:`children()`.

    Args:
        taxon_id: An existing taxon_id as in ncbi_taxonomy.
        expectation: A tuple of Class NCBITaxonomy objects.
    """
    with self.dbc.session_scope() as session:
        results = Taxonomy.children(session, taxon_id)
        rows = list(results)
        for row in rows:
            row.pop("_sa_instance_state")
        results = tuple(rows)
        assert results == expectation

test_children_none(taxon_id, expectation)

Tests :func:children() with extant leaf taxon_id, no children expected of course.

Parameters:

Name Type Description Default
taxon_id int

The root taxon_id not in ncbi_taxonomy.

required
expectation ContextManager

NoResultFound() exception.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
220
221
222
223
224
225
226
227
228
229
230
231
@pytest.mark.parametrize("taxon_id, expectation", [(9615, raises(NoResultFound))])
def test_children_none(self, taxon_id: int, expectation: ContextManager) -> None:
    """Tests :func:`children()` with extant leaf taxon_id, no children expected of course.

    Args:
        taxon_id: The root taxon_id not in ncbi_taxonomy.
        expectation: NoResultFound() exception.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            results = Taxonomy.children(session, taxon_id)
            assert results == expectation

test_fetch_ancestors(taxon_id, expectation)

Tests :func:fetch_ancestors().

Parameters:

Name Type Description Default
taxon_id int

An existing taxon_id as in ncbi_taxonomy.

required
expectation tuple

A tuple of dictionary from Taxonomy objects.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
@pytest.mark.parametrize("taxon_id, expectation", [(33208, result_tuple2)])
def test_fetch_ancestors(self, taxon_id: int, expectation: tuple) -> None:
    """Tests :func:`fetch_ancestors()`.

    Args:
        taxon_id: An existing taxon_id as in ncbi_taxonomy.
        expectation: A tuple of dictionary from Taxonomy objects.
    """
    with self.dbc.session_scope() as session:
        results = Taxonomy.fetch_ancestors(session, taxon_id)
        rows = list(results)
        for row in rows:
            row.pop("_sa_instance_state")
        results = tuple(rows)
        assert results == expectation

test_fetch_ancestors_false_taxon(taxon_id, expectation)

Tests :func:fetch_ancestors() with non-existant taxon_id.

Parameters:

Name Type Description Default
taxon_id int

False taxon_id not in ncbi_taxonomy.

required
expectation ContextManager

NoResultFound() exception.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
398
399
400
401
402
403
404
405
406
407
408
409
@pytest.mark.parametrize("taxon_id, expectation", [(0, raises(NoResultFound))])
def test_fetch_ancestors_false_taxon(self, taxon_id: int, expectation: ContextManager) -> None:
    """Tests :func:`fetch_ancestors()` with non-existant taxon_id.

    Args:
        taxon_id: False taxon_id not in ncbi_taxonomy.
        expectation: NoResultFound() exception.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            result = Taxonomy.fetch_ancestors(session, taxon_id)
            assert result == expectation

test_fetch_ancestors_root(taxon_id, expectation)

Tests :func:fetch_ancestors() with root taxon_id.

Parameters:

Name Type Description Default
taxon_id int

The root taxon_id as in ncbi_taxonomy.

required
expectation ContextManager

NoResultFound() exception.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
385
386
387
388
389
390
391
392
393
394
395
396
@pytest.mark.parametrize("taxon_id, expectation", [(1, raises(NoResultFound))])
def test_fetch_ancestors_root(self, taxon_id: int, expectation: ContextManager) -> None:
    """Tests :func:`fetch_ancestors()` with root taxon_id.

    Args:
        taxon_id: The root taxon_id as in ncbi_taxonomy.
        expectation: NoResultFound() exception.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            result = Taxonomy.fetch_ancestors(session, taxon_id)
            assert result == expectation

test_fetch_node_by_id(taxon_id, expectation)

Tests :func:fetch_node_by_id()

Parameters:

Name Type Description Default
taxon_id int

An existing taxon_id as in ncbi_taxonomy.

required
expectation NCBITaxonomy

NCBITaxonomy object.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
61
62
63
64
65
66
67
68
69
70
71
72
73
@pytest.mark.parametrize("taxon_id, expectation", [(9615, result_dict)])
def test_fetch_node_by_id(self, taxon_id: int, expectation: NCBITaxonomy) -> None:
    """Tests :func:`fetch_node_by_id()`

    Args:
        taxon_id: An existing taxon_id as in ncbi_taxonomy.
        expectation: NCBITaxonomy object.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.fetch_node_by_id(session, taxon_id)
        result = result.__dict__
        result.pop("_sa_instance_state")
        assert result == expectation

test_fetch_node_by_id_false_id(taxon_id, expectation)

Tests :func:fetch_node_by_id() with a non-existant taxon_id.

Parameters:

Name Type Description Default
taxon_id int

A taxon_id that is not in ncbi_taxonomy.

required
expectation ContextManager

NoResultFound() exception.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
75
76
77
78
79
80
81
82
83
84
85
86
@pytest.mark.parametrize("taxon_id, expectation", [(9616, raises(NoResultFound))])
def test_fetch_node_by_id_false_id(self, taxon_id: int, expectation: ContextManager) -> None:
    """Tests :func:`fetch_node_by_id()` with a non-existant taxon_id.

    Args:
        taxon_id: A taxon_id that is not in ncbi_taxonomy.
        expectation: NoResultFound() exception.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            result = Taxonomy.fetch_node_by_id(session, taxon_id)
            assert result == expectation

test_fetch_taxon_by_species_name(name, expectation)

Tests :func:fetch_taxon_by_species_name().

Parameters:

Name Type Description Default
name int

An existing scientific name as in ncbi_taxonomy.

required
expectation NCBITaxonomy

Class NCBITaxonomy object.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
100
101
102
103
104
105
106
107
108
109
110
111
112
@pytest.mark.parametrize("name, expectation", [("Canis lupus familiaris", result_dict2)])
def test_fetch_taxon_by_species_name(self, name: int, expectation: NCBITaxonomy) -> None:
    """Tests :func:`fetch_taxon_by_species_name()`.

    Args:
        name: An existing scientific name as in ncbi_taxonomy.
        expectation: Class NCBITaxonomy object.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.fetch_taxon_by_species_name(session, name)
        result = result.__dict__
        result.pop("_sa_instance_state")
        assert result == expectation

test_fetch_taxon_by_species_name_false_name(name, expectation)

Tests :func:fetch_taxon_by_species_name() with a non-existant name.

Parameters:

Name Type Description Default
name int

A false scientific name not in ncbi_taxonomy.

required
expectation ContextManager

NoResultFound() exception.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
114
115
116
117
118
119
120
121
122
123
124
125
@pytest.mark.parametrize("name, expectation", [("Canis loopy familiaris", raises(NoResultFound))])
def test_fetch_taxon_by_species_name_false_name(self, name: int, expectation: ContextManager) -> None:
    """Tests :func:`fetch_taxon_by_species_name()` with a non-existant name.

    Args:
        name: A false scientific name not in ncbi_taxonomy.
        expectation: NoResultFound() exception.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            result = Taxonomy.fetch_taxon_by_species_name(session, name)
            assert result == expectation

test_is_leaf(taxon_id, expectation)

Tests :func:is_leaf().

Parameters:

Name Type Description Default
taxon_id int

leaf taxon_id as in ncbi_taxonomy.

required
expectation bool

True.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
306
307
308
309
310
311
312
313
314
315
316
@pytest.mark.parametrize("taxon_id, expectation", [(9615, True)])
def test_is_leaf(self, taxon_id: int, expectation: bool) -> None:
    """Tests :func:`is_leaf()`.

    Args:
        taxon_id: leaf taxon_id as in ncbi_taxonomy.
        expectation: True.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.is_leaf(session, taxon_id)
        assert result == expectation

test_is_leaf_not_leaf(taxon_id, expectation)

Tests :func:is_leaf() with root taxon_id - so not a leaf.

Parameters:

Name Type Description Default
taxon_id int

The root taxon_id in ncbi_taxonomy.

required
expectation bool

False.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
318
319
320
321
322
323
324
325
326
327
328
@pytest.mark.parametrize("taxon_id, expectation", [(1, False)])
def test_is_leaf_not_leaf(self, taxon_id: int, expectation: bool) -> None:
    """Tests :func:`is_leaf()` with root taxon_id - so not a leaf.

    Args:
        taxon_id: The root taxon_id in ncbi_taxonomy.
        expectation: False.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.is_leaf(session, taxon_id)
        assert result == expectation

test_is_root(taxon_id, expectation)

Tests :func:is_root().

Parameters:

Name Type Description Default
taxon_id int

Root taxon_id as in ncbi_taxonomy.

required
expectation bool

True.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
233
234
235
236
237
238
239
240
241
242
243
@pytest.mark.parametrize("taxon_id, expectation", [(1, True)])
def test_is_root(self, taxon_id: int, expectation: bool) -> None:
    """Tests :func:`is_root()`.

    Args:
        taxon_id: Root taxon_id as in ncbi_taxonomy.
        expectation: True.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.is_root(session, taxon_id)
        assert result == expectation

test_is_root_not_root(taxon_id, expectation)

Tests :func:is_root() with extant leaf taxon_id, not a root.

Parameters:

Name Type Description Default
taxon_id int

A leaf taxon_id in ncbi_taxonomy.

required
expectation bool

False.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
245
246
247
248
249
250
251
252
253
254
255
@pytest.mark.parametrize("taxon_id, expectation", [(9615, False)])
def test_is_root_not_root(self, taxon_id: int, expectation: bool) -> None:
    """Tests :func:`is_root()` with extant leaf taxon_id, not a root.

    Args:
        taxon_id: A leaf taxon_id in ncbi_taxonomy.
        expectation: False.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.is_root(session, taxon_id)
        assert result == expectation

test_last_common_ancestors(taxon_id1, taxon_id2, expectation)

Tests :func:all_common_ancestors().

Parameters:

Name Type Description Default
taxon_id1 int

An existing taxon_id as in ncbi_taxonomy.

required
taxon_id2 int

An existing taxon_id as in ncbi_taxonomy.

required
expectation tuple

A Class NCBITaxonomy objects.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
506
507
508
509
510
511
512
513
514
515
516
517
518
519
@pytest.mark.parametrize("taxon_id1, taxon_id2, expectation", [(33154, 131567, result_dict4)])
def test_last_common_ancestors(self, taxon_id1: int, taxon_id2: int, expectation: tuple) -> None:
    """Tests :func:`all_common_ancestors()`.

    Args:
        taxon_id1: An existing taxon_id as in ncbi_taxonomy.
        taxon_id2: An existing taxon_id as in ncbi_taxonomy.
        expectation: A Class NCBITaxonomy objects.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.last_common_ancestor(session, taxon_id1, taxon_id2)
        result = result.__dict__
        result.pop("_sa_instance_state")
        assert result == expectation

test_num_descendants(taxon_id, expectation)

Tests :func:num_descendants().

Parameters:

Name Type Description Default
taxon_id int

An internal node taxon_id in ncbi_taxonomy.

required
expectation int

Correct number of descendants.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
257
258
259
260
261
262
263
264
265
266
267
@pytest.mark.parametrize("taxon_id, expectation", [(9612, 1)])
def test_num_descendants(self, taxon_id: int, expectation: int) -> None:
    """Tests :func:`num_descendants()`.

    Args:
        taxon_id: An internal node taxon_id in ncbi_taxonomy.
        expectation: Correct number of descendants.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.num_descendants(session, taxon_id)
        assert result == expectation

test_num_descendants_false_taxon(taxon_id, expectation)

Tests :func:num_descendants() with leaf taxon_id.

Parameters:

Name Type Description Default
taxon_id int

Leaf taxon_id not in ncbi_taxonomy.

required
expectation ContextManager

Correct number of descendants.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
293
294
295
296
297
298
299
300
301
302
303
304
@pytest.mark.parametrize("taxon_id, expectation", [(0, raises(NoResultFound))])
def test_num_descendants_false_taxon(self, taxon_id: int, expectation: ContextManager) -> None:
    """Tests :func:`num_descendants()` with leaf taxon_id.

    Args:
        taxon_id: Leaf taxon_id not in ncbi_taxonomy.
        expectation: Correct number of descendants.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            result = Taxonomy.num_descendants(session, taxon_id)
            assert result == expectation

test_num_descendants_large(taxon_id, expectation)

Tests :func:num_descendants().

Parameters:

Name Type Description Default
taxon_id int

The root node taxon_id in ncbi_taxonomy.

required
expectation int

The total number taxon entries in db - 1.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
269
270
271
272
273
274
275
276
277
278
279
@pytest.mark.parametrize("taxon_id, expectation", [(1, 381)])
def test_num_descendants_large(self, taxon_id: int, expectation: int) -> None:
    """Tests :func:`num_descendants()`.

    Args:
        taxon_id: The root node taxon_id in ncbi_taxonomy.
        expectation: The total number taxon entries in db - 1.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.num_descendants(session, taxon_id)
        assert result == expectation

test_num_descendants_leaf_taxon(taxon_id, expectation)

Tests :func:num_descendants() with leaf taxon_id.

Parameters:

Name Type Description Default
taxon_id int

Leaf taxon_id not in ncbi_taxonomy.

required
expectation int

Correct number of descendants.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
281
282
283
284
285
286
287
288
289
290
291
@pytest.mark.parametrize("taxon_id, expectation", [(9615, 0)])
def test_num_descendants_leaf_taxon(self, taxon_id: int, expectation: int) -> None:
    """Tests :func:`num_descendants()` with leaf taxon_id.

    Args:
        taxon_id: Leaf taxon_id not in ncbi_taxonomy.
        expectation: Correct number of descendants.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.num_descendants(session, taxon_id)
        assert result == expectation

test_parent(taxon_id, expectation)

Tests :func:parent().

Parameters:

Name Type Description Default
taxon_id int

An existing taxon_id as in ncbi_taxonomy.

required
expectation NCBITaxonomy

Class NCBITaxonomy object.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
139
140
141
142
143
144
145
146
147
148
149
150
151
@pytest.mark.parametrize("taxon_id, expectation", [(9615, result_dict3)])
def test_parent(self, taxon_id: int, expectation: NCBITaxonomy) -> None:
    """Tests :func:`parent()`.

    Args:
        taxon_id: An existing taxon_id as in ncbi_taxonomy.
        expectation: Class NCBITaxonomy object.
    """
    with self.dbc.session_scope() as session:
        result = Taxonomy.parent(session, taxon_id)
        result = result.__dict__
        result.pop("_sa_instance_state")
        assert result == expectation

test_parent_as_root_id(taxon_id, expectation)

Tests :func:parent() with root taxon_id, no parent expected of course.

Parameters:

Name Type Description Default
taxon_id int

The root taxon_id not in ncbi_taxonomy.

required
expectation ContextManager

NoResultFound() exception.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
166
167
168
169
170
171
172
173
174
175
176
177
@pytest.mark.parametrize("taxon_id, expectation", [(1, raises(NoResultFound))])
def test_parent_as_root_id(self, taxon_id: int, expectation: ContextManager) -> None:
    """Tests :func:`parent()` with root taxon_id, no parent expected of course.

    Args:
        taxon_id: The root taxon_id not in ncbi_taxonomy.
        expectation: NoResultFound() exception.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            result = Taxonomy.parent(session, taxon_id)
            assert result == expectation

test_parent_false_id(taxon_id, expectation)

Tests :func:parent() with non-existant taxon_id.

Parameters:

Name Type Description Default
taxon_id int

A false taxon_id not in ncbi_taxonomy.

required
expectation ContextManager

NoResultFound() exception.

required
Source code in src/python/tests/ncbi_taxonomy/test_ncbi_taxonomy.py
153
154
155
156
157
158
159
160
161
162
163
164
@pytest.mark.parametrize("taxon_id, expectation", [(9616, raises(NoResultFound))])
def test_parent_false_id(self, taxon_id: int, expectation: ContextManager) -> None:
    """Tests :func:`parent()` with non-existant taxon_id.

    Args:
        taxon_id: A false taxon_id not in ncbi_taxonomy.
        expectation: NoResultFound() exception.
    """
    with expectation:
        with self.dbc.session_scope() as session:
            result = Taxonomy.parent(session, taxon_id)
            assert result == expectation