test_import_igc_data.py 3.64 KB
Newer Older
1
2
3
4
5
6
from unittest import TestCase

from rest_framework.exceptions import ValidationError
from rest_framework.test import APITestCase

from metagenedb.apps.catalog.models import Gene
7
from scripts.populate_db.import_igc_data import parse_gene, upsert_gene, select_taxonomy
8
9
10
11


class TestParseGene(TestCase):

12
    def setUp(self):
13
14
15
        raw_data = [
            'gene_id',
            'gene_name',
16
            'length',
17
18
19
20
21
22
23
24
25
26
27
28
            'gene_completeness_status',
            'cohort_origin',
            'taxo_phylum',
            'taxo_genus',
            'kegg',
            'eggnog',
            'sample_occurence_freq',
            'ind_occurence_freq',
            'kegg_functional_cat',
            'eggnog_functional_cat',
            'cohort_assembled'
        ]
29
30
31
32
33
34
        self.raw_line = "\t".join(raw_data)

    def test_parse_gene_default_selected_keys(self):
        """
        This test should failed and need to be updated when SELECTED_KEYS are changed
        """
35
        expected_dict = {
36
            'gene_id': 'gene_name',
37
            'length': 'length',
38
39
40
            'kegg_ko': 'kegg',
            'taxo_phylum': 'taxo_phylum',
            'taxo_genus': 'taxo_genus',
41
        }
42
43
44
45
46
47
48
        tested_dict = parse_gene(self.raw_line)
        self.assertDictEqual(tested_dict, expected_dict)

    def test_parse_gene(self):
        """
        This test should failed and need to be updated when SELECTED_KEYS are changed
        """
49
        selected_keys = ['gene_id', 'length']
50
51
        expected_dict = {
            'gene_id': 'gene_name',
52
            'length': 'length'
53
54
55
56
57
58
59
60
        }
        tested_dict = parse_gene(self.raw_line, selected_keys=selected_keys)
        self.assertDictEqual(tested_dict, expected_dict)

    def test_parse_gene_unknown_key(self):
        """
        Unknown key should be ignored
        """
61
        selected_keys = ['gene_id', 'length', 'secret_code']
62
63
        expected_dict = {
            'gene_id': 'gene_name',
64
            'length': 'length'
65
66
        }
        tested_dict = parse_gene(self.raw_line, selected_keys=selected_keys)
67
68
69
70
71
72
73
74
        self.assertDictEqual(tested_dict, expected_dict)


class TestUpsertGene(APITestCase):

    def test_insert_valid_gene_no_kegg(self):
        valid_gene = {
            'gene_id': 'test_gene01',
75
            'length': 3556
76
77
78
79
        }
        upsert_gene(valid_gene)
        self.assertEqual(Gene.objects.all().count(), 1)

80
    def test_insert_invalid_length(self):
81
82
        invalid_gene = {
            'gene_id': 'test_gene01',
83
            'length': 'wrong_format'
84
85
86
87
88
89
90
        }
        with self.assertRaises(ValidationError) as context:  # noqa
            upsert_gene(invalid_gene)

    def test_update_gene(self):
        valid_gene = {
            'gene_id': 'test_gene01',
91
            'length': 3556
92
93
94
        }
        updated_gene = {
            'gene_id': 'test_gene01',
95
            'length': 356
96
97
        }
        upsert_gene(valid_gene)
98
        self.assertEqual(Gene.objects.get(gene_id="test_gene01").length, 3556)
99
        upsert_gene(updated_gene)
100
        self.assertEqual(Gene.objects.get(gene_id="test_gene01").length, 356)
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116


class TestSelectTaxonomy(TestCase):

    def test_genus_only(self):
        pass  # @TODO with #31

    def test_phylum_only(self):
        pass  # @TODO with #31

    def test_genus_phylum(self):
        pass  # @TODO with #31

    def test_both_unknown(self):
        gene_dict = {
            'gene_id': 'gene',
117
            'length': 135,
118
119
120
121
122
            'taxo_phylum': 'unknown',
            'taxo_genus': 'unknown'
        }
        expected_dict = {
            'gene_id': 'gene',
123
            'length': 135
124
125
126
        }
        tested_dict = select_taxonomy(gene_dict)
        self.assertDictEqual(tested_dict, expected_dict)