From adce06420cbe94b91975f82461b5d2cac0c90e19 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 5 Jul 2023 17:23:50 +0200 Subject: [PATCH] Added basic tests --- Collection.go | 33 ++++++++++++++++++++++++++++----- Collection_test.go | 35 +++++++++++++++++++++++++++++++++++ Namespace.go | 15 +++++++++++---- Namespace_test.go | 13 +++++++++++++ README.md | 2 +- go.mod | 2 ++ go.sum | 2 ++ 7 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 Collection_test.go create mode 100644 Namespace_test.go create mode 100644 go.sum diff --git a/Collection.go b/Collection.go index 67be42f..ba7a623 100644 --- a/Collection.go +++ b/Collection.go @@ -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 +} diff --git a/Collection_test.go b/Collection_test.go new file mode 100644 index 0000000..1a83c30 --- /dev/null +++ b/Collection_test.go @@ -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")) +} diff --git a/Namespace.go b/Namespace.go index cdbc387..0d5d6f1 100644 --- a/Namespace.go +++ b/Namespace.go @@ -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 +} diff --git a/Namespace_test.go b/Namespace_test.go new file mode 100644 index 0000000..47f38a4 --- /dev/null +++ b/Namespace_test.go @@ -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) +} diff --git a/README.md b/README.md index f59bb20..69b93cc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # ocean -In-memory key/value store that writes JSON to disk. \ No newline at end of file +In-memory key value store that writes JSON to disk. diff --git a/go.mod b/go.mod index e0ad0ca..6023697 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.akyoto.dev/go/ocean go 1.20 + +require git.akyoto.dev/go/assert v0.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b160cf2 --- /dev/null +++ b/go.sum @@ -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=