github.com/maynardminer/ethereumprogpow@v1.8.23/swarm/shed/schema_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  	"bytes"
    21  	"testing"
    22  )
    23  
    24  // TestDB_schemaFieldKey validates correctness of schemaFieldKey.
    25  func TestDB_schemaFieldKey(t *testing.T) {
    26  	db, cleanupFunc := newTestDB(t)
    27  	defer cleanupFunc()
    28  
    29  	t.Run("empty name or type", func(t *testing.T) {
    30  		_, err := db.schemaFieldKey("", "")
    31  		if err == nil {
    32  			t.Errorf("error not returned, but expected")
    33  		}
    34  		_, err = db.schemaFieldKey("", "type")
    35  		if err == nil {
    36  			t.Errorf("error not returned, but expected")
    37  		}
    38  
    39  		_, err = db.schemaFieldKey("test", "")
    40  		if err == nil {
    41  			t.Errorf("error not returned, but expected")
    42  		}
    43  	})
    44  
    45  	t.Run("same field", func(t *testing.T) {
    46  		key1, err := db.schemaFieldKey("test", "undefined")
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  
    51  		key2, err := db.schemaFieldKey("test", "undefined")
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  
    56  		if !bytes.Equal(key1, key2) {
    57  			t.Errorf("schema keys for the same field name are not the same: %q, %q", string(key1), string(key2))
    58  		}
    59  	})
    60  
    61  	t.Run("different fields", func(t *testing.T) {
    62  		key1, err := db.schemaFieldKey("test1", "undefined")
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  
    67  		key2, err := db.schemaFieldKey("test2", "undefined")
    68  		if err != nil {
    69  			t.Fatal(err)
    70  		}
    71  
    72  		if bytes.Equal(key1, key2) {
    73  			t.Error("schema keys for the same field name are the same, but must not be")
    74  		}
    75  	})
    76  
    77  	t.Run("same field name different types", func(t *testing.T) {
    78  		_, err := db.schemaFieldKey("the-field", "one-type")
    79  		if err != nil {
    80  			t.Fatal(err)
    81  		}
    82  
    83  		_, err = db.schemaFieldKey("the-field", "another-type")
    84  		if err == nil {
    85  			t.Errorf("error not returned, but expected")
    86  		}
    87  	})
    88  }
    89  
    90  // TestDB_schemaIndexPrefix validates correctness of schemaIndexPrefix.
    91  func TestDB_schemaIndexPrefix(t *testing.T) {
    92  	db, cleanupFunc := newTestDB(t)
    93  	defer cleanupFunc()
    94  
    95  	t.Run("same name", func(t *testing.T) {
    96  		id1, err := db.schemaIndexPrefix("test")
    97  		if err != nil {
    98  			t.Fatal(err)
    99  		}
   100  
   101  		id2, err := db.schemaIndexPrefix("test")
   102  		if err != nil {
   103  			t.Fatal(err)
   104  		}
   105  
   106  		if id1 != id2 {
   107  			t.Errorf("schema keys for the same field name are not the same: %v, %v", id1, id2)
   108  		}
   109  	})
   110  
   111  	t.Run("different names", func(t *testing.T) {
   112  		id1, err := db.schemaIndexPrefix("test1")
   113  		if err != nil {
   114  			t.Fatal(err)
   115  		}
   116  
   117  		id2, err := db.schemaIndexPrefix("test2")
   118  		if err != nil {
   119  			t.Fatal(err)
   120  		}
   121  
   122  		if id1 == id2 {
   123  			t.Error("schema ids for the same index name are the same, but must not be")
   124  		}
   125  	})
   126  }