From de395789b588195956ab14915bf74820b6f8b474 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Tue, 14 May 2019 04:56:03 +0800 Subject: [PATCH] new: dev: test api and invalid id --- tests/test_server.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_server.py b/tests/test_server.py index 1bab16c..c040bd1 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -43,3 +43,33 @@ def test_api_empty_db(client, url, exp_res): rd = client.get(url) assert rd.status_code == 200 assert rd.get_json() == exp_res + + +@pytest.mark.parametrize( + 'url, exp_res, status_code', [ + ['/api/tags/1', {'message': 'This resource does not exist.'}, 404], + ['/api/bookmarks/1', {'message': 'failure', 'status': 1}, 400], + ] +) +def test_invalid_id(client, url, exp_res, status_code): + rd = client.get(url) + assert rd.status_code == status_code + assert rd.get_json() == exp_res + + +def test_api(client): + url = 'http://google.com' + rd = client.post('/api/bookmarks', data={'url': url}) + assert rd.status_code == 200 + assert rd.get_json() == {'message': 'success', 'status': 0} + rd = client.post('/api/bookmarks', data={'url': url}) + assert rd.status_code == 400 + assert rd.get_json() == {'message': 'failure', 'status': 1} + rd = client.get('/api/bookmarks/search', query_string={'keywords': 'google.com'}) + assert rd.status_code == 200 + assert rd.get_json() == {'bookmarks': [ + {'description': '', 'id': 1, 'tags': [], 'title': '', 'url': url}]} + rd = client.get('/api/bookmarks/1') + assert rd.status_code == 200 + assert rd.get_json() == { + 'description': '', 'tags': [], 'title': '', 'url': 'http://google.com'}