github.com/braveheart12/insolar-09-08-19@v0.8.7/ledger/storage/nodes/nodes_cmp_test.go (about)

     1  /*
     2   *    Copyright 2019 Insolar
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package nodes_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/gofuzz"
    23  	"github.com/insolar/insolar"
    24  	"github.com/insolar/insolar/core"
    25  	"github.com/insolar/insolar/gen"
    26  	"github.com/insolar/insolar/ledger/storage/nodes"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestNodes(t *testing.T) {
    31  	storage := nodes.NewStorage()
    32  
    33  	var (
    34  		virtuals  []insolar.Node
    35  		materials []insolar.Node
    36  		all       []insolar.Node
    37  	)
    38  	{
    39  		f := fuzz.New().Funcs(func(e *insolar.Node, c fuzz.Continue) {
    40  			e.ID = gen.Reference()
    41  			e.Role = core.StaticRoleVirtual
    42  		})
    43  		f.NumElements(5, 10).NilChance(0).Fuzz(&virtuals)
    44  	}
    45  	{
    46  		f := fuzz.New().Funcs(func(e *insolar.Node, c fuzz.Continue) {
    47  			e.ID = gen.Reference()
    48  			e.Role = core.StaticRoleLightMaterial
    49  		})
    50  		f.NumElements(5, 10).NilChance(0).Fuzz(&materials)
    51  	}
    52  	all = append(virtuals, materials...)
    53  	pulse := gen.PulseNumber()
    54  
    55  	t.Run("saves nodes", func(t *testing.T) {
    56  		err := storage.Set(pulse, all)
    57  		assert.NoError(t, err)
    58  	})
    59  	t.Run("returns all nodes", func(t *testing.T) {
    60  		result, err := storage.All(pulse)
    61  		assert.NoError(t, err)
    62  		assert.Equal(t, all, result)
    63  	})
    64  	t.Run("returns in role nodes", func(t *testing.T) {
    65  		result, err := storage.InRole(pulse, core.StaticRoleVirtual)
    66  		assert.NoError(t, err)
    67  		assert.Equal(t, virtuals, result)
    68  	})
    69  	t.Run("deletes nodes", func(t *testing.T) {
    70  		storage.Delete(pulse)
    71  		result, err := storage.All(pulse)
    72  		assert.Equal(t, core.ErrNoNodes, err)
    73  		assert.Nil(t, result)
    74  	})
    75  }