github.com/eris-ltd/erisdb@v0.25.0/storage/key_format_store_test.go (about)

     1  package storage
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  type testKeyStore = struct {
    10  	Accounts *MustKeyFormat
    11  	Storage  *MustKeyFormat
    12  	foo      string
    13  }
    14  
    15  func TestEnsureKeyStore(t *testing.T) {
    16  	keyStore := testKeyStore{
    17  		Accounts: NewMustKeyFormat("foo", 4, 5, 6),
    18  		Storage:  NewMustKeyFormat("foos", 4, 5, 6),
    19  	}
    20  	err := EnsureKeyFormatStore(keyStore)
    21  	require.NoError(t, err)
    22  
    23  	err = EnsureKeyFormatStore(&keyStore)
    24  	require.NoError(t, err, "pointer to keystore should work")
    25  
    26  	keyStore = testKeyStore{
    27  		Accounts: NewMustKeyFormat("foo", 4, 5, 6),
    28  		Storage:  NewMustKeyFormat("foo", 4, 5, 6),
    29  	}
    30  	err = EnsureKeyFormatStore(&keyStore)
    31  	require.Error(t, err, "duplicate prefixes should be detected")
    32  
    33  	// Test missing formats
    34  	keyStore = testKeyStore{}
    35  	err = EnsureKeyFormatStore(&keyStore)
    36  	require.Error(t, err, "all formats should be set")
    37  
    38  	keyStore = testKeyStore{
    39  		Accounts: NewMustKeyFormat("foo", 4, 5, 6),
    40  	}
    41  	err = EnsureKeyFormatStore(&keyStore)
    42  	require.Error(t, err, "all formats should be set")
    43  
    44  	keyStore2 := struct {
    45  		Accounts MustKeyFormat
    46  		Storage  *MustKeyFormat
    47  	}{
    48  		Accounts: *NewMustKeyFormat("foo", 56, 6),
    49  		Storage:  NewMustKeyFormat("foo2", 1, 2),
    50  	}
    51  
    52  	err = EnsureKeyFormatStore(keyStore2)
    53  	require.NoError(t, err)
    54  
    55  	keyStore2 = struct {
    56  		Accounts MustKeyFormat
    57  		Storage  *MustKeyFormat
    58  	}{
    59  		Storage: NewMustKeyFormat("foo2", 1, 2),
    60  	}
    61  	err = EnsureKeyFormatStore(keyStore2)
    62  	require.NoError(t, err)
    63  
    64  	err = EnsureKeyFormatStore(keyStore2)
    65  	require.NoError(t, err)
    66  
    67  	keyStore2 = struct {
    68  		Accounts MustKeyFormat
    69  		Storage  *MustKeyFormat
    70  	}{
    71  		Accounts: *NewMustKeyFormat("foo", 56, 6),
    72  		Storage:  NewMustKeyFormat("foo", 1, 2),
    73  	}
    74  
    75  	err = EnsureKeyFormatStore(keyStore2)
    76  	require.Error(t, err, "duplicate prefixes should be detected")
    77  }