From 7705ec94f909e180c1faf86554f996dcc77f30f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Sat, 27 May 2023 12:12:06 +0800 Subject: [PATCH] test --- -s | 1 + response_code | 8 ++++++++ response_data | 8 ++++++++ test.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 -s create mode 100644 response_code create mode 100644 response_data create mode 100755 test.sh diff --git a/-s b/-s new file mode 100644 index 0000000..fb34782 --- /dev/null +++ b/-s @@ -0,0 +1 @@ +400 - email already exists \ No newline at end of file diff --git a/response_code b/response_code new file mode 100644 index 0000000..685ea6e --- /dev/null +++ b/response_code @@ -0,0 +1,8 @@ +{ + "id": 1, + "name": "test", + "email": "", + "admin": true, + "created_at": "2023-05-27T10:10:49.311071275+08:00", + "updated_at": "2023-05-27T10:10:49.321101159+08:00" +} \ No newline at end of file diff --git a/response_data b/response_data new file mode 100644 index 0000000..e7d4369 --- /dev/null +++ b/response_data @@ -0,0 +1,8 @@ +{ + "id": 1, + "name": "test", + "email": "", + "admin": true, + "created_at": "2023-05-27T10:07:49.784669929+08:00", + "updated_at": "2023-05-27T10:07:49.792498193+08:00" +} \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..35bd194 --- /dev/null +++ b/test.sh @@ -0,0 +1,53 @@ +#!/bin/bash + + +# 流程測試, 啓動服務, 設定進程名 go_test +go run main.go -procname go_test & +sleep 2 + + +# 退出服務的函數 +function exit_service() { pkill -f go_test && rm -f data/sqlite3.db && echo "退出服務: $1" && exit 1; } + + +# 創建用戶 (POST /api/users) +response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","password":"test","email":""}' -s -w "%{http_code}" http://localhost:8080/api/users) +[[ ${response: -3} -eq 200 ]] && { echo "創建用戶成功: ${response%???}"; } || exit_service "創建用戶失敗: ${response%???}" + + +## 獲取用戶列表 (GET /api/users) +#response=$(curl -X GET -H "Content-Type: application/json" -s -w "%{http_code}" http://localhost:8080/api/users) +#[[ ${response: -3} -eq 200 ]] && { echo "獲取用戶列表成功: ${response%???}"; } || exit_service "獲取用戶列表失敗: ${response%???}" + + +# 登錄 (POST /api/sessions) +response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","password":"test"}' -s -w "%{http_code}" http://localhost:8080/api/sessions) +[[ ${response: -3} -eq 200 ]] && { echo "登錄成功: ${response%???}"; } || exit_service "登錄失敗: ${response%???}" + + +# 不使用jq, 從一段json中取出id字段的值 +session_id=$(echo "$response" | head -n -1 | grep -o '"id": "[^"]*' | cut -d '"' -f 4) +echo "session_id: $session_id" + + +# 創建數據集, 應當在cookie中攜帶session_id (POST /api/datasets) +response=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"test","description":"test"}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets) +[[ ${response: -3} -eq 200 ]] && { echo "創建數據集成功: ${response%???}"; } || exit_service "創建數據集失敗: ${response%???}" + + +# 取數據集id的值, 值爲 int +dataset_id=$(echo "${response%???}" | grep -o '"id": [0-9]*' | awk '{print $2}') +echo "dataset_id: $dataset_id" + + +## 獲取數據集列表 (GET /api/datasets) +#response=$(curl -X GET -H "Content-Type: application/json" -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets) +#[[ ${response: -3} -eq 200 ]] && { echo "獲取數據集列表成功: ${response%???}"; } || exit_service "獲取數據集列表失敗: ${response%???}" + + +# 修改數據集, images 中增加 url (PATCH /api/datasets/:id) +response=$(curl -X PATCH -H "Content-Type: application/json" -d '{"images":[{"url":"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"}]}' -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/datasets/$dataset_id) +[[ ${response: -3} -eq 200 ]] && { echo "修改數據集成功: ${response%???}"; } || exit_service "修改數據集失敗: ${response%???}" + + +exit_service "測試結束, 全部通過"