github.com/maynardminer/ethereumprogpow@v1.8.23/swarm/shed/field_string_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/syndtr/goleveldb/leveldb"
    23  )
    24  
    25  // TestStringField validates put and get operations
    26  // of the StringField.
    27  func TestStringField(t *testing.T) {
    28  	db, cleanupFunc := newTestDB(t)
    29  	defer cleanupFunc()
    30  
    31  	simpleString, err := db.NewStringField("simple-string")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	t.Run("get empty", func(t *testing.T) {
    37  		got, err := simpleString.Get()
    38  		if err != nil {
    39  			t.Fatal(err)
    40  		}
    41  		want := ""
    42  		if got != want {
    43  			t.Errorf("got string %q, want %q", got, want)
    44  		}
    45  	})
    46  
    47  	t.Run("put", func(t *testing.T) {
    48  		want := "simple string value"
    49  		err = simpleString.Put(want)
    50  		if err != nil {
    51  			t.Fatal(err)
    52  		}
    53  		got, err := simpleString.Get()
    54  		if err != nil {
    55  			t.Fatal(err)
    56  		}
    57  		if got != want {
    58  			t.Errorf("got string %q, want %q", got, want)
    59  		}
    60  
    61  		t.Run("overwrite", func(t *testing.T) {
    62  			want := "overwritten string value"
    63  			err = simpleString.Put(want)
    64  			if err != nil {
    65  				t.Fatal(err)
    66  			}
    67  			got, err := simpleString.Get()
    68  			if err != nil {
    69  				t.Fatal(err)
    70  			}
    71  			if got != want {
    72  				t.Errorf("got string %q, want %q", got, want)
    73  			}
    74  		})
    75  	})
    76  
    77  	t.Run("put in batch", func(t *testing.T) {
    78  		batch := new(leveldb.Batch)
    79  		want := "simple string batch value"
    80  		simpleString.PutInBatch(batch, want)
    81  		err = db.WriteBatch(batch)
    82  		if err != nil {
    83  			t.Fatal(err)
    84  		}
    85  		got, err := simpleString.Get()
    86  		if err != nil {
    87  			t.Fatal(err)
    88  		}
    89  		if got != want {
    90  			t.Errorf("got string %q, want %q", got, want)
    91  		}
    92  
    93  		t.Run("overwrite", func(t *testing.T) {
    94  			batch := new(leveldb.Batch)
    95  			want := "overwritten string batch value"
    96  			simpleString.PutInBatch(batch, want)
    97  			err = db.WriteBatch(batch)
    98  			if err != nil {
    99  				t.Fatal(err)
   100  			}
   101  			got, err := simpleString.Get()
   102  			if err != nil {
   103  				t.Fatal(err)
   104  			}
   105  			if got != want {
   106  				t.Errorf("got string %q, want %q", got, want)
   107  			}
   108  		})
   109  	})
   110  }