Pytest Day 3 of 3
Day 12 of 100
The challenges I experienced continued today. Trying to figure out Pytest is a challenge with all of these new API's that I haven't worked with before but after reading through some documentation, looking through some code examples, and oh so many blog posts, I was able to write some tests for the final three functions that I did not yesterday - creating an entry, updating an entry, and deleting an entry. I know there is still a lot of room for improvement in this code but I am very pleased with how much I was able to get done. Here is the sample Flask code that was given:
from flask import Flask, jsonify, abort, make_response, request
NOT_FOUND = 'Not found'
BAD_REQUEST = 'Bad request'
app = Flask(__name__)
items = [
{
'id': 1,
'name': 'laptop',
'value': 1000
},
{
'id': 2,
'name': 'chair',
'value': 300,
},
{
'id': 3,
'name': 'book',
'value': 20,
},
]
def _get_item(id):
return [item for item in items if item['id'] == id]
def _record_exists(name):
return [item for item in items if item["name"] == name]
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': NOT_FOUND}), 404)
@app.errorhandler(400)
def bad_request(error):
return make_response(jsonify({'error': BAD_REQUEST}), 400)
@app.route('/api/v1.0/items', methods=['GET'])
def get_items():
return jsonify({'items': items})
@app.route('/api/v1.0/items/<int:id>', methods=['GET'])
def get_item(id):
item = _get_item(id)
if not item:
abort(404)
return jsonify({'items': item})
@app.route('/api/v1.0/items', methods=['POST'])
def create_item():
if not request.json or 'name' not in request.json or 'value' not in request.json:
abort(400)
item_id = items[-1].get("id") + 1
name = request.json.get('name')
if _record_exists(name):
abort(400)
value = request.json.get('value')
if type(value) is not int:
abort(400)
item = {"id": item_id, "name": name,
"value": value}
items.append(item)
return jsonify({'item': item}), 201
@app.route('/api/v1.0/items/<int:id>', methods=['PUT'])
def update_item(id):
item = _get_item(id)
if len(item) == 0:
abort(404)
if not request.json:
abort(400)
name = request.json.get('name', item[0]['name'])
value = request.json.get('value', item[0]['value'])
if type(value) is not int:
abort(400)
item[0]['name'] = name
item[0]['value'] = value
return jsonify({'item': item[0]}), 200
@app.route('/api/v1.0/items/<int:id>', methods=['DELETE'])
def delete_item(id):
item = _get_item(id)
if len(item) == 0:
abort(404)
items.remove(item[0])
return jsonify({}), 204
if __name__ == '__main__':
app.run(debug=True)
Here are the tests I wrote (with a lot of help/inspiration):
from app import app, get_items, _record_exists, _get_item, create_item
import pytest
import requests
from flask import json
url = 'http://127.0.0.1:5000'
@pytest.fixture
def client():
client = app.test_client()
return client
def test_get_item():
assert _get_item(1) == [{'id': 1, 'name': 'laptop', 'value': 1000}]
assert _get_item(2) == [{'id': 2, 'name': 'chair', 'value': 300}]
assert _get_item(3) == [{'id': 3, 'name': 'book', 'value': 20}]
assert _get_item(4) == []
assert _get_item('a') == []
def test_record_exists():
assert _record_exists('laptop') == [{'id': 1, 'name': 'laptop', 'value': 1000}]
assert _record_exists('chair') == [{'id': 2, 'name': 'chair', 'value': 300}]
assert _record_exists('book') == [{'id': 3, 'name': 'book', 'value': 20}]
assert _record_exists(4) == []
assert _record_exists('a') == []
@pytest.fixture
def client():
client = app.test_client()
return client
def test_get_items():
res = requests.get(url + '/api/v1.0/items')
assert res.status_code == 200
data = res.json()
assert data['items'][0]['id'] == 1
assert data['items'][1]['id'] == 2
assert data['items'][2]['id'] == 3
def test_get_item():
# Verify a successful connection
res = requests.get(url + '/api/v1.0/items/1')
assert res.status_code == 200
# Verify correct data
data = res.json()
assert data['items'][0]['id'] == 1
# Check for a item outside the list
res = requests.get(url + '/api/v1.0/items/42')
assert res.status_code == 404
def test_create_item(client):
# check for 400 return if id is missing
res = requests.post(url + '/api/v1.0/items', data=json.dumps({'name': 'lamp', 'value': 5000}))
assert res.status_code == 400
# check for 400 return if value is missing
res = requests.post(url + '/api/v1.0/items', data=json.dumps({'id': '4', 'value': 5000}))
assert res.status_code == 400
# return 400 if data is not json
input = {'name': 'lamp', 'value': 250}
res = client.post(url + '/api/v1.0/items', data=input, content_type='application/json')
assert res.status_code == 400
# check for 400 if name already exists
res = requests.post(url + '/api/v1.0/items', data=json.dumps({'id': '4', 'name ': 'laptop', 'value': 5000}))
assert res.status_code == 400
# check for 400 if value is not an int
res = requests.post(url + '/api/v1.0/items', data=json.dumps({'id': '4', 'name ': 'laptop', 'value': 'Forty-two'}))
assert res.status_code == 400
# check for the correct addition of an item
input = {'name': 'lamp', 'value': 250}
res = client.post(url + '/api/v1.0/items', data=json.dumps(input), content_type='application/json')
assert res.status_code == 201
data = json.loads(res.get_data())
assert data['item']['name'] == 'lamp'
def test_update_item(client):
# return 404 if id is not given
input = {'id': '', 'name': 'lamp', 'value': 5000}
res = client.put(url + '/api/v1.0/items/', data=json.dumps(input), content_type='application/json')
assert res.status_code == 404
# return 400 if data is not json
input = {'id': '', 'name': 'lamp', 'value': 5000}
res = client.put(url + '/api/v1.0/items/', data=input, content_type='application/json')
assert res.status_code == 404
# check for 400 if value is not an int
res = client.put(url + '/api/v1.0/items/1', data=json.dumps({'id': '1', 'name ': 'laptop', 'value': 'Forty-two'}))
assert res.status_code == 400
# check for the correct update of an item
input = {'id': '1', 'name': 'lamp', 'value': 250}
res = client.put(url + '/api/v1.0/items/1', data=json.dumps(input), content_type='application/json')
assert res.status_code == 200
data = json.loads(res.get_data())
assert data['item']['name'] == 'lamp'
def test_delete_item(client):
# return 404 if id is not given
input = {'id': '', 'name': 'lamp', 'value': 5000}
res = client.delete(url + '/api/v1.0/items/', data=json.dumps(input), content_type='application/json')
assert res.status_code == 404
# check for the correct deletion of an item
res = client.delete(url + '/api/v1.0/items/1')
assert res.status_code == 204
res = client.get(url + '/api/v1.0/items/1')
assert res.status_code == 404