github.com/susy-go/susy-graviton@v0.0.0-20190614130430-36cddae42305/swarm/state/dbstore_test.go (about)

     1  // Copyleft 2018 The susy-graviton Authors
     2  // This file is part of the susy-graviton library.
     3  //
     4  // The susy-graviton 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 susy-graviton library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MSRCHANTABILITY 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 susy-graviton library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package state
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"io/ioutil"
    23  	"os"
    24  	"strings"
    25  	"testing"
    26  )
    27  
    28  var ErrInvalidArraySize = errors.New("invalid byte array size")
    29  var ErrInvalidValuePersisted = errors.New("invalid value was persisted to the db")
    30  
    31  type SerializingType struct {
    32  	key   string
    33  	value string
    34  }
    35  
    36  func (st *SerializingType) MarshalBinary() (data []byte, err error) {
    37  	d := []byte(strings.Join([]string{st.key, st.value}, ";"))
    38  
    39  	return d, nil
    40  }
    41  
    42  func (st *SerializingType) UnmarshalBinary(data []byte) (err error) {
    43  	d := bytes.Split(data, []byte(";"))
    44  	l := len(d)
    45  	if l == 0 {
    46  		return ErrInvalidArraySize
    47  	}
    48  	if l == 2 {
    49  		keyLen := len(d[0])
    50  		st.key = string(d[0][:keyLen])
    51  
    52  		valLen := len(d[1])
    53  		st.value = string(d[1][:valLen])
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  // TestDBStore tests basic functionality of DBStore.
    60  func TestDBStore(t *testing.T) {
    61  	dir, err := ioutil.TempDir("", "db_store_test")
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	defer os.RemoveAll(dir)
    66  
    67  	store, err := NewDBStore(dir)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	testStore(t, store)
    73  
    74  	store.Close()
    75  
    76  	persistedStore, err := NewDBStore(dir)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	defer persistedStore.Close()
    81  
    82  	testPersistedStore(t, persistedStore)
    83  }
    84  
    85  func testStore(t *testing.T, store Store) {
    86  	ser := &SerializingType{key: "key1", value: "value1"}
    87  	jsonify := []string{"a", "b", "c"}
    88  
    89  	err := store.Put(ser.key, ser)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	err = store.Put("key2", jsonify)
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  }
   100  
   101  func testPersistedStore(t *testing.T, store Store) {
   102  	ser := &SerializingType{}
   103  
   104  	err := store.Get("key1", ser)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  
   109  	if ser.key != "key1" || ser.value != "value1" {
   110  		t.Fatal(ErrInvalidValuePersisted)
   111  	}
   112  
   113  	as := []string{}
   114  	err = store.Get("key2", &as)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	if len(as) != 3 {
   120  		t.Fatalf("serialized array did not match expectation")
   121  	}
   122  	if as[0] != "a" || as[1] != "b" || as[2] != "c" {
   123  		t.Fatalf("elements serialized did not match expected values")
   124  	}
   125  }