metagenedb.py 1.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from requests.exceptions import HTTPError

from django.urls import reverse
from django.utils.http import urlencode

from metagenedb.common.utils.api import MetageneDBCatalogGeneAPI


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 = ''

    def __init__(self, client):
        self.client = client
        self.reverse_path = ':'.join([self.BASE_REVERSE, self.REVERSE_PATH])

    def get_all(self, params=None):
        url = reverse(f'{self.reverse_path}-list')
        if params is not None:
            query_params = urlencode(params)
            return self.client.get(f"{url}?{query_params}").json()
        return self.client.get(f"{url}").json()

    def get(self, entry_id):
        response = self.client.get(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}))
        if response.status_code == 404:
            raise HTTPError
        return response.json()

    def post(self, data):
        response = self.client.post(reverse(f'{self.reverse_path}-list'), data, format='json')
        if response.status_code == 400:
            raise HTTPError
        return response.json()

41
42
43
44
45
    def put(self, data, entry_id=None):
        if entry_id:
            return self.client.put(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}),
                                   data, format='json').json()
        return self.client.put(reverse(f'{self.reverse_path}-list'), data, format='json').json()
46
47
48
49
50
51
52
53
54
55


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


class MetageneDBCatalogTaxonomyAPIMock(MetageneDBAPIMock):
    KEY_ID = 'gene_id'
    REVERSE_PATH = 'catalog:v1:taxonomy'
56
57
58
59
60


class MetageneDBCatalogFunctionAPIMock(MetageneDBAPIMock):
    KEY_ID = 'function_id'
    REVERSE_PATH = 'catalog:v1:functions'