home/internal/entry/repository/fs/fs_page_test.go

102 lines
2.5 KiB
Go

package fs_test
import (
"context"
"path"
"path/filepath"
"testing"
"testing/fstest"
"github.com/google/go-cmp/cmp"
"source.toby3d.me/toby3d/home/internal/domain"
repository "source.toby3d.me/toby3d/home/internal/entry/repository/fs"
)
func TestGet(t *testing.T) {
t.Parallel()
testData := fstest.MapFS{
filepath.Join("both", "index.md"): &fstest.MapFile{Data: []byte("both/index.md")},
filepath.Join("both.md"): &fstest.MapFile{Data: []byte("both.md")},
filepath.Join("file.md"): &fstest.MapFile{Data: []byte("file.md")},
filepath.Join("folder", "index.md"): &fstest.MapFile{Data: []byte("folder/index.md")},
filepath.Join("index.md"): &fstest.MapFile{Data: []byte("index.md")},
}
repo := repository.NewFileSystemPageRepository(testData)
for name, tc := range map[string]struct {
expect *domain.Page
input string
}{
"index": {
input: path.Join("index"),
expect: &domain.Page{
Content: []byte("index.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
},
},
"file": {
input: path.Join("file"),
expect: &domain.Page{
Content: []byte("file.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
},
},
"folder": {
input: path.Join("folder", "index"),
expect: &domain.Page{
Content: []byte("folder/index.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
},
},
"both-file": {
input: path.Join("both"),
expect: &domain.Page{
Content: []byte("both.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
},
},
"both-folder": {
input: path.Join("both", "index"),
expect: &domain.Page{
Content: []byte("both/index.md"),
Params: make(map[string]any),
Resources: make([]*domain.Resource, 0),
Translations: make([]*domain.Page, 0),
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
out, err := repo.Get(context.Background(), domain.LanguageUnd, tc.input)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(out, tc.expect, cmp.FilterPath(func(p cmp.Path) bool {
switch p.String() {
default:
return false
case "File", "Language":
return true
}
}, cmp.Ignore())); diff != "" {
t.Error(diff)
}
})
}
}