From a5eec95e9081b1da7b94630a341ea371251dd9de Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Thu, 16 Apr 2020 14:49:28 -0600 Subject: [PATCH] Start writing tests --- back/store.go | 18 ++++++++++++++++++ back/store_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 67 insertions(+) create mode 100644 back/store_test.go diff --git a/back/store.go b/back/store.go index 276ab95..587f594 100644 --- a/back/store.go +++ b/back/store.go @@ -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() } diff --git a/back/store_test.go b/back/store_test.go new file mode 100644 index 0000000..d365d1a --- /dev/null +++ b/back/store_test.go @@ -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)) +} diff --git a/go.mod b/go.mod index 25b6e6e..59d734c 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index d1aeadd..feaf13c 100644 --- a/go.sum +++ b/go.sum @@ -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=