-
Notifications
You must be signed in to change notification settings - Fork 3
/
samler_test.go
158 lines (133 loc) · 2.95 KB
/
samler_test.go
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
/*
SaMLer - Smart Meter data colletor at the edge
Copyright (C) 2024 Florian Heubeck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"log"
"os"
"reflect"
"testing"
"time"
)
func tempDir() string {
dir, err := os.MkdirTemp("", "samler_test")
if err != nil {
log.Fatal()
}
return dir
}
func TestSuccessfullSending(t *testing.T) {
// Given
measurement := Measurement{
Ident: "ident",
Unit: "unit",
Prefix: "prefix",
Suffix: "suffix",
Value: 42.23,
Time: time.Now(),
}
var sent Measurement
messages := make(chan Measurement)
send := func(m Measurement) bool {
sent = m
return true
}
RunSamler(messages, send, tempDir(), "")
// When
messages <- measurement
// Then
if sent.Value != 42.23 {
t.Error()
}
}
func TestFailedSending(t *testing.T) {
// Given
measurement := Measurement{
Ident: "ident",
Unit: "unit",
Prefix: "prefix",
Suffix: "suffix",
Value: 23.5,
Time: time.Now(),
}
result := true
var sent Measurement
messages := make(chan Measurement)
send := func(m Measurement) bool {
if !result {
sent = m
}
result = !result
return result
}
RunSamler(messages, send, tempDir(), "")
// When
messages <- measurement
// Then
if sent.Value != 0 {
t.Error()
}
time.Sleep(32 * time.Second)
if sent.Value != 23.5 {
t.Error()
}
}
func TestToEmptyFilterList(t *testing.T) {
// Given
empties := []string{"-", "", " ", ",", " , , "}
// When
mapped := [5][]string{}
for i, v := range empties {
mapped[i] = toFilterList(v)
}
// Then
for _, v := range mapped {
if len(v) != 0 {
t.Fatalf("Expected empty array, got %s", v)
}
}
}
func TestToFilterList(t *testing.T) {
if !reflect.DeepEqual(toFilterList("1.2.3, , 4.3.2"), []string{"1.2.3", "4.3.2"}) {
t.Error()
}
if !reflect.DeepEqual(toFilterList(" 1.2.3 , , , 4.3.2 "), []string{"1.2.3", "4.3.2"}) {
t.Error()
}
if !reflect.DeepEqual(toFilterList(" 1.2.5 ,4.3.1, , 4.1.2 "), []string{"1.2.5", "4.3.1", "4.1.2"}) {
t.Error()
}
}
func TestIsRelevantWithValues(t *testing.T) {
// Given
filter := []string{"1.8.1", "2.4.1"}
// When && Then
if isRelevant("2.8.1", filter) {
t.Error()
}
if isRelevant("1.8", filter) {
t.Error()
}
if !isRelevant("1.8.1", filter) {
t.Error()
}
if !isRelevant("2.4.1", filter) {
t.Error()
}
}
func TestIsRelevantEmptyList(t *testing.T) {
if !isRelevant("1.2.3", []string{}) {
t.Error()
}
}