-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_api.py
More file actions
115 lines (78 loc) · 2.54 KB
/
Copy pathtest_api.py
File metadata and controls
115 lines (78 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import pytest
from website import create_app, db
from website.models import Note
@pytest.fixture
def app():
app = create_app()
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
with app.app_context():
db.create_all()
yield app
db.drop_all()
@pytest.fixture
def client(app):
return app.test_client()
# -------------------------
# CREATE
# -------------------------
def test_create_note(client):
response = client.post('/api/notes', json={
"data": "pytest note"
})
assert response.status_code == 201
data = response.get_json()
assert "id" in data
assert data["data"] == "pytest note"
# -------------------------
# READ ALL
# -------------------------
def test_get_all_notes(client):
client.post('/api/notes', json={"data": "note 1"})
client.post('/api/notes', json={"data": "note 2"})
response = client.get('/api/notes')
assert response.status_code == 200
data = response.get_json()
assert isinstance(data, list)
assert len(data) == 2
# -------------------------
# READ SINGLE
# -------------------------
def test_get_single_note(client):
create = client.post('/api/notes', json={"data": "single note"})
note_id = create.get_json()["id"]
response = client.get(f'/api/notes/{note_id}')
assert response.status_code == 200
data = response.get_json()
assert data["data"] == "single note"
def test_get_single_note_not_found(client):
response = client.get('/api/notes/999')
assert response.status_code == 404
# -------------------------
# UPDATE
# -------------------------
def test_update_note(client):
create = client.post('/api/notes', json={"data": "old note"})
note_id = create.get_json()["id"]
response = client.put(f'/api/notes/{note_id}', json={
"data": "updated note"
})
assert response.status_code == 200
data = response.get_json()
assert data["data"] == "updated note"
def test_update_note_not_found(client):
response = client.put('/api/notes/999', json={
"data": "nothing"
})
assert response.status_code == 404
# -------------------------
# DELETE
# -------------------------
def test_delete_note(client):
create = client.post('/api/notes', json={"data": "to delete"})
note_id = create.get_json()["id"]
response = client.delete(f'/api/notes/{note_id}')
assert response.status_code == 200
def test_delete_note_not_found(client):
response = client.delete('/api/notes/999')
assert response.status_code == 404