2022-09-12 22:47:27 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2022-09-27 23:52:13 +00:00
|
|
|
"bytes"
|
2022-09-12 22:47:27 +00:00
|
|
|
"context"
|
2022-09-27 23:52:13 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-09-12 22:47:27 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
2022-09-12 22:47:27 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func useDocs(t *testing.T, num int) []DocumentOut {
|
2022-09-12 22:47:27 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
results := make([]DocumentOut, 0, num)
|
2022-09-12 22:47:27 +00:00
|
|
|
ids := make([]uuid.UUID, 0, num)
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
2022-09-27 23:52:13 +00:00
|
|
|
doc, err := tRepos.Docs.Create(context.Background(), tGroup.ID, DocumentCreate{
|
|
|
|
Title: fk.Str(10) + ".md",
|
|
|
|
Content: bytes.NewReader([]byte(fk.Str(10))),
|
2022-09-12 22:47:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
assert.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
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func TestDocumentRepository_CreateUpdateDelete(t *testing.T) {
|
|
|
|
temp := t.TempDir()
|
|
|
|
r := DocumentRepository{
|
|
|
|
db: tClient,
|
|
|
|
dir: temp,
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
gid uuid.UUID
|
|
|
|
doc DocumentCreate
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
2022-09-27 23:52:13 +00:00
|
|
|
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.")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
2022-09-27 23:52:13 +00:00
|
|
|
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)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.title, got.Title)
|
|
|
|
assert.Equal(t, fmt.Sprintf("%s/%s/documents", temp, tt.args.gid), filepath.Dir(got.Path))
|
|
|
|
|
|
|
|
ensureRead := func() {
|
|
|
|
// Read Document
|
|
|
|
bts, err := os.ReadFile(got.Path)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, tt.content, string(bts))
|
|
|
|
}
|
|
|
|
ensureRead()
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
// Update Document
|
|
|
|
got, err = r.Rename(tt.args.ctx, got.ID, "__"+tt.title+"__")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "__"+tt.title+"__", got.Title)
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
ensureRead()
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
// Delete Document
|
|
|
|
err = r.Delete(tt.args.ctx, got.ID)
|
|
|
|
assert.NoError(t, err)
|
2022-09-12 22:47:27 +00:00
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
_, err = os.Stat(got.Path)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
}
|