123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package qt
- import (
- "testing"
- "os"
- "path"
- "sort"
- )
- func TestKvdb(t *testing.T) {
- var d_path = path.Join(os.TempDir(), "qtbinding_kvdb_test_012345.db")
- t.Log(d_path)
- var _, s_err = os.Stat(d_path)
- if s_err == nil {
- var err = os.Remove(d_path)
- if err != nil { panic(err) }
- }
- var d, err = KvdbOpen(d_path, true)
- if err != nil { panic(err) }
- defer d.Close()
- var data = map[string] string {
- "a": "1",
- "b": "2",
- "c": "3",
- "foo": "||",
- "uvw": "&&",
- "zzz": "**",
- }
- var data_keys = (func() ([] string) {
- var keys = make([] string, 0)
- for key := range data { keys = append(keys, key) }
- sort.Strings(keys)
- return keys
- })()
- var get_category = func(key string) int32 {
- return int32(len(key))
- }
- // Insert
- for _, key := range data_keys {
- var category = get_category(key)
- var value = ([] byte)(data[key])
- var err = d.Insert(key, category, value)
- if err != nil {
- t.Fatal(err)
- }
- }
- // Remove
- { var err = d.Remove("c"); delete(data, "c")
- if err != nil {
- t.Fatal(err)
- } }
- // Lookup
- { var value, found, err = d.Lookup("b")
- if err != nil {
- t.Fatal(err)
- }
- if !(found) || (string(value) != "2") {
- t.Fatalf("bad Lookup result")
- } }
- // Update
- { var err = d.Update("foo", ([] byte)("!!!!")); data["foo"] = "!!!!"
- if err != nil {
- t.Fatal(err)
- } }
- // ReadAll
- { var q, err = d.ReadAll()
- if err != nil { t.Fatal(err) }
- q.Consume(func(key string, value []byte) {
- var correct_value, exists = data[key]
- if !(exists) || (string(value) != correct_value) {
- t.Fatalf("bad ReadAll result for key '%s'", key)
- }
- t.Log("[all]", key, string(value))
- }) }
- // ReadCategory
- { var q, err = d.ReadCategory(1)
- if err != nil { panic(err) }
- q.Consume(func(key string, value []byte) {
- if get_category(key) != 1 {
- t.Fatalf("bad ReadCategory result")
- }
- t.Log("[1]", key, string(value))
- }) }
- }
|