Added basic tests

This commit is contained in:
Eduard Urbach 2023-07-05 17:23:50 +02:00
parent 4e93f3a2d0
commit adce06420c
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
7 changed files with 92 additions and 10 deletions

View File

@ -3,17 +3,40 @@ package ocean
import "sync"
type Collection interface {
Get(key string)
Get(key string) (value any, ok bool)
}
// collection is a hash map of homogeneous data.
type collection struct {
data sync.Map
name string
}
func TestNewCollection() {
users := collection{}
users.name = "User"
users.data.Store("1", "Test")
// Force interface implementation
var _ Collection = (*collection)(nil)
// NewCollection creates a new collection with the given name.
func NewCollection(name string) *collection {
return &collection{name: name}
}
// Get returns the value for the given key.
func (c *collection) Get(key string) (value any, ok bool) {
return c.data.Load(key)
}
// Set sets the value for the given key.
func (c *collection) Set(key string, value any) {
c.data.Store(key, value)
}
// Delete deletes a key from the collection.
func (c *collection) Delete(key string) {
c.data.Delete(key)
}
// Exists returns whether or not the key exists.
func (c *collection) Exists(key string) bool {
_, exists := c.data.Load(key)
return exists
}

35
Collection_test.go Normal file
View File

@ -0,0 +1,35 @@
package ocean_test
import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/ocean"
)
func TestNewCollection(t *testing.T) {
users := ocean.NewCollection("User")
assert.False(t, users.Exists("Hello"))
assert.False(t, users.Exists("World"))
users.Set("Hello", "Hello data")
assert.True(t, users.Exists("Hello"))
assert.False(t, users.Exists("World"))
users.Set("World", "World data")
assert.True(t, users.Exists("Hello"))
assert.True(t, users.Exists("World"))
users.Delete("Hello")
assert.False(t, users.Exists("Hello"))
assert.True(t, users.Exists("World"))
users.Delete("World")
assert.False(t, users.Exists("Hello"))
assert.False(t, users.Exists("World"))
}

View File

@ -1,15 +1,22 @@
package ocean
type Namespace interface {
Collection(name string)
}
// Force interface implementation
var _ Namespace = (*namespace)(nil)
// namespaces groups multiple collections under a common name.
type namespace struct {
name string
}
func TestNewNamespace() {
ns := namespace{}
ns.name = "test"
// NewNamespace creates a new namespace to group multiple collections under a common name.
func NewNamespace(name string) *namespace {
return &namespace{name: name}
}
// Count counts the number of collections in the namespace.
func (ns *namespace) Count() int {
return 0
}

13
Namespace_test.go Normal file
View File

@ -0,0 +1,13 @@
package ocean_test
import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/ocean"
)
func TestNewNamespace(t *testing.T) {
ns := ocean.NewNamespace("test")
assert.Equal(t, ns.Count(), 0)
}

View File

@ -1,3 +1,3 @@
# ocean
In-memory key/value store that writes JSON to disk.
In-memory key value store that writes JSON to disk.

2
go.mod
View File

@ -1,3 +1,5 @@
module git.akyoto.dev/go/ocean
go 1.20
require git.akyoto.dev/go/assert v0.1.0

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
git.akyoto.dev/go/assert v0.1.0 h1:+DRuXKk4QdNcqJYlQizQhOr5DKGi5/HQMNCOQUWxIDU=
git.akyoto.dev/go/assert v0.1.0/go.mod h1:Zr/UFuiqmqRmFFgpBGwF71jbzb6iYJfXFeePYHGtWsg=