Skip to content

Commit 07822f1

Browse files
committed
Lägg till flera postman/newman-tester
1 parent d6e1bbd commit 07822f1

File tree

3 files changed

+182
-11
lines changed

3 files changed

+182
-11
lines changed

app/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ def delete_todo(todo_id: int):
151151

152152
return redirect(url_for("dashboard"))
153153

154+
@app.get("/todo/get")
155+
def todo_get():
156+
with get_db(app) as conn:
157+
rows = conn.execute(
158+
"SELECT id, title, description, status, created, edited FROM todo ORDER BY id DESC"
159+
).fetchall()
160+
161+
todos = [
162+
{
163+
"id": r["id"],
164+
"title": r["title"],
165+
"description": r["description"],
166+
"status": r["status"],
167+
"created": r["created"],
168+
"edited": r["edited"],
169+
}
170+
for r in rows
171+
]
172+
return {"todos": todos}
173+
154174
return app
155175

156176
# =====================================================

tests/postman/todo.collection.json

Lines changed: 159 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,97 @@
11
{
22
"info": {
3-
"name": "Todo API",
3+
"name": "Todo API (Newman)",
44
"_postman_id": "11111111-1111-1111-1111-111111111111",
55
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
66
},
77
"item": [
8+
89
{
9-
"name": "GET dashboard",
10+
"name": "Set unique title + dashboard ok",
1011
"request": {
1112
"method": "GET",
1213
"url": "{{baseUrl}}/dashboard"
1314
},
1415
"event": [
16+
{
17+
"listen": "prerequest",
18+
"script": {
19+
"exec": [
20+
"pm.environment.set('title', 'Newman Todo ' + Date.now());"
21+
]
22+
}
23+
},
1524
{
1625
"listen": "test",
1726
"script": {
1827
"exec": [
19-
"pm.test('status 200', function () {",
20-
" pm.response.to.have.status(200);",
28+
"pm.test('dashboard returns 200', () => pm.response.to.have.status(200));"
29+
]
30+
}
31+
}
32+
]
33+
},
34+
35+
{
36+
"name": "Create todo",
37+
"request": {
38+
"method": "POST",
39+
"body": {
40+
"mode": "urlencoded",
41+
"urlencoded": [
42+
{ "key": "title", "value": "{{title}}", "type": "text" },
43+
{ "key": "description", "value": "Created by newman", "type": "text" },
44+
{ "key": "status", "value": "not-started", "type": "text" }
45+
]
46+
},
47+
"url": "{{baseUrl}}/todo/create"
48+
},
49+
"event": [
50+
{
51+
"listen": "test",
52+
"script": {
53+
"exec": [
54+
"pm.test('title is set', () => {",
55+
" pm.expect(pm.environment.get('title')).to.be.ok;",
56+
"});",
57+
"pm.test('create returns 200 or 302', () => {",
58+
" pm.expect([200, 302]).to.include(pm.response.code);",
2159
"});"
2260
]
2361
}
2462
}
2563
]
2664
},
65+
2766
{
28-
"name": "POST create todo",
67+
"name": "Find created todo id (todo/get)",
68+
"request": {
69+
"method": "GET",
70+
"url": "{{baseUrl}}/todo/get"
71+
},
72+
"event": [
73+
{
74+
"listen": "test",
75+
"script": {
76+
"exec": [
77+
"pm.test('todo/get returns 200', () => pm.response.to.have.status(200));",
78+
"var body = pm.response.json();",
79+
"pm.expect(body).to.have.property('todos');",
80+
"var title = pm.environment.get('title');",
81+
"var found = body.todos.find(t => t.title === title);",
82+
"pm.expect(found, 'created todo should exist').to.be.ok;",
83+
"pm.environment.set('todo_id', String(found.id));",
84+
"pm.test('todo_id saved', () => {",
85+
" pm.expect(pm.environment.get('todo_id')).to.be.ok;",
86+
"});"
87+
]
88+
}
89+
}
90+
]
91+
},
92+
93+
{
94+
"name": "Update status to done",
2995
"request": {
3096
"method": "POST",
3197
"header": [
@@ -34,25 +100,108 @@
34100
"body": {
35101
"mode": "urlencoded",
36102
"urlencoded": [
37-
{ "key": "title", "value": "Newman todo", "type": "text" },
38-
{ "key": "description", "value": "Created by newman", "type": "text" },
39-
{ "key": "status", "value": "not-started", "type": "text" }
103+
{ "key": "status", "value": "done", "type": "text" }
40104
]
41105
},
42-
"url": "{{baseUrl}}/todo/create"
106+
"url": "{{baseUrl}}/todo/{{todo_id}}/status"
43107
},
44108
"event": [
45109
{
46110
"listen": "test",
47111
"script": {
48112
"exec": [
49-
"pm.test('status is 200 or redirect', function () {",
113+
"pm.test('todo_id exists', () => {",
114+
" pm.expect(pm.environment.get('todo_id')).to.be.ok;",
115+
"});",
116+
"",
117+
"pm.test('update returns 200 or 302', () => {",
50118
" pm.expect([200, 302]).to.include(pm.response.code);",
51119
"});"
52120
]
53121
}
54122
}
55123
]
124+
},
125+
126+
{
127+
"name": "Verify status is done (todo/get)",
128+
"request": {
129+
"method": "GET",
130+
"url": "{{baseUrl}}/todo/get"
131+
},
132+
"event": [
133+
{
134+
"listen": "test",
135+
"script": {
136+
"exec": [
137+
"pm.test('todo/get returns 200', () => pm.response.to.have.status(200));",
138+
"",
139+
"var body2 = pm.response.json();",
140+
"var id2 = pm.environment.get('todo_id');",
141+
"pm.expect(id2, 'todo_id should be set').to.be.ok;",
142+
"",
143+
"var found2 = body2.todos.find(t => String(t.id) === String(id2));",
144+
"pm.expect(found2, 'todo should exist').to.be.ok;",
145+
"",
146+
"pm.test('status is done', () => {",
147+
" pm.expect(found2.status).to.eql('done');",
148+
"});"
149+
]
150+
}
151+
}
152+
]
153+
},
154+
155+
{
156+
"name": "Delete todo",
157+
"request": {
158+
"method": "POST",
159+
"url": "{{baseUrl}}/todo/{{todo_id}}/delete"
160+
},
161+
"event": [
162+
{
163+
"listen": "test",
164+
"script": {
165+
"exec": [
166+
"pm.test('todo_id exists', () => {",
167+
" pm.expect(pm.environment.get('todo_id')).to.be.ok;",
168+
"});",
169+
"",
170+
"pm.test('delete returns 200 or 302', () => {",
171+
" pm.expect([200, 302]).to.include(pm.response.code);",
172+
"});"
173+
]
174+
}
175+
}
176+
]
177+
},
178+
179+
{
180+
"name": "Verify todo removed (todo/get)",
181+
"request": {
182+
"method": "GET",
183+
"url": "{{baseUrl}}/todo/get"
184+
},
185+
"event": [
186+
{
187+
"listen": "test",
188+
"script": {
189+
"exec": [
190+
"pm.test('todo/get returns 200', () => pm.response.to.have.status(200));",
191+
"",
192+
"var body3 = pm.response.json();",
193+
"var id3 = pm.environment.get('todo_id');",
194+
"pm.expect(id3, 'todo_id should be set').to.be.ok;",
195+
"",
196+
"var found3 = body3.todos.find(t => String(t.id) === String(id3));",
197+
"",
198+
"pm.test('todo is deleted', () => {",
199+
" pm.expect(found3).to.not.be.ok;",
200+
"});"
201+
]
202+
}
203+
}
204+
]
56205
}
57206
]
58207
}

tests/postman/todo.env.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"id": "22222222-2222-2222-2222-222222222222",
33
"name": "local",
44
"values": [
5-
{ "key": "baseUrl", "value": "http://127.0.0.1:5001", "enabled": true }
5+
{ "key": "baseUrl", "value": "http://127.0.0.1:5001", "enabled": true },
6+
{ "key": "title", "value": "", "enabled": true },
7+
{ "key": "todo_id", "value": "", "enabled": true }
68
]
79
}

0 commit comments

Comments
 (0)