namespaced_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package db
  7. import (
  8. "testing"
  9. "time"
  10. "github.com/syndtr/goleveldb/leveldb"
  11. "github.com/syndtr/goleveldb/leveldb/storage"
  12. )
  13. func TestNamespacedInt(t *testing.T) {
  14. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. n1 := NewNamespacedKV(ldb, "foo")
  19. n2 := NewNamespacedKV(ldb, "bar")
  20. // Key is missing to start with
  21. if v, ok := n1.Int64("test"); v != 0 || ok {
  22. t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
  23. }
  24. n1.PutInt64("test", 42)
  25. // It should now exist in n1
  26. if v, ok := n1.Int64("test"); v != 42 || !ok {
  27. t.Errorf("Incorrect return v %v != 42 || ok %v != true", v, ok)
  28. }
  29. // ... but not in n2, which is in a different namespace
  30. if v, ok := n2.Int64("test"); v != 0 || ok {
  31. t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
  32. }
  33. n1.Delete("test")
  34. // It should no longer exist
  35. if v, ok := n1.Int64("test"); v != 0 || ok {
  36. t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
  37. }
  38. }
  39. func TestNamespacedTime(t *testing.T) {
  40. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. n1 := NewNamespacedKV(ldb, "foo")
  45. if v, ok := n1.Time("test"); v != (time.Time{}) || ok {
  46. t.Errorf("Incorrect return v %v != %v || ok %v != false", v, time.Time{}, ok)
  47. }
  48. now := time.Now()
  49. n1.PutTime("test", now)
  50. if v, ok := n1.Time("test"); v != now || !ok {
  51. t.Errorf("Incorrect return v %v != %v || ok %v != true", v, now, ok)
  52. }
  53. }
  54. func TestNamespacedString(t *testing.T) {
  55. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. n1 := NewNamespacedKV(ldb, "foo")
  60. if v, ok := n1.String("test"); v != "" || ok {
  61. t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
  62. }
  63. n1.PutString("test", "yo")
  64. if v, ok := n1.String("test"); v != "yo" || !ok {
  65. t.Errorf("Incorrect return v %q != \"yo\" || ok %v != true", v, ok)
  66. }
  67. }