Skip to content

Commit 3806507

Browse files
committed
modified publications endpoint and tests
1 parent cb8d72f commit 3806507

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

api/resources/gene_information.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,37 @@ def get(self, species="", gene_id=""):
7171
return BARUtils.error_exit("There are no data found for the given gene")
7272

7373

74-
@gene_information.route("/gene_publications/<string:gene_id>")
74+
@gene_information.route("/gene_publications/<string:species>/<string:gene_id>")
7575
class GenePublications(Resource):
76-
# @gene_information.param("species", _in="path", default="arabidopsis")
76+
@gene_information.param("species", _in="path", default="arabidopsis")
7777
@gene_information.param("gene_id", _in="path", default="AT1G01010")
78-
def get(self, gene_id=""):
78+
def get(self, species="", gene_id=""):
7979
"""This end point provides publications given a gene ID."""
8080
publications = []
8181

8282
# Escape input
83+
species = escape(species)
8384
gene_id = escape(gene_id)
8485

85-
# truncate
86+
# truncate gene ID
8687
for i in range(len(gene_id)):
8788
if gene_id[i] == ".":
8889
gene_id = gene_id[0:i]
8990
break
9091

91-
if BARUtils.is_arabidopsis_gene_valid(gene_id):
92-
rows = db.session.execute(db.select(EPlant2Publications).where(EPlant2Publications.gene == gene_id)).scalars().all()
93-
for row in rows:
94-
publications.append({"gene_id": row.gene, "author": row.author, "year": row.year, "journal": row.journal, "title": row.title, "pubmed": row.pubmed})
92+
# Set the database and check if genes are valid
93+
if species == "arabidopsis":
94+
database = EPlant2Publications
95+
96+
if not BARUtils.is_arabidopsis_gene_valid(gene_id):
97+
return BARUtils.error_exit("Invalid gene id"), 400
9598
else:
96-
return BARUtils.error_exit("Invalid gene id"), 400
99+
return BARUtils.error_exit("No data for the given species")
100+
101+
# Get data
102+
rows = db.session.execute(db.select(database).where(database.gene == gene_id)).scalars().all()
103+
for row in rows:
104+
publications.append({"gene_id": row.gene, "author": row.author, "year": row.year, "journal": row.journal, "title": row.title, "pubmed": row.pubmed})
97105

98106
# Return results if there are data
99107
if len(publications) > 0:

tests/resources/test_gene_information.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_get_arabidopsis_gene_publications(self):
4646
:return:
4747
"""
4848
# Valid data
49-
response = self.app_client.get("/gene_information/gene_publications/AT1G01020")
49+
response = self.app_client.get("/gene_information/gene_publications/arabidopsis/AT1G01020")
5050
expected = {
5151
"wasSuccessful": True,
5252
"data": [
@@ -70,7 +70,7 @@ def test_get_arabidopsis_gene_publications(self):
7070
}
7171
self.assertEqual(response.json, expected)
7272

73-
response = self.app_client.get("/gene_information/gene_publications/AT1G01020.")
73+
response = self.app_client.get("/gene_information/gene_publications/arabidopsis/AT1G01020.")
7474
expected = {
7575
"wasSuccessful": True,
7676
"data": [
@@ -94,7 +94,7 @@ def test_get_arabidopsis_gene_publications(self):
9494
}
9595
self.assertEqual(response.json, expected)
9696

97-
response = self.app_client.get("/gene_information/gene_publications/AT1G01020.0")
97+
response = self.app_client.get("/gene_information/gene_publications/arabidopsis/AT1G01020.0")
9898
expected = {
9999
"wasSuccessful": True,
100100
"data": [
@@ -118,7 +118,7 @@ def test_get_arabidopsis_gene_publications(self):
118118
}
119119
self.assertEqual(response.json, expected)
120120

121-
response = self.app_client.get("/gene_information/gene_publications/AT1G01020.1")
121+
response = self.app_client.get("/gene_information/gene_publications/arabidopsis/AT1G01020.1")
122122
expected = {
123123
"wasSuccessful": True,
124124
"data": [
@@ -142,7 +142,7 @@ def test_get_arabidopsis_gene_publications(self):
142142
}
143143
self.assertEqual(response.json, expected)
144144

145-
response = self.app_client.get("/gene_information/gene_publications/AT1G01020.12345")
145+
response = self.app_client.get("/gene_information/gene_publications/arabidopsis/AT1G01020.12345")
146146
expected = {
147147
"wasSuccessful": True,
148148
"data": [
@@ -167,25 +167,30 @@ def test_get_arabidopsis_gene_publications(self):
167167
self.assertEqual(response.json, expected)
168168

169169
# Data not found, but gene is valid
170-
response = self.app_client.get("/gene_information/gene_publications/AT1G01035")
170+
response = self.app_client.get("/gene_information/gene_publications/arabidopsis/AT1G01035")
171171
expected = {
172172
"wasSuccessful": False,
173173
"error": "There are no data found for the given gene",
174174
}
175175
self.assertEqual(response.json, expected)
176176

177-
response = self.app_client.get("/gene_information/gene_publications/AT1G010400")
177+
response = self.app_client.get("/gene_information/gene_publications/arabidopsis/AT1G010400")
178178
expected = {
179179
"wasSuccessful": False,
180180
"error": "There are no data found for the given gene",
181181
}
182182
self.assertEqual(response.json, expected)
183183

184184
# Invalid Gene
185-
response = self.app_client.get("/gene_information/gene_publications/001G01030")
185+
response = self.app_client.get("/gene_information/gene_publications/arabidopsis/001G01030")
186186
expected = {"wasSuccessful": False, "error": "Invalid gene id"}
187187
self.assertEqual(response.json, expected)
188188

189+
# Invalid Species
190+
response = self.app_client.get("/gene_information/gene_publications/x/AT1G01020")
191+
expected = {"wasSuccessful": False, "error": "No data for the given species"}
192+
self.assertEqual(response.json, expected)
193+
189194
def test_get_arabidopsis_gene_isoform(self):
190195
"""This tests checks GET request for gene isoforms Arabidopsis
191196
:return:

0 commit comments

Comments
 (0)