github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/shed/schema_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:44</date>
    10  //</624450118094950400>
    11  
    12  
    13  package shed
    14  
    15  import (
    16  	"bytes"
    17  	"testing"
    18  )
    19  
    20  //testdb_schemafieldkey验证schemafieldkey的正确性。
    21  func TestDB_schemaFieldKey(t *testing.T) {
    22  	db, cleanupFunc := newTestDB(t)
    23  	defer cleanupFunc()
    24  
    25  	t.Run("empty name or type", func(t *testing.T) {
    26  		_, err := db.schemaFieldKey("", "")
    27  		if err == nil {
    28  			t.Errorf("error not returned, but expected")
    29  		}
    30  		_, err = db.schemaFieldKey("", "type")
    31  		if err == nil {
    32  			t.Errorf("error not returned, but expected")
    33  		}
    34  
    35  		_, err = db.schemaFieldKey("test", "")
    36  		if err == nil {
    37  			t.Errorf("error not returned, but expected")
    38  		}
    39  	})
    40  
    41  	t.Run("same field", func(t *testing.T) {
    42  		key1, err := db.schemaFieldKey("test", "undefined")
    43  		if err != nil {
    44  			t.Fatal(err)
    45  		}
    46  
    47  		key2, err := db.schemaFieldKey("test", "undefined")
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  
    52  		if !bytes.Equal(key1, key2) {
    53  			t.Errorf("schema keys for the same field name are not the same: %q, %q", string(key1), string(key2))
    54  		}
    55  	})
    56  
    57  	t.Run("different fields", func(t *testing.T) {
    58  		key1, err := db.schemaFieldKey("test1", "undefined")
    59  		if err != nil {
    60  			t.Fatal(err)
    61  		}
    62  
    63  		key2, err := db.schemaFieldKey("test2", "undefined")
    64  		if err != nil {
    65  			t.Fatal(err)
    66  		}
    67  
    68  		if bytes.Equal(key1, key2) {
    69  			t.Error("schema keys for the same field name are the same, but must not be")
    70  		}
    71  	})
    72  
    73  	t.Run("same field name different types", func(t *testing.T) {
    74  		_, err := db.schemaFieldKey("the-field", "one-type")
    75  		if err != nil {
    76  			t.Fatal(err)
    77  		}
    78  
    79  		_, err = db.schemaFieldKey("the-field", "another-type")
    80  		if err == nil {
    81  			t.Errorf("error not returned, but expected")
    82  		}
    83  	})
    84  }
    85  
    86  //testdb_schemaIndexPrefix验证schemaIndexPrefix的正确性。
    87  func TestDB_schemaIndexPrefix(t *testing.T) {
    88  	db, cleanupFunc := newTestDB(t)
    89  	defer cleanupFunc()
    90  
    91  	t.Run("same name", func(t *testing.T) {
    92  		id1, err := db.schemaIndexPrefix("test")
    93  		if err != nil {
    94  			t.Fatal(err)
    95  		}
    96  
    97  		id2, err := db.schemaIndexPrefix("test")
    98  		if err != nil {
    99  			t.Fatal(err)
   100  		}
   101  
   102  		if id1 != id2 {
   103  			t.Errorf("schema keys for the same field name are not the same: %v, %v", id1, id2)
   104  		}
   105  	})
   106  
   107  	t.Run("different names", func(t *testing.T) {
   108  		id1, err := db.schemaIndexPrefix("test1")
   109  		if err != nil {
   110  			t.Fatal(err)
   111  		}
   112  
   113  		id2, err := db.schemaIndexPrefix("test2")
   114  		if err != nil {
   115  			t.Fatal(err)
   116  		}
   117  
   118  		if id1 == id2 {
   119  			t.Error("schema ids for the same index name are the same, but must not be")
   120  		}
   121  	})
   122  }
   123