-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathtypes_test.go
More file actions
66 lines (59 loc) · 1.71 KB
/
types_test.go
File metadata and controls
66 lines (59 loc) · 1.71 KB
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
package metrics
import (
"context"
"testing"
"time"
"github.com/cybertec-postgresql/pgwatch/v5/internal/log"
"github.com/stretchr/testify/assert"
)
var ctx = log.WithLogger(context.Background(), log.NewNoopLogger())
func TestGetSQL(t *testing.T) {
m := Metric{}
m.SQLs = SQLs{
1: "one",
3: "three",
5: "five",
6: "six",
}
tests := map[int]string{
2: "one",
3: "three",
4: "three",
5: "five",
6: "six",
10: "six",
0: "",
}
for i, tt := range tests {
if got := m.GetSQL(i); got != tt {
t.Errorf("VersionToInt() = %v, want %v", got, tt)
}
}
}
func TestPrimaryOnly(t *testing.T) {
m := Metric{NodeStatus: "primary"}
assert.True(t, m.PrimaryOnly())
assert.False(t, m.StandbyOnly())
m.NodeStatus = "standby"
assert.False(t, m.PrimaryOnly())
assert.True(t, m.StandbyOnly())
}
func TestMeasurement(t *testing.T) {
m := NewMeasurement(1234567890)
assert.Equal(t, int64(1234567890), m.GetEpoch(), "epoch should be equal")
m[EpochColumnName] = "wrong type"
assert.True(t, time.Now().UnixNano()-m.GetEpoch() < int64(time.Second), "epoch should be close to now")
}
func TestMeasurements(t *testing.T) {
m := Measurements{}
assert.False(t, m.IsEpochSet(), "epoch should not be set")
assert.True(t, time.Now().UnixNano()-m.GetEpoch() < 100, "epoch should be close to now")
m = append(m, NewMeasurement(1234567890))
assert.True(t, m.IsEpochSet(), "epoch should be set")
assert.Equal(t, int64(1234567890), m.GetEpoch(), "epoch should be equal")
m1 := m.DeepCopy()
assert.Equal(t, m, m1, "deep copy should be equal")
m1.Touch()
assert.NotEqual(t, m, m1, "deep copy should be different")
assert.True(t, time.Now().UnixNano()-m1.GetEpoch() < int64(time.Second), "epoch should be close to now")
}