owl-blogs/infra/binary_file_repository_test.go

60 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-07-08 09:08:55 +00:00
package infra_test
import (
"owl-blogs/app/repository"
"owl-blogs/infra"
"owl-blogs/test"
"testing"
"github.com/stretchr/testify/require"
)
func setupBinaryRepo() repository.BinaryRepository {
db := test.NewMockDb()
repo := infra.NewBinaryFileRepo(db)
return repo
}
func TestBinaryRepoCreate(t *testing.T) {
repo := setupBinaryRepo()
2023-07-09 19:27:59 +00:00
file, err := repo.Create("name", []byte("😀 😃 😄 😁"), nil)
2023-07-08 09:08:55 +00:00
require.NoError(t, err)
file, err = repo.FindById(file.Id)
require.NoError(t, err)
require.Equal(t, file.Name, "name")
require.Equal(t, file.Data, []byte("😀 😃 😄 😁"))
}
func TestBinaryRepoNoSideEffect(t *testing.T) {
repo := setupBinaryRepo()
2023-07-09 19:27:59 +00:00
file, err := repo.Create("name1", []byte("111"), nil)
2023-07-08 09:08:55 +00:00
require.NoError(t, err)
2023-07-09 19:27:59 +00:00
file2, err := repo.Create("name2", []byte("222"), nil)
2023-07-08 09:08:55 +00:00
require.NoError(t, err)
file, err = repo.FindById(file.Id)
require.NoError(t, err)
file2, err = repo.FindById(file2.Id)
require.NoError(t, err)
require.Equal(t, file.Name, "name1")
require.Equal(t, file.Data, []byte("111"))
require.Equal(t, file2.Name, "name2")
require.Equal(t, file2.Data, []byte("222"))
}
2023-07-19 18:52:19 +00:00
func TestBinaryWithSpaceInName(t *testing.T) {
repo := setupBinaryRepo()
file, err := repo.Create("name with space", []byte("111"), nil)
require.NoError(t, err)
file, err = repo.FindById(file.Id)
require.NoError(t, err)
require.Equal(t, file.Name, "name with space")
require.Equal(t, file.Data, []byte("111"))
}