Start writing tests

This commit is contained in:
Brandon Dyck 2020-04-16 14:49:28 -06:00
parent 051e5f4601
commit a5eec95e90
4 changed files with 67 additions and 0 deletions

View File

@ -45,6 +45,24 @@ func NewStore(filename string, genString GenString) (*Store, error) {
return store, nil
}
func NewMemoryStore(genString GenString) (*Store, error) {
pool, err := sqlitex.Open("file::memory:?mode=memory&cache=shared", 0, 10)
if err != nil {
return nil, err
}
store := &Store{
pool: pool,
genString: genString,
}
log.Println("creating schema")
err = store.createSchema()
if err != nil {
defer pool.Close()
return nil, err
}
return store, nil
}
func (s *Store) Close() error {
return s.pool.Close()
}

46
back/store_test.go Normal file
View File

@ -0,0 +1,46 @@
package back_test
import (
"context"
"testing"
"time"
"github.com/matryer/is"
"gitlab.codemonkeysoftware.net/b/henwen/back"
)
func TestNewMemoryStore(t *testing.T) {
is := is.New(t)
store, err := back.NewMemoryStore(back.SecureGenString)
is.NoErr(err)
is.True(store != nil)
store.Close()
}
func TestCreateEvent(t *testing.T) {
is := is.New(t)
store, err := back.NewMemoryStore(back.SecureGenString)
is.NoErr(err)
defer store.Close()
earliest := time.Date(2020, 04, 05, 0, 0, 0, 0, time.UTC)
latest := time.Date(2020, 04, 05, 0, 0, 0, 0, time.UTC)
const name = "abc"
const description = "def"
createResult, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: name,
Description: description,
EarliestDate: earliest,
LatestDate: latest,
})
is.NoErr(err)
metadataResult, err := store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
AlphaID: createResult.AlphaID,
})
is.NoErr(err)
is.Equal(metadataResult.Name, name)
is.Equal(metadataResult.Description, description)
is.True(metadataResult.EarliestDate.Equal(earliest))
is.True(metadataResult.LatestDate.Equal(latest))
}

1
go.mod
View File

@ -4,5 +4,6 @@ go 1.14
require (
crawshaw.io/sqlite v0.2.5
github.com/matryer/is v1.3.0
gitlab.codemonkeysoftware.net/b/hatmill v0.0.5
)

2
go.sum
View File

@ -2,5 +2,7 @@ crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797/go.mod h1:sXBiorCo8c46JlQV3oX
crawshaw.io/sqlite v0.2.5 h1:lUjWtEQJZApoL4V83cHsNeaGZkY5Ofmh8cGGBdZNxPU=
crawshaw.io/sqlite v0.2.5/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4=
github.com/leanovate/gopter v0.2.4/go.mod h1:gNcbPWNEWRe4lm+bycKqxUYoH5uoVje5SkOJ3uoLer8=
github.com/matryer/is v1.3.0 h1:9qiso3jaJrOe6qBRJRBt2Ldht05qDiFP9le0JOIhRSI=
github.com/matryer/is v1.3.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
gitlab.codemonkeysoftware.net/b/hatmill v0.0.5 h1:7L70G1Qmdm7bSwH58qWY5psXOwoqcvokLhc4BPSIjWo=
gitlab.codemonkeysoftware.net/b/hatmill v0.0.5/go.mod h1:3jggrD9qkK8kXt0d3+Kwg3zti2PfPcociz7r3oxtjWU=