commit cab74c82992d31cad0c3fffd4b0087754e944101
parent 8f06e6a16fbf3b2500c651a5a24d95b60dc895a1
Author: lash <dev@holbrook.no>
Date: Wed, 11 Sep 2024 04:02:53 +0100
Reach over 80% coverage in basedb packagE
Diffstat:
M | db/db.go | | | 2 | +- |
M | db/db_test.go | | | 91 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
2 files changed, 88 insertions(+), 5 deletions(-)
diff --git a/db/db.go b/db/db.go
@@ -8,7 +8,7 @@ import (
)
const (
- safeLock =DATATYPE_BIN | DATATYPE_MENU | DATATYPE_TEMPLATE | DATATYPE_STATICLOAD
+ safeLock = DATATYPE_BIN | DATATYPE_MENU | DATATYPE_TEMPLATE | DATATYPE_STATICLOAD
)
const (
diff --git a/db/db_test.go b/db/db_test.go
@@ -1,13 +1,96 @@
package db
+import (
+ "bytes"
+ "context"
+ "testing"
+
+ "git.defalsify.org/vise.git/lang"
+)
+
func TestDbBase(t *testing.T) {
store := NewDbBase()
- store.SetPrefix(USERDATA_STATE)
- if !store.Prefix() == USERDATA_STATE {
- t.Fatal("expected %d, got %d", USERDATA_STATE, store.Prefix())
+ store.SetPrefix(DATATYPE_STATE)
+ if store.Prefix() != DATATYPE_STATE {
+ t.Fatalf("expected %d, got %d", DATATYPE_STATE, store.Prefix())
+ }
+ if !store.Safe() {
+ t.Fatal("expected safe")
+ }
+ store.SetLock(DATATYPE_MENU, false)
+ if store.Safe() {
+ t.Fatal("expected unsafe")
+ }
+ store.SetPrefix(DATATYPE_TEMPLATE)
+ if store.CheckPut() {
+ t.Fatal("expected checkput false")
+ }
+ store.SetLock(DATATYPE_TEMPLATE, false)
+ if !store.CheckPut() {
+ t.Fatal("expected checkput true")
+ }
+}
+
+func TestDbKeyLanguage(t *testing.T) {
+ ctx := context.Background()
+ store := NewDbBase()
+ store.SetPrefix(DATATYPE_TEMPLATE)
+ l, err := lang.LanguageFromCode("nor")
+ if err != nil {
+ t.Fatal(err)
}
- l, err := store.SetLanguage(lang.LanguageFromCode("nor"))
+ store.SetLanguage(&l)
+ k, err := store.ToKey(ctx, []byte("foo"))
if err != nil {
t.Fatal(err)
}
+ v := append([]byte{DATATYPE_TEMPLATE}, []byte("foo")...)
+ if !bytes.Equal(k.Default, v) {
+ t.Fatalf("expected %x, got %x", v, k.Default)
+ }
+ v = append(v, []byte("_nor")...)
+ if !bytes.Equal(k.Translation, v) {
+ t.Fatalf("expected %x, got %x", v, k.Translation)
+ }
+}
+
+func TestDbKeyNALanguage(t *testing.T) {
+ ctx := context.Background()
+ store := NewDbBase()
+ store.SetPrefix(DATATYPE_STATE)
+ l, err := lang.LanguageFromCode("nor")
+ if err != nil {
+ t.Fatal(err)
+ }
+ store.SetLanguage(&l)
+ k, err := store.ToKey(ctx, []byte("foo"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ v := append([]byte{DATATYPE_STATE}, []byte("foo")...)
+ if !bytes.Equal(k.Default, v) {
+ t.Fatalf("expected %x, got %x", v, k.Default)
+ }
+ if len(k.Translation) != 0 {
+ t.Fatalf("expected no translation key, got %x", k.Translation)
+ }
+
+}
+
+func TestDbKeyNoLanguage(t *testing.T) {
+ ctx := context.Background()
+ store := NewDbBase()
+ store.SetPrefix(DATATYPE_TEMPLATE)
+ k, err := store.ToKey(ctx, []byte("foo"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ v := append([]byte{DATATYPE_TEMPLATE}, []byte("foo")...)
+ if !bytes.Equal(k.Default, v) {
+ t.Fatalf("expected %x, got %x", v, k.Default)
+ }
+ if len(k.Translation) != 0 {
+ t.Fatalf("expected no translation key, got %x", k.Translation)
+ }
+
}