metagenedb.py 3.31 KB
Newer Older
1
2
from requests.exceptions import HTTPError

3
from dabeplech import MetageneDBCatalogGeneAPI
4
5
6
7
8
9
10
11
12
13
14
from django.urls import reverse


class MetageneDBAPIMock(MetageneDBCatalogGeneAPI):
    """
    Just a simple mock to go through the Test client. The idea is to test the upsert behaviour and not
    the insertion to the db.
    """
    KEY_ID = ''
    BASE_REVERSE = 'api'
    REVERSE_PATH = ''
15
    BAD_REQUESTS = range(400, 452)
16

Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
17
    def __init__(self, client, jwt_token=None):
18
19
        self.client = client
        self.reverse_path = ':'.join([self.BASE_REVERSE, self.REVERSE_PATH])
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
20
21
22
23
24
        self.headers = {}
        if jwt_token is not None:
            self.headers.update({
                'HTTP_AUTHORIZATION': f"JWT {jwt_token}",
            })
25
26

    def get_all(self, params=None):
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
27
        response = self.client.get(reverse(f'{self.reverse_path}-list'), params, **self.headers)
28
29
30
31
32
        if response.status_code in self.BAD_REQUESTS:
            raise HTTPError
        return response.json()

    def get(self, entry_id, params=None):
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
33
34
        response = self.client.get(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}),
                                   params, **self.headers)
35
        if response.status_code in self.BAD_REQUESTS:
36
37
38
39
            raise HTTPError
        return response.json()

    def post(self, data):
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
40
        response = self.client.post(reverse(f'{self.reverse_path}-list'), data, format='json', **self.headers)
41
        if response.status_code in self.BAD_REQUESTS:
42
43
44
            raise HTTPError
        return response.json()

45
46
    def put(self, data, entry_id=None):
        if entry_id:
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
47
48
49
50
51
52
53
            response = self.client.put(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}),
                                       data, format='json', **self.headers)
        else:
            response = self.client.put(reverse(f'{self.reverse_path}-list'), data, format='json', **self.headers)
        if response.status_code in self.BAD_REQUESTS:
            raise HTTPError
        return response.json()
54
55
56
57
58
59


class MetageneDBCatalogGeneAPIMock(MetageneDBAPIMock):
    KEY_ID = 'gene_id'
    REVERSE_PATH = 'catalog:v1:genes'

60
61
62
63
64
    def get_gene_length(self, params=None):
        reverse_path = f"{self.reverse_path}-gene-length"
        response = self.client.get(reverse(reverse_path), params)
        if response.status_code in self.BAD_REQUESTS:
            raise HTTPError
65
66
        if response.status_code == 204:  # no content
            return {}
67
68
        return response.json()

69
70
71
72
73
74
75
76
77
    def get_tax_counts(self, params=None):
        reverse_path = f"{self.reverse_path}-taxonomy-counts"
        response = self.client.get(reverse(reverse_path), params)
        if response.status_code in self.BAD_REQUESTS:
            raise HTTPError
        if response.status_code == 204:  # no content
            return {}
        return response.json()

78
79
80
81

class MetageneDBCatalogTaxonomyAPIMock(MetageneDBAPIMock):
    KEY_ID = 'gene_id'
    REVERSE_PATH = 'catalog:v1:taxonomy'
82
83
84
85
86


class MetageneDBCatalogFunctionAPIMock(MetageneDBAPIMock):
    KEY_ID = 'function_id'
    REVERSE_PATH = 'catalog:v1:functions'
87
88
89
90
91
92


class MetageneDBCatalogKeggOrthologyAPIMock(MetageneDBCatalogFunctionAPIMock):
    REVERSE_PATH = 'catalog:v1:kegg-orthologies'


93
class MetageneDBCatalogEggNOGAPIMock(MetageneDBCatalogFunctionAPIMock):
94
    REVERSE_PATH = 'catalog:v1:eggnogs'