aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/yandex/tvm/roles_entities_index_test.go
blob: e1abaa0f0e68296120ce888b46a707b35a765a0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package tvm

import (
	"math/rand"
	"reflect"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestRolesSubTreeContainsExactEntity(t *testing.T) {
	origEntities := []Entity{
		{"key#1": "value#1"},
		{"key#1": "value#1", "key#2": "value#2", "key#4": "value#4"},
		{"key#1": "value#1", "key#2": "value#2"},
		{"key#1": "value#2", "key#2": "value#2"},
		{"key#3": "value#3"},
	}
	entities := buildEntities(origEntities)

	for _, e := range generatedRandEntities() {
		found := false
		for _, o := range origEntities {
			if reflect.DeepEqual(e, o) {
				found = true
				break
			}
		}

		require.Equal(t, found, entities.subtree.containsExactEntity(e), e)
	}
}

func generatedRandEntities() []Entity {
	rand.Seed(time.Now().UnixNano())

	keysStages := createStages([]string{"key#1", "key#2", "key#3", "key#4", "key#5"})
	valuesSet := []string{"value#1", "value#2", "value#3", "value#4", "value#5"}

	res := make([]Entity, 0)

	keySet := make([]string, 0, 5)
	for keysStages.getNextStage(&keySet) {
		entity := Entity{}
		for _, key := range keySet {
			entity[key] = valuesSet[rand.Intn(len(valuesSet))]

			e := Entity{}
			for k, v := range entity {
				e[k] = v
			}
			res = append(res, e)
		}
	}

	return res
}

func TestRolesGetEntitiesWithAttrs(t *testing.T) {
	type TestCase struct {
		in  Entity
		out []Entity
	}

	cases := []TestCase{
		{
			out: []Entity{
				{"key#1": "value#1"},
				{"key#1": "value#1", "key#2": "value#2", "key#4": "value#4"},
				{"key#1": "value#2", "key#2": "value#2"},
				{"key#3": "value#3"},
			},
		},
		{
			in: Entity{"key#1": "value#1"},
			out: []Entity{
				{"key#1": "value#1"},
				{"key#1": "value#1", "key#2": "value#2", "key#4": "value#4"},
			},
		},
		{
			in: Entity{"key#1": "value#2"},
			out: []Entity{
				{"key#1": "value#2", "key#2": "value#2"},
			},
		},
		{
			in: Entity{"key#2": "value#2"},
			out: []Entity{
				{"key#1": "value#1", "key#2": "value#2", "key#4": "value#4"},
				{"key#1": "value#2", "key#2": "value#2"},
			},
		},
		{
			in: Entity{"key#3": "value#3"},
			out: []Entity{
				{"key#3": "value#3"},
			},
		},
	}

	entities := buildEntities([]Entity{
		{"key#1": "value#1"},
		{"key#1": "value#1", "key#2": "value#2", "key#4": "value#4"},
		{"key#1": "value#2", "key#2": "value#2"},
		{"key#3": "value#3"},
	})

	for idx, c := range cases {
		require.Equal(t, c.out, entities.subtree.getEntitiesWithAttrs(c.in), idx)
	}
}