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