|
| 1 | +import pytest |
| 2 | +import json |
| 3 | + |
| 4 | +from app import app |
| 5 | + |
| 6 | +BASE_URL = "http://127.0.0.1:5000/api/v1.0/items" |
| 7 | +BAD_ITEM_URL = "{}/5".format(BASE_URL) |
| 8 | +GOOD_ITEM_URL = "{}/3".format(BASE_URL) |
| 9 | + |
| 10 | + |
| 11 | +def setUp(): |
| 12 | + backup_items = deepcopy(app.items) |
| 13 | + app = app.test_client() |
| 14 | + assert app.testing == False |
| 15 | + |
| 16 | + |
| 17 | +def test_get_items(): |
| 18 | + with app.test_client() as tc: |
| 19 | + # get items |
| 20 | + response = tc.get(BASE_URL) |
| 21 | + assert response.status_code == 200 |
| 22 | + data = json.loads(response.data) |
| 23 | + # items count in response |
| 24 | + assert len(data["items"]) == 3 |
| 25 | + |
| 26 | + |
| 27 | +def test_get_item(): |
| 28 | + with app.test_client() as tc: |
| 29 | + # get one non exist item |
| 30 | + response = tc.get(BAD_ITEM_URL) |
| 31 | + assert response.status_code == 404 |
| 32 | + # get one exist item |
| 33 | + response = tc.get(GOOD_ITEM_URL) |
| 34 | + data = json.loads(response.data) |
| 35 | + assert response.status_code == 200 |
| 36 | + assert len(data["items"]) == 1 |
| 37 | + |
| 38 | + |
| 39 | +def test_not_found(): |
| 40 | + with app.test_client() as tc: |
| 41 | + response = tc.get(BAD_ITEM_URL) |
| 42 | + assert response.status_code == 404 |
| 43 | + |
| 44 | + |
| 45 | +def test_bad_request(): |
| 46 | + item = {"name": "laptop", "some-value": "1000"} |
| 47 | + # item wrong request body |
| 48 | + with app.test_client() as tc: |
| 49 | + response = tc.post( |
| 50 | + BASE_URL, data=json.dumps(item), content_type="application/json" |
| 51 | + ) |
| 52 | + assert response.status_code == 400 |
| 53 | + |
| 54 | + |
| 55 | +def test_create_item(): |
| 56 | + with app.test_client() as tc: |
| 57 | + # wrong request body |
| 58 | + item = {"name": "laptop", "some-value": "1000"} |
| 59 | + response = tc.post( |
| 60 | + BASE_URL, data=json.dumps(item), content_type="application/json" |
| 61 | + ) |
| 62 | + assert response.status_code == 400 |
| 63 | + # item exist |
| 64 | + item = {"name": "laptop", "value": int(1)} |
| 65 | + response = tc.post( |
| 66 | + BASE_URL, data=json.dumps(item), content_type="application/json" |
| 67 | + ) |
| 68 | + assert response.status_code == 400 |
| 69 | + # item wrong request value type |
| 70 | + item = {"name": "monitor", "value": str(1)} |
| 71 | + response = tc.post( |
| 72 | + BASE_URL, data=json.dumps(item), content_type="application/json" |
| 73 | + ) |
| 74 | + assert response.status_code == 400 |
| 75 | + # create item |
| 76 | + item = {"name": "printer", "value": 100} |
| 77 | + reponse = tc.post( |
| 78 | + BASE_URL, data=json.dumps(item), content_type="application/json" |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def test_update_item(): |
| 83 | + item = {"name": "laptop", "some-value": "1000"} |
| 84 | + # item wrong request body |
| 85 | + with app.test_client() as tc: |
| 86 | + response = tc.post( |
| 87 | + BASE_URL, data=json.dumps(item), content_type="application/json" |
| 88 | + ) |
| 89 | + assert response.status_code == 400 |
| 90 | + |
| 91 | + |
| 92 | +def test_update_item(): |
| 93 | + with app.test_client() as tc: |
| 94 | + # update non exist item |
| 95 | + response = tc.put(BAD_ITEM_URL) |
| 96 | + assert response.status_code == 404 |
| 97 | + # empty request body |
| 98 | + response = tc.put( |
| 99 | + GOOD_ITEM_URL, data=json.dumps({}), content_type="application/json" |
| 100 | + ) |
| 101 | + assert response.status_code == 400 |
| 102 | + # request body contains str instead of int value |
| 103 | + response = tc.put( |
| 104 | + GOOD_ITEM_URL, |
| 105 | + data=json.dumps({"name": "book", "value": str(10)}), |
| 106 | + content_type="application/json", |
| 107 | + ) |
| 108 | + assert response.status_code == 400 |
| 109 | + # correct request |
| 110 | + response = tc.put( |
| 111 | + GOOD_ITEM_URL, |
| 112 | + data=json.dumps({"name": "book", "value": int(10)}), |
| 113 | + content_type="application/json", |
| 114 | + ) |
| 115 | + assert response.status_code == 200 |
| 116 | + |
| 117 | + |
| 118 | +def test_delete_item(): |
| 119 | + with app.test_client() as tc: |
| 120 | + # delete non exist item |
| 121 | + response = tc.delete(BAD_ITEM_URL) |
| 122 | + assert response.status_code == 404 |
| 123 | + # delete exist item |
| 124 | + response = tc.delete(GOOD_ITEM_URL) |
| 125 | + assert response.status_code == 204 |
| 126 | + |
| 127 | + |
| 128 | +def setDown(): |
| 129 | + # reset state |
| 130 | + app.items = backup_items |
0 commit comments