kvdb_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package qt
  2. import (
  3. "testing"
  4. "os"
  5. "path"
  6. "sort"
  7. )
  8. func TestKvdb(t *testing.T) {
  9. var d_path = path.Join(os.TempDir(), "qtbinding_kvdb_test_012345.db")
  10. t.Log(d_path)
  11. var _, s_err = os.Stat(d_path)
  12. if s_err == nil {
  13. var err = os.Remove(d_path)
  14. if err != nil { panic(err) }
  15. }
  16. var d, err = KvdbOpen(d_path, true)
  17. if err != nil { panic(err) }
  18. defer d.Close()
  19. var data = map[string] string {
  20. "a": "1",
  21. "b": "2",
  22. "c": "3",
  23. "foo": "||",
  24. "uvw": "&&",
  25. "zzz": "**",
  26. }
  27. var data_keys = (func() ([] string) {
  28. var keys = make([] string, 0)
  29. for key := range data { keys = append(keys, key) }
  30. sort.Strings(keys)
  31. return keys
  32. })()
  33. var get_category = func(key string) int32 {
  34. return int32(len(key))
  35. }
  36. // Insert
  37. for _, key := range data_keys {
  38. var category = get_category(key)
  39. var value = ([] byte)(data[key])
  40. var err = d.Insert(key, category, value)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. }
  45. // Remove
  46. { var err = d.Remove("c"); delete(data, "c")
  47. if err != nil {
  48. t.Fatal(err)
  49. } }
  50. // Lookup
  51. { var value, found, err = d.Lookup("b")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if !(found) || (string(value) != "2") {
  56. t.Fatalf("bad Lookup result")
  57. } }
  58. // Update
  59. { var err = d.Update("foo", ([] byte)("!!!!")); data["foo"] = "!!!!"
  60. if err != nil {
  61. t.Fatal(err)
  62. } }
  63. // ReadAll
  64. { var q, err = d.ReadAll()
  65. if err != nil { t.Fatal(err) }
  66. q.Consume(func(key string, value []byte) {
  67. var correct_value, exists = data[key]
  68. if !(exists) || (string(value) != correct_value) {
  69. t.Fatalf("bad ReadAll result for key '%s'", key)
  70. }
  71. t.Log("[all]", key, string(value))
  72. }) }
  73. // ReadCategory
  74. { var q, err = d.ReadCategory(1)
  75. if err != nil { panic(err) }
  76. q.Consume(func(key string, value []byte) {
  77. if get_category(key) != 1 {
  78. t.Fatalf("bad ReadCategory result")
  79. }
  80. t.Log("[1]", key, string(value))
  81. }) }
  82. }