Feat: add more tests

This commit is contained in:
2025-12-07 23:59:24 +08:00
parent 1f313fc17d
commit f191aef810
17 changed files with 307 additions and 25 deletions

View File

@@ -86,7 +86,10 @@ const docTemplate = `{
],
"responses": {
"200": {
"description": "OK"
"description": "OK",
"schema": {
"$ref": "#/definitions/api.postImageOutput"
}
},
"401": {
"description": "Unauthorized"
@@ -264,6 +267,9 @@ const docTemplate = `{
"type": "integer"
}
},
"extension": {
"type": "string"
},
"id": {
"type": "integer"
},
@@ -275,6 +281,23 @@ const docTemplate = `{
}
}
},
"api.postImageOutput": {
"type": "object",
"properties": {
"extension": {
"type": "string"
},
"id": {
"type": "integer"
},
"uploadedAt": {
"type": "string"
},
"uploadedUserId": {
"type": "string"
}
}
},
"api.putImageAliasesInput": {
"type": "object",
"properties": {

View File

@@ -78,7 +78,10 @@
],
"responses": {
"200": {
"description": "OK"
"description": "OK",
"schema": {
"$ref": "#/definitions/api.postImageOutput"
}
},
"401": {
"description": "Unauthorized"
@@ -256,6 +259,9 @@
"type": "integer"
}
},
"extension": {
"type": "string"
},
"id": {
"type": "integer"
},
@@ -267,6 +273,23 @@
}
}
},
"api.postImageOutput": {
"type": "object",
"properties": {
"extension": {
"type": "string"
},
"id": {
"type": "integer"
},
"uploadedAt": {
"type": "string"
},
"uploadedUserId": {
"type": "string"
}
}
},
"api.putImageAliasesInput": {
"type": "object",
"properties": {

View File

@@ -13,6 +13,8 @@ definitions:
items:
type: integer
type: array
extension:
type: string
id:
type: integer
uploadedAt:
@@ -20,6 +22,17 @@ definitions:
uploadedUserId:
type: string
type: object
api.postImageOutput:
properties:
extension:
type: string
id:
type: integer
uploadedAt:
type: string
uploadedUserId:
type: string
type: object
api.putImageAliasesInput:
properties:
aliases:
@@ -97,6 +110,8 @@ paths:
responses:
"200":
description: OK
schema:
$ref: '#/definitions/api.postImageOutput'
"401":
description: Unauthorized
/api/image/{id}:

View File

@@ -1,16 +1,21 @@
package api
import (
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"gitea.konchin.com/go2025/backend/middlewares"
"gitea.konchin.com/go2025/backend/tracing"
"github.com/uptrace/bunrouter"
"go.uber.org/zap"
)
type getImagesOutputImage struct {
Id int64 `json:"id"`
Extension string `json:"extension"`
Uploader string `json:"uploadedUserId"`
UploadTS int64 `json:"uploadedAt"`
AliasesIds []int64 `json:"aliasesIds"`
@@ -29,11 +34,18 @@ func (self *Handlers) GetImages(
) error {
ctx := req.Context()
rmf := func(s string) bool { return len(s) == 0 }
rawReqImages := strings.Split(req.URL.Query().Get("images"), ",")
rawReqImages = slices.DeleteFunc(rawReqImages, rmf)
rawReqAliases := strings.Split(req.URL.Query().Get("aliases"), ",")
rawReqAliases = slices.DeleteFunc(rawReqAliases, rmf)
if (len(rawReqImages) == 0 && len(rawReqAliases) == 0) ||
(len(rawReqImages) > 0 && len(rawReqAliases) > 0) {
tracing.Logger.Ctx(ctx).
Debug("rawReq",
zap.String("rawReqImages", fmt.Sprintf("%+v", rawReqImages)),
zap.String("rawReqAlias", fmt.Sprintf("%+v", rawReqAliases)))
return middlewares.HTTPError{
StatusCode: http.StatusBadRequest,
Message: "images and aliases should exist exactly one",
@@ -79,6 +91,7 @@ func (self *Handlers) GetImages(
}
output = append(output, getImagesOutputImage{
Id: img.Id,
Extension: img.Extension,
Uploader: img.Uploader,
UploadTS: img.UploadTS.Unix(),
AliasesIds: aliases,

View File

@@ -8,17 +8,23 @@ import (
"gitea.konchin.com/go2025/backend/middlewares"
"gitea.konchin.com/go2025/backend/models"
"gitea.konchin.com/go2025/backend/types"
"gitea.konchin.com/go2025/backend/utils"
"github.com/uptrace/bunrouter"
)
type postImageOutput struct {
Id int64 `json:"id"`
Extension string `json:"extension"`
Uploader string `json:"uploadedUserId"`
UploadTS time.Time `json:"uploadedAt"`
}
// PostImage
//
// @param userinfo header string true "userinfo from /auth/gen-login-url"
// @accept image/png
// @accept image/jpeg
// @accept image/gif
// @success 200
// @success 200 {object} postImageOutput
// @failure 401
// @router /api/image [post]
func (self *Handlers) PostImage(
@@ -65,5 +71,10 @@ func (self *Handlers) PostImage(
OriginError: err,
}
}
return utils.Success(w)
return bunrouter.JSON(w, postImageOutput{
Id: image.Id,
Extension: image.Extension,
Uploader: image.Uploader,
UploadTS: image.UploadTS,
})
}

View File

@@ -154,7 +154,7 @@ func (self *BunDatabase) GetImages(
if len(imageIds) > 0 {
err := self.db.NewSelect().
Model(&ret).
Where("image_id IN (?)", bun.In(imageIds)).
Where("id IN (?)", bun.In(imageIds)).
Relation("Aliases").
Scan(ctx)
if err != nil {
@@ -178,7 +178,7 @@ func (self *BunDatabase) UpdateAliases(
}
_, err := self.db.NewInsert().
Model(&aliases).
On("CONFLICT (id) DO NOTHING").
On("CONFLICT (name) DO NOTHING").
Exec(ctx)
if err != nil {
return err
@@ -193,7 +193,7 @@ func (self *BunDatabase) UpdateAliases(
}
_, err = self.db.NewInsert().
Model(&rels).
On("CONFLICT (alias_id, image_id) DO NOTHING").
On(`CONFLICT ("alias_id", "image_id") DO NOTHING`).
Exec(ctx)
if err != nil {
return err

View File

@@ -6,7 +6,7 @@ type Alias struct {
bun.BaseModel `bun:"table:alias"`
Id int64 `bun:"id,pk,autoincrement"`
Name string `bun:"name,notnull"`
Name string `bun:"name,unique"`
Images []Image `bun:"m2m:alias_image,join:Alias=Image"`
}

View File

@@ -1,4 +1,4 @@
package main
package tests
import (
"net/http"

View File

@@ -1,15 +0,0 @@
package main
import (
"net/http"
"testing"
)
func Test_02_GetImages(t *testing.T) {
resp, err := client.R().
Get("http://localhost:8080/api/aliases")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to fetch aliases")
}
}

View File

@@ -0,0 +1,37 @@
package tests
import (
"io"
"net/http"
"os"
"testing"
)
type postImagePayload struct {
Id int64 `json:"id"`
Extension string `json:"extension"`
}
var rawImage []byte
var image postImagePayload
func Test_02_PostImage(t *testing.T) {
f, err := os.Open("resources/huh.png")
if err != nil {
t.Fatal("failed to open test png")
}
rawImage, err = io.ReadAll(f)
if err != nil {
t.Fatal("failed to read from file")
}
resp, err := client.R().
SetBody(rawImage).
SetResult(&image).
Post("http://localhost:8080/api/image")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to post image")
}
}

View File

@@ -0,0 +1,25 @@
package tests
import (
"fmt"
"net/http"
"testing"
)
type putImageAliasPayload struct {
Aliases []string `json:"aliases"`
}
func Test_03_PutImageAliases(t *testing.T) {
payload := putImageAliasPayload{
Aliases: []string{"huh"},
}
resp, err := client.R().
SetBody(payload).
Put(fmt.Sprintf("http://localhost:8080/api/image/%d/aliases",
image.Id))
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to put image alias")
}
}

View File

@@ -0,0 +1,26 @@
package tests
import (
"net/http"
"testing"
)
type aliasPayload struct {
Id int64 `json:"id"`
Name string `json:"name"`
}
var aliases []aliasPayload
func Test_04_GetAliases(t *testing.T) {
resp, err := client.R().
SetResult(&aliases).
Get("http://localhost:8080/api/aliases")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to get aliases")
}
if len(aliases) == 0 {
t.Fatal("no aliases")
}
}

View File

@@ -0,0 +1,28 @@
package tests
import (
"net/http"
"strconv"
"testing"
)
func Test_05_GetImages(t *testing.T) {
t.Run("check with alias id", func(t *testing.T) {
resp, err := client.R().
SetQueryParam("aliases", strconv.FormatInt(aliases[0].Id, 10)).
Get("http://localhost:8080/api/images")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to get images by alias id")
}
})
t.Run("check with image id", func(t *testing.T) {
resp, err := client.R().
SetQueryParam("images", strconv.FormatInt(image.Id, 10)).
Get("http://localhost:8080/api/images")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to get images by image id")
}
})
}

21
tests/06_getImage_test.go Normal file
View File

@@ -0,0 +1,21 @@
package tests
import (
"bytes"
"fmt"
"net/http"
"testing"
)
func Test_06_GetImage(t *testing.T) {
resp, err := client.R().
Get(fmt.Sprintf("http://localhost:8080/img/%d.%s",
image.Id, image.Extension))
if err != nil || resp.StatusCode() != http.StatusOK {
t.Fatal("failed to get image")
}
if bytes.Compare(rawImage, resp.Body()) != 0 {
t.Fatal("image differ")
}
}

View File

@@ -0,0 +1,29 @@
package tests
import (
"fmt"
"net/http"
"testing"
)
func Test_07_DeleteAlias(t *testing.T) {
resp, err := client.R().
Delete(fmt.Sprintf("http://localhost:8080/api/alias/%d",
aliases[0].Id))
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to delete alias")
}
resp, err = client.R().
SetResult(&aliases).
Get("http://localhost:8080/api/aliases")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to get aliases")
}
if len(aliases) > 0 {
t.Fatal("alias not deleted")
}
}

View File

@@ -0,0 +1,46 @@
package tests
import (
"fmt"
"net/http"
"strconv"
"testing"
)
type getImagesPayload struct {
Id int64 `json:"id"`
}
func Test_08_DeleteImage(t *testing.T) {
resp, err := client.R().
SetBody(rawImage).
SetResult(&image).
Post("http://localhost:8080/api/image")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to post image")
}
resp, err = client.R().
Delete(fmt.Sprintf("http://localhost:8080/api/image/%d",
image.Id))
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to delete image")
}
var imgs []getImagesPayload
resp, err = client.R().
SetResult(&imgs).
SetQueryParam("images", strconv.FormatInt(image.Id, 10)).
Get("http://localhost:8080/api/images")
if err != nil || resp.StatusCode() != http.StatusOK {
t.Logf("%+v", resp)
t.Fatal("failed to get image by image id")
}
if len(imgs) > 0 {
t.Fatal("image not deleted")
}
}

BIN
tests/resources/huh.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB