Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Jun 9, 2017
1 parent e51f1cc commit 0465fd4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 104 deletions.
4 changes: 2 additions & 2 deletions project/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class User(db.Model):
active = db.Column(db.Boolean(), default=False, nullable=False)
created_at = db.Column(db.DateTime, nullable=False)

def __init__(self, username, email):
def __init__(self, username, email, created_at=datetime.datetime.now()):
self.username = username
self.email = email
self.created_at = datetime.datetime.now()
self.created_at = created_at
14 changes: 2 additions & 12 deletions project/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ def get_single_user(user_id):
except ValueError:
return make_response(jsonify(response_object)), 404


@users_blueprint.route('/users', methods=['GET'])
def get_all_users():
"""Get all users"""
users = User.query.all()
users = User.query.order_by(User.created_at.desc()).all()
users_list = []
for user in users:
user_object = {
Expand All @@ -99,14 +100,3 @@ def get_all_users():
}
}
return make_response(jsonify(response_object)), 200


@users_blueprint.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
db.session.add(User(username=username, email=email))
db.session.commit()
users = User.query.order_by(User.created_at.desc()).all()
return render_template('index.html', users=users)
50 changes: 0 additions & 50 deletions project/templates/index.html

This file was deleted.

49 changes: 9 additions & 40 deletions project/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@


import json
import datetime

from project import db
from project.api.models import User
from project.tests.base import BaseTestCase


def add_user(username, email):
user = User(username=username, email=email)
def add_user(username, email, created_at=datetime.datetime.now()):
user = User(username=username, email=email, created_at=created_at)
db.session.add(user)
db.session.commit()
return user
Expand Down Expand Up @@ -127,7 +128,8 @@ def test_single_user_incorrect_id(self):

def test_all_users(self):
"""Ensure get all users behaves correctly."""
add_user('michael', 'michael@realpython.com')
created = datetime.datetime.now() + datetime.timedelta(-30)
add_user('michael', 'michael@realpython.com', created)
add_user('fletcher', 'fletcher@realpython.com')
with self.client:
response = self.client.get('/users')
Expand All @@ -136,43 +138,10 @@ def test_all_users(self):
self.assertEqual(len(data['data']['users']), 2)
self.assertTrue('created_at' in data['data']['users'][0])
self.assertTrue('created_at' in data['data']['users'][1])
self.assertIn('michael', data['data']['users'][0]['username'])
self.assertIn('michael', data['data']['users'][1]['username'])
self.assertIn(
'michael@realpython.com', data['data']['users'][0]['email'])
self.assertIn('fletcher', data['data']['users'][1]['username'])
'michael@realpython.com', data['data']['users'][1]['email'])
self.assertIn('fletcher', data['data']['users'][0]['username'])
self.assertIn(
'fletcher@realpython.com', data['data']['users'][1]['email'])
'fletcher@realpython.com', data['data']['users'][0]['email'])
self.assertIn('success', data['status'])

def test_main_no_users(self):
"""Ensure the main route behaves correctly when no users have been
added to the database."""
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
self.assertIn(b'<h1>All Users</h1>', response.data)
self.assertIn(b'<p>No users!</p>', response.data)

def test_main_with_users(self):
"""Ensure the main route behaves correctly when users have been
added to the database."""
add_user('michael', 'michael@realpython.com')
add_user('fletcher', 'fletcher@realpython.com')
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
self.assertIn(b'<h1>All Users</h1>', response.data)
self.assertNotIn(b'<p>No users!</p>', response.data)
self.assertIn(b'<strong>michael</strong>', response.data)
self.assertIn(b'<strong>fletcher</strong>', response.data)

def test_main_add_user(self):
"""Ensure a new user can be added to the database."""
with self.client:
response = self.client.post(
'/',
data=dict(username='michael', email='michael@realpython.com'),
follow_redirects=True
)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<h1>All Users</h1>', response.data)
self.assertNotIn(b'<p>No users!</p>', response.data)
self.assertIn(b'<strong>michael</strong>', response.data)

0 comments on commit 0465fd4

Please sign in to comment.