github.com/ethersphere/bee/v2@v2.2.0/pkg/shed/field_string_test.go (about) 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 17 package shed 18 19 import ( 20 "testing" 21 22 "github.com/syndtr/goleveldb/leveldb" 23 ) 24 25 // TestStringField validates put and get operations 26 // of the StringField. 27 func TestStringField(t *testing.T) { 28 t.Parallel() 29 30 t.Run("get empty", func(t *testing.T) { 31 t.Parallel() 32 33 db := newTestDB(t) 34 simpleString, err := db.NewStringField("simple-string") 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 got, err := simpleString.Get() 40 if err != nil { 41 t.Fatal(err) 42 } 43 want := "" 44 if got != want { 45 t.Errorf("got string %q, want %q", got, want) 46 } 47 }) 48 49 t.Run("put", func(t *testing.T) { 50 t.Parallel() 51 52 db := newTestDB(t) 53 simpleString, err := db.NewStringField("simple-string") 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 want := "simple string value" 59 err = simpleString.Put(want) 60 if err != nil { 61 t.Fatal(err) 62 } 63 got, err := simpleString.Get() 64 if err != nil { 65 t.Fatal(err) 66 } 67 if got != want { 68 t.Errorf("got string %q, want %q", got, want) 69 } 70 71 t.Run("overwrite", func(t *testing.T) { 72 t.Parallel() 73 74 want := "overwritten string value" 75 err = simpleString.Put(want) 76 if err != nil { 77 t.Fatal(err) 78 } 79 got, err := simpleString.Get() 80 if err != nil { 81 t.Fatal(err) 82 } 83 if got != want { 84 t.Errorf("got string %q, want %q", got, want) 85 } 86 }) 87 }) 88 89 t.Run("put in batch", func(t *testing.T) { 90 t.Parallel() 91 92 db := newTestDB(t) 93 simpleString, err := db.NewStringField("simple-string") 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 batch := new(leveldb.Batch) 99 want := "simple string batch value" 100 simpleString.PutInBatch(batch, want) 101 err = db.WriteBatch(batch) 102 if err != nil { 103 t.Fatal(err) 104 } 105 got, err := simpleString.Get() 106 if err != nil { 107 t.Fatal(err) 108 } 109 if got != want { 110 t.Errorf("got string %q, want %q", got, want) 111 } 112 113 t.Run("overwrite", func(t *testing.T) { 114 t.Parallel() 115 116 batch := new(leveldb.Batch) 117 want := "overwritten string batch value" 118 simpleString.PutInBatch(batch, want) 119 err = db.WriteBatch(batch) 120 if err != nil { 121 t.Fatal(err) 122 } 123 got, err := simpleString.Get() 124 if err != nil { 125 t.Fatal(err) 126 } 127 if got != want { 128 t.Errorf("got string %q, want %q", got, want) 129 } 130 }) 131 }) 132 }