diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-08-05 22:58:38 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-08-05 22:58:38 +0200 |
| commit | f482480b6ee6971b4e436f9ce901b991fb269644 (patch) | |
| tree | 84d96a4e083ebb723a86e28a747fbfea64d36dd6 /kstore/store_test.go | |
| parent | 016b10e33161ec3274cf4b4d34eb939a290a9243 (diff) | |
kstore: add atomic document storev0.10.0
Diffstat (limited to 'kstore/store_test.go')
| -rw-r--r-- | kstore/store_test.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/kstore/store_test.go b/kstore/store_test.go new file mode 100644 index 0000000..bbbd97a --- /dev/null +++ b/kstore/store_test.go @@ -0,0 +1,92 @@ +package kstore + +import ( + "errors" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type document struct { + Name string `json:"name" yaml:"name"` + Count int `json:"count" yaml:"count"` +} + +func TestJSONRoundTrip(t *testing.T) { + path := filepath.Join(t.TempDir(), "document.json") + want := document{Name: "example", Count: 3} + + require.NoError(t, Save(path, want, JSON, 0o600)) + got, err := Load[document](path, JSON) + + require.NoError(t, err) + assert.Equal(t, want, got) +} + +func TestYAMLRoundTrip(t *testing.T) { + path := filepath.Join(t.TempDir(), "document.yaml") + want := document{Name: "example", Count: 3} + + require.NoError(t, Save(path, want, YAML, 0o640)) + got, err := Load[document](path, YAML) + + require.NoError(t, err) + assert.Equal(t, want, got) +} + +func TestLoadMalformedDocument(t *testing.T) { + path := filepath.Join(t.TempDir(), "document.json") + require.NoError(t, os.WriteFile(path, []byte("not json"), 0o600)) + + _, err := Load[document](path, JSON) + + assert.Error(t, err) +} + +func TestLoadMissingDocument(t *testing.T) { + path := filepath.Join(t.TempDir(), "missing.json") + + _, err := Load[document](path, JSON) + + assert.ErrorIs(t, err, os.ErrNotExist) +} + +func TestSaveSetsRequestedPermissions(t *testing.T) { + path := filepath.Join(t.TempDir(), "document.json") + + require.NoError(t, Save(path, document{}, JSON, 0o640)) + info, err := os.Stat(path) + + require.NoError(t, err) + assert.Equal(t, os.FileMode(0o640), info.Mode().Perm()) +} + +func TestSaveFailedWriteLeavesExistingDocumentUnchanged(t *testing.T) { + path := filepath.Join(t.TempDir(), "document.json") + require.NoError(t, Save(path, document{Name: "original"}, JSON, 0o600)) + + err := Save(filepath.Join(path, "child"), document{Name: "replacement"}, JSON, 0o600) + + assert.Error(t, err) + loaded, loadErr := Load[document](path, JSON) + require.NoError(t, loadErr) + assert.Equal(t, "original", loaded.Name) +} + +func TestSaveReturnsCodecErrors(t *testing.T) { + path := filepath.Join(t.TempDir(), "document.json") + codec := failingCodec{err: errors.New("codec failed")} + + err := Save(path, document{}, codec, 0o600) + + assert.ErrorIs(t, err, codec.err) +} + +type failingCodec struct{ err error } + +func (c failingCodec) Marshal(any) ([]byte, error) { return nil, c.err } + +func (c failingCodec) Unmarshal([]byte, any) error { return c.err } |
