112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
package test
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/article"
|
|
"RenewCMS/internal/domain/contenttype"
|
|
"RenewCMS/internal/domain/entry"
|
|
"RenewCMS/internal/infrastructure/persistence/database"
|
|
persistence "RenewCMS/internal/infrastructure/persistence/repositories"
|
|
"testing"
|
|
)
|
|
|
|
func TestArticleRepositoryVisibilityFilterOnSQLite(t *testing.T) {
|
|
db := openContractSQLite(t)
|
|
repo := persistence.NewArticleRepository(db)
|
|
defaultSite, err := persistence.NewSiteRepository(db).GetDefault()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
create := func(owner uint32, online bool) {
|
|
t.Helper()
|
|
record := articleOn(defaultSite.ID)
|
|
record.CreatedBy = owner
|
|
record.IsOnline = online
|
|
if _, err := repo.Create(record); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
create(1, true)
|
|
create(1, false)
|
|
create(2, true)
|
|
create(2, false)
|
|
create(2, false)
|
|
one := uint32(1)
|
|
|
|
t.Run("FindPage keeps online articles and the viewer's drafts", func(t *testing.T) {
|
|
found, err := repo.FindPage(article.Filters{VisibleTo: &one, Unpaged: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if found.Total != 3 || len(found.Items) != 3 {
|
|
t.Fatalf("total = %d, items = %d, want 3", found.Total, len(found.Items))
|
|
}
|
|
for _, item := range found.Items {
|
|
if !item.IsOnline && item.CreatedBy != one {
|
|
t.Errorf("leaked draft %+v", item)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("FindPage pages the filtered set", func(t *testing.T) {
|
|
found, err := repo.FindPage(article.Filters{VisibleTo: &one, Page: 2, Size: 2})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if found.Total != 3 || len(found.Items) != 1 {
|
|
t.Fatalf("total = %d, items = %d, want 3 and 1", found.Total, len(found.Items))
|
|
}
|
|
})
|
|
|
|
t.Run("FindPage combines the filter with the draft status", func(t *testing.T) {
|
|
found, err := repo.FindPage(article.Filters{VisibleTo: &one, Status: article.StatusDraft, Unpaged: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if found.Total != 1 || found.Items[0].CreatedBy != one {
|
|
t.Fatalf("found = %+v", found)
|
|
}
|
|
})
|
|
|
|
t.Run("CountVisibleTo counts online articles and the viewer's drafts", func(t *testing.T) {
|
|
counts, err := repo.CountVisibleTo(one)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if counts != (article.Counts{All: 3, Online: 2, Draft: 1}) {
|
|
t.Fatalf("counts = %+v", counts)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestEntryRepositoryCreatedByFilterOnSQLite(t *testing.T) {
|
|
db := openUnmigratedSQLite(t)
|
|
if err := database.Migrate(db, migrationPublicURL); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
types := persistence.NewContentTypeRepository(db)
|
|
entries := persistence.NewEntryRepository(db)
|
|
created, err := types.Create(contenttype.ContentType{Key: "notes", Name: "Notes", Fields: []contenttype.Field{{Key: "flag", Label: "Flag", Type: contenttype.TypeBoolean}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, owner := range []uint32{1, 2, 1, 2, 1} {
|
|
if _, err := entries.Create(entry.Entry{ContentTypeID: created.ID, Data: map[string]any{}, CreatedBy: owner, UpdatedBy: owner}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
one := uint32(1)
|
|
|
|
found, err := entries.ListPage(entry.Filters{ContentTypeID: created.ID, CreatedBy: &one, Page: 1, Size: 2, Sort: entry.Sort{Field: entry.SortByID, Direction: entry.SortAsc}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if found.Total != 3 || len(found.Items) != 2 {
|
|
t.Fatalf("total = %d, items = %d, want 3 and 2", found.Total, len(found.Items))
|
|
}
|
|
for _, item := range found.Items {
|
|
if item.CreatedBy != one {
|
|
t.Errorf("leaked entry %+v", item)
|
|
}
|
|
}
|
|
}
|