dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/metric_test.go (about) 1 /******************************************************************************* 2 * Copyright 2022 Intel Corp. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 *******************************************************************************/ 14 15 package dtos 16 17 import ( 18 "fmt" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 25 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 26 ) 27 28 func TestNewMetric(t *testing.T) { 29 expectedTimestamp := time.Now().UnixNano() 30 expectedApiVersion := common.ApiVersion 31 expectedName := "my-metric" 32 33 validFields := []MetricField{ 34 { 35 Name: "count", 36 Value: 50, 37 }, 38 { 39 Name: "max", 40 Value: 5.0, 41 }, 42 { 43 Name: "rate", 44 Value: 2.5, 45 }, 46 } 47 48 invalidFields := []MetricField{ 49 { 50 Name: " ", 51 Value: 50, 52 }, 53 } 54 55 validTags := []MetricTag{ 56 { 57 Name: "service", 58 Value: "my-service", 59 }, 60 { 61 Name: "my-tag", 62 Value: "my-tag-value", 63 }, 64 { 65 Name: "gateway", 66 Value: "my-gateway", 67 }, 68 } 69 70 invalidTags := []MetricTag{ 71 { 72 Name: " ", 73 Value: "my-service", 74 }, 75 } 76 77 tests := []struct { 78 Name string 79 ExpectedFields []MetricField 80 ExpectedTags []MetricTag 81 ErrorExpected bool 82 }{ 83 {"Happy Path", validFields, nil, false}, 84 {"Happy Path - with tags", validFields, validTags, false}, 85 {"Error Path - invalid field", invalidFields, nil, true}, 86 {"Error Path - invalid tag", validFields, invalidTags, true}, 87 } 88 89 for _, test := range tests { 90 t.Run(test.Name, func(t *testing.T) { 91 actual, err := NewMetric(expectedName, test.ExpectedFields, test.ExpectedTags) 92 if test.ErrorExpected { 93 require.Error(t, err) 94 return 95 } 96 97 require.NoError(t, err) 98 assert.Equal(t, expectedApiVersion, actual.ApiVersion) 99 assert.Equal(t, expectedName, actual.Name) 100 assert.Equal(t, test.ExpectedFields, actual.Fields) 101 assert.GreaterOrEqual(t, actual.Timestamp, expectedTimestamp) 102 assert.Equal(t, test.ExpectedTags, actual.Tags) 103 }) 104 } 105 } 106 107 func TestMetric_ToLineProtocol(t *testing.T) { 108 singleField := []MetricField{ 109 { 110 Name: "count", 111 Value: 50, 112 }, 113 } 114 115 multipleFields := []MetricField{ 116 { 117 Name: "count", 118 Value: 50, 119 }, 120 { 121 Name: "max", 122 Value: 5.0, 123 }, 124 { 125 Name: "rate", 126 Value: 2.5, 127 }, 128 } 129 130 additionalTags := []MetricTag{ 131 { 132 Name: "service", 133 Value: "my-service", 134 }, 135 { 136 Name: "my-tag", 137 Value: "my-tag-value", 138 }, 139 { 140 Name: "gateway", 141 Value: "my-gateway", 142 }, 143 } 144 145 tests := []struct { 146 Name string 147 ExpectedResult string 148 Fields []MetricField 149 AdditionalTags []MetricTag 150 }{ 151 {"On Field", "unit.test count=50i %d", singleField, nil}, 152 {"Multi fields", "unit.test count=50i,max=5,rate=2.5 %d", multipleFields, nil}, 153 {"On Field with added tags", "unit.test,service=my-service,my-tag=my-tag-value,gateway=my-gateway count=50i %d", singleField, additionalTags}, 154 {"Multi fields with added tags", "unit.test,service=my-service,my-tag=my-tag-value,gateway=my-gateway count=50i,max=5,rate=2.5 %d", multipleFields, additionalTags}, 155 } 156 157 for _, test := range tests { 158 t.Run(test.Name, func(t *testing.T) { 159 metric, err := NewMetric("unit.test", test.Fields, test.AdditionalTags) 160 require.NoError(t, err) 161 162 expected := fmt.Sprintf(test.ExpectedResult, metric.Timestamp) 163 actual := metric.ToLineProtocol() 164 165 assert.Equal(t, expected, actual) 166 }) 167 } 168 }