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

ProteinsWebTeam / interpro7-api / 1

16 Sep 2024 10:36AM UTC coverage: 26.483% (-67.4%) from 93.93%
1

push

github

web-flow
Merge pull request #160 from ProteinsWebTeam/dev

InterPro 102.0

9 of 57 new or added lines in 9 files covered. (15.79%)

6479 existing lines in 51 files now uncovered.

2608 of 9848 relevant lines covered (26.48%)

1.32 hits per line

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

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

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

7
from webfront.models import TaxonomyPerEntry
5✔
8
from webfront.serializers.content_serializers import ModelContentSerializer
5✔
9
from webfront.tests.fixtures_reader import FixtureReader
5✔
10
from django.core.validators import URLValidator
5✔
11

12
plurals = ModelContentSerializer.plurals
5✔
13

14
validateURL = URLValidator()
5✔
15

16
chains = {
5✔
17
    "1JM7": ["A", "B"],
18
    "1T2V": ["A", "B", "C", "D", "E"],
19
    "2BKM": ["A", "B"],
20
    "1JZ8": ["A", "B"],
21
}
22

23

24
@override_settings(SEARCHER_URL=settings.SEARCHER_TEST_URL)
5✔
25
@override_settings(SEARCHER_PASSWORD=settings.SEARCHER_TEST_PASSWORD)
5✔
26
@override_settings(SEARCHER_INDEX="test")
5✔
27
class InterproRESTTestCase(APITransactionTestCase):
5✔
28
    fixtures = [
5✔
29
        "webfront/tests/fixtures_entry.json",
30
        "webfront/tests/fixtures_protein.json",
31
        "webfront/tests/fixtures_structure.json",
32
        "webfront/tests/fixtures_organisms.json",
33
        "webfront/tests/fixtures_set.json",
34
        "webfront/tests/fixtures_database.json",
35
        "webfront/tests/fixtures_entryannotation.json",
36
    ]
37
    links_fixtures = "webfront/tests/relationship_features.json"
5✔
38

39
    @classmethod
5✔
40
    def setUpClass(cls):
5✔
41
        super(InterproRESTTestCase, cls).setUpClass()
5✔
42
        cls.fr = FixtureReader(cls.fixtures + [cls.links_fixtures])
5✔
43
        cls.docs = cls.fr.get_fixtures()
5✔
44
        cls.fr.add_to_search_engine(cls.docs)
5✔
45

46
    @classmethod
5✔
47
    def tearDownClass(cls):
5✔
48
        # cls.fr.clear_search_engine()
UNCOV
49
        super(InterproRESTTestCase, cls).tearDownClass()
×
50

51
    def setUp(self):
5✔
UNCOV
52
        if TaxonomyPerEntry.objects.all().count() == 0:
×
UNCOV
53
            self.fr.generate_tax_per_entry_fixtures(self.docs)
×
UNCOV
54
            self.fr.generate_proteom_per_entry_fixtures(self.docs)
×
55

56
    # methods to check entry related responses
57
    def _check_single_entry_response(self, response, msg=""):
5✔
UNCOV
58
        self.assertEqual(response.status_code, status.HTTP_200_OK, msg)
×
UNCOV
59
        self.assertIn("entries", response.data, msg)
×
UNCOV
60
        self.assertEqual(
×
61
            len(response.data["entries"]),
62
            1,
63
            f"only one entry should be included when the ID is specified{msg}",
64
        )
65
        # self.assertIn("entry", response.data["entries"][0], msg)
66
        # self._check_entry_details(response.data["entries"][0]["entry"], msg)
67

68
    def _check_entry_details(self, obj, msg=""):
5✔
UNCOV
69
        self.assertIn("entry_id", obj, msg)
×
UNCOV
70
        self.assertIn("type", obj, msg)
×
UNCOV
71
        self.assertIn("literature", obj, msg)
×
UNCOV
72
        self.assertIn("integrated", obj, msg)
×
UNCOV
73
        self.assertIn("member_databases", obj, msg)
×
UNCOV
74
        self.assertIn("accession", obj, msg)
×
UNCOV
75
        self.assertIn("counters", obj, msg)
×
76

77
    def _check_entry_from_searcher(self, obj, msg=""):
5✔
UNCOV
78
        self.assertIn("accession", obj, msg)
×
UNCOV
79
        self.assertIn("entry_protein_locations", obj, msg)
×
UNCOV
80
        self.assertIn("protein_length", obj, msg)
×
UNCOV
81
        self.assertIn("source_database", obj, msg)
×
UNCOV
82
        self.assertIn("entry_type", obj, msg)
×
UNCOV
83
        self.assertIn("entry_integrated", obj, msg)
×
84

85
    def _check_entry_count_overview(self, main_obj, msg=""):
5✔
UNCOV
86
        obj = main_obj["entries"]
×
UNCOV
87
        self.assertIn("member_databases", obj, msg)
×
UNCOV
88
        self.assertIn("interpro", obj, msg)
×
UNCOV
89
        self.assertIn("unintegrated", obj, msg)
×
90

91
    def _check_is_list_of_metadata_objects(self, _list, msg=""):
5✔
UNCOV
92
        for obj in _list:
×
UNCOV
93
            self.assertIn("metadata", obj, msg)
×
UNCOV
94
            self.assertTrue(isinstance(obj, dict))
×
UNCOV
95
            if (
×
96
                "children" not in obj["metadata"]
97
                and "is_reference" not in obj["metadata"]
98
            ):
UNCOV
99
                self.assertIn("source_database", obj["metadata"], msg)
×
UNCOV
100
            self.assertIn("accession", obj["metadata"], msg)
×
UNCOV
101
            self.assertIn("name", obj["metadata"], msg)
×
102

103
    def _check_is_list_of_objects_with_key(
5✔
104
        self, _list, key, msg="", extra_checks_fn=None
105
    ):
UNCOV
106
        for obj in _list:
×
UNCOV
107
            self.assertIn(key, obj, msg)
×
UNCOV
108
            if extra_checks_fn is not None:
×
UNCOV
109
                extra_checks_fn(obj)
×
110

111
    def _check_HTTP_response_code(self, url, code=status.HTTP_204_NO_CONTENT, msg=""):
5✔
UNCOV
112
        prev = settings.DEBUG
×
UNCOV
113
        settings.DEBUG = False
×
UNCOV
114
        response = self.client.get(url)
×
UNCOV
115
        self.assertEqual(response.status_code, code, msg)
×
UNCOV
116
        settings.DEBUG = prev
×
117

118
    def _get_in_debug_mode(self, url):
5✔
UNCOV
119
        prev = settings.DEBUG
×
UNCOV
120
        settings.DEBUG = False
×
UNCOV
121
        response = self.client.get(url)
×
UNCOV
122
        settings.DEBUG = prev
×
UNCOV
123
        return response
×
124

125
    # methods to check protein related responses
126
    def _check_protein_count_overview(self, main_obj, msg=""):
5✔
UNCOV
127
        obj = main_obj["proteins"]
×
UNCOV
128
        self.assertIn("uniprot", obj, msg)
×
UNCOV
129
        if (isinstance(obj["uniprot"], int) and obj["uniprot"] > 0) or (
×
130
            isinstance(obj["uniprot"], dict)
131
            and isinstance(obj["uniprot"]["proteins"], int)
132
            and obj["uniprot"]["proteins"] > 0
133
        ):
UNCOV
134
            self.assertTrue("unreviewed" in obj or "reviewed" in obj, msg)
×
135

136
    def _check_protein_details(self, obj):
5✔
UNCOV
137
        self.assertIn("description", obj)
×
UNCOV
138
        self.assertIn("name", obj)
×
UNCOV
139
        self.assertIn("protein_evidence", obj)
×
UNCOV
140
        self.assertIn("source_organism", obj)
×
UNCOV
141
        self.assertIn("length", obj)
×
UNCOV
142
        self.assertIn("accession", obj)
×
UNCOV
143
        self.assertIn("counters", obj)
×
144

145
    def _check_match(self, obj, msg="", include_coordinates=True):
5✔
UNCOV
146
        if include_coordinates:
×
UNCOV
147
            try:
×
UNCOV
148
                self.assertIn("entry_protein_locations", obj, msg)
×
UNCOV
149
            except Exception:
×
UNCOV
150
                self.assertIn("structure_protein_locations", obj, msg)
×
151

152
        # self.assertIsInstance(obj["coordinates"], list, msg)
153
        # TODO: Find a way to check JSON from elasticsearch
UNCOV
154
        try:
×
UNCOV
155
            self.assertIn("accession", obj, msg)
×
UNCOV
156
        except Exception:
×
UNCOV
157
            self.assertIn("chain", obj, msg)
×
UNCOV
158
        self.assertIn("source_database", obj, msg)
×
159

160
    def _check__organism_match(self, obj, msg=""):
5✔
161
        self.assertIn("accession", obj, msg)
×
162
        self.assertIn("lineage", obj, msg)
×
163
        self.assertIn("source_database", obj, msg)
×
164

165
    def _check__proteome_match(self, obj, msg=""):
5✔
UNCOV
166
        self.assertIn("accession", obj, msg)
×
UNCOV
167
        self.assertIn("taxonomy", obj, msg)
×
UNCOV
168
        self.assertIn("source_database", obj, msg)
×
169

170
    def _check_list_of_matches(self, obj, msg="", check_coordinates=True):
5✔
UNCOV
171
        for match in obj:
×
UNCOV
172
            if "taxonomy" in match:
×
UNCOV
173
                self._check__proteome_match(match, msg)
×
UNCOV
174
            elif "tax_lineage" in match:
×
175
                self._check__organism_match(match, msg)
×
176
            else:
UNCOV
177
                self._check_match(match, msg, include_coordinates=check_coordinates)
×
178

179
    # methods to check structure related responses
180
    # TODO: Extend this tests
181
    def _check_structure_count_overview(self, main_obj, msg=""):
5✔
UNCOV
182
        obj = main_obj["structures"]
×
UNCOV
183
        self.assertIn("pdb", obj, msg)
×
184

185
    def _check_structure_details(self, obj):
5✔
UNCOV
186
        self.assertIn("chains", obj)
×
UNCOV
187
        self.assertIn("accession", obj)
×
UNCOV
188
        self.assertIn("counters", obj)
×
189

190
    def _check_structure_chain_details(self, obj):
5✔
UNCOV
191
        self.assertIn("structure_protein_locations", obj)
×
UNCOV
192
        self.assertIn("organism", obj)
×
193

194
    def _check_entry_structure_details(self, obj):
5✔
UNCOV
195
        self.assertIn("entry_structure_locations", obj)
×
UNCOV
196
        self.assertIn("chain", obj)
×
197

198
    def _check_counter_by_endpoint(self, endpoint, obj, msg=""):
5✔
UNCOV
199
        if "entry" == endpoint:
×
UNCOV
200
            self._check_entry_count_overview(obj, msg)
×
UNCOV
201
        elif "protein" == endpoint:
×
UNCOV
202
            self._check_protein_count_overview(obj, msg)
×
UNCOV
203
        elif "structure" == endpoint:
×
UNCOV
204
            self._check_structure_count_overview(obj, msg)
×
UNCOV
205
        elif "organism" == endpoint:
×
206
            self._check_organism_count_overview(obj, msg)
×
207

208
    def _check_object_by_accesssion(self, obj, msg=""):
5✔
UNCOV
209
        self.assertIn("metadata", obj, msg)
×
UNCOV
210
        if "children" not in obj["metadata"] and "is_reference" not in obj["metadata"]:
×
UNCOV
211
            self.assertIn("source_database", obj["metadata"], msg)
×
UNCOV
212
            self.assertIn("counters", obj["metadata"], msg)
×
213
            # TODO: do we need counters for organism
UNCOV
214
        self.assertIn("accession", obj["metadata"], msg)
×
UNCOV
215
        self.assertIn("name", obj["metadata"], msg)
×
216

217
    def _check_count_overview_per_endpoints(self, obj, endpoints1, endpoints2, msg=""):
5✔
UNCOV
218
        for inner_obj in obj[endpoints2]:
×
UNCOV
219
            if inner_obj in [
×
220
                "interpro",
221
                "unintegrated",
222
                "uniprot",
223
                "reviewed",
224
                "unreviewed",
225
                "pdb",
226
            ] and not (
227
                inner_obj in ["unintegrated", "interpro"]
228
                and obj[endpoints2][inner_obj] == 0
229
            ):
UNCOV
230
                self.assertIn(endpoints1, obj[endpoints2][inner_obj], msg)
×
UNCOV
231
                self.assertIn(endpoints2, obj[endpoints2][inner_obj], msg)
×
232

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

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

281
    def _check_structure_chains_as_filter(
5✔
282
        self, endpoint, db, acc, prefix="", postfix="", key1=None
283
    ):
UNCOV
284
        urls = []
×
UNCOV
285
        if "structure" == endpoint:
×
UNCOV
286
            for chain in chains[acc]:
×
UNCOV
287
                current = f"/api/{prefix}/{endpoint}/{db}/{acc}/{chain}{postfix}"
×
UNCOV
288
                urls.append(current)
×
UNCOV
289
                response = self._get_in_debug_mode(current)
×
UNCOV
290
                if response.status_code == status.HTTP_200_OK:
×
UNCOV
291
                    obj = (
×
292
                        [response.data]
293
                        if "structures" in response.data
294
                        else response.data["results"]
295
                    )
UNCOV
296
                    self._check_is_list_of_metadata_objects(obj)
×
UNCOV
297
                    for result in [x[key1] for x in obj if key1 in x]:
×
UNCOV
298
                        self._check_list_of_matches(result, f"URL : [{current}]")
×
UNCOV
299
                        self.assertGreaterEqual(len(result), 1)
×
UNCOV
300
                        for r in result:
×
UNCOV
301
                            self.assertEqual(r["chain"].upper(), chain)
×
302

UNCOV
303
                elif response.status_code != status.HTTP_204_NO_CONTENT:
×
304
                    self.client.get(current)
×
UNCOV
305
        return urls
×
306

307
    def assertSubset(self, subset, superset, proper=False):
5✔
UNCOV
308
        self.assertLessEqual(
×
309
            len(subset),
310
            len(superset),
311
            "Can't be subset if more elements in subset than in set",
312
        )
UNCOV
313
        for element in subset:
×
UNCOV
314
            self.assertIn(
×
315
                element, superset, f"Element {element} in subset but not in set"
316
            )
317

318
    def _check_details_url_with_and_without_subset(
5✔
319
        self,
320
        url,
321
        endpoint,
322
        check_metadata_fn=None,
323
        check_subset_fn=None,
324
        check_inner_subset_fn=None,
325
    ):
UNCOV
326
        response = self.client.get(url)
×
UNCOV
327
        self.assertEqual(response.status_code, status.HTTP_200_OK, f"URL : [{url}]")
×
UNCOV
328
        if check_metadata_fn is not None:
×
UNCOV
329
            check_metadata_fn(response.data["metadata"])
×
UNCOV
330
        self.assertIn(f"{plurals[endpoint]}_url", response.data)
×
UNCOV
331
        self.assertURL(response.data[f"{plurals[endpoint]}_url"])
×
UNCOV
332
        response = self.client.get(url + "?show-subset")
×
UNCOV
333
        self.assertEqual(response.status_code, status.HTTP_200_OK, f"URL : [{url}]")
×
UNCOV
334
        self.assertIn(f"{endpoint}_subset", response.data)
×
UNCOV
335
        if check_subset_fn is not None:
×
UNCOV
336
            check_subset_fn(response.data[f"{endpoint}_subset"])
×
UNCOV
337
        if check_inner_subset_fn is not None:
×
UNCOV
338
            for st in response.data[f"{endpoint}_subset"]:
×
UNCOV
339
                check_inner_subset_fn(st)
×
340

341
    def _check_list_url_with_and_without_subset(
5✔
342
        self,
343
        url,
344
        endpoint,
345
        check_results_fn=None,
346
        check_result_fn=None,
347
        check_metadata_fn=None,
348
        check_subset_fn=None,
349
        check_inner_subset_fn=None,
350
    ):
UNCOV
351
        response = self.client.get(url)
×
UNCOV
352
        self.assertEqual(response.status_code, status.HTTP_200_OK, f"URL : [{url}]")
×
UNCOV
353
        self._check_is_list_of_objects_with_key(response.data["results"], "metadata")
×
UNCOV
354
        if check_results_fn is not None:
×
UNCOV
355
            check_results_fn(response.data["results"])
×
UNCOV
356
        self._check_is_list_of_objects_with_key(
×
357
            response.data["results"], f"{plurals[endpoint]}_url"
358
        )
UNCOV
359
        for result in response.data["results"]:
×
UNCOV
360
            self.assertURL(result[f"{plurals[endpoint]}_url"])
×
361

UNCOV
362
        response = self.client.get(url + "?show-subset")
×
UNCOV
363
        self.assertEqual(response.status_code, status.HTTP_200_OK, f"URL : [{url}]")
×
UNCOV
364
        self._check_is_list_of_objects_with_key(
×
365
            response.data["results"], f"{endpoint}_subset"
366
        )
UNCOV
367
        for result in response.data["results"]:
×
UNCOV
368
            if check_result_fn is not None:
×
UNCOV
369
                check_result_fn(result)
×
UNCOV
370
            if check_metadata_fn is not None:
×
UNCOV
371
                check_metadata_fn(result["metadata"])
×
UNCOV
372
            if check_subset_fn is not None:
×
373
                check_subset_fn(result[f"{endpoint}_subset"])
×
UNCOV
374
            for s in result[f"{endpoint}_subset"]:
×
UNCOV
375
                if check_inner_subset_fn is not None:
×
UNCOV
376
                    check_inner_subset_fn(s)
×
377

378
    def assertURL(self, url, msg=None):
5✔
UNCOV
379
        try:
×
UNCOV
380
            validateURL(url)
×
381
        except:
×
382
            raise self.failureException(msg)
×
383

384
    def _check_taxonomy_details(self, obj, is_complete=True, msg=""):
5✔
UNCOV
385
        self.assertIn("accession", obj, msg)
×
UNCOV
386
        self.assertIn("name", obj, msg)
×
UNCOV
387
        self.assertIn("children", obj, msg)
×
UNCOV
388
        self.assertIn("parent", obj, msg)
×
UNCOV
389
        if is_complete:
×
UNCOV
390
            self.assertIn("lineage", obj, msg)
×
UNCOV
391
            self.assertIn("rank", obj, msg)
×
392

393
    def _check_taxonomy_from_searcher(self, obj, msg=""):
5✔
UNCOV
394
        self.assertIn("accession", obj, msg)
×
UNCOV
395
        self.assertIn("lineage", obj, msg)
×
UNCOV
396
        self.assertIn("source_database", obj, msg)
×
397

398
    def _check_proteome_from_searcher(self, obj, msg=""):
5✔
UNCOV
399
        self.assertIn("accession", obj, msg)
×
UNCOV
400
        self.assertIn("taxonomy", obj, msg)
×
UNCOV
401
        self.assertIn("source_database", obj, msg)
×
402

403
    def _check_proteome_details(self, obj, is_complete=True, msg=""):
5✔
UNCOV
404
        self.assertIn("accession", obj, msg)
×
UNCOV
405
        self.assertIn("taxonomy", obj, msg)
×
UNCOV
406
        self.assertIn("name", obj, msg)
×
UNCOV
407
        if is_complete:
×
UNCOV
408
            self.assertIn("strain", obj, msg)
×
UNCOV
409
            self.assertIn("assembly", obj, msg)
×
410

411
    def _check_taxonomy_count_overview(self, main_obj, msg=""):
5✔
UNCOV
412
        self.assertIn("taxa", main_obj, msg)
×
UNCOV
413
        self.assertIn("uniprot", main_obj["taxa"], msg)
×
414

415
    def _check_proteome_count_overview(self, main_obj, msg=""):
5✔
UNCOV
416
        self.assertIn("proteomes", main_obj, msg)
×
UNCOV
417
        self.assertIn("uniprot", main_obj["proteomes"], msg)
×
418

419
    def _check_set_details(self, obj, is_complete=True, msg=""):
5✔
UNCOV
420
        self.assertIn("accession", obj, msg)
×
UNCOV
421
        self.assertIn("name", obj, msg)
×
UNCOV
422
        self.assertIn("source_database", obj, msg)
×
UNCOV
423
        if is_complete:
×
UNCOV
424
            self.assertIn("relationships", obj, msg)
×
UNCOV
425
            self.assertIn("description", obj, msg)
×
426

427
    def _check_set_count_overview(self, main_obj, msg=""):
5✔
UNCOV
428
        self.assertIn("sets", main_obj, msg)
×
UNCOV
429
        for s in main_obj["sets"]:
×
UNCOV
430
            self.assertIn(s, list(settings.ENTRY_SETS.keys()) + ["all"], msg)
×
431

432
    def _check_set_from_searcher(self, obj, msg=""):
5✔
UNCOV
433
        self.assertIn("accession", obj, msg)
×
UNCOV
434
        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