buildr/modules/state/store_test.go
Peter 5af8ddceab
Some checks failed
continuous-integration/drone/push Build is failing
refactor: rework plugins to track their state
- introduce individual commands to manage plugins
- store plugin in state to skip loading them to memory every time
2023-06-29 20:14:52 +02:00

130 lines
2.6 KiB
Go

package state_test
import (
"context"
"path/filepath"
"testing"
"code.icb4dc0.de/buildr/buildr/modules"
"code.icb4dc0.de/buildr/buildr/modules/state"
)
func TestEntStore_Set(t *testing.T) {
t.Parallel()
type args struct {
key state.StringsKey
state []byte
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Set empty JSON state",
args: args{
key: state.KeyOfStrings(
"script_state",
modules.CategoryTask,
"go_test",
),
state: []byte(`{}`),
},
wantErr: false,
},
{
name: "Set JSON state",
args: args{
key: state.KeyOfStrings(
"tool_state",
modules.CategoryTool,
"mockery",
),
state: []byte(`{"InstalledVersion": "1.0.0"}`),
},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
db, err := state.NewDB(ctx, filepath.Join(t.TempDir(), "store.sqlite"))
if err != nil {
t.Errorf("NewEntStore() error = %v", err)
return
}
if err := db.State.Set(ctx, tt.args.key, tt.args.state); (err != nil) != tt.wantErr {
t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestEntStore_Get_NonExisting(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
db, err := state.NewDB(ctx, filepath.Join(t.TempDir(), "store.sqlite"))
if err != nil {
t.Errorf("NewEntStore() error = %v", err)
return
}
key := state.KeyOfStrings(
"script_state",
modules.CategoryTask,
"go_test",
)
gotState, _, err := db.State.Get(ctx, key)
if err != nil {
t.Errorf("Get() error = %v", err)
return
}
if len(gotState) != 0 {
t.Errorf("Expected empty state, got %v", gotState)
}
}
func TestEntStore_SetGet(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
db, err := state.NewDB(ctx, filepath.Join(t.TempDir(), "store.sqlite"))
if err != nil {
t.Errorf("NewEntStore() error = %v", err)
return
}
key := state.KeyOfStrings(
"script_state",
modules.CategoryTask,
"go_test",
)
if err := db.State.Set(ctx, key, []byte(`{}`)); err != nil {
t.Errorf("Set() error = %v", err)
return
}
gotState, gotMeta, err := db.State.Get(ctx, key)
if err != nil {
t.Errorf("Get() error = %v", err)
return
}
if string(gotState) != `{}` {
t.Errorf("Get() gotState = %s", gotState)
}
if gotMeta.ModifiedAt.IsZero() {
t.Errorf("Get() gotMeta.ModifiedAt = %v", gotMeta.ModifiedAt)
}
}