owl-blogs/infra/entry_repository_test.go

193 lines
4.6 KiB
Go
Raw Normal View History

2023-06-25 18:04:06 +00:00
package infra_test
import (
2023-06-25 19:54:04 +00:00
"owl-blogs/app"
2023-06-25 18:04:06 +00:00
"owl-blogs/app/repository"
"owl-blogs/infra"
"owl-blogs/test"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func setupRepo() repository.EntryRepository {
db := test.NewMockDb()
2023-06-25 19:54:04 +00:00
register := app.NewEntryTypeRegistry()
register.Register(&test.MockEntry{})
repo := infra.NewEntryRepository(db, register)
2023-06-25 18:04:06 +00:00
return repo
}
func TestRepoCreate(t *testing.T) {
repo := setupRepo()
entry := &test.MockEntry{}
now := time.Now()
2023-07-09 20:12:06 +00:00
entry.SetPublishedAt(&now)
2023-07-13 19:55:08 +00:00
entry.SetAuthorId("authorId")
2023-07-09 20:12:06 +00:00
entry.SetMetaData(&test.MockEntryMetaData{
2023-06-25 18:04:06 +00:00
Str: "str",
Number: 1,
Date: now,
})
2023-07-09 20:12:06 +00:00
err := repo.Create(entry)
2023-06-25 18:04:06 +00:00
require.NoError(t, err)
2023-07-06 17:36:24 +00:00
entry2, err := repo.FindById(entry.ID())
2023-06-25 18:04:06 +00:00
require.NoError(t, err)
require.Equal(t, entry.ID(), entry2.ID())
require.Equal(t, entry.Content(), entry2.Content())
2023-07-13 19:55:08 +00:00
require.Equal(t, entry.AuthorId(), entry2.AuthorId())
2023-06-25 18:04:06 +00:00
require.Equal(t, entry.PublishedAt().Unix(), entry2.PublishedAt().Unix())
meta := entry.MetaData().(*test.MockEntryMetaData)
meta2 := entry2.MetaData().(*test.MockEntryMetaData)
require.Equal(t, meta.Str, meta2.Str)
require.Equal(t, meta.Number, meta2.Number)
require.Equal(t, meta.Date.Unix(), meta2.Date.Unix())
}
func TestRepoDelete(t *testing.T) {
repo := setupRepo()
entry := &test.MockEntry{}
now := time.Now()
2023-07-09 20:12:06 +00:00
entry.SetPublishedAt(&now)
entry.SetMetaData(&test.MockEntryMetaData{
2023-06-25 18:04:06 +00:00
Str: "str",
Number: 1,
Date: now,
})
2023-07-09 20:12:06 +00:00
err := repo.Create(entry)
2023-06-25 18:04:06 +00:00
require.NoError(t, err)
err = repo.Delete(entry)
require.NoError(t, err)
_, err = repo.FindById("id")
require.Error(t, err)
}
func TestRepoFindAll(t *testing.T) {
repo := setupRepo()
entry := &test.MockEntry{}
now := time.Now()
2023-07-09 20:12:06 +00:00
entry.SetPublishedAt(&now)
entry.SetMetaData(&test.MockEntryMetaData{
2023-06-25 18:04:06 +00:00
Str: "str",
Number: 1,
Date: now,
})
2023-07-09 20:12:06 +00:00
err := repo.Create(entry)
2023-07-06 17:36:24 +00:00
2023-06-25 18:04:06 +00:00
require.NoError(t, err)
entry2 := &test.MockEntry{}
now2 := time.Now()
2023-07-09 20:12:06 +00:00
entry2.SetPublishedAt(&now2)
entry2.SetMetaData(&test.MockEntryMetaData{
2023-07-06 17:36:24 +00:00
Str: "str",
Number: 1,
Date: now,
2023-06-25 18:04:06 +00:00
})
2023-07-09 20:12:06 +00:00
err = repo.Create(entry2)
2023-06-25 18:04:06 +00:00
require.NoError(t, err)
entries, err := repo.FindAll(nil)
require.NoError(t, err)
require.Equal(t, 2, len(entries))
entries, err = repo.FindAll(&[]string{"MockEntry"})
require.NoError(t, err)
require.Equal(t, 2, len(entries))
entries, err = repo.FindAll(&[]string{"MockEntry2"})
require.NoError(t, err)
require.Equal(t, 0, len(entries))
}
func TestRepoUpdate(t *testing.T) {
repo := setupRepo()
entry := &test.MockEntry{}
now := time.Now()
2023-07-09 20:12:06 +00:00
entry.SetPublishedAt(&now)
2023-07-13 19:55:08 +00:00
entry.SetAuthorId("authorId")
2023-07-09 20:12:06 +00:00
entry.SetMetaData(&test.MockEntryMetaData{
2023-06-25 18:04:06 +00:00
Str: "str",
Number: 1,
Date: now,
})
2023-07-09 20:12:06 +00:00
err := repo.Create(entry)
2023-06-25 18:04:06 +00:00
require.NoError(t, err)
entry2 := &test.MockEntry{}
now2 := time.Now()
2023-07-09 20:12:06 +00:00
entry2.SetPublishedAt(&now2)
2023-07-13 19:55:08 +00:00
entry.SetAuthorId("authorId2")
2023-07-09 20:12:06 +00:00
entry2.SetMetaData(&test.MockEntryMetaData{
2023-06-25 18:04:06 +00:00
Str: "str2",
Number: 2,
Date: now2,
})
2023-07-09 20:12:06 +00:00
err = repo.Create(entry2)
2023-07-06 17:36:24 +00:00
require.NoError(t, err)
2023-06-25 18:04:06 +00:00
err = repo.Update(entry2)
require.NoError(t, err)
2023-07-06 17:36:24 +00:00
entry3, err := repo.FindById(entry2.ID())
2023-06-25 18:04:06 +00:00
require.NoError(t, err)
require.Equal(t, entry3.Content(), entry2.Content())
require.Equal(t, entry3.PublishedAt().Unix(), entry2.PublishedAt().Unix())
2023-07-13 19:55:08 +00:00
require.Equal(t, entry3.AuthorId(), entry2.AuthorId())
2023-06-25 18:04:06 +00:00
meta := entry3.MetaData().(*test.MockEntryMetaData)
meta2 := entry2.MetaData().(*test.MockEntryMetaData)
require.Equal(t, meta.Str, meta2.Str)
require.Equal(t, meta.Number, meta2.Number)
require.Equal(t, meta.Date.Unix(), meta2.Date.Unix())
}
2023-07-08 09:08:55 +00:00
func TestRepoNoSideEffect(t *testing.T) {
repo := setupRepo()
entry1 := &test.MockEntry{}
now1 := time.Now()
2023-07-09 20:12:06 +00:00
entry1.SetPublishedAt(&now1)
entry1.SetMetaData(&test.MockEntryMetaData{
2023-07-08 09:08:55 +00:00
Str: "1",
Number: 1,
Date: now1,
})
2023-07-09 20:12:06 +00:00
err := repo.Create(entry1)
2023-07-08 09:08:55 +00:00
require.NoError(t, err)
entry2 := &test.MockEntry{}
now2 := time.Now()
2023-07-09 20:12:06 +00:00
entry2.SetPublishedAt(&now2)
entry2.SetMetaData(&test.MockEntryMetaData{
2023-07-08 09:08:55 +00:00
Str: "2",
Number: 2,
Date: now2,
})
2023-07-09 20:12:06 +00:00
err = repo.Create(entry2)
2023-07-08 09:08:55 +00:00
require.NoError(t, err)
r1, err := repo.FindById(entry1.ID())
require.NoError(t, err)
r2, err := repo.FindById(entry2.ID())
require.NoError(t, err)
require.Equal(t, r1.MetaData().(*test.MockEntryMetaData).Str, "1")
require.Equal(t, r1.MetaData().(*test.MockEntryMetaData).Number, 1)
require.Equal(t, r1.MetaData().(*test.MockEntryMetaData).Date.Unix(), now1.Unix())
require.Equal(t, r2.MetaData().(*test.MockEntryMetaData).Str, "2")
require.Equal(t, r2.MetaData().(*test.MockEntryMetaData).Number, 2)
require.Equal(t, r2.MetaData().(*test.MockEntryMetaData).Date.Unix(), now2.Unix())
}