Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Feat filter for /all
Browse files Browse the repository at this point in the history
  • Loading branch information
tyoc213 committed Apr 8, 2021
1 parent d927557 commit b41ffc9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
18 changes: 14 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import flask
from flask import request, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from ghdb import db, get_ghu_page
from ghdb import db, get_ghu_page, get_ghu_total
from flask_caching import Cache
from flask_paginate import Pagination, get_page_args
import os
Expand Down Expand Up @@ -30,7 +30,7 @@ def favicon():
@cache.cached(timeout=10, query_string=True)
def main():
'''Displays a specific `page` with `total` users'''
total=5000 # TODO: get from db
total=get_ghu_total()
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='total')
pagination = Pagination(page=page, per_page=per_page, total=total)
Expand All @@ -44,11 +44,21 @@ def main():
pagination=pagination))

@app.route('/all', methods=['POST', 'GET'])
@cache.memoize(10)
@cache.cached(timeout=10, query_string=True)
def all_users():
'''JSON response with all users.'''
data = get_ghu_page()
total = get_ghu_total()
username = request.args.get('username')
order_by = request.args.get('order_by')
if request.args.get('page') is None and request.args.get('total') is None:
data = get_ghu_page(username=username)
else:
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='total')
pagination = Pagination(page=page, per_page=per_page, total=total)
data = get_ghu_page(page, per_page, username=username, order_by=order_by)
user_list = data[0]

# total_pages = data[1]
user_list = [o.serialize() for o in user_list]
return flask.json.jsonify(user_list)
Expand Down
22 changes: 17 additions & 5 deletions ghdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __repr__(self):
return '<User %r>' % self.user

def update_or_insert(user_dict):
'''Fetch github users and upsert them on DB'''
'''Fetch github users and upsert them on DB.'''
# get all incoming ids and ids on database
all_incoming_ids = set(user_dict.keys())
all_ids = set(dict(db.session.query(GithubUser.id, GithubUser)))
Expand All @@ -46,14 +46,26 @@ def update_or_insert(user_dict):
def create_all():
SQLAlchemy.create_all(db)

def get_ghu_page(page=0, total=None):
'''Get a specific page with `total` users'''
def get_ghu_page(page=0, total=None, username=None,order_by=None):
'''Get a specific page with `total` users.'''
# TODO: GithubUser.id.asc() throws error
if total is None:
if username:
print('username', username)
postlist = GithubUser.query.filter_by(user=username)
elif total is None:
postlist = GithubUser.query.filter(GithubUser.id).offset(page).all()
else:
# calculate an offset to the page, but at start of page
page_offset = page * total
postlist = GithubUser.query.filter(GithubUser.id).offset(page_offset).limit(total).all()
if order_by is not None:
if order_by == 'id': order_by = GithubUser.id
elif order_by == 'user': order_by = GithubUser.user
elif order_by == 'profile': order_by = GithubUser.profile
elif order_by == 'type': order_by = GithubUser.typ3
postlist = GithubUser.query.filter(GithubUser.id).order_by(order_by).offset(page_offset).limit(total).all()
count = db.session.query(GithubUser).count()
return postlist, count

def get_ghu_total():
'''Get the `total` users.'''
return db.session.query(GithubUser).count()
41 changes: 36 additions & 5 deletions test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@


def test_favicon():
'''Teset favicon is there.'''
with app.test_client() as test_client:
response = test_client.get('/favicon.ico')
assert response.status_code == 200

def test_home_page():
'''Check the title of the index'''
# with app.app_context():
# # Create a test client using the Flask application configured for testing
'''Check the title of the index.'''
with app.test_client() as test_client:
response = test_client.get('/')
assert response.status_code == 200
assert b'Github user list' in response.data

def test_pagination():
'''Test different pagination combinations'''
with app.test_client() as test_client:
response = test_client.get('/?total=1&page=0')
assert response.status_code == 200
Expand All @@ -31,13 +31,44 @@ def test_pagination():
assert b'defunkt' in response.data


def test_json_list():
def test_all_json_list():
'''Test that it returns all users in database.'''
with app.test_client() as test_client:
response = test_client.get('/all')
response = test_client.post('/all')
assert response.status_code == 200
assert response.content_type == 'application/json'
assert len(response.json) == len(dict(db.session.query(GithubUser.id, GithubUser)))

def test_json_filter():
'''If `username` is set, it should only return 1 element.'''
with app.test_client() as test_client:
response = test_client.post('/all?username=defunkt&pagination=20&order_by=id')
assert response.status_code == 200
assert response.content_type == 'application/json'
assert len(response.json) == 1

def test_pager_json_list():
'''Paging should work'''
with app.test_client() as test_client:
response1 = test_client.post('/all?total=20&page=0&order_by=user')
assert response1.status_code == 200
assert response1.content_type == 'application/json'
assert len(response1.json) == 20

response2 = test_client.post('/all?total=20&page=100&order_by=profile')
assert response2.status_code == 200
assert response2.content_type == 'application/json'
assert len(response2.json) == 20
assert response1.json[0] != response2.json[0]
assert response1.json[-1] != response2.json[-1]

response3 = test_client.post('/all?total=20&page=100&order_by=type')
assert response3.status_code == 200
assert response3.content_type == 'application/json'
assert len(response3.json) == 20
assert response1.json[0] != response3.json[0]
assert response1.json[-1] != response3.json[-1]

def test_datatables():
with app.test_client() as test_client:
response = test_client.get('/dt')
Expand Down

0 comments on commit b41ffc9

Please sign in to comment.