summaryrefslogtreecommitdiff
path: root/service/env/service_test.go
blob: a354214c1a284c3ff8fe2b757e30050138127a14 (plain)
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
package env

import (
	"testing"

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

func Test_NewService(t *testing.T) {
	s := NewService(map[string]string{"version": "test"})
	assert.Len(t, s.values, 1)
}

func Test_Init(t *testing.T) {
	var err error
	s := &Service{}
	_, err = s.Init(&Config{})
	if err != nil {
		t.Errorf("error during the s.Init: error %v", err)
	}
	assert.Len(t, s.values, 1)

	values, err := s.GetEnv()
	assert.NoError(t, err)
	assert.Equal(t, "true", values["RR"])
}

func Test_Extend(t *testing.T) {
	var err error
	s := NewService(map[string]string{"RR": "version"})

	_, err = s.Init(&Config{Values: map[string]string{"key": "value"}})
	if err != nil {
		t.Errorf("error during the s.Init: error %v", err)
	}
	assert.Len(t, s.values, 2)

	values, err := s.GetEnv()
	assert.NoError(t, err)
	assert.Len(t, values, 2)
	assert.Equal(t, "version", values["RR"])
	assert.Equal(t, "value", values["key"])
}

func Test_Set(t *testing.T) {
	var err error
	s := NewService(map[string]string{"RR": "version"})

	_, err = s.Init(&Config{Values: map[string]string{"key": "value"}})
	if err != nil {
		t.Errorf("error during the s.Init: error %v", err)
	}
	assert.Len(t, s.values, 2)

	s.SetEnv("key", "value-new")
	s.SetEnv("other", "new")

	values, err := s.GetEnv()
	assert.NoError(t, err)
	assert.Len(t, values, 3)
	assert.Equal(t, "version", values["RR"])
	assert.Equal(t, "value-new", values["key"])
	assert.Equal(t, "new", values["other"])
}

func Test_Copy(t *testing.T) {
	s1 := NewService(map[string]string{"RR": "version"})
	s2 := NewService(map[string]string{})

	s1.SetEnv("key", "value-new")
	s1.SetEnv("other", "new")

	assert.NoError(t, s1.Copy(s2))

	values, err := s2.GetEnv()
	assert.NoError(t, err)
	assert.Len(t, values, 3)
	assert.Equal(t, "version", values["RR"])
	assert.Equal(t, "value-new", values["key"])
	assert.Equal(t, "new", values["other"])
}