Skip to content

Commit

Permalink
Add Mock dependency for redis calls
Browse files Browse the repository at this point in the history
  • Loading branch information
carlomorelli committed Jun 18, 2016
1 parent a054b8c commit 0ddbd69
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 54 deletions.
72 changes: 43 additions & 29 deletions acrewstic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@
"""
tasks = [
{
'id' : 1,
'title' : u'Buy groceries',
'description' : u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done' : False
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id' : 2,
'title' : u'Learn Python',
'description' : u'Need to find a good Python tutorial on the web',
'done' : False
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]

"""
Rest implementations
"""


@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
Expand All @@ -44,44 +45,52 @@ def not_found(error):
def get_tasks():
return jsonify({'tasks': tasks})


@app.route('/acrewstic/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
return jsonify({'task': task[0]})


@app.route('/acrewstic/tasks', methods=['POST'])
def create_task():
if not request.get_json() or not 'title' in request.get_json():
if not request.get_json() or 'title' not in request.get_json():
abort(400)
task = {
'id' : tasks[-1]['id'] + 1,
'title' : request.get_json()['title'],
'description' : request.get_json().get('description', ""),
'done' : False
'id': tasks[-1]['id'] + 1,
'title': request.get_json()['title'],
'description': request.get_json().get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task': task}), 201


@app.route('/acrewstic/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
if len(task) == 0:
abort(404)
if not request.get_json():
abort(400)
if 'title' in request.get_json() and type(request.get_json()['title']) != unicode:
abort(400)
if 'description' in request.get_json() and type(request.get_json()['description']) is not unicode:
abort(400)
if 'done' in request.get_json() and type(request.get_json()['done']) is not bool:
abort(400)
task[0]['title'] = request.get_json().get('title', task[0]['title'])
task[0]['description'] = request.get_json().get('description', task[0]['description'])
task[0]['done'] = request.get_json().get('done', task[0]['done'])
if 'title' in request.get_json():
if type(request.get_json()['title']) is not unicode:
abort(400)
if 'description' in request.get_json():
if type(request.get_json()['description']) is not unicode:
abort(400)
if 'done' in request.get_json():
if type(request.get_json()['done']) is not bool:
abort(400)
task[0]['title'] = request.get_json().get('title', task[0]['title'])
task[0]['description'] = request.get_json().get(
'description', task[0]['description'])
task[0]['done'] = request.get_json().get('done', task[0]['done'])
return jsonify({'task': task[0]})


@app.route('/acrewstic/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
task = [task for task in tasks if task['id'] == task_id]
Expand All @@ -98,17 +107,22 @@ def get_version():
'version': '0.1'
}
flask_info = {
'version': '0.11.1'
'version': '0.11.1'
}
redis_info = redis.info()
try:
redis_info = redis.info()
except:
redis_info = '<<<Unable to connect to database>>>'
return jsonify({
'app_info' : app_info,
'flask_info' : flask_info,
'redis_info' : redis_info
})
'app_info': app_info,
'flask_info': flask_info,
'redis_info': redis_info
})


"""
Main loop
"""

if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
app.run(host='0.0.0.0', debug=True)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mock
nose
flask
redis
15 changes: 10 additions & 5 deletions tests/test_data_map.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import os
import acrewstic
import tempfile
import redis
import mock
from nose import with_setup



class TestDataMap():
class TestDataMap:

def setup(self):
pass
# self.db_filedescriptor, acrewstic.app.config['DATABASE'] = tempfile.mkstemp()
# self.db_filedescriptor, acrewstic.app.config['DATABASE']
# = tempfile.mkstemp()
# acrewstic.app.config['TESTING'] = True
# self.app = acrewstic.app.test_client()
# with acrewstic.app.app_context():
Expand All @@ -20,7 +22,10 @@ def teardown(self):
# os.close(self.db_filedescriptor)
# os.unlink(acrewstic.app.config['DATABASE'])


@with_setup(setup, teardown)
def test(self):
assert True
assert True

@mock.patch.object(redis.StrictRedis, 'info')
def test_get_version(self, mock_info):
mock_info.assert_not_called()
54 changes: 34 additions & 20 deletions tests/test_rest_api.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
import json
import logging
import mock
import redis
import acrewstic


class TestRestApi:
class TestRestApi:

@classmethod
def setup_class(self):
print "Setting up application"
print "Setting up application..."
self.client = acrewstic.app.test_client()
self.client.testing = True
self.client.testing = True

@classmethod
def teardown_class(self):
print "Shutting down application"
pass
print "Shutting down application..."

@mock.patch.object(redis.StrictRedis, 'info')
def test_0_get_version(self, mock_info):
# result = self.client.get('/acrewstic/version')
# assert result.status_code == 200
self.client.get('/acrewstic/version')
mock_info.assert_called()

def test_1_get_tasks(self):
result = self.client.get('/acrewstic/tasks')
result = self.client.get('/acrewstic/tasks')
assert result.status_code == 200
data = json.loads(result.data)
data = json.loads(result.data)
print "Receving data: %s" % data
assert len(data['tasks']) == 2

def test_2_get_task(self):
task_id = 2
result = self.client.get('/acrewstic/tasks/%s' % task_id)
result = self.client.get('/acrewstic/tasks/%s' % task_id)
assert result.status_code == 200
data = json.loads(result.data)
print "Receving data: %s" % data
Expand All @@ -34,33 +41,40 @@ def test_2_get_task(self):

def test_3_post_task(self):
new_task = {
'title' : 'NewTitle',
'description' : 'NewDescription'
'title': 'NewTitle',
'description': 'NewDescription'
}
result = self.client.post('/acrewstic/tasks', data=json.dumps(new_task), content_type = 'application/json')
result = self.client.post(
'/acrewstic/tasks',
data=json.dumps(new_task),
content_type='application/json'
)
assert result.status_code == 201

def test_4_update_task(self):
update_task = {
'title' : 'NewTitle2',
'done' : True
'title': 'NewTitle2',
'done': True
}
task_id = 2
result = self.client.put('/acrewstic/tasks/%s' % task_id, data=json.dumps(update_task), content_type = 'application/json')
result = self.client.put(
'/acrewstic/tasks/%s' % task_id,
data=json.dumps(update_task),
content_type='application/json'
)
assert result.status_code == 200
result = self.client.get('/acrewstic/tasks/%s' % task_id)
assert result.status_code == 200
data = json.loads(result.data)
print "Receving data: %s" % data
assert data['task']['id'] == task_id
assert data['task']['done'] == True
assert data['task']['done']
assert data['task']['title'] == 'NewTitle2'

def test_5_delete_task(self):
task_id = 1
result = self.client.delete('/acrewstic/tasks/%s' % task_id)
result = self.client.delete('/acrewstic/tasks/%s' % task_id)
assert result.status_code == 200
data = json.loads(result.data)
print "Receving data: %s" % data
assert data['result'] == True

assert data['result']

0 comments on commit 0ddbd69

Please sign in to comment.