"""Test suite for TreeOfLife module."""
from django.urls import reverse
from django.test import TestCase
from django.db.utils import IntegrityError
from unittest import skip
from rest_framework import status
from rest_framework.test import APITestCase
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from pangea.contrib.treeoflife.utils import populate_md2
from pangea.contrib.treeoflife.models import (
TaxonName,
TreeNode,
Bacteria,
)
from .populate_test_db import populate_test_db
[docs]class TestTreeOfLife(TestCase):
[docs] @classmethod
def setUpTestData(cls):
populate_test_db()
[docs] def test_get_root_node(self):
root = TreeNode.objects.get(taxon_id='1')
self.assertTrue(root.uuid)
self.assertFalse(root.parent)
[docs] def test_get_root_by_name(self):
root = TaxonName.objects.get(name__iexact='root')
self.assertTrue(root.uuid)
[docs] def test_get_specific_genus_by_name(self):
taxa = TaxonName.objects.get(name__iexact='Escherichia')
self.assertTrue(taxa.uuid)
[docs] def test_get_specific_species_by_name(self):
taxa = TaxonName.objects.get(name__iexact='Escherichia coli')
self.assertTrue(taxa.uuid)
[docs]class TestMicrobeDir(TestCase):
[docs] @classmethod
def setUpTestData(cls):
populate_test_db()
populate_md2(limit=1000)
[docs] def test_get_root_annotation(self):
annotation = TaxonName.objects.get(name__iexact='root').tree_node.annotation
self.assertFalse(annotation)
[docs] def test_get_bacteria_entry(self):
bact = Bacteria.objects.get(taxon_id='562') # Escherichia coli
self.assertTrue(bact.uuid)
[docs] def test_get_ecoli_annotation(self):
annotation = TaxonName.objects.get(name__iexact='Escherichia coli').tree_node.annotation
self.assertTrue(annotation)
[docs]class TestMicrobeDirAPI(APITestCase):
[docs] @classmethod
def setUpTestData(cls):
populate_test_db()
populate_md2(limit=1000)
[docs] def test_annotate_taxa(self):
"""Ensure we can update a defunct taxa name."""
query = 'Escherichia coli'
url = reverse('treeoflife-annotate-taxa') + f'?query={query}'
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn('Escherichia coli', response.data)
self.assertEqual(response.data[query]['gram_stain'].lower(), 'negative')
[docs] def test_get_annotated_descendants(self):
"""Ensure we can get descendants."""
query = 'Escherichia'
url = reverse('treeoflife-get-descendants') + f'?query={query}&annotate=true'
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['depth'], 1)
self.assertTrue(len(response.data[query]['children']) >= 2)
self.assertIn('annotation', response.data[query]['children'][0])
[docs]class TestTreeOfLifeAPI(APITestCase):
[docs] @classmethod
def setUpTestData(cls):
populate_test_db()
[docs] def test_canonicalize_taxa_name(self):
"""Ensure we can update a defunct taxa name."""
query = 'Bacillus coli'
url = reverse('treeoflife-correct-taxa-names') + f'?query={query}'
response = self.client.get(url, format='json')
names = {el['name'] for el in response.data[query]['names']}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn('Escherichia coli', names)
self.assertEqual(1, len(names))
[docs] def test_canonicalize_taxa_name_case_insensitive(self):
"""Ensure we can update a defunct taxa name."""
query = 'Bacillus coli'.lower()
url = reverse('treeoflife-correct-taxa-names') + f'?query={query}'
response = self.client.get(url, format='json')
names = {el['name'] for el in response.data[query]['names']}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn('Escherichia coli', names)
self.assertEqual(1, len(names))
[docs] def test_canonicalize_multiple_taxa_name(self):
"""Ensure we can update a defunct taxa name."""
q1, q2 = 'Bacillus coli', 'Chondromyces aurantiacus'
url = reverse('treeoflife-correct-taxa-names') + f'?query={q1},{q2}'
response = self.client.get(url, format='json')
names1 = {el['name'] for el in response.data[q1]['names']}
names2 = {el['name'] for el in response.data[q2]['names']}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn('Escherichia coli', names1)
self.assertEqual(1, len(names1))
self.assertIn('Stigmatella aurantiaca', names2)
self.assertEqual(1, len(names2))
[docs] def test_search_taxa_name(self):
"""Ensure we can update a defunct taxa name."""
query = 'Escherichia'
url = reverse('treeoflife-correct-taxa-names') + f'?query={query}'
response = self.client.get(url, format='json')
names = {el['name'] for el in response.data[query]['names']}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn('Shigella dysenteriae', names)
self.assertIn('Escherichia coli', names)
self.assertTrue(len(names) >= 10)
[docs] def test_search_non_canon_name(self):
"""Ensure we can update a defunct taxa name."""
query = 'Escherichia'
url = reverse('treeoflife-correct-taxa-names') + f'?query={query}&canon=false'
response = self.client.get(url, format='json')
names = {el['name'] for el in response.data[query]['names']}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertFalse(response.data['canon'])
self.assertIn('Shigella dysenteriae', names)
self.assertIn('Escherichia coli', names)
self.assertIn('Bacillus coli', names)
self.assertTrue(len(names) >= 11)
[docs] def test_search_taxa_name_rank_specified(self):
"""Ensure we can update a defunct taxa name."""
query = 'Escherichia'
url = reverse('treeoflife-correct-taxa-names') + f'?query={query}&rank=species'
response = self.client.get(url, format='json')
names = {el['name'] for el in response.data[query]['names']}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['rank'], 'species')
self.assertIn('Shigella dysenteriae', names)
self.assertIn('Escherichia coli', names)
self.assertNotIn('Escherichia', names)
self.assertTrue(len(names) >= 9)
[docs] def test_get_descendants(self):
"""Ensure we can get descendants."""
query = 'Escherichia'
url = reverse('treeoflife-get-descendants') + f'?query={query}'
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['depth'], 1)
self.assertTrue(len(response.data[query]['children']) >= 2)