Skip to content

Commit

Permalink
Add test_gifts.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam-Deacon committed Oct 8, 2020
1 parent 28b4cb7 commit 0a3282c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
8 changes: 4 additions & 4 deletions online_store/backend/gift_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ class SqlDatabaseGiftList(AbstractGiftList):
"""A gift list implementation using SQL ORM models."""
def __init__(self, username_or_id):
self.user = self.get_user(username_or_id)
self.gift_list = (
GiftListModel.query.filter_by(user_id=self._get_user_id()).first()
or self.create_giftlist()
)
self.gift_list = \
GiftListModel.query \
.filter_by(user_id=self._get_user_id()) \
.first() or self.create_list()

def _get_user_id(self) -> int:
"""Helper method to return a user id."""
Expand Down
18 changes: 17 additions & 1 deletion test/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@ VALUES (
'test',
'$pbkdf2-sha256$29000$GiPk/H/P.R9jDOHcO6f0vg$d1hc5fqAKcY92Xpd9BccjpMzHSEVWKk4pIc4PN0HLCQ',
'[email protected]'
);
);
VALUES (
'testuser',
'$pbkdf2-sha256$29000$GiPk/H/P.R9jDOHcO6f0vg$d1hc5fqAKcY92Xpd9BccjpMzHSEVWKk4pIc4PN0HLCQ',
'[email protected]'
);

/* add a dummy gift list */
INSERT INTO gift_lists (id, user_id)
VALUES (1, 2);
INSERT INTO gift_lists (id, user_id)
VALUES (2, 1);

INSERT INTO gifts (id, item_id, list_id, available, purchased)
VALUES (1, 1, 1, 1, 0);
INSERT INTO gifts (id, item_id, list_id, available, purchased)
VALUES (2, 2, 1, 2, 0);
58 changes: 58 additions & 0 deletions test/test_gifts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
import json


def test_list_get(client, test_auth_headers):
response = client.get('/api/v1/gifts/list',
headers=test_auth_headers)
assert response.status_code == 204
try:
data = json.loads(response.get_data().decode())
assert data
assert isinstance(data, list)
except json.decoder.JSONDecodeError:
pass


@pytest.mark.skip('TODO')
def test_list_gift_id_purchase_post(client, test_auth_headers):
pass


@pytest.mark.skip('TODO')
def test_list_gift_id_delete(client, test_auth_headers):
pass


@pytest.mark.skip('TODO')
def test_list_gift_id_get(client, test_auth_headers):
pass


@pytest.mark.skip('TODO')
def test_list_add_post(client, test_auth_headers):
pass


def test_list_report_get(client, test_auth_headers):
response = client.get('/api/v1/gifts/list/report',
headers=test_auth_headers)
assert response.status_code == 200
data = json.loads(response.get_data().decode())
assert data
assert 'available' in data.keys()
assert 'purchased' in data.keys()
assert isinstance(data['available'], list)
assert isinstance(data['purchased'], list)


@pytest.mark.parametrize(
('endpoint', 'method', 'data', 'has_auth', 'code'),
[('/api/v1/gifts/list', 'GET', None, True, 204)]
)
def test_protected_endpoint(client, test_auth_headers,
endpoint, method, data, has_auth, code):
request = getattr(client, method.lower())
response = request(endpoint, headers=test_auth_headers if has_auth else {},
data=data, mimetype='application/json')
assert response.status_code == code

0 comments on commit 0a3282c

Please sign in to comment.