Skip to content

Commit 9117f0f

Browse files
authored
removed pagination for GET languages and GET categories (#340)
* removed pagination for GET languages and GET categories * removed references to pagination in docs * multiple removed pagination references, updated total_counts, added assertions for test_languages and test_categories, changed all "Linux" references to "Unix/Linux/*nix" in resources.yml * fixed test failure on test_languages
1 parent 174a5d8 commit 9117f0f

9 files changed

Lines changed: 69 additions & 98 deletions

File tree

app/api/routes/categories.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from flask import redirect, request
1+
from flask import redirect
22

33
from app import utils as utils
44
from app.api import bp
55
from app.api.routes.helpers import failures_counter, latency_summary, logger
66
from app.models import Category
7-
from configs import Config
87

98

109
@latency_summary.time()
@@ -23,23 +22,21 @@ def category(id):
2322

2423
def get_categories():
2524
try:
26-
category_paginator = utils.Paginator(Config.CATEGORY_PAGINATOR, request)
27-
query = Category.query
25+
categories = Category.query.all()
2826

29-
paginated_categories = category_paginator.paginated_data(query)
30-
if not paginated_categories:
27+
if not categories:
3128
return redirect('/404')
3229
category_list = [
33-
category.serialize for category in paginated_categories.items
30+
category.serialize for category in categories
3431
]
35-
pagination_details = category_paginator.pagination_details(paginated_categories)
32+
details = {'details': {'total_count': len(categories)}}
3633
except Exception as e:
3734
logger.exception(e)
3835
return utils.standardize_response(status_code=500)
3936

4037
return utils.standardize_response(payload=dict(
4138
data=category_list,
42-
**pagination_details),
39+
**details),
4340
datatype="categories")
4441

4542

app/api/routes/languages.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from flask import redirect, request
1+
from flask import redirect
22

33
from app import utils as utils
44
from app.api import bp
55
from app.api.routes.helpers import failures_counter, latency_summary, logger
66
from app.models import Language
7-
from configs import Config
87

98

109
@latency_summary.time()
@@ -22,24 +21,22 @@ def language(id):
2221

2322

2423
def get_languages():
25-
language_paginator = utils.Paginator(Config.LANGUAGE_PAGINATOR, request)
26-
query = Language.query
27-
2824
try:
29-
paginated_languages = language_paginator.paginated_data(query)
30-
if not paginated_languages:
25+
languages = Language.query.all()
26+
27+
if not languages:
3128
return redirect('/404')
3229
language_list = [
33-
language.serialize for language in paginated_languages.items
30+
language.serialize for language in languages
3431
]
35-
pagination_details = language_paginator.pagination_details(paginated_languages)
32+
details = {'details': {'total_count': len(languages)}}
3633
except Exception as e:
3734
logger.exception(e)
3835
return utils.standardize_response(status_code=500)
3936

4037
return utils.standardize_response(payload=dict(
4138
data=language_list,
42-
**pagination_details),
39+
**details),
4340
datatype="languages")
4441

4542

app/static/openapi.yaml

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ paths:
167167
/categories:
168168
get:
169169
summary: List Categories
170-
description: Gets a list of all available categories. Note: The `page` attribute in the response of the `/search` endpoint begins with `0`, but the `page` attribute in the response of the `List Categories` endpoint begins with `1`.
170+
description: Gets a list of all available categories.
171171
tags:
172172
- List Categories
173173
responses:
@@ -210,24 +210,25 @@ paths:
210210
name: 'Docker'
211211
- id: 15
212212
name: 'Regular Expressions'
213-
- id: 1
213+
- id: 16
214214
name: 'Mentorship'
215-
- id: 1
215+
- id: 17
216216
name: 'News'
217-
- id: 1
217+
- id: 18
218218
name: 'Back End Dev'
219-
- id: 1
219+
- id: 19
220220
name: 'Mobile Dev'
221-
- id: 1
221+
- id: 20
222222
name: 'Cheat Sheets'
223-
has_next: true
224-
has_prev: false
225-
number_of_pages: 4
226-
page: 1
227-
records_per_page: 20
223+
- id: 21
224+
name: 'Open Source'
225+
- id: 22
226+
name: 'Front End Dev'
227+
- id: 23
228+
name: 'Web Design'
228229
status: 'ok'
229230
status_code: 200
230-
total_count: 72
231+
total_count: 23
231232
404:
232233
$ref: '#/components/responses/NotFound'
233234
/categories/{id}:
@@ -265,7 +266,7 @@ paths:
265266
/languages:
266267
get:
267268
summary: List Languages
268-
description: Gets list of all available language entries. Note: The `page` attribute in the response of the `/search` endpoint begins with `0`, but the `page` attribute in the response of the `List Languages` endpoint begins with `1`.
269+
description: Gets list of all available language entries.
269270
tags:
270271
- List Languages
271272
responses:
@@ -318,14 +319,13 @@ paths:
318319
name: 'Android'
319320
- id: 20
320321
name: 'CSS'
321-
has_next: true
322-
has_prev: false
323-
number_of_pages: 4
324-
page: 1
325-
records_per_page: 20
322+
- id: 21
323+
name: 'jQuery'
324+
- id: 22
325+
name: 'Git'
326326
status: 'ok'
327327
status_code: 200
328-
total_count: 73
328+
total_count: 22
329329
404:
330330
$ref: '#/components/responses/NotFound'
331331
/languages/{id}:
@@ -932,21 +932,6 @@ components:
932932
type: string
933933
category:
934934
$ref: '#/components/schemas/Category_data'
935-
has_next:
936-
type: boolean
937-
description: Additional pages
938-
has_previous:
939-
type: boolean
940-
description: Previous pages
941-
number_of_pages:
942-
type: integer
943-
description: Total pages
944-
page:
945-
type: integer
946-
description: Current page
947-
records_per_page:
948-
type: integer
949-
description: Records per page
950935
status:
951936
type: string
952937
description: Status message
@@ -974,23 +959,8 @@ components:
974959
apiVersion:
975960
type: string
976961
description: API version
977-
language:
962+
languages:
978963
$ref: '#/components/schemas/Language_data'
979-
has_next:
980-
type: boolean
981-
description: Additional pages
982-
has_previous:
983-
type: boolean
984-
description: Previous pages
985-
number_of_pages:
986-
type: integer
987-
description: Total pages
988-
page:
989-
type: integer
990-
description: Current page
991-
records_per_page:
992-
type: integer
993-
description: Records per page
994964
status:
995965
type: string
996966
description: Status message

app/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def paginated_data(self, query):
5050

5151
def pagination_details(self, paginated_data):
5252
return {
53-
"pagination_details": {
53+
"details": {
5454
"page": paginated_data.page,
5555
"number_of_pages": paginated_data.pages,
5656
"records_per_page": paginated_data.per_page,
@@ -101,7 +101,7 @@ def standardize_response(
101101
"""
102102
data = payload.get("data")
103103
errors = payload.get("errors")
104-
pagination_details = payload.get("pagination_details")
104+
details = payload.get("details")
105105
resp = dict(
106106
apiVersion=version,
107107
status="ok",
@@ -129,8 +129,8 @@ def standardize_response(
129129
else:
130130
resp[datatype] = data
131131

132-
if pagination_details:
133-
resp.update(pagination_details)
132+
if details:
133+
resp.update(details)
134134

135135
return jsonify(resp), resp["status_code"], {'Content-Type': 'application/json'}
136136

resources.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,35 +1260,35 @@
12601260
- name: Linux Journey
12611261
url: https://linuxjourney.com/
12621262
category: Open Source
1263-
languages: ["Linux"]
1263+
languages: ["Unix/Linux/*nix"]
12641264
paid: false
12651265
notes:
12661266

12671267
- name: Linux from Scratch
12681268
url: http://linuxfromscratch.org/
12691269
category: Open Source
1270-
languages: ["Linux"]
1270+
languages: ["Unix/Linux/*nix"]
12711271
paid: false
12721272
notes:
12731273

12741274
- name: System Administration Guide from Red Hat
12751275
url: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Introduction_To_System_Administration/index.html
12761276
category: Open Source
1277-
languages: ["Linux"]
1277+
languages: ["Unix/Linux/*nix"]
12781278
paid: false
12791279
notes:
12801280

12811281
- name: Linux Tutorials
12821282
url: https://www.linux.com/tutorials
12831283
category: Open Source
1284-
languages: ["Linux"]
1284+
languages: ["Unix/Linux/*nix"]
12851285
paid: false
12861286
notes:
12871287

12881288
- name: Linux Survival
12891289
url: http://linuxsurvival.com/
12901290
category: Open Source
1291-
languages: ["Linux"]
1291+
languages: ["Unix/Linux/*nix"]
12921292
paid: false
12931293
notes:
12941294

tests/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@ def fake_key_query_error(mocker):
184184
mocker.patch('app.models.Key.query', new=None)
185185

186186

187+
@pytest.fixture(scope='function')
188+
def fake_category_query_error(mocker):
189+
"""
190+
Mocks an exception being raised during a query to test error handling
191+
"""
192+
193+
mocker.patch('app.models.Category.query', new=None)
194+
195+
196+
@pytest.fixture(scope='function')
197+
def fake_language_query_error(mocker):
198+
"""
199+
Mocks an exception being raised during a query to test error handling
200+
"""
201+
202+
mocker.patch('app.models.Language.query', new=None)
203+
204+
187205
@pytest.fixture(scope='function')
188206
def fake_algolia_save(mocker):
189207
"""

tests/unit/test_routes/test_categories.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@ def test_categories(module_client, module_db):
1010
assert (isinstance(category.get('id'), int))
1111
assert (isinstance(category.get('name'), str))
1212
assert (category.get('name'))
13-
assert (response.json['number_of_pages'] is not None)
14-
15-
16-
def test_categories_page_out_of_bounds(module_client, module_db):
17-
client = module_client
18-
too_far = 99999999
19-
response = client.get(
20-
f"api/v1/categories?page_size=100&page={too_far}", follow_redirects=True)
21-
22-
assert_correct_response(response, 404)
13+
assert (response.json['total_count'] is not None)
14+
assert (len(response.json['categories']) == response.json['total_count'])
2315

2416

2517
def test_get_single_category(module_client, module_db):

tests/unit/test_routes/test_languages.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,8 @@ def test_languages(module_client, module_db):
99
assert (isinstance(language.get('id'), int))
1010
assert (isinstance(language.get('name'), str))
1111
assert (language.get('name'))
12-
assert (response.json['number_of_pages'] is not None)
13-
14-
15-
def test_languages_page_out_of_bounds(module_client, module_db):
16-
client = module_client
17-
too_far = 99999999
18-
response = client.get(
19-
f"api/v1/languages?page_size=100&page={too_far}", follow_redirects=True)
20-
assert_correct_response(response, 404)
12+
assert (response.json['total_count'] is not None)
13+
assert (len(response.json['languages']) == response.json['total_count'])
2114

2215

2316
def test_get_single_language(module_client, module_db):

tests/unit/test_routes/test_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66

77
def test_internal_server_error_handler(
8-
module_client, module_db, fake_paginated_data_error):
8+
module_client,
9+
module_db,
10+
fake_language_query_error,
11+
fake_category_query_error,
12+
fake_paginated_data_error):
913
client = module_client
1014

1115
response = client.get('api/v1/resources')

0 commit comments

Comments
 (0)