Skip to content

Commit

Permalink
store: add more tests to store
Browse files Browse the repository at this point in the history
  • Loading branch information
jfontan committed Jul 11, 2021
1 parent d1945e3 commit 46d4930
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
5 changes: 2 additions & 3 deletions server/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ type Version struct {

type Store interface {
AddEffect(e Effect) error
Add(parent int, parentVersion int, imageURL string, user string,
version string,
) (int, error)
Add(parent int, parentVersion int, user string, version string) (int, error)
AddVersion(id int, code string) (int, error)
Page(num int, size int, hidden bool) ([]Effect, error)
Effect(id int) (Effect, error)
Hide(id int, hidden bool) error
}
62 changes: 62 additions & 0 deletions server/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -16,6 +17,8 @@ type helper struct {
var tests = []helper{
{"import", testImport},
{"hidden", testHidden},
{"add version", testAddVersion},
{"hide", testHide},
}

func TestMemory(t *testing.T) {
Expand Down Expand Up @@ -74,3 +77,62 @@ func testHidden(t *testing.T, s Store) {
require.Equal(t, 2, es[0].ID)
require.Equal(t, 1, es[1].ID)
}

func testAddVersion(t *testing.T, s Store) {
id, err := s.Add(10, 5, "user", "first")
require.NoError(t, err)
require.Equal(t, 0, id)

e, err := s.Effect(0)
require.NoError(t, err)
require.Equal(t, 0, e.ID)
require.Equal(t, false, e.Hidden)
require.True(t, e.CreatedAt.Before(time.Now()))
require.False(t, e.CreatedAt.IsZero())
require.True(t, e.ModifiedAt.Before(time.Now()))
require.False(t, e.ModifiedAt.IsZero())
require.Equal(t, "0.png", e.ImageURL)
require.Equal(t, 10, e.Parent)
require.Equal(t, 5, e.ParentVersion)
require.Equal(t, "user", e.User)
require.Len(t, e.Versions, 1)

v := e.Versions[0]
require.Equal(t, "first", v.Code)
require.True(t, v.CreatedAt.Before(time.Now()))
require.False(t, v.CreatedAt.IsZero())

vid, err := s.AddVersion(0, "second")
require.NoError(t, err)
require.Equal(t, 1, vid)

_, err = s.AddVersion(1, "invalid")
require.Error(t, err)
require.Equal(t, err, ErrNotFound)
}

func testHide(t *testing.T, s Store) {
id, err := s.Add(10, 5, "user", "first")
require.NoError(t, err)
require.Equal(t, 0, id)

e, err := s.Effect(0)
require.NoError(t, err)
require.False(t, e.Hidden)

err = s.Hide(0, true)
require.NoError(t, err)
e, err = s.Effect(0)
require.NoError(t, err)
require.True(t, e.Hidden)

err = s.Hide(0, false)
require.NoError(t, err)
e, err = s.Effect(0)
require.NoError(t, err)
require.False(t, e.Hidden)

err = s.Hide(1, true)
require.Error(t, err)
require.Equal(t, ErrNotFound, err)
}

0 comments on commit 46d4930

Please sign in to comment.