test_gene.py 2.23 KB
Newer Older
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
1
2
3
4
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse

5
import pandas as pd
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
6
7
8
from rest_framework import status
from rest_framework_jwt.settings import api_settings

9
10
from metagenedb.api.catalog.views.gene import GeneViewSet

Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
11
12
13
14
15

class TestGenes(TestCase):

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

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

        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)

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


class TestCountWindows(TestCase):

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

    def test_simple_count_window10(self):
        expected_dict = {
53
            'labels': ['<0.01k', '0.01k-0.02k', '0.02k-0.03k', '>0.03k'],
54
55
56
57
58
            '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)
59
60
61
62
63
64
65
66
67
68

    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)