|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.test import Client |
| 5 | + |
| 6 | +from nerd_herder.faq.models import Entry |
| 7 | + |
| 8 | +BASE_URL = "/api/v1/faq/" |
| 9 | +REQUIRED_ERROR = ["This field is required."] |
| 10 | +NULL_ERROR = ["This field may not be null."] |
| 11 | +FIELDS = ["question", "answer", "position"] |
| 12 | +ENTRIES = [ |
| 13 | + { |
| 14 | + "question": "What version of Python should I use?", |
| 15 | + "answer": "Python 3 or above", |
| 16 | + "position": 0, |
| 17 | + }, |
| 18 | + { |
| 19 | + "question": "Flask or Django?", |
| 20 | + "answer": "Whatever floats your boat", |
| 21 | + "position": 1, |
| 22 | + }, |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.django_db |
| 27 | +def test_add_entry(client: Client): |
| 28 | + entry = ENTRIES[0] |
| 29 | + payload = json.dumps(entry) |
| 30 | + r = client.post(BASE_URL, payload, content_type="application/json") |
| 31 | + |
| 32 | + assert 201 == r.status_code |
| 33 | + body = r.json() |
| 34 | + assert entry["question"] == body["question"] |
| 35 | + assert entry["answer"] == body["answer"] |
| 36 | + assert entry["position"] == body["position"] |
| 37 | + assert 1 == len(Entry.objects.all()) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.django_db |
| 41 | +@pytest.mark.parametrize( |
| 42 | + "payload,expected", |
| 43 | + [ |
| 44 | + ({}, {field: REQUIRED_ERROR for field in FIELDS}), |
| 45 | + ({field: None for field in FIELDS}, {field: NULL_ERROR for field in FIELDS}), |
| 46 | + ({**ENTRIES[0], "question": False}, {"question": ["Not a valid string."]}), |
| 47 | + ({**ENTRIES[0], "answer": False}, {"answer": ["Not a valid string."]}), |
| 48 | + ( |
| 49 | + {**ENTRIES[0], "position": False}, |
| 50 | + {"position": ["A valid integer is required."]}, |
| 51 | + ), |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_add_entry_bad(client: Client, payload, expected): |
| 55 | + req = client.post(BASE_URL, json.dumps(payload), "application/json") |
| 56 | + |
| 57 | + assert 400 == req.status_code |
| 58 | + assert expected == req.json() |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.django_db |
| 62 | +def test_update_entry(client: Client): |
| 63 | + entry = Entry.objects.create(**ENTRIES[0]) |
| 64 | + question = "I am a new title" |
| 65 | + url = f"{BASE_URL}{entry.id}" |
| 66 | + r = client.put( |
| 67 | + url, |
| 68 | + json.dumps({**ENTRIES[0], "question": question}), |
| 69 | + content_type="application/json", |
| 70 | + ) |
| 71 | + assert 200 == r.status_code |
| 72 | + assert {**ENTRIES[0], "id": entry.id, "question": question} == r.json() |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.django_db |
| 76 | +@pytest.mark.parametrize("field", FIELDS) |
| 77 | +def test_update_entry_bad(client: Client, field): |
| 78 | + entry = Entry.objects.create(**ENTRIES[0]) |
| 79 | + url = f"{BASE_URL}{entry.id}" |
| 80 | + |
| 81 | + r = client.put( |
| 82 | + url, json.dumps({**ENTRIES[0], field: None}), content_type="application/json" |
| 83 | + ) |
| 84 | + |
| 85 | + assert 400 == r.status_code |
| 86 | + |
| 87 | + copy = {**ENTRIES[0]} |
| 88 | + copy.pop(field) |
| 89 | + r = client.put(url, json.dumps(copy), content_type="application/json") |
| 90 | + |
| 91 | + assert 400 == r.status_code |
| 92 | + |
| 93 | + r = client.put( |
| 94 | + url, json.dumps({**ENTRIES[0], field: False}), content_type="application/json" |
| 95 | + ) |
| 96 | + |
| 97 | + assert 400 == r.status_code |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.django_db |
| 101 | +def test_delete_entry(client: Client): |
| 102 | + entry = Entry.objects.create(**ENTRIES[0]) |
| 103 | + url = f"{BASE_URL}{entry.id}" |
| 104 | + r = client.delete(url) |
| 105 | + |
| 106 | + assert 204 == r.status_code |
| 107 | + |
| 108 | + r = client.get(url) |
| 109 | + |
| 110 | + assert 404 == r.status_code |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.django_db |
| 114 | +def test_get_entries(client: Client): |
| 115 | + for entry in ENTRIES: |
| 116 | + Entry.objects.create(**entry) |
| 117 | + |
| 118 | + r = client.get(BASE_URL, content_type="application/json") |
| 119 | + |
| 120 | + assert r.status_code == 200 |
| 121 | + body = r.json() |
| 122 | + assert 2 == len(body) |
| 123 | + |
| 124 | + for idx, entry in enumerate(body): |
| 125 | + assert ENTRIES[idx]["question"] == entry["question"] |
| 126 | + assert ENTRIES[idx]["answer"] == entry["answer"] |
| 127 | + assert ENTRIES[idx]["position"] == entry["position"] |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.django_db |
| 131 | +def test_change_entry_order(client: Client): |
| 132 | + entries = [] |
| 133 | + |
| 134 | + for entry in ENTRIES: |
| 135 | + entries.append(Entry.objects.create(**entry)) |
| 136 | + |
| 137 | + payload = { |
| 138 | + 'order': { |
| 139 | + str(entries[0].id): 1, |
| 140 | + str(entries[1].id): 0, |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + r = client.put(BASE_URL, json.dumps(payload), content_type='application/json') |
| 145 | + |
| 146 | + assert 200 == r.status_code |
| 147 | + assert payload == r.json() |
| 148 | + |
| 149 | + r = client.get(BASE_URL) |
| 150 | + |
| 151 | + assert 200 == r.status_code |
| 152 | + body = r.json() |
| 153 | + assert entries[0].id == body[1]['id'] |
| 154 | + assert entries[1].id == body[0]['id'] |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.django_db |
| 158 | +def test_change_entry_order_bad(client: Client): |
| 159 | + entries = [] |
| 160 | + |
| 161 | + for entry in ENTRIES: |
| 162 | + entries.append(Entry.objects.create(**entry)) |
| 163 | + |
| 164 | + payload = { |
| 165 | + 'order': { |
| 166 | + str(entries[0].id): 1, |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + r = client.put(BASE_URL, json.dumps(payload), content_type='application/json') |
| 171 | + |
| 172 | + assert 400 == r.status_code |
| 173 | + assert {'order': [f'Missing position value for entry with id "{entries[1].id}"']} == r.json() |
| 174 | + |
0 commit comments