-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscraping_test.go
182 lines (159 loc) · 4.59 KB
/
scraping_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"net/http/httptest"
"github.com/prometheus/client_golang/prometheus"
)
func checkExpectError(err error, expect bool, t *testing.T) {
if err != nil && !expect {
t.Errorf("Expected success, got error: %s", err)
}
if err == nil && expect {
t.Errorf("Expected error, got none")
}
}
func TestNormalizeValue(t *testing.T) {
res, err := normalizeValue("10,123 kWh")
checkExpectError(err, false, t)
if res.Unit != "kWh" {
t.Errorf("Expected kWh as unit, but got %s", res.Unit)
}
if res.Value != 10.123 {
t.Errorf("Expected 10.123 as value, but got %f", res.Value)
}
res, err = normalizeValue("10")
checkExpectError(err, false, t)
if res.Unit != "" {
t.Error("Expected empty unit")
}
if res.Value != 10 {
t.Fail()
}
res, err = normalizeValue("3,345 MWh")
checkExpectError(err, false, t)
if res.Unit != "kWh" {
t.Errorf("Expected conversion to kWh, but got %s", res.Unit)
}
if res.Value != 3345 {
t.Errorf("Expected 3345, but got %f", res.Value)
}
res, err = normalizeValue("18.321kWh")
checkExpectError(err, false, t)
if res.Unit != "kWh" {
t.Errorf("Expected kWh as unit, but got %s", res.Unit)
}
if res.Value != 18.321 {
t.Errorf("Expected 18.321, but got %f", res.Value)
}
res, err = normalizeValue("15,8°C")
checkExpectError(err, false, t)
if res.Unit != "°C" {
t.Errorf("Expected °C as unit, but got %s", res.Unit)
}
if res.Value != 15.8 {
t.Errorf("Expected 15.8, but got %f", res.Value)
}
res, err = normalizeValue("-15,8°C")
checkExpectError(err, false, t)
if res.Unit != "°C" {
t.Errorf("Expected °C as unit, but got %s", res.Unit)
}
if res.Value != -15.8 {
t.Errorf("Expected -15.8, but got %f", res.Value)
}
res, err = normalizeValue("1 l/min")
checkExpectError(err, false, t)
res, err = normalizeValue("1 %")
checkExpectError(err, false, t)
res, err = normalizeValue("1 m³/h")
checkExpectError(err, false, t)
res, err = normalizeValue("On")
checkExpectError(err, false, t)
if res.Value != 1 {
t.Errorf("Expected 1, but got %f", res.Value)
}
res, err = normalizeValue("Off")
checkExpectError(err, false, t)
if res.Value != 0 {
t.Errorf("Expected 0, but got %f", res.Value)
}
res, err = normalizeValue("fnvdnvsdbvdfk")
checkExpectError(err, true, t)
}
func TestNormalizeLabel(t *testing.T) {
res := normalizeLabel("RAUMISTTEMP. HK1")
if res != "raumisttemp_hk1" {
t.Fail()
}
res = normalizeLabel("FORTLUFT IST LÜFTERDREHZAHL ")
if res != "fortluft_ist_luefterdrehzahl" {
t.Errorf("Expected fortluft_ist_luefterdrehzahl, but got %s", res)
}
res = normalizeLabel("WW-SOLLTEMP.")
if res != "ww_solltemp" {
t.Errorf("Expected ww_solltemp, but got %s", res)
}
res = normalizeLabel("M*1E6")
if res != "m1e6" {
t.Errorf("Expected m1e6, but got %s", res)
}
}
func TestPage(t *testing.T) {
options.Mode = MODE_WEBSCRAPING
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
page := req.URL.Query().Get("s")
if page == "1,0" {
content, err := os.ReadFile("test_resources/sample_1_0.html")
if err != nil {
t.Errorf("Failed delivering sample for 1,0")
}
fmt.Fprint(w, string(content))
} else if page == "1,1" {
content, err := os.ReadFile("test_resources/sample_1_1.html")
if err != nil {
t.Errorf("Failed delivering sample for 1,1")
}
fmt.Fprint(w, string(content))
} else if page == "4,7" {
content, err := os.ReadFile("test_resources/sample_4_7.html")
if err != nil {
t.Errorf("Failed delivering sample for 4,7")
}
fmt.Fprint(w, string(content))
} else if page == "onoff" {
content, err := os.ReadFile("test_resources/sample_onoff.html")
if err != nil {
t.Errorf("Failed delivering sample for onoff")
}
fmt.Fprint(w, string(content))
} else if page == "specialchars" {
content, err := os.ReadFile("test_resources/sample_specialchars.html")
if err != nil {
t.Errorf("Failed delivering sample for specialchars")
}
fmt.Fprint(w, string(content))
} else {
fmt.Fprint(w, "")
}
}))
defer ts.Close()
options.URL = ts.URL
gaugesMap = make(map[string]*prometheus.GaugeVec)
flagGaugesMap = make(map[string]prometheus.Gauge)
valuesMap = make(map[string][]IsgValue)
prepare()
flagRemovalList := make(map[string]prometheus.Gauge)
parsePage("1,1", flagRemovalList)
parsePage("4,7", flagRemovalList)
parsePage("onoff", flagRemovalList)
parsePage("specialchars", flagRemovalList)
if valuesMap["festwertbetrieb"] == nil {
t.Errorf("Failed to find expected onoff value")
}
json, _ := json.MarshalIndent(valuesMap, "", " ")
fmt.Println(string(json))
}