Skip to content

Commit a77a16d

Browse files
DigbigpigAaron Suarez
authored and
Aaron Suarez
committed
Improve test coverage (#142)
Improve test coverage
1 parent c80b754 commit a77a16d

File tree

4 files changed

+19
-43
lines changed

4 files changed

+19
-43
lines changed

app/api/routes.py

+5-37
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from traceback import print_tb
2-
31
from flask import request, redirect
42
from sqlalchemy import or_, func
5-
from sqlalchemy.orm.exc import NoResultFound
63

74
from app.api import bp
85
from app.api.auth import is_user_oc_member, authenticate
@@ -99,14 +96,7 @@ def apikey():
9996

10097
# Helpers
10198
def get_resource(id):
102-
resource = None
103-
try:
104-
resource = Resource.query.get(id)
105-
106-
except NoResultFound as e:
107-
print_tb(e.__traceback__)
108-
logger.exception(e)
109-
return redirect('/404')
99+
resource = Resource.query.get(id)
110100

111101
if resource:
112102
return standardize_response(payload=dict(data=(resource.serialize)))
@@ -245,22 +235,12 @@ def get_attributes(json):
245235

246236

247237
def update_votes(id, vote_direction):
248-
try:
249-
resource = Resource.query.get(id)
250238

251-
if not resource:
252-
return redirect('/404')
239+
resource = Resource.query.get(id)
253240

254-
except NoResultFound as e:
255-
print_tb(e.__traceback__)
256-
logger.exception(e)
241+
if not resource:
257242
return redirect('/404')
258243

259-
except Exception as e:
260-
print_tb(e.__traceback__)
261-
logger.exception(e)
262-
return standardize_response(status_code=500)
263-
264244
initial_count = getattr(resource, vote_direction)
265245
setattr(resource, vote_direction, initial_count+1)
266246
db.session.commit()
@@ -269,22 +249,11 @@ def update_votes(id, vote_direction):
269249

270250

271251
def add_click(id):
272-
try:
273-
resource = Resource.query.get(id)
274-
275-
if not resource:
276-
return redirect('/404')
252+
resource = Resource.query.get(id)
277253

278-
except NoResultFound as e:
279-
print_tb(e.__traceback__)
280-
logger.exception(e)
254+
if not resource:
281255
return redirect('/404')
282256

283-
except Exception as e:
284-
print_tb(e.__traceback__)
285-
logger.exception(e)
286-
return standardize_response(status_code=500)
287-
288257
initial_count = getattr(resource, 'times_clicked')
289258
setattr(resource, 'times_clicked', initial_count + 1)
290259
db.session.commit()
@@ -293,7 +262,6 @@ def add_click(id):
293262

294263

295264
def set_resource(id, json, db):
296-
resource = None
297265
resource = Resource.query.get(id)
298266

299267
if not resource:

app/cli.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .models import Category, Language, Resource
88

99

10-
def import_resources(db):
10+
def import_resources(db): # pragma: no cover
1111
# Step 1: Get data
1212
with open('resources.yml', encoding='utf-8') as f:
1313
data = yaml.full_load(f)
@@ -115,7 +115,7 @@ def create_resource(resource, db):
115115
print('exception', e)
116116

117117

118-
def update_resource(resource, existing_resource):
118+
def update_resource(resource, existing_resource): # pragma: no cover
119119
existing_resource.name = resource['name']
120120
existing_resource.url = resource['url']
121121
existing_resource.category = resource['category']
@@ -124,7 +124,7 @@ def update_resource(resource, existing_resource):
124124
existing_resource.languages = resource['languages']
125125

126126

127-
def register(app, db):
127+
def register(app, db): # pragma: no cover
128128
@app.cli.group()
129129
def db_migrate():
130130
""" migration commands"""

app/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def standardize_response(payload={}, status_code=200):
9797
def setup_logger(name, log_file, level=logging.INFO):
9898
"""Function setup as many loggers as you want"""
9999
if not os.path.exists('log'):
100-
os.makedirs('log')
100+
os.makedirs('log') # pragma: no cover
101101

102102
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
103103
handler = logging.FileHandler(log_file)

tests/unit/test_routes.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ def test_single_resource_out_of_bounds(module_client, module_db):
8383

8484
too_low = 0
8585
too_high = 9999
86-
response = client.get(f"api/v1/resources{too_low}")
86+
response = client.get(f"api/v1/resources/{too_low}", follow_redirects=True)
8787

8888
assert (response.status_code == 404)
8989

90-
response = client.get(f"api/v1/resources{too_high}")
90+
response = client.get(f"api/v1/resources/{too_high}", follow_redirects=True)
9191

9292
assert (response.status_code == 404)
9393

@@ -417,6 +417,7 @@ def test_key_query_error(module_client, module_db, fake_auth_from_oc, fake_key_q
417417
))
418418
assert (response.status_code == 500)
419419

420+
420421
def test_internal_server_error_handler(module_client, module_db, fake_paginated_data_error):
421422
client = module_client
422423

@@ -433,6 +434,13 @@ def test_internal_server_error_handler(module_client, module_db, fake_paginated_
433434
assert (response.json['errors'][0].get("code") == "server-error")
434435

435436

437+
def test_method_not_allowed_handler(module_client):
438+
client = module_client
439+
440+
response = client.patch('api/v1/resources')
441+
assert (response.status_code == 405)
442+
assert (response.json['errors'][0].get("code") == "method-not-allowed")
443+
436444
##########################################
437445
## Other Routes
438446
##########################################

0 commit comments

Comments
 (0)