File: extractor_test.go

package info (click to toggle)
prometheus-mqtt-exporter 0.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 460 kB
  • sloc: sh: 77; makefile: 60
file content (158 lines) | stat: -rw-r--r-- 3,910 bytes parent folder | download
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
146
147
148
149
150
151
152
153
154
155
156
157
158
package metrics

import (
	"reflect"
	"testing"

	"github.com/hikhvar/mqtt2prometheus/pkg/config"
	"github.com/prometheus/client_golang/prometheus"
)

func TestNewJSONObjectExtractor_parseMetric(t *testing.T) {
	now = testNow
	type fields struct {
		metricConfigs map[string][]config.MetricConfig
	}
	type args struct {
		metricPath string
		deviceID   string
		value      string
	}
	tests := []struct {
		name      string
		separator string
		fields    fields
		args      args
		want      Metric
		wantErr   bool
		noValue   bool
	}{
		{
			name:      "string value",
			separator: "->",
			fields: fields{
				map[string][]config.MetricConfig{
					"SDS0X1->PM2->5": []config.MetricConfig{
						{
							PrometheusName: "temperature",
							MQTTName:       "SDS0X1.PM2.5",
							ValueType:      "gauge",
						},
					},
				},
			},
			args: args{
				metricPath: "topic",
				deviceID:   "dht22",
				value:      "{\"SDS0X1\":{\"PM2\":{\"5\":4.9}}}",
			},
			want: Metric{
				Description: prometheus.NewDesc("temperature", "", []string{"sensor", "topic"}, nil),
				ValueType:   prometheus.GaugeValue,
				Value:       4.9,
				IngestTime:  testNow(),
				Topic:       "topic",
			},
		}, {
			name:      "string value with dots in path",
			separator: "->",
			fields: fields{
				map[string][]config.MetricConfig{
					"SDS0X1->PM2.5": []config.MetricConfig{
						{
							PrometheusName: "temperature",
							MQTTName:       "SDS0X1->PM2.5",
							ValueType:      "gauge",
						},
					},
				},
			},
			args: args{
				metricPath: "topic",
				deviceID:   "dht22",
				value:      "{\"SDS0X1\":{\"PM2.5\":4.9,\"PM10\":8.5}}",
			},
			want: Metric{
				Description: prometheus.NewDesc("temperature", "", []string{"sensor", "topic"}, nil),
				ValueType:   prometheus.GaugeValue,
				Value:       4.9,
				IngestTime:  testNow(),
				Topic:       "topic",
			},
		}, {
			name:      "metric matching SensorNameFilter",
			separator: ".",
			fields: fields{
				map[string][]config.MetricConfig{
					"temperature": []config.MetricConfig{
						{
							PrometheusName:   "temperature",
							MQTTName:         "temperature",
							ValueType:        "gauge",
							SensorNameFilter: *config.MustNewRegexp(".*22$"),
						},
					},
				},
			},
			args: args{
				metricPath: "topic",
				deviceID:   "dht22",
				value:      "{\"temperature\": 8.5}",
			},
			want: Metric{
				Description: prometheus.NewDesc("temperature", "", []string{"sensor", "topic"}, nil),
				ValueType:   prometheus.GaugeValue,
				Value:       8.5,
				IngestTime:  testNow(),
				Topic:       "topic",
			},
		}, {
			name:      "metric not matching SensorNameFilter",
			separator: ".",
			fields: fields{
				map[string][]config.MetricConfig{
					"temperature": []config.MetricConfig{
						{
							PrometheusName:   "temperature",
							MQTTName:         "temperature",
							ValueType:        "gauge",
							SensorNameFilter: *config.MustNewRegexp(".*fail$"),
						},
					},
				},
			},
			args: args{
				metricPath: "topic",
				deviceID:   "dht22",
				value:      "{\"temperature\": 8.5}",
			},
			want:    Metric{},
			noValue: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			p := Parser{
				separator:     tt.separator,
				metricConfigs: tt.fields.metricConfigs,
			}
			extractor := NewJSONObjectExtractor(p)

			got, err := extractor(tt.args.metricPath, []byte(tt.args.value), tt.args.deviceID)
			if (err != nil) != tt.wantErr {
				t.Errorf("parseMetric() error = %v, wantErr %v", err, tt.wantErr)
				return
			}

			if len(got) == 0 {
				if !tt.noValue {
					t.Errorf("parseMetric() got = %v, want %v", nil, tt.want)
				}
			} else if !reflect.DeepEqual(got[0], tt.want) {
				t.Errorf("parseMetric() got = %v, want %v", got[0], tt.want)
			} else if len(got) > 1 {
				t.Errorf("unexpected result got = %v, want %v", got, tt.want)
			}
		})
	}
}