This repository was archived by the owner on Jun 12, 2024. It is now read-only.
generated from hay-kot/go-web-template
-
-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathrepo_documents_test.go
114 lines (99 loc) · 2.52 KB
/
repo_documents_test.go
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
package repo
import (
"bytes"
"context"
"fmt"
"io"
"path/filepath"
"testing"
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/core/blobstore"
"github.com/hay-kot/homebox/backend/internal/data/ent"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func useDocs(t *testing.T, num int) []DocumentOut {
t.Helper()
results := make([]DocumentOut, 0, num)
ids := make([]uuid.UUID, 0, num)
for i := 0; i < num; i++ {
doc, err := tRepos.Docs.Create(context.Background(), tGroup.ID, DocumentCreate{
Title: fk.Str(10) + ".md",
Content: bytes.NewReader([]byte(fk.Str(10))),
})
require.NoError(t, err)
assert.NotNil(t, doc)
results = append(results, doc)
ids = append(ids, doc.ID)
}
t.Cleanup(func() {
for _, id := range ids {
err := tRepos.Docs.Delete(context.Background(), id)
if err != nil {
assert.True(t, ent.IsNotFound(err))
}
}
})
return results
}
func TestDocumentRepository_CreateUpdateDelete(t *testing.T) {
temp := t.TempDir()
r := DocumentRepository{
db: tClient,
bs: blobstore.NewLocalBlobStore(temp),
}
type args struct {
ctx context.Context
gid uuid.UUID
doc DocumentCreate
}
tests := []struct {
name string
content string
args args
title string
wantErr bool
}{
{
name: "basic create",
title: "test.md",
content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
args: args{
ctx: context.Background(),
gid: tGroup.ID,
doc: DocumentCreate{
Title: "test.md",
Content: bytes.NewReader([]byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create Document
got, err := r.Create(tt.args.ctx, tt.args.gid, tt.args.doc)
require.NoError(t, err)
assert.Equal(t, tt.title, got.Title)
assert.Equal(t, fmt.Sprintf("%s/documents", tt.args.gid), filepath.Dir(got.Path))
ensureRead := func() {
// Read Document
bts, err := r.bs.Get(tt.args.ctx, got.Path)
require.NoError(t, err)
buf, err := io.ReadAll(bts)
require.NoError(t, err)
assert.Equal(t, tt.content, string(buf))
}
ensureRead()
// Update Document
got, err = r.Rename(tt.args.ctx, got.ID, "__"+tt.title+"__")
require.NoError(t, err)
assert.Equal(t, "__"+tt.title+"__", got.Title)
ensureRead()
// Delete Document
err = r.Delete(tt.args.ctx, got.ID)
require.NoError(t, err)
_, err = r.bs.Get(tt.args.ctx, got.Path)
require.Error(t, err)
})
}
}