• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ProteinsWebTeam / interpro7-api / 1

18 Jun 2024 09:19AM UTC coverage: 25.541% (-68.5%) from 94.023%
1

push

github

web-flow
Merge pull request #147 from ProteinsWebTeam/set-sortable

sort by in sets

1 of 2 new or added lines in 1 file covered. (50.0%)

6742 existing lines in 50 files now uncovered.

2559 of 10019 relevant lines covered (25.54%)

0.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

22.67
/webfront/tests/InterproRESTTestCase.py
1
from django.test import override_settings
2✔
2

3
from django.conf import settings
2✔
4
from rest_framework.test import APITransactionTestCase
2✔
5
from rest_framework import status
2✔
6

7
from webfront.models import TaxonomyPerEntry
2✔
8
from webfront.tests.fixtures_reader import FixtureReader
2✔
9

10
chains = {
2✔
11
    "1JM7": ["A", "B"],
12
    "1T2V": ["A", "B", "C", "D", "E"],
13
    "2BKM": ["A", "B"],
14
    "1JZ8": ["A", "B"],
15
}
16

17

18
@override_settings(SEARCHER_URL=settings.SEARCHER_TEST_URL)
2✔
19
@override_settings(SEARCHER_PASSWORD=settings.SEARCHER_TEST_PASSWORD)
2✔
20
@override_settings(SEARCHER_INDEX="test")
2✔
21
class InterproRESTTestCase(APITransactionTestCase):
2✔
22
    fixtures = [
2✔
23
        "webfront/tests/fixtures_entry.json",
24
        "webfront/tests/fixtures_protein.json",
25
        "webfront/tests/fixtures_structure.json",
26
        "webfront/tests/fixtures_organisms.json",
27
        "webfront/tests/fixtures_set.json",
28
        "webfront/tests/fixtures_database.json",
29
        "webfront/tests/fixtures_entryannotation.json",
30
    ]
31
    links_fixtures = "webfront/tests/relationship_features.json"
2✔
32

33
    @classmethod
2✔
34
    def setUpClass(cls):
2✔
35
        super(InterproRESTTestCase, cls).setUpClass()
2✔
36
        cls.fr = FixtureReader(cls.fixtures + [cls.links_fixtures])
2✔
37
        cls.docs = cls.fr.get_fixtures()
2✔
38
        cls.fr.add_to_search_engine(cls.docs)
2✔
39

40
    @classmethod
2✔
41
    def tearDownClass(cls):
2✔
42
        # cls.fr.clear_search_engine()
UNCOV
43
        super(InterproRESTTestCase, cls).tearDownClass()
×
44

45
    def setUp(self):
2✔
UNCOV
46
        if TaxonomyPerEntry.objects.all().count() == 0:
×
UNCOV
47
            self.fr.generate_tax_per_entry_fixtures(self.docs)
×
UNCOV
48
            self.fr.generate_proteom_per_entry_fixtures(self.docs)
×
49

50
    # methods to check entry related responses
51
    def _check_single_entry_response(self, response, msg=""):
2✔
UNCOV
52
        self.assertEqual(response.status_code, status.HTTP_200_OK, msg)
×
UNCOV
53
        self.assertIn("entries", response.data, msg)
×
UNCOV
54
        self.assertEqual(
×
55
            len(response.data["entries"]),
56
            1,
57
            "only one entry should be included when the ID is specified" + msg,
58
        )
59
        # self.assertIn("entry", response.data["entries"][0], msg)
60
        # self._check_entry_details(response.data["entries"][0]["entry"], msg)
61

62
    def _check_entry_details(self, obj, msg=""):
2✔
UNCOV
63
        self.assertIn("entry_id", obj, msg)
×
UNCOV
64
        self.assertIn("type", obj, msg)
×
UNCOV
65
        self.assertIn("literature", obj, msg)
×
UNCOV
66
        self.assertIn("integrated", obj, msg)
×
UNCOV
67
        self.assertIn("member_databases", obj, msg)
×
UNCOV
68
        self.assertIn("accession", obj, msg)
×
UNCOV
69
        self.assertIn("counters", obj, msg)
×
70

71
    def _check_entry_from_searcher(self, obj, msg=""):
2✔
UNCOV
72
        self.assertIn("accession", obj, msg)
×
UNCOV
73
        self.assertIn("entry_protein_locations", obj, msg)
×
UNCOV
74
        self.assertIn("protein_length", obj, msg)
×
UNCOV
75
        self.assertIn("source_database", obj, msg)
×
UNCOV
76
        self.assertIn("entry_type", obj, msg)
×
UNCOV
77
        self.assertIn("entry_integrated", obj, msg)
×
78

79
    def _check_entry_count_overview(self, main_obj, msg=""):
2✔
UNCOV
80
        obj = main_obj["entries"]
×
UNCOV
81
        self.assertIn("member_databases", obj, msg)
×
UNCOV
82
        self.assertIn("interpro", obj, msg)
×
UNCOV
83
        self.assertIn("unintegrated", obj, msg)
×
84

85
    def _check_is_list_of_metadata_objects(self, _list, msg=""):
2✔
UNCOV
86
        for obj in _list:
×
UNCOV
87
            self.assertIn("metadata", obj, msg)
×
UNCOV
88
            self.assertTrue(isinstance(obj, dict))
×
UNCOV
89
            if (
×
90
                "children" not in obj["metadata"]
91
                and "is_reference" not in obj["metadata"]
92
            ):
UNCOV
93
                self.assertIn("source_database", obj["metadata"], msg)
×
UNCOV
94
            self.assertIn("accession", obj["metadata"], msg)
×
UNCOV
95
            self.assertIn("name", obj["metadata"], msg)
×
96

97
    def _check_is_list_of_objects_with_key(
2✔
98
        self, _list, key, msg="", extra_checks_fn=None
99
    ):
UNCOV
100
        for obj in _list:
×
UNCOV
101
            self.assertIn(key, obj, msg)
×
UNCOV
102
            if extra_checks_fn is not None:
×
UNCOV
103
                extra_checks_fn(obj)
×
104

105
    def _check_HTTP_response_code(self, url, code=status.HTTP_204_NO_CONTENT, msg=""):
2✔
UNCOV
106
        prev = settings.DEBUG
×
UNCOV
107
        settings.DEBUG = False
×
UNCOV
108
        response = self.client.get(url)
×
UNCOV
109
        self.assertEqual(response.status_code, code, msg)
×
UNCOV
110
        settings.DEBUG = prev
×
111

112
    def _get_in_debug_mode(self, url):
2✔
UNCOV
113
        prev = settings.DEBUG
×
UNCOV
114
        settings.DEBUG = False
×
UNCOV
115
        response = self.client.get(url)
×
UNCOV
116
        settings.DEBUG = prev
×
UNCOV
117
        return response
×
118

119
    # methods to check protein related responses
120
    def _check_protein_count_overview(self, main_obj, msg=""):
2✔
UNCOV
121
        obj = main_obj["proteins"]
×
UNCOV
122
        self.assertIn("uniprot", obj, msg)
×
UNCOV
123
        if (isinstance(obj["uniprot"], int) and obj["uniprot"] > 0) or (
×
124
            isinstance(obj["uniprot"], dict)
125
            and isinstance(obj["uniprot"]["proteins"], int)
126
            and obj["uniprot"]["proteins"] > 0
127
        ):
UNCOV
128
            self.assertTrue("unreviewed" in obj or "reviewed" in obj, msg)
×
129

130
    def _check_protein_details(self, obj):
2✔
UNCOV
131
        self.assertIn("description", obj)
×
UNCOV
132
        self.assertIn("name", obj)
×
UNCOV
133
        self.assertIn("protein_evidence", obj)
×
UNCOV
134
        self.assertIn("source_organism", obj)
×
UNCOV
135
        self.assertIn("length", obj)
×
UNCOV
136
        self.assertIn("accession", obj)
×
UNCOV
137
        self.assertIn("counters", obj)
×
138

139
    def _check_match(self, obj, msg="", include_coordinates=True):
2✔
UNCOV
140
        if include_coordinates:
×
UNCOV
141
            try:
×
UNCOV
142
                self.assertIn("entry_protein_locations", obj, msg)
×
UNCOV
143
            except Exception:
×
UNCOV
144
                self.assertIn("structure_protein_locations", obj, msg)
×
145

146
        # self.assertIsInstance(obj["coordinates"], list, msg)
147
        # TODO: Find a way to check JSON from elasticsearch
UNCOV
148
        try:
×
UNCOV
149
            self.assertIn("accession", obj, msg)
×
UNCOV
150
        except Exception:
×
UNCOV
151
            self.assertIn("chain", obj, msg)
×
UNCOV
152
        self.assertIn("source_database", obj, msg)
×
153

154
    def _check__organism_match(self, obj, msg=""):
2✔
155
        self.assertIn("accession", obj, msg)
×
156
        self.assertIn("lineage", obj, msg)
×
157
        self.assertIn("source_database", obj, msg)
×
158

159
    def _check__proteome_match(self, obj, msg=""):
2✔
UNCOV
160
        self.assertIn("accession", obj, msg)
×
UNCOV
161
        self.assertIn("taxonomy", obj, msg)
×
UNCOV
162
        self.assertIn("source_database", obj, msg)
×
163

164
    def _check_list_of_matches(self, obj, msg="", check_coordinates=True):
2✔
UNCOV
165
        for match in obj:
×
UNCOV
166
            if "taxonomy" in match:
×
UNCOV
167
                self._check__proteome_match(match, msg)
×
UNCOV
168
            elif "tax_lineage" in match:
×
169
                self._check__organism_match(match, msg)
×
170
            else:
UNCOV
171
                self._check_match(match, msg, include_coordinates=check_coordinates)
×
172

173
    # methods to check structure related responses
174
    # TODO: Extend this tests
175
    def _check_structure_count_overview(self, main_obj, msg=""):
2✔
UNCOV
176
        obj = main_obj["structures"]
×
UNCOV
177
        self.assertIn("pdb", obj, msg)
×
178

179
    def _check_structure_details(self, obj):
2✔
UNCOV
180
        self.assertIn("chains", obj)
×
UNCOV
181
        self.assertIn("accession", obj)
×
UNCOV
182
        self.assertIn("counters", obj)
×
183

184
    def _check_structure_chain_details(self, obj):
2✔
UNCOV
185
        self.assertIn("structure_protein_locations", obj)
×
UNCOV
186
        self.assertIn("organism", obj)
×
187

188
    def _check_entry_structure_details(self, obj):
2✔
UNCOV
189
        self.assertIn("entry_structure_locations", obj)
×
UNCOV
190
        self.assertIn("chain", obj)
×
191

192
    def _check_counter_by_endpoint(self, endpoint, obj, msg=""):
2✔
UNCOV
193
        if "entry" == endpoint:
×
UNCOV
194
            self._check_entry_count_overview(obj, msg)
×
UNCOV
195
        elif "protein" == endpoint:
×
UNCOV
196
            self._check_protein_count_overview(obj, msg)
×
UNCOV
197
        elif "structure" == endpoint:
×
UNCOV
198
            self._check_structure_count_overview(obj, msg)
×
UNCOV
199
        elif "organism" == endpoint:
×
200
            self._check_organism_count_overview(obj, msg)
×
201

202
    def _check_object_by_accesssion(self, obj, msg=""):
2✔
UNCOV
203
        self.assertIn("metadata", obj, msg)
×
UNCOV
204
        if "children" not in obj["metadata"] and "is_reference" not in obj["metadata"]:
×
UNCOV
205
            self.assertIn("source_database", obj["metadata"], msg)
×
UNCOV
206
            self.assertIn("counters", obj["metadata"], msg)
×
207
            # TODO: do we need counters for organism
UNCOV
208
        self.assertIn("accession", obj["metadata"], msg)
×
UNCOV
209
        self.assertIn("name", obj["metadata"], msg)
×
210

211
    def _check_count_overview_per_endpoints(self, obj, endpoints1, endpoints2, msg=""):
2✔
UNCOV
212
        for inner_obj in obj[endpoints2]:
×
UNCOV
213
            if inner_obj in [
×
214
                "interpro",
215
                "unintegrated",
216
                "uniprot",
217
                "reviewed",
218
                "unreviewed",
219
                "pdb",
220
            ] and not (
221
                inner_obj in ["unintegrated", "interpro"]
222
                and obj[endpoints2][inner_obj] == 0
223
            ):
UNCOV
224
                self.assertIn(endpoints1, obj[endpoints2][inner_obj], msg)
×
UNCOV
225
                self.assertIn(endpoints2, obj[endpoints2][inner_obj], msg)
×
226

227
    def _check_structure_and_chains(
2✔
228
        self, response, endpoint, db, acc, postfix="", key=None, msg=""
229
    ):
UNCOV
230
        urls = []
×
UNCOV
231
        if "structure" == endpoint:
×
232
            # _chains = [list(ch.keys())[0] for ch in chains[acc]]
UNCOV
233
            self.assertEqual(chains[acc], response.data["metadata"]["chains"], msg)
×
UNCOV
234
            for chain in chains[acc]:
×
UNCOV
235
                current = (
×
236
                    "/api/" + endpoint + "/" + db + "/" + acc + "/" + chain + postfix
237
                )
UNCOV
238
                urls.append(current)
×
UNCOV
239
                response_acc = self._get_in_debug_mode(current)
×
UNCOV
240
                if response_acc.status_code == status.HTTP_200_OK:
×
UNCOV
241
                    self.assertEqual(response_acc.status_code, status.HTTP_200_OK, msg)
×
UNCOV
242
                    self._check_object_by_accesssion(response_acc.data, msg)
×
UNCOV
243
                    self.assertEqual(
×
244
                        len(response_acc.data["metadata"]["chains"]), 1, msg
245
                    )
UNCOV
246
                    if key is not None:
×
UNCOV
247
                        for ch2 in response_acc.data[key]:
×
UNCOV
248
                            if "lineage" not in ch2 and "taxonomy" not in ch2:
×
UNCOV
249
                                self.assertEqual(ch2["chain"].upper(), chain, msg)
×
UNCOV
250
                    self.assertIn(chain, response_acc.data["metadata"]["chains"], msg)
×
UNCOV
251
                    self._check_match(
×
252
                        response_acc.data["metadata"]["chains"][chain], msg
253
                    )
UNCOV
254
                elif response_acc.status_code != status.HTTP_204_NO_CONTENT:
×
UNCOV
255
                    self.client.get(current)
×
UNCOV
256
        return urls
×
257

258
    def _check_structure_chains_as_counter_filter(
2✔
259
        self, endpoint, db, acc, prefix="", postfix="", key1=None, key2=None
260
    ):
UNCOV
261
        urls = []
×
UNCOV
262
        if "structure" == endpoint:
×
UNCOV
263
            for chain in chains[acc]:
×
UNCOV
264
                current = (
×
265
                    "/api/"
266
                    + prefix
267
                    + "/"
268
                    + endpoint
269
                    + "/"
270
                    + db
271
                    + "/"
272
                    + acc
273
                    + "/"
274
                    + chain
275
                    + postfix
276
                )
UNCOV
277
                urls.append(current)
×
UNCOV
278
                response = self._get_in_debug_mode(current)
×
UNCOV
279
                if response.status_code == status.HTTP_200_OK:
×
UNCOV
280
                    self._check_counter_by_endpoint(
×
281
                        prefix, response.data, "URL : [{}]".format(current)
282
                    )
283
                    # self._check_count_overview_per_endpoints(response.data, key1, key2,
284
                    #                                          "URL : [{}]".format(current))
UNCOV
285
                elif response.status_code != status.HTTP_204_NO_CONTENT:
×
286
                    self.client.get(current)
×
UNCOV
287
        return urls
×
288

289
    def _check_structure_chains_as_filter(
2✔
290
        self, endpoint, db, acc, prefix="", postfix="", key1=None
291
    ):
UNCOV
292
        urls = []
×
UNCOV
293
        if "structure" == endpoint:
×
UNCOV
294
            for chain in chains[acc]:
×
UNCOV
295
                current = (
×
296
                    "/api/"
297
                    + prefix
298
                    + "/"
299
                    + endpoint
300
                    + "/"
301
                    + db
302
                    + "/"
303
                    + acc
304
                    + "/"
305
                    + chain
306
                    + postfix
307
                )
UNCOV
308
                urls.append(current)
×
UNCOV
309
                response = self._get_in_debug_mode(current)
×
UNCOV
310
                if response.status_code == status.HTTP_200_OK:
×
UNCOV
311
                    obj = (
×
312
                        [response.data]
313
                        if "structures" in response.data
314
                        else response.data["results"]
315
                    )
UNCOV
316
                    self._check_is_list_of_metadata_objects(obj)
×
UNCOV
317
                    for result in [x[key1] for x in obj if key1 in x]:
×
UNCOV
318
                        self._check_list_of_matches(
×
319
                            result, "URL : [{}]".format(current)
320
                        )
UNCOV
321
                        self.assertGreaterEqual(len(result), 1)
×
UNCOV
322
                        for r in result:
×
UNCOV
323
                            self.assertEqual(r["chain"].upper(), chain)
×
324

UNCOV
325
                elif response.status_code != status.HTTP_204_NO_CONTENT:
×
326
                    self.client.get(current)
×
UNCOV
327
        return urls
×
328

329
    def assertSubset(self, subset, superset, proper=False):
2✔
UNCOV
330
        self.assertLessEqual(
×
331
            len(subset),
332
            len(superset),
333
            "Can't be subset if more elements in subset than in set",
334
        )
UNCOV
335
        for element in subset:
×
UNCOV
336
            self.assertIn(
×
337
                element, superset, "Element {} in subset but not in set".format(element)
338
            )
339

340
    def _check_taxonomy_details(self, obj, is_complete=True, msg=""):
2✔
UNCOV
341
        self.assertIn("accession", obj, msg)
×
UNCOV
342
        self.assertIn("name", obj, msg)
×
UNCOV
343
        self.assertIn("children", obj, msg)
×
UNCOV
344
        self.assertIn("parent", obj, msg)
×
UNCOV
345
        if is_complete:
×
UNCOV
346
            self.assertIn("lineage", obj, msg)
×
UNCOV
347
            self.assertIn("rank", obj, msg)
×
348

349
    def _check_taxonomy_from_searcher(self, obj, msg=""):
2✔
UNCOV
350
        self.assertIn("accession", obj, msg)
×
UNCOV
351
        self.assertIn("lineage", obj, msg)
×
UNCOV
352
        self.assertIn("source_database", obj, msg)
×
353

354
    def _check_proteome_from_searcher(self, obj, msg=""):
2✔
UNCOV
355
        self.assertIn("accession", obj, msg)
×
UNCOV
356
        self.assertIn("taxonomy", obj, msg)
×
UNCOV
357
        self.assertIn("source_database", obj, msg)
×
358

359
    def _check_proteome_details(self, obj, is_complete=True, msg=""):
2✔
UNCOV
360
        self.assertIn("accession", obj, msg)
×
UNCOV
361
        self.assertIn("taxonomy", obj, msg)
×
UNCOV
362
        self.assertIn("is_reference", obj, msg)
×
UNCOV
363
        self.assertIn("name", obj, msg)
×
UNCOV
364
        if is_complete:
×
UNCOV
365
            self.assertIn("strain", obj, msg)
×
UNCOV
366
            self.assertIn("assembly", obj, msg)
×
367

368
    def _check_taxonomy_count_overview(self, main_obj, msg=""):
2✔
UNCOV
369
        self.assertIn("taxa", main_obj, msg)
×
UNCOV
370
        self.assertIn("uniprot", main_obj["taxa"], msg)
×
371

372
    def _check_proteome_count_overview(self, main_obj, msg=""):
2✔
UNCOV
373
        self.assertIn("proteomes", main_obj, msg)
×
UNCOV
374
        self.assertIn("uniprot", main_obj["proteomes"], msg)
×
375

376
    def _check_set_details(self, obj, is_complete=True, msg=""):
2✔
UNCOV
377
        self.assertIn("accession", obj, msg)
×
UNCOV
378
        self.assertIn("name", obj, msg)
×
UNCOV
379
        self.assertIn("source_database", obj, msg)
×
UNCOV
380
        if is_complete:
×
UNCOV
381
            self.assertIn("relationships", obj, msg)
×
UNCOV
382
            self.assertIn("description", obj, msg)
×
383

384
    def _check_set_count_overview(self, main_obj, msg=""):
2✔
UNCOV
385
        self.assertIn("sets", main_obj, msg)
×
UNCOV
386
        for s in main_obj["sets"]:
×
UNCOV
387
            self.assertIn(s, list(settings.ENTRY_SETS.keys()) + ["all"], msg)
×
388

389
    def _check_set_from_searcher(self, obj, msg=""):
2✔
UNCOV
390
        self.assertIn("accession", obj, msg)
×
UNCOV
391
        self.assertIn("source_database", obj, msg)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc