github.com/ethersphere/bee/v2@v2.2.0/pkg/shed/schema_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 "bytes" 21 "testing" 22 ) 23 24 // TestDB_schemaFieldKey validates correctness of schemaFieldKey. 25 func TestDB_schemaFieldKey(t *testing.T) { 26 t.Parallel() 27 28 t.Run("empty name or type", func(t *testing.T) { 29 t.Parallel() 30 31 db := newTestDB(t) 32 _, err := db.schemaFieldKey("", "") 33 if err == nil { 34 t.Error("error not returned, but expected") 35 } 36 _, err = db.schemaFieldKey("", "type") 37 if err == nil { 38 t.Error("error not returned, but expected") 39 } 40 41 _, err = db.schemaFieldKey("test", "") 42 if err == nil { 43 t.Error("error not returned, but expected") 44 } 45 }) 46 47 t.Run("same field", func(t *testing.T) { 48 t.Parallel() 49 50 db := newTestDB(t) 51 key1, err := db.schemaFieldKey("test", "undefined") 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 key2, err := db.schemaFieldKey("test", "undefined") 57 if err != nil { 58 t.Fatal(err) 59 } 60 61 if !bytes.Equal(key1, key2) { 62 t.Errorf("schema keys for the same field name are not the same: %q, %q", string(key1), string(key2)) 63 } 64 }) 65 66 t.Run("different fields", func(t *testing.T) { 67 t.Parallel() 68 69 db := newTestDB(t) 70 key1, err := db.schemaFieldKey("test1", "undefined") 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 key2, err := db.schemaFieldKey("test2", "undefined") 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 if bytes.Equal(key1, key2) { 81 t.Error("schema keys for the same field name are the same, but must not be") 82 } 83 }) 84 85 t.Run("same field name different types", func(t *testing.T) { 86 t.Parallel() 87 88 db := newTestDB(t) 89 _, err := db.schemaFieldKey("the-field", "one-type") 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 _, err = db.schemaFieldKey("the-field", "another-type") 95 if err == nil { 96 t.Error("error not returned, but expected") 97 } 98 }) 99 } 100 101 // TestDB_schemaIndexPrefix validates correctness of schemaIndexPrefix. 102 func TestDB_schemaIndexPrefix(t *testing.T) { 103 t.Parallel() 104 105 t.Run("same name", func(t *testing.T) { 106 t.Parallel() 107 108 db := newTestDB(t) 109 id1, err := db.schemaIndexPrefix("test") 110 if err != nil { 111 t.Fatal(err) 112 } 113 114 id2, err := db.schemaIndexPrefix("test") 115 if err != nil { 116 t.Fatal(err) 117 } 118 119 if id1 != id2 { 120 t.Errorf("schema keys for the same field name are not the same: %v, %v", id1, id2) 121 } 122 }) 123 124 t.Run("different names", func(t *testing.T) { 125 t.Parallel() 126 127 db := newTestDB(t) 128 id1, err := db.schemaIndexPrefix("test1") 129 if err != nil { 130 t.Fatal(err) 131 } 132 133 id2, err := db.schemaIndexPrefix("test2") 134 if err != nil { 135 t.Fatal(err) 136 } 137 138 if id1 == id2 { 139 t.Error("schema ids for the same index name are the same, but must not be") 140 } 141 }) 142 } 143 144 // TestDB_RenameIndex checks if index name is correctly changed. 145 func TestDB_RenameIndex(t *testing.T) { 146 t.Parallel() 147 148 t.Run("empty names", func(t *testing.T) { 149 t.Parallel() 150 151 db := newTestDB(t) 152 153 // empty names 154 renamed, err := db.RenameIndex("", "") 155 if err == nil { 156 t.Error("error not returned, but expected") 157 } 158 if renamed { 159 t.Fatal("index should not be renamed") 160 } 161 162 // empty index name 163 renamed, err = db.RenameIndex("", "new") 164 if err == nil { 165 t.Error("error not returned, but expected") 166 } 167 if renamed { 168 t.Fatal("index should not be renamed") 169 } 170 171 // empty new index name 172 renamed, err = db.RenameIndex("current", "") 173 if err == nil { 174 t.Error("error not returned, but expected") 175 } 176 if renamed { 177 t.Fatal("index should not be renamed") 178 } 179 }) 180 181 t.Run("same names", func(t *testing.T) { 182 t.Parallel() 183 184 db := newTestDB(t) 185 186 renamed, err := db.RenameIndex("index1", "index1") 187 if err != nil { 188 t.Error(err) 189 } 190 if renamed { 191 t.Fatal("index should not be renamed") 192 } 193 }) 194 195 t.Run("unknown name", func(t *testing.T) { 196 t.Parallel() 197 198 db := newTestDB(t) 199 200 renamed, err := db.RenameIndex("index1", "index1new") 201 if err != nil { 202 t.Error(err) 203 } 204 if renamed { 205 t.Fatal("index should not be renamed") 206 } 207 }) 208 209 t.Run("valid names", func(t *testing.T) { 210 t.Parallel() 211 212 db := newTestDB(t) 213 214 // initial indexes 215 key1, err := db.schemaIndexPrefix("index1") 216 if err != nil { 217 t.Fatal(err) 218 } 219 key2, err := db.schemaIndexPrefix("index2") 220 if err != nil { 221 t.Fatal(err) 222 } 223 224 // name the first one 225 renamed, err := db.RenameIndex("index1", "index1new") 226 if err != nil { 227 t.Fatal(err) 228 } 229 if !renamed { 230 t.Fatal("index not renamed") 231 } 232 233 // validate that the index key stays the same 234 key1same, err := db.schemaIndexPrefix("index1new") 235 if err != nil { 236 t.Fatal(err) 237 } 238 if key1 != key1same { 239 t.Fatal("indexes renamed, but keys are not the same") 240 } 241 242 // validate that the independent index is not changed 243 key2same, err := db.schemaIndexPrefix("index2") 244 if err != nil { 245 t.Fatal(err) 246 } 247 if key2 != key2same { 248 t.Fatal("independent index key has changed") 249 } 250 251 // validate that it is safe to create a new index with previous name 252 key1renew, err := db.schemaIndexPrefix("index1") 253 if err != nil { 254 t.Fatal(err) 255 } 256 if key1 == key1renew { 257 t.Fatal("renewed index and the original one have the same key") 258 } 259 if key2 == key1renew { 260 t.Fatal("renewed index and the independent one have the same key") 261 } 262 }) 263 }