-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathquery.sql_test.go
121 lines (102 loc) · 3.17 KB
/
query.sql_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
package enums
import (
"context"
"github.com/jackc/pgtype"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net"
"testing"
"time"
)
func TestNewQuerier_FindAllDevices(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
q := NewQuerier(conn)
mac, _ := net.ParseMAC("00:00:5e:00:53:01")
insertDevice(t, q, mac, DeviceTypeIot)
t.Run("FindAllDevices", func(t *testing.T) {
devices, err := q.FindAllDevices(ctx)
require.NoError(t, err)
assert.Equal(t,
[]FindAllDevicesRow{
{Mac: pgtype.Macaddr{Addr: mac, Status: pgtype.Present}, Type: DeviceTypeIot},
},
devices,
)
})
}
var allDeviceTypes = []DeviceType{
DeviceTypeUndefined,
DeviceTypePhone,
DeviceTypeLaptop,
DeviceTypeIpad,
DeviceTypeDesktop,
DeviceTypeIot,
}
func TestNewQuerier_FindOneDeviceArray(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
q := NewQuerier(conn)
t.Run("FindOneDeviceArray", func(t *testing.T) {
devices, err := q.FindOneDeviceArray(ctx)
require.NoError(t, err)
assert.Equal(t, allDeviceTypes, devices)
})
}
func TestNewQuerier_FindManyDeviceArray(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
q := NewQuerier(conn)
t.Run("FindManyDeviceArray", func(t *testing.T) {
devices, err := q.FindManyDeviceArray(ctx)
require.NoError(t, err)
assert.Equal(t, [][]DeviceType{allDeviceTypes[3:], allDeviceTypes}, devices)
})
}
func TestNewQuerier_FindManyDeviceArrayWithNum(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
q := NewQuerier(conn)
one, two := int32(1), int32(2)
t.Run("FindManyDeviceArrayWithNum", func(t *testing.T) {
devices, err := q.FindManyDeviceArrayWithNum(ctx)
require.NoError(t, err)
assert.Equal(t, []FindManyDeviceArrayWithNumRow{
{Num: &one, DeviceTypes: allDeviceTypes[3:]},
{Num: &two, DeviceTypes: allDeviceTypes},
}, devices)
})
}
func TestNewQuerier_EnumInsideComposite(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()
q := NewQuerier(conn)
mac, _ := net.ParseMAC("08:00:2b:01:02:03")
t.Run("EnumInsideComposite", func(t *testing.T) {
device, err := q.EnumInsideComposite(ctx)
require.NoError(t, err)
assert.Equal(t,
Device{Mac: pgtype.Macaddr{Addr: mac, Status: pgtype.Present}, Type: DeviceTypePhone},
device,
)
})
}
func insertDevice(t *testing.T, q *DBQuerier, mac net.HardwareAddr, device DeviceType) {
t.Helper()
_, err := q.InsertDevice(context.Background(),
pgtype.Macaddr{Addr: mac, Status: pgtype.Present},
device,
)
require.NoError(t, err)
}