fix: add tests and fix some bugs found through testing

This commit is contained in:
Ash Keel 2023-05-19 15:05:18 +02:00
parent ec8f333aea
commit d37c7ff1b7
9 changed files with 263 additions and 4 deletions

View File

@ -24,7 +24,6 @@ var (
type LocalDBClient struct {
client *kv.LocalClient
hub *kv.Hub
logger *zap.Logger
}
type KvPair struct {
@ -50,7 +49,6 @@ func NewLocalClient(hub *kv.Hub, logger *zap.Logger) (*LocalDBClient, error) {
return &LocalDBClient{
client: localClient,
hub: hub,
logger: logger,
}, nil
}

79
database/database_test.go Normal file
View File

@ -0,0 +1,79 @@
package database
import (
kv "github.com/strimertul/kilovolt/v10"
"go.uber.org/zap/zaptest"
"testing"
)
func TestLocalDBClient_PutKey(t *testing.T) {
client, store := createLocalClient(t)
defer cleanupClient(client)
// Store a key using the local client
key := "test"
value := "value"
err := client.PutKey(key, value)
if err != nil {
t.Fatal(err)
}
// Retrieve the key from the store and verify it
stored, err := store.Get(key)
if err != nil {
t.Fatal(err)
}
if stored != value {
t.Fatalf("expected %s, got %s", value, stored)
}
}
func TestLocalDBClient_GetKey(t *testing.T) {
client, store := createLocalClient(t)
defer cleanupClient(client)
// Store a key directly in the store
key := "test"
value := "value"
err := store.Set(key, value)
if err != nil {
t.Fatal(err)
}
// Retrieve the key using the local client
stored, err := client.GetKey(key)
if err != nil {
t.Fatal(err)
}
if stored != value {
t.Fatalf("expected %s, got %s", value, stored)
}
}
func createLocalClient(t *testing.T) (*LocalDBClient, kv.Driver) {
logger := zaptest.NewLogger(t)
// Create in-memory store and hub
inmemStore := kv.MakeBackend()
hub, err := kv.NewHub(inmemStore, kv.HubOptions{}, logger)
if err != nil {
t.Fatal(err)
}
go hub.Run()
// Create local client
client, err := NewLocalClient(hub, logger)
if err != nil {
t.Fatal(err)
}
return client, inmemStore
}
func cleanupClient(client *LocalDBClient) {
if client.hub != nil {
_ = client.Close()
client.hub.Close()
}
}

1
go.mod
View File

@ -28,6 +28,7 @@ require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/apenwarr/w32 v0.0.0-20190407065021-aa00fece76ab // indirect
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bep/debounce v1.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect

1
go.sum
View File

@ -25,6 +25,7 @@ github.com/ashkeel/helix/v2 v2.20.0-ws h1:IvwMHs4PBCoVgu0IVlTZGyQG8h40zBY/FYmRXG
github.com/ashkeel/helix/v2 v2.20.0-ws/go.mod h1:zZcKsyyBWDli34x3QleYsVMiiNGMXPAEU5NjsiZDtvY=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=

View File

@ -14,6 +14,6 @@ func (f ByDate) Swap(i, j int) {
func (f ByDate) Less(i, j int) bool {
firstInfo, _ := f[i].Info()
secondInfo, _ := f[i].Info()
secondInfo, _ := f[j].Info()
return firstInfo.ModTime().Before(secondInfo.ModTime())
}

87
utils/file_test.go Normal file
View File

@ -0,0 +1,87 @@
package utils
import (
"io/fs"
"sort"
"testing"
"time"
)
func TestByDate(t *testing.T) {
// Create some mocked dir entries with predictable dates
entries := []fs.DirEntry{
MockDirEntry{MockFileInfo{"a", 1, false, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)}},
MockDirEntry{MockFileInfo{"b", 2, false, time.Date(2020, 1, 4, 0, 0, 0, 0, time.UTC)}},
MockDirEntry{MockFileInfo{"c", 3, false, time.Date(2020, 1, 3, 0, 0, 0, 0, time.UTC)}},
}
// Sort them by date
sort.Sort(ByDate(entries))
// Check the order
if entries[0].Name() != "a" {
t.Error("Expected a to be first")
}
if entries[1].Name() != "c" {
t.Error("Expected c to be second")
}
if entries[2].Name() != "b" {
t.Error("Expected b to be third")
}
}
// Mock a DirEntry instance
type MockDirEntry struct {
info MockFileInfo
}
func (m MockDirEntry) Type() fs.FileMode {
return m.info.Mode()
}
func (m MockDirEntry) Name() string {
return m.info.Name()
}
func (m MockDirEntry) Size() int64 {
return m.info.Size()
}
func (m MockDirEntry) IsDir() bool {
return m.info.IsDir()
}
func (m MockDirEntry) Info() (fs.FileInfo, error) {
return m.info, nil
}
type MockFileInfo struct {
name string
size int64
isDir bool
modTime time.Time
}
func (m MockFileInfo) IsDir() bool {
return m.isDir
}
func (m MockFileInfo) Sys() any {
return nil
}
func (m MockFileInfo) Name() string {
return m.name
}
func (m MockFileInfo) Size() int64 {
return m.size
}
func (m MockFileInfo) Mode() fs.FileMode {
return fs.FileMode(0)
}
func (m MockFileInfo) ModTime() time.Time {
return m.modTime
}

37
utils/json_test.go Normal file
View File

@ -0,0 +1,37 @@
package utils
import (
"git.sr.ht/~hamcha/containers/sync"
jsoniter "github.com/json-iterator/go"
"testing"
)
func TestLoadJSONToWrapped(t *testing.T) {
// Create test struct and object
type test struct {
TestValue string
}
testObj := test{
TestValue: "test",
}
// Encode test object to JSON
testStr, err := jsoniter.ConfigFastest.MarshalToString(testObj)
if err != nil {
t.Fatal(err)
}
// Create a wrapped instance of the test object
wrapped := sync.NewSync[test](test{})
// Load JSON to wrapped
err = LoadJSONToWrapped[test](testStr, wrapped)
if err != nil {
t.Fatal(err)
}
// Get the wrapped value and compare to original
if wrapped.Get().TestValue != testObj.TestValue {
t.Fatal("JSON was not loaded correctly")
}
}

View File

@ -9,5 +9,7 @@ func MergeMap[T comparable, V any](a, b map[T]V) {
}
func MergeSyncMap[T comparable, V any](a, b *sync.Map[T, V]) {
b.Set(a.Copy())
merged := a.Copy()
MergeMap(merged, b.Copy())
a.Set(merged)
}

54
utils/map_test.go Normal file
View File

@ -0,0 +1,54 @@
package utils
import (
"git.sr.ht/~hamcha/containers/sync"
"testing"
)
func TestMergeMap(t *testing.T) {
// Create two map and merge them.
m1 := map[string]string{
"a": "1",
"b": "2",
"c": "3",
}
m2 := map[string]string{
"a": "4",
"d": "5",
"e": "6",
}
MergeMap(m1, m2)
// Check if the merged map is correct.
if m1["a"] != "4" {
t.Error("MergeMap failed: wrong value in overwritten key")
}
if len(m1) != 5 {
t.Error("MergeMap failed: wrong length")
}
}
func TestMergeSyncMap(t *testing.T) {
// Create two map and merge them.
m1 := sync.NewMap[string, string]()
m1.Set(map[string]string{
"a": "1",
"b": "2",
"c": "3",
})
m2 := sync.NewMap[string, string]()
m2.Set(map[string]string{
"a": "4",
"d": "5",
"e": "6",
})
MergeSyncMap(m1, m2)
// Check if the merged map is correct.
if val, ok := m1.GetKey("a"); !ok || val != "4" {
t.Error("MergeSyncMap failed: wrong value in overwritten key")
}
if len(m1.Copy()) != 5 {
t.Error("MergeSyncMap failed: wrong length")
}
}