test_gene.py 3.28 KB
Newer Older
1
import pandas as pd
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
2
3
4
5
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
6
from rest_framework.test import APITestCase
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
7
8
from rest_framework_jwt.settings import api_settings

9
from metagenedb.api.catalog.views.gene import GeneViewSet
10
11
from metagenedb.apps.catalog.factory import GeneFactory
from metagenedb.common.utils.mocks.metagenedb import MetageneDBCatalogGeneAPIMock
12

Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
13
14
15
16
17

class TestGenes(TestCase):

    def test_get_genes_no_auth(self):
        """
18
        Unauthenticated users should be able to access genes
19
        @TODO make unaccessible
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
20
        """
21
        url = reverse('api:catalog:v1:genes-list')
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
22
        resp = self.client.get(url)
23
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
24
25
26

    def test_get_genes_auth(self):
        """
27
        Authenticated users should be able to access genes
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
28
29
30
31
32
33
34
35
36
37
38
        """

        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        user = User.objects.create_user(username='user', email='user@foo.com', password='pass')
        user.is_active = True
        user.save()
        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)

39
        url = reverse('api:catalog:v1:genes-list')
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
40
41
        resp = self.client.get(url, format='json', HTTP_AUTHORIZATION=f"JWT {token}")
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
42
43
44
45
46


class TestCountWindows(TestCase):

    def setUp(self):
47
        self.window_col = "length"
48
49
50
51
52
53
54
        self.df = pd.DataFrame(
            [22, 29, 35],
            columns=[self.window_col]
        )

    def test_simple_count_window10(self):
        expected_dict = {
55
            'labels': ['<0.01k', '0.01k-0.02k', '0.02k-0.03k', '>0.03k'],
56
57
58
59
60
            'counts': [0, 0, 2, 1]
        }
        geneviewset = GeneViewSet()
        test_dict = geneviewset._count_windows(self.df, 10, window_col=self.window_col)
        self.assertDictEqual(test_dict, expected_dict)
61
62
63
64
65
66
67
68
69
70

    def test_simple_count_window10_stop20(self):
        expected_dict = {
            'labels': ['<0.01k', '0.01k-0.02k', '>0.02k'],
            'counts': [0, 0, 3]
        }
        geneviewset = GeneViewSet()
        test_dict = geneviewset._count_windows(self.df, window_size=10,
                                               window_col=self.window_col, stop_at=20)
        self.assertDictEqual(test_dict, expected_dict)
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


class TestCountWindowsAPI(APITestCase):

    def setUp(self):
        self.gene_api = MetageneDBCatalogGeneAPIMock(self.client)
        for i in range(2000, 4000, 350):
            GeneFactory.create(length=i)

    def test_gene_length_api(self):
        expected_dict = {
            'results': {
                'counts': [0, 0, 3, 3],
                'labels': ['<1.0k', '1.0k-2.0k', '2.0k-3.0k', '>3.0k']
            }
        }
        self.assertDictEqual(self.gene_api.get_gene_length(), expected_dict)

    def test_gene_length_api_stop_at_2000(self):
        expected_dict = {
            'results': {
                'counts': [0, 0, 6],
                'labels': ['<1.0k', '1.0k-2.0k', '>2.0k']
            }
        }
        query_params = {
            'stop_at': 2000
        }
        self.assertDictEqual(self.gene_api.get_gene_length(params=query_params), expected_dict)