db_test.go (2194B)
1 package db 2 3 import ( 4 "bytes" 5 "context" 6 "testing" 7 8 "git.defalsify.org/vise.git/lang" 9 ) 10 11 func TestDbBase(t *testing.T) { 12 store := NewDbBase() 13 store.SetPrefix(DATATYPE_STATE) 14 if store.Prefix() != DATATYPE_STATE { 15 t.Fatalf("expected %d, got %d", DATATYPE_STATE, store.Prefix()) 16 } 17 if !store.Safe() { 18 t.Fatal("expected safe") 19 } 20 store.SetLock(DATATYPE_MENU, false) 21 if store.Safe() { 22 t.Fatal("expected unsafe") 23 } 24 store.SetPrefix(DATATYPE_TEMPLATE) 25 if store.CheckPut() { 26 t.Fatal("expected checkput false") 27 } 28 store.SetLock(DATATYPE_TEMPLATE, false) 29 if !store.CheckPut() { 30 t.Fatal("expected checkput true") 31 } 32 } 33 34 func TestDbKeyLanguage(t *testing.T) { 35 ctx := context.Background() 36 store := NewDbBase() 37 store.SetPrefix(DATATYPE_TEMPLATE) 38 l, err := lang.LanguageFromCode("nor") 39 if err != nil { 40 t.Fatal(err) 41 } 42 store.SetLanguage(&l) 43 k, err := store.ToKey(ctx, []byte("foo")) 44 if err != nil { 45 t.Fatal(err) 46 } 47 v := append([]byte{DATATYPE_TEMPLATE}, []byte("foo")...) 48 if !bytes.Equal(k.Default, v) { 49 t.Fatalf("expected %x, got %x", v, k.Default) 50 } 51 v = append(v, []byte("_nor")...) 52 if !bytes.Equal(k.Translation, v) { 53 t.Fatalf("expected %x, got %x", v, k.Translation) 54 } 55 } 56 57 func TestDbKeyNALanguage(t *testing.T) { 58 ctx := context.Background() 59 store := NewDbBase() 60 store.SetPrefix(DATATYPE_STATE) 61 l, err := lang.LanguageFromCode("nor") 62 if err != nil { 63 t.Fatal(err) 64 } 65 store.SetLanguage(&l) 66 k, err := store.ToKey(ctx, []byte("foo")) 67 if err != nil { 68 t.Fatal(err) 69 } 70 v := append([]byte{DATATYPE_STATE}, []byte("foo")...) 71 if !bytes.Equal(k.Default, v) { 72 t.Fatalf("expected %x, got %x", v, k.Default) 73 } 74 if len(k.Translation) != 0 { 75 t.Fatalf("expected no translation key, got %x", k.Translation) 76 } 77 78 } 79 80 func TestDbKeyNoLanguage(t *testing.T) { 81 ctx := context.Background() 82 store := NewDbBase() 83 store.SetPrefix(DATATYPE_TEMPLATE) 84 k, err := store.ToKey(ctx, []byte("foo")) 85 if err != nil { 86 t.Fatal(err) 87 } 88 v := append([]byte{DATATYPE_TEMPLATE}, []byte("foo")...) 89 if !bytes.Equal(k.Default, v) { 90 t.Fatalf("expected %x, got %x", v, k.Default) 91 } 92 if len(k.Translation) != 0 { 93 t.Fatalf("expected no translation key, got %x", k.Translation) 94 } 95 96 }