This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
null_cell_test.go
85 lines (73 loc) · 1.55 KB
/
null_cell_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
package sqlr
import (
"context"
"errors"
"fmt"
"reflect"
"testing"
)
func TestNullScannerCell(t *testing.T) {
db := postgresDB(t)
defer db.Close()
mustExec(t, db, `drop table if exists nullables;`)
defer func() {
mustExec(t, db, `drop table if exists nullables`)
}()
mustExec(t, db, `
create table nullables(
id integer not null primary key,
enum_val text null
);
insert into nullables(id, enum_val) values(1, 'One');
insert into nullables(id, enum_val) values(2, null);
`)
schema := NewSchema(ForDB(db))
ctx := context.Background()
sess := NewSession(ctx, db, schema)
defer sess.Close()
type rowT struct {
ID int
EnumVal TestEnum `sql:"null"`
}
var nullables []rowT
n, err := sess.Select(&nullables, "select {} from nullables order by id")
if err != nil {
t.Fatalf("got %v, want nil", err)
}
if got, want := n, 2; got != want {
t.Fatalf("got %v, want %v", got, want)
}
{
want := []rowT{
{ID: 1, EnumVal: TestEnumOne},
{ID: 2, EnumVal: TestEnumZero},
}
if got := nullables; !reflect.DeepEqual(want, got) {
t.Fatalf("want=%v, got=%v", want, got)
}
}
}
type TestEnum int
const (
TestEnumZero TestEnum = 0
TestEnumOne TestEnum = 1
)
func (ne *TestEnum) Scan(src interface{}) error {
str, ok := src.(string)
if !ok {
bytes, ok := src.([]byte)
if !ok {
return errors.New("invalid type for TestEnum")
}
str = string(bytes[:])
}
switch str {
case "Zero":
*ne = TestEnumZero
case "One":
*ne = TestEnumOne
default:
return fmt.Errorf("unknown value for TestEnum: %q", str)
}
return nil
}