-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalues_map_test.go
More file actions
90 lines (70 loc) · 1.96 KB
/
values_map_test.go
File metadata and controls
90 lines (70 loc) · 1.96 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package llsr
import (
"database/sql"
"testing"
_ "github.com/lib/pq"
"github.com/liquidm/llsr/decoderbufs"
)
type testValueMapOidCallback func(*testing.T, int)
func withValueMapOid(t *testing.T, cb testValueMapOidCallback) {
db, err := sql.Open("postgres", "sslmode=disable user="+dbUser()+" dbname="+dbName())
if err != nil {
t.Fatal(err)
}
defer db.Close()
_, err = db.Exec("CREATE TYPE llsr_test_enum AS ENUM ('foo', 'bar', 'llsr_foobar')")
if err != nil {
t.Fatal(err)
}
defer db.Exec("DROP TYPE llsr_test_enum")
var oid int
err = db.QueryRow("SELECT enumtypid FROM pg_enum WHERE enumlabel = 'llsr_foobar'").Scan(&oid)
if err != nil {
t.Fatal(err)
}
cb(t, oid)
}
func TestValueMapDiscovery(t *testing.T) {
withValueMapOid(t, func(t *testing.T, oid int) {
dbConfig := NewDatabaseConfig(dbName())
dbConfig.User = dbUser()
valuesMap, err := loadValuesMap(dbConfig)
if err != nil {
t.Fatal(err)
}
if !valuesMap[oid] {
t.Fatal("Expected ValueMap.load() to discover enum types")
}
if valuesMap[1] {
t.Fatal("Expected valuesMap to contain only enum oids")
}
})
}
func TestExtractValue(t *testing.T) {
withValueMapOid(t, func(t *testing.T, oid int) {
dbConfig := NewDatabaseConfig(dbName())
dbConfig.User = dbUser()
valuesMap, err := loadValuesMap(dbConfig)
if err != nil {
t.Fatal(err)
}
oid64 := int64(oid)
datumMessage := &decoderbufs.DatumMessage{
ColumnType: &oid64,
DatumBytes: []byte("enum_label"),
}
extractedValue, err := valuesMap.Extract(datumMessage)
if err != nil {
t.Fatalf("Expected Extract not to return error. Got: %v", err)
}
if *(extractedValue.(*string)) != "enum_label" {
t.Fatalf("Expected Extract to return enum label. Got: %v", extractedValue)
}
oid64 += 1
datumMessage.ColumnType = &oid64
extractedValue, err = valuesMap.Extract(datumMessage)
if err != ErrUnknownOID {
t.Fatalf("Expected ExtractValue to return ErrUnknownOID error. Got: %v", err)
}
})
}