github.com/susy-go/susy-graviton@v0.0.0-20190614130430-36cddae42305/swarm/shed/field_struct_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 shed
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/syndtr/goleveldb/leveldb"
    23  )
    24  
    25  // TestStructField validates put and get operations
    26  // of the StructField.
    27  func TestStructField(t *testing.T) {
    28  	db, cleanupFunc := newTestDB(t)
    29  	defer cleanupFunc()
    30  
    31  	complexField, err := db.NewStructField("complex-field")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	type complexStructure struct {
    37  		A string
    38  	}
    39  
    40  	t.Run("get empty", func(t *testing.T) {
    41  		var s complexStructure
    42  		err := complexField.Get(&s)
    43  		if err != leveldb.ErrNotFound {
    44  			t.Fatalf("got error %v, want %v", err, leveldb.ErrNotFound)
    45  		}
    46  		want := ""
    47  		if s.A != want {
    48  			t.Errorf("got string %q, want %q", s.A, want)
    49  		}
    50  	})
    51  
    52  	t.Run("put", func(t *testing.T) {
    53  		want := complexStructure{
    54  			A: "simple string value",
    55  		}
    56  		err = complexField.Put(want)
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  		var got complexStructure
    61  		err = complexField.Get(&got)
    62  		if err != nil {
    63  			t.Fatal(err)
    64  		}
    65  		if got.A != want.A {
    66  			t.Errorf("got string %q, want %q", got.A, want.A)
    67  		}
    68  
    69  		t.Run("overwrite", func(t *testing.T) {
    70  			want := complexStructure{
    71  				A: "overwritten string value",
    72  			}
    73  			err = complexField.Put(want)
    74  			if err != nil {
    75  				t.Fatal(err)
    76  			}
    77  			var got complexStructure
    78  			err = complexField.Get(&got)
    79  			if err != nil {
    80  				t.Fatal(err)
    81  			}
    82  			if got.A != want.A {
    83  				t.Errorf("got string %q, want %q", got.A, want.A)
    84  			}
    85  		})
    86  	})
    87  
    88  	t.Run("put in batch", func(t *testing.T) {
    89  		batch := new(leveldb.Batch)
    90  		want := complexStructure{
    91  			A: "simple string batch value",
    92  		}
    93  		complexField.PutInBatch(batch, want)
    94  		err = db.WriteBatch(batch)
    95  		if err != nil {
    96  			t.Fatal(err)
    97  		}
    98  		var got complexStructure
    99  		err := complexField.Get(&got)
   100  		if err != nil {
   101  			t.Fatal(err)
   102  		}
   103  		if got.A != want.A {
   104  			t.Errorf("got string %q, want %q", got, want)
   105  		}
   106  
   107  		t.Run("overwrite", func(t *testing.T) {
   108  			batch := new(leveldb.Batch)
   109  			want := complexStructure{
   110  				A: "overwritten string batch value",
   111  			}
   112  			complexField.PutInBatch(batch, want)
   113  			err = db.WriteBatch(batch)
   114  			if err != nil {
   115  				t.Fatal(err)
   116  			}
   117  			var got complexStructure
   118  			err := complexField.Get(&got)
   119  			if err != nil {
   120  				t.Fatal(err)
   121  			}
   122  			if got.A != want.A {
   123  				t.Errorf("got string %q, want %q", got, want)
   124  			}
   125  		})
   126  	})
   127  }