From 3132f7fad497d03a1ad7055a40acfb31533f4b4c Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 11 Jul 2018 16:03:42 +0400 Subject: [PATCH] add tests for genesis Refs #693 --- types/genesis_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/types/genesis_test.go b/types/genesis_test.go index 24398a9a5..ee320051f 100644 --- a/types/genesis_test.go +++ b/types/genesis_test.go @@ -1,9 +1,13 @@ package types import ( + "io/ioutil" + "os" "testing" + "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto" ) @@ -59,3 +63,44 @@ func TestGenesisGood(t *testing.T) { genDoc, err = GenesisDocFromJSON(genDocBytes) assert.Error(t, err, "expected error for genDoc json with block size of 0") } + +func TestGenesisSaveAs(t *testing.T) { + tmpfile, err := ioutil.TempFile("", "genesis") + require.NoError(t, err) + defer os.Remove(tmpfile.Name()) + + genDoc := randomGenesisDoc() + + // save + genDoc.SaveAs(tmpfile.Name()) + stat, err := tmpfile.Stat() + require.NoError(t, err) + if err != nil && stat.Size() <= 0 { + t.Fatalf("SaveAs failed to write any bytes to %v", tmpfile.Name()) + } + + err = tmpfile.Close() + require.NoError(t, err) + + // load + genDoc2, err := GenesisDocFromFile(tmpfile.Name()) + require.NoError(t, err) + + // fails to unknown reason + // assert.EqualValues(t, genDoc2, genDoc) + assert.Equal(t, genDoc2.Validators, genDoc.Validators) +} + +func TestGenesisValidatorHash(t *testing.T) { + genDoc := randomGenesisDoc() + assert.NotEmpty(t, genDoc.ValidatorHash()) +} + +func randomGenesisDoc() *GenesisDoc { + return &GenesisDoc{ + GenesisTime: time.Now().UTC(), + ChainID: "abc", + Validators: []GenesisValidator{{crypto.GenPrivKeyEd25519().PubKey(), 10, "myval"}}, + ConsensusParams: DefaultConsensusParams(), + } +}