github.com/ethersphere/bee/v2@v2.2.0/pkg/shed/db_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/ethersphere/bee/v2/pkg/util/testutil"
    23  )
    24  
    25  // TestNewDB constructs a new DB
    26  // and validates if the schema is initialized properly.
    27  func TestNewDB(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	db := newTestDB(t)
    31  
    32  	s, err := db.getSchema()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	if s.Fields == nil {
    37  		t.Error("schema fields are empty")
    38  	}
    39  	if len(s.Fields) != 0 {
    40  		t.Errorf("got schema fields length %v, want %v", len(s.Fields), 0)
    41  	}
    42  	if s.Indexes == nil {
    43  		t.Error("schema indexes are empty")
    44  	}
    45  	if len(s.Indexes) != 0 {
    46  		t.Errorf("got schema indexes length %v, want %v", len(s.Indexes), 0)
    47  	}
    48  }
    49  
    50  // TestDB_persistence creates one DB, saves a field and closes that DB.
    51  // Then, it constructs another DB and tries to retrieve the saved value.
    52  func TestDB_persistence(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	dir := t.TempDir()
    56  
    57  	db, err := NewDB(dir, nil)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	stringField, err := db.NewStringField("preserve-me")
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	want := "persistent value"
    66  	err = stringField.Put(want)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	err = db.Close()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	db2, err := NewDB(dir, nil)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	stringField2, err := db2.NewStringField("preserve-me")
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	got, err := stringField2.Get()
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	if got != want {
    88  		t.Errorf("got string %q, want %q", got, want)
    89  	}
    90  	err = db2.Close()
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  }
    95  
    96  // newTestDB is a helper function that constructs a
    97  // temporary database and returns a cleanup function that must
    98  // be called to remove the data.
    99  func newTestDB(t *testing.T) *DB {
   100  	t.Helper()
   101  	db, err := NewDB("", nil)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	testutil.CleanupCloser(t, db)
   106  
   107  	return db
   108  }