package state import ( "github.com/google/uuid" "github.com/stretchr/testify/assert" "os" "testing" ) func TestStoreStateAsJsonToHDD(t *testing.T) { data := struct { Name string Test string `json:"-"` Age int something string }{ Name: "Max", Test: "foo", Age: 1337, something: "nothing", } store, err := NewLocaleFilesystem("/tmp/statetest") defer os.RemoveAll("/tmp/statetest") assert.Nil(t, err, "should be able to create filesystem store without error") err = store.StoreState("geheim", data) assert.Nil(t, err, "should be store data without error") if _, err := os.Stat("/tmp/statetest/geheim.json"); os.IsNotExist(err) { t.Error("state file does not exists") } databyte, err := os.ReadFile("/tmp/statetest/geheim.json") assert.Nil(t, err, "should be able to read file without error") assert.Equal(t, "{\"Name\":\"Max\",\"Age\":1337}", string(databyte), "json should match") } func TestStoreStateFromHDD(t *testing.T) { data := struct { Name string Test string `json:"-"` Age int something string }{} result := struct { Name string Test string `json:"-"` Age int something string }{ Name: "Max", Test: "", Age: 1337, something: "", } store, err := NewLocaleFilesystem("/tmp/statetest") defer os.RemoveAll("/tmp/statetest") assert.Nil(t, err, "should be able to create filesystem store without error") key := uuid.NewString() os.WriteFile("/tmp/statetest/"+key+".json", []byte("{\"Name\":\"Max\",\"Age\":1337}"), 0744) store.GetState(key, &data) assert.Equal(t, result, data, "should get same data from hdd") } func TestRamState(t *testing.T) { store, err := NewRamFilesystem() assert.Nil(t, err, "should be able to create store without error") data := struct { Name string Test string `json:"-"` Age int something string }{ Name: "Max", Test: "foo", Age: 1337, something: "nothing", } dataFromStore := struct { Name string Test string `json:"-"` Age int something string }{} result := struct { Name string Test string `json:"-"` Age int something string }{ Name: "Max", Test: "", Age: 1337, something: "", } err = store.StoreState("something", data) assert.Nil(t, err, "should be save stater without error") err = store.GetState("something", &dataFromStore) assert.Nil(t, err, "should be able to get data from store without error") assert.Equal(t, result, dataFromStore, "should get correct data from store") }