-
Notifications
You must be signed in to change notification settings - Fork 3
/
mysql_test.go
115 lines (97 loc) · 2.6 KB
/
mysql_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
/*
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 (
"context"
"database/sql"
"fmt"
"testing"
"time"
testcontainers "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestFailingMySqlSend(t *testing.T) {
// Given
sender := InitializeMySQL("", "")
m := Measurement{}
// When
success := sender(m)
// Then
if success != false {
t.Fatal()
}
}
func TestSuccessfulMySQLSend(t *testing.T) {
// Setup testcontainer
req := testcontainers.ContainerRequest{
Image: "mysql:8",
ExposedPorts: []string{"3306/tcp"},
WaitingFor: wait.ForExposedPort(),
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "password",
"MYSQL_DATABASE": "samler",
},
}
ctx, _ := context.WithTimeout(context.Background(), 120*time.Second)
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
t.Fatal(err)
}
host, _ := container.Host(ctx)
port, _ := container.MappedPort(ctx, "3306/tcp")
// Given
dsn := fmt.Sprintf("root:password@tcp(%s:%d)/samler", host, port.Int())
sender := selectBackend(map[string]string{
Backend: "mysql",
MySqlDSN: dsn,
MySqlTable: "measures",
})
m := Measurement{
Time: time.Now(),
Value: 22.5,
Unit: "°",
Ident: "0.8.15",
Prefix: "1-2",
Suffix: "667",
}
// When
success := sender(m)
// Then
if !success {
t.Fatal()
}
db, err := sql.Open("mysql", dsn)
if err != nil {
t.Fatal(err)
}
res, err := db.Query("select ident, value, unit, prefix, suffix from measures")
if err != nil {
t.Fatal(err)
}
if !res.Next() {
t.Fatal("Didn't get a result")
}
var value float32
var ident, unit, prefix, suffix string
if err := res.Scan(&ident, &value, &unit, &prefix, &suffix); err != nil {
t.Fatal(err)
}
if value != 22.5 || unit != "°" || prefix != "1-2" || suffix != "667" || ident != "0.8.15" {
t.Fatal()
}
}