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