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

ProteinsWebTeam / interpro7-api / 9887981828

11 Jul 2024 08:20AM UTC coverage: 93.827% (-0.2%) from 94.023%
9887981828

Pull #151

github

web-flow
Update webfront/serializers/content_serializers.py

Co-authored-by: Matthias Blum <mat.blum@gmail.com>
Pull Request #151: Replacing `_subset` with `_url` in filter by DB requests

429 of 436 new or added lines in 29 files covered. (98.39%)

6 existing lines in 5 files now uncovered.

9089 of 9687 relevant lines covered (93.83%)

0.94 hits per line

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

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

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

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

12
plurals = ModelContentSerializer.plurals
1✔
13

14
validateURL = URLValidator()
1✔
15

16
chains = {
1✔
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)
1✔
25
@override_settings(SEARCHER_PASSWORD=settings.SEARCHER_TEST_PASSWORD)
1✔
26
@override_settings(SEARCHER_INDEX="test")
1✔
27
class InterproRESTTestCase(APITransactionTestCase):
1✔
28
    fixtures = [
1✔
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"
1✔
38

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

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

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

56
    # methods to check entry related responses
57
    def _check_single_entry_response(self, response, msg=""):
1✔
58
        self.assertEqual(response.status_code, status.HTTP_200_OK, msg)
1✔
59
        self.assertIn("entries", response.data, msg)
1✔
60
        self.assertEqual(
1✔
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=""):
1✔
69
        self.assertIn("entry_id", obj, msg)
1✔
70
        self.assertIn("type", obj, msg)
1✔
71
        self.assertIn("literature", obj, msg)
1✔
72
        self.assertIn("integrated", obj, msg)
1✔
73
        self.assertIn("member_databases", obj, msg)
1✔
74
        self.assertIn("accession", obj, msg)
1✔
75
        self.assertIn("counters", obj, msg)
1✔
76

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

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

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

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

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

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

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

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

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

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

160
    def _check__organism_match(self, obj, msg=""):
1✔
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=""):
1✔
166
        self.assertIn("accession", obj, msg)
1✔
167
        self.assertIn("taxonomy", obj, msg)
1✔
168
        self.assertIn("source_database", obj, msg)
1✔
169

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

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

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

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

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

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

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

217
    def _check_count_overview_per_endpoints(self, obj, endpoints1, endpoints2, msg=""):
1✔
218
        for inner_obj in obj[endpoints2]:
1✔
219
            if inner_obj in [
1✔
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
            ):
230
                self.assertIn(endpoints1, obj[endpoints2][inner_obj], msg)
1✔
231
                self.assertIn(endpoints2, obj[endpoints2][inner_obj], msg)
1✔
232

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

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

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

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

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

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

341
    def _check_list_url_with_and_without_subset(
1✔
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
    ):
351
        response = self.client.get(url)
1✔
352
        self.assertEqual(response.status_code, status.HTTP_200_OK, f"URL : [{url}]")
1✔
353
        self._check_is_list_of_objects_with_key(response.data["results"], "metadata")
1✔
354
        if check_results_fn is not None:
1✔
355
            check_results_fn(response.data["results"])
1✔
356
        self._check_is_list_of_objects_with_key(
1✔
357
            response.data["results"], f"{plurals[endpoint]}_url"
358
        )
359
        for result in response.data["results"]:
1✔
360
            self.assertURL(result[f"{plurals[endpoint]}_url"])
1✔
361

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

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

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

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

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

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

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

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

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

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

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

© 2025 Coveralls, Inc