File: emf_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (145 lines) | stat: -rw-r--r-- 3,823 bytes parent folder | download | duplicates (4)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// This package is designated as private and is intended for use only by the
// smithy client runtime. The exported API therein is not considered stable and
// is subject to breaking changes without notice.

package emf

import (
	"fmt"
	"reflect"
	"testing"
	"time"

	"github.com/aws/aws-sdk-go-v2/aws/middleware/private/metrics"
	"github.com/aws/aws-sdk-go-v2/internal/sdk"
)

type TestSerializerWithError struct{}

func (TestSerializerWithError) Serialize(obj interface{}) (string, error) {
	return "", fmt.Errorf("serialization error")
}

func TestCreateNewEntry(t *testing.T) {

	sdk.NowTime = func() time.Time {
		return time.Unix(1234, 0)
	}

	cases := map[string]struct {
		Namespace     string
		ExpectedEntry Entry
	}{
		"success": {
			Namespace: "testNamespace",
			ExpectedEntry: Entry{
				namespace:  "testNamespace",
				serializer: metrics.DefaultSerializer{},
				metrics:    []metric{},
				dimensions: [][]string{{}},
				fields:     map[string]interface{}{},
			},
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			actualEntry := NewEntry(c.Namespace, metrics.DefaultSerializer{})
			if !reflect.DeepEqual(actualEntry, c.ExpectedEntry) {
				t.Errorf("Entry contained unexpected values")
			}
		})
	}
}

func TestBuild(t *testing.T) {

	sdk.NowTime = func() time.Time {
		return time.Unix(1234, 0)
	}

	cases := map[string]struct {
		Namespace      string
		Configure      func(entry *Entry)
		Serializer     metrics.Serializer
		ExpectedError  error
		ExpectedResult string
	}{
		"completeEntry": {
			Namespace:  "testNamespace",
			Serializer: metrics.DefaultSerializer{},
			Configure: func(entry *Entry) {
				entry.AddMetric("testMetric1", 1)
				entry.AddMetric("testMetric2", 2)
				entry.AddDimension("testDimension1", "dim1")
				entry.AddDimension("testDimension2", "dim2")
				entry.AddProperty("testProperty1", "prop1")
				entry.AddProperty("testProperty2", "prop2")
			},
			ExpectedError:  nil,
			ExpectedResult: completeEntry,
		},
		"noMetrics": {
			Namespace:  "testNamespace",
			Serializer: metrics.DefaultSerializer{},
			Configure: func(entry *Entry) {
				entry.AddDimension("testDimension1", "dim1")
				entry.AddDimension("testDimension2", "dim2")
				entry.AddProperty("testProperty1", "prop1")
				entry.AddProperty("testProperty2", "prop2")
			},
			ExpectedError:  nil,
			ExpectedResult: noMetrics,
		},
		"noDimensions": {
			Namespace:  "testNamespace",
			Serializer: metrics.DefaultSerializer{},
			Configure: func(entry *Entry) {
				entry.AddMetric("testMetric1", 1)
				entry.AddMetric("testMetric2", 2)
				entry.AddProperty("testProperty1", "prop1")
				entry.AddProperty("testProperty2", "prop2")
			},
			ExpectedError:  nil,
			ExpectedResult: noDimensions,
		},
		"noProperties": {
			Namespace:  "testNamespace",
			Serializer: metrics.DefaultSerializer{},
			Configure: func(entry *Entry) {
				entry.AddMetric("testMetric1", 1)
				entry.AddMetric("testMetric2", 2)
				entry.AddDimension("testDimension1", "dim1")
				entry.AddDimension("testDimension2", "dim2")
			},
			ExpectedError:  nil,
			ExpectedResult: noProperties,
		},
		"serializationError": {
			Namespace:  "testNamespace",
			Serializer: TestSerializerWithError{},
			Configure: func(entry *Entry) {
			},
			ExpectedError:  fmt.Errorf("serialization error"),
			ExpectedResult: "",
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			entry := NewEntry(c.Namespace, c.Serializer)

			c.Configure(&entry)

			result, err := entry.Build()

			if !reflect.DeepEqual(err, c.ExpectedError) {
				t.Errorf("Unexpected error, should be '%s' but was '%s'", c.ExpectedError, err)
			}

			if !reflect.DeepEqual(result, c.ExpectedResult) {
				t.Errorf("Unexpected result, should be '%s' but was '%s'", c.ExpectedResult, result)
			}
		})
	}
}