namespaced_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package db
  16. import (
  17. "testing"
  18. "time"
  19. "github.com/syndtr/goleveldb/leveldb"
  20. "github.com/syndtr/goleveldb/leveldb/storage"
  21. )
  22. func TestNamespacedInt(t *testing.T) {
  23. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. n1 := NewNamespacedKV(ldb, "foo")
  28. n2 := NewNamespacedKV(ldb, "bar")
  29. // Key is missing to start with
  30. if v, ok := n1.Int64("test"); v != 0 || ok {
  31. t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
  32. }
  33. n1.PutInt64("test", 42)
  34. // It should now exist in n1
  35. if v, ok := n1.Int64("test"); v != 42 || !ok {
  36. t.Errorf("Incorrect return v %v != 42 || ok %v != true", v, ok)
  37. }
  38. // ... but not in n2, which is in a different namespace
  39. if v, ok := n2.Int64("test"); v != 0 || ok {
  40. t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
  41. }
  42. n1.Delete("test")
  43. // It should no longer exist
  44. if v, ok := n1.Int64("test"); v != 0 || ok {
  45. t.Errorf("Incorrect return v %v != 0 || ok %v != false", v, ok)
  46. }
  47. }
  48. func TestNamespacedTime(t *testing.T) {
  49. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. n1 := NewNamespacedKV(ldb, "foo")
  54. if v, ok := n1.Time("test"); v != (time.Time{}) || ok {
  55. t.Errorf("Incorrect return v %v != %v || ok %v != false", v, time.Time{}, ok)
  56. }
  57. now := time.Now()
  58. n1.PutTime("test", now)
  59. if v, ok := n1.Time("test"); v != now || !ok {
  60. t.Errorf("Incorrect return v %v != %v || ok %v != true", v, now, ok)
  61. }
  62. }
  63. func TestNamespacedString(t *testing.T) {
  64. ldb, err := leveldb.Open(storage.NewMemStorage(), nil)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. n1 := NewNamespacedKV(ldb, "foo")
  69. if v, ok := n1.String("test"); v != "" || ok {
  70. t.Errorf("Incorrect return v %q != \"\" || ok %v != false", v, ok)
  71. }
  72. n1.PutString("test", "yo")
  73. if v, ok := n1.String("test"); v != "yo" || !ok {
  74. t.Errorf("Incorrect return v %q != \"yo\" || ok %v != true", v, ok)
  75. }
  76. }