mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
chore: add unit test
This commit is contained in:
parent
ce0a376723
commit
abdac1993a
2 changed files with 180 additions and 8 deletions
|
|
@ -1,9 +1,12 @@
|
|||
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||||
// Copyright 2026 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repo_test
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"forgejo.org/models/db"
|
||||
|
|
@ -88,14 +91,67 @@ func TestUpdateAttachment(t *testing.T) {
|
|||
unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{Name: "new_name"})
|
||||
}
|
||||
|
||||
func TestGetAttachmentsByUUIDs(t *testing.T) {
|
||||
func TestFindRepoAttachmentsByUUID(t *testing.T) {
|
||||
defer unittest.OverrideFixtures("models/repo/fixtures/TestFindRepoAttachmentsByUUID")()
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
attachList, err := repo_model.GetAttachmentsByUUIDs(db.DefaultContext, []string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, attachList, 2)
|
||||
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attachList[0].UUID)
|
||||
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", attachList[1].UUID)
|
||||
assert.Equal(t, int64(1), attachList[0].IssueID)
|
||||
assert.Equal(t, int64(5), attachList[1].IssueID)
|
||||
sort := func(x []*repo_model.Attachment) {
|
||||
slices.SortFunc(x, func(a, b *repo_model.Attachment) int {
|
||||
return cmp.Compare(a.ID, b.ID)
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("Empty UUIDs", func(t *testing.T) {
|
||||
attachments, err := repo_model.FindRepoAttachmentsByUUID(t.Context(), 1001, []string{}, repo_model.FindAttachmentOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, attachments)
|
||||
})
|
||||
|
||||
t.Run("Wrong repository", func(t *testing.T) {
|
||||
attachments, err := repo_model.FindRepoAttachmentsByUUID(t.Context(), 1002, []string{"31b6f65e-2745-4e87-b02c-e6bb9890d399", "e19fd169-c2d1-4fd0-a6d5-9658fd4affed", "758e41f6-e3b7-4420-b34f-1920da0858aa"}, repo_model.FindAttachmentOptions{})
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, attachments)
|
||||
})
|
||||
|
||||
t.Run("Not attached", func(t *testing.T) {
|
||||
attachments, err := repo_model.FindRepoAttachmentsByUUID(t.Context(), 1001, []string{"31b6f65e-2745-4e87-b02c-e6bb9890d399", "e19fd169-c2d1-4fd0-a6d5-9658fd4affed", "758e41f6-e3b7-4420-b34f-1920da0858aa"}, repo_model.FindAttachmentOptions{})
|
||||
require.NoError(t, err)
|
||||
if assert.Len(t, attachments, 1) {
|
||||
assert.Equal(t, "31b6f65e-2745-4e87-b02c-e6bb9890d399", attachments[0].UUID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Issue", func(t *testing.T) {
|
||||
attachments, err := repo_model.FindRepoAttachmentsByUUID(t.Context(), 1001, []string{"17bcdb6b-dd84-4da1-b37a-671165402d8d", "e19fd169-c2d1-4fd0-a6d5-9658fd4affed", "774f276e-c85d-488e-b735-7bc07860c756"}, repo_model.FindAttachmentOptions{IssueID: 1001})
|
||||
require.NoError(t, err)
|
||||
sort(attachments)
|
||||
if assert.Len(t, attachments, 2) {
|
||||
assert.Equal(t, "17bcdb6b-dd84-4da1-b37a-671165402d8d", attachments[0].UUID)
|
||||
assert.Equal(t, "e19fd169-c2d1-4fd0-a6d5-9658fd4affed", attachments[1].UUID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Comment", func(t *testing.T) {
|
||||
attachments, err := repo_model.FindRepoAttachmentsByUUID(t.Context(), 1001, []string{"edf0d986-8a12-447a-a4bb-e9aefead251b", "774f276e-c85d-488e-b735-7bc07860c756", "e19fd169-c2d1-4fd0-a6d5-9658fd4affed"}, repo_model.FindAttachmentOptions{IssueID: 1001, CommentID: 1001})
|
||||
require.NoError(t, err)
|
||||
if assert.Len(t, attachments, 1) {
|
||||
assert.Equal(t, "edf0d986-8a12-447a-a4bb-e9aefead251b", attachments[0].UUID)
|
||||
}
|
||||
|
||||
attachments, err = repo_model.FindRepoAttachmentsByUUID(t.Context(), 1001, []string{"edf0d986-8a12-447a-a4bb-e9aefead251b", "774f276e-c85d-488e-b735-7bc07860c756", "e19fd169-c2d1-4fd0-a6d5-9658fd4affed"}, repo_model.FindAttachmentOptions{IssueID: 1001, CommentID: 1002})
|
||||
require.NoError(t, err)
|
||||
if assert.Len(t, attachments, 1) {
|
||||
assert.Equal(t, "774f276e-c85d-488e-b735-7bc07860c756", attachments[0].UUID)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Release", func(t *testing.T) {
|
||||
attachments, err := repo_model.FindRepoAttachmentsByUUID(t.Context(), 1001, []string{"d2570bab-c843-486f-b7b7-23e011c42815", "758e41f6-e3b7-4420-b34f-1920da0858aa", "e19fd169-c2d1-4fd0-a6d5-9658fd4affed"}, repo_model.FindAttachmentOptions{ReleaseID: 1001})
|
||||
require.NoError(t, err)
|
||||
if assert.Len(t, attachments, 2) {
|
||||
sort(attachments)
|
||||
assert.Equal(t, "758e41f6-e3b7-4420-b34f-1920da0858aa", attachments[0].UUID)
|
||||
assert.Equal(t, "d2570bab-c843-486f-b7b7-23e011c42815", attachments[1].UUID)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,116 @@
|
|||
-
|
||||
id: 1001
|
||||
uuid: 70d3e7b8-5e46-41eb-bd2d-afaba53056bd
|
||||
repo_id: 0
|
||||
issue_id: 0
|
||||
release_id: 0
|
||||
uploader_id: 0
|
||||
comment_id: 0
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300000
|
||||
|
||||
-
|
||||
id: 1002
|
||||
uuid: 31b6f65e-2745-4e87-b02c-e6bb9890d399
|
||||
repo_id: 1001
|
||||
issue_id: 0
|
||||
release_id: 0
|
||||
uploader_id: 1001
|
||||
comment_id: 0
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300001
|
||||
|
||||
-
|
||||
id: 1003
|
||||
uuid: 03158f6c-487c-4bc5-b24b-10f13e21c2e7
|
||||
repo_id: 1001
|
||||
issue_id: 0
|
||||
release_id: 0
|
||||
uploader_id: 1002
|
||||
comment_id: 0
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300002
|
||||
|
||||
-
|
||||
id: 1004
|
||||
uuid: 17bcdb6b-dd84-4da1-b37a-671165402d8d
|
||||
repo_id: 1001
|
||||
issue_id: 1001
|
||||
release_id: 0
|
||||
uploader_id: 1001
|
||||
comment_id: 0
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300003
|
||||
|
||||
-
|
||||
id: 1005
|
||||
uuid: e19fd169-c2d1-4fd0-a6d5-9658fd4affed
|
||||
repo_id: 1001
|
||||
issue_id: 1001
|
||||
release_id: 0
|
||||
uploader_id: 1002
|
||||
comment_id: 0
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300004
|
||||
|
||||
-
|
||||
id: 1006
|
||||
uuid: 758e41f6-e3b7-4420-b34f-1920da0858aa
|
||||
repo_id: 1001
|
||||
issue_id: 0
|
||||
release_id: 1001
|
||||
uploader_id: 1001
|
||||
comment_id: 0
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300005
|
||||
|
||||
-
|
||||
id: 1007
|
||||
uuid: d2570bab-c843-486f-b7b7-23e011c42815
|
||||
repo_id: 1001
|
||||
issue_id: 0
|
||||
release_id: 1001
|
||||
uploader_id: 1002
|
||||
comment_id: 0
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300006
|
||||
|
||||
-
|
||||
id: 1008
|
||||
uuid: edf0d986-8a12-447a-a4bb-e9aefead251b
|
||||
repo_id: 1001
|
||||
issue_id: 1001
|
||||
release_id: 0
|
||||
uploader_id: 1001
|
||||
comment_id: 1001
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300007
|
||||
|
||||
-
|
||||
id: 1009
|
||||
uuid: 774f276e-c85d-488e-b735-7bc07860c756
|
||||
repo_id: 1001
|
||||
issue_id: 1001
|
||||
release_id: 0
|
||||
uploader_id: 1002
|
||||
comment_id: 1002
|
||||
name: attach1
|
||||
download_count: 0
|
||||
size: 0
|
||||
created_unix: 1771300008
|
||||
Loading…
Add table
Add a link
Reference in a new issue