field_string_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package shed
  17. import (
  18. "testing"
  19. "github.com/syndtr/goleveldb/leveldb"
  20. )
  21. // TestStringField validates put and get operations
  22. // of the StringField.
  23. func TestStringField(t *testing.T) {
  24. db, cleanupFunc := newTestDB(t)
  25. defer cleanupFunc()
  26. simpleString, err := db.NewStringField("simple-string")
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. t.Run("get empty", func(t *testing.T) {
  31. got, err := simpleString.Get()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. want := ""
  36. if got != want {
  37. t.Errorf("got string %q, want %q", got, want)
  38. }
  39. })
  40. t.Run("put", func(t *testing.T) {
  41. want := "simple string value"
  42. err = simpleString.Put(want)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. got, err := simpleString.Get()
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. if got != want {
  51. t.Errorf("got string %q, want %q", got, want)
  52. }
  53. t.Run("overwrite", func(t *testing.T) {
  54. want := "overwritten string value"
  55. err = simpleString.Put(want)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. got, err := simpleString.Get()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if got != want {
  64. t.Errorf("got string %q, want %q", got, want)
  65. }
  66. })
  67. })
  68. t.Run("put in batch", func(t *testing.T) {
  69. batch := new(leveldb.Batch)
  70. want := "simple string batch value"
  71. simpleString.PutInBatch(batch, want)
  72. err = db.WriteBatch(batch)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. got, err := simpleString.Get()
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. if got != want {
  81. t.Errorf("got string %q, want %q", got, want)
  82. }
  83. t.Run("overwrite", func(t *testing.T) {
  84. batch := new(leveldb.Batch)
  85. want := "overwritten string batch value"
  86. simpleString.PutInBatch(batch, want)
  87. err = db.WriteBatch(batch)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. got, err := simpleString.Get()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if got != want {
  96. t.Errorf("got string %q, want %q", got, want)
  97. }
  98. })
  99. })
  100. }