-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnap_error_test.go
More file actions
83 lines (66 loc) · 1.77 KB
/
Copy pathsnap_error_test.go
File metadata and controls
83 lines (66 loc) · 1.77 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
package pgsnap
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/stretchr/testify/assert"
)
// wrong query, should not cause a panic.
func Test_error_case(t *testing.T) {
s := NewSnap(t, addr)
defer s.Finish()
ctx := context.Background()
conn, _ := pgx.Connect(ctx, s.Addr())
defer conn.Close(ctx)
t.Run("non_existent_table", func(t *testing.T) {
_, err := conn.Query(ctx, "SELECT * FROM non_existing_table WHERE id = $1", 1)
assert.Equal(t, err, &pgconn.PgError{
Severity: "ERROR",
Code: "42P01",
Message: "relation \"non_existing_table\" does not exist",
Position: 15,
InternalPosition: 0,
File: "parse_relation.c",
Line: 1376,
Routine: "parserOpenTable",
})
})
t.Run("empty_rows", func(t *testing.T) {
rows, err := conn.Query(ctx, "SELECT * FROM mytable WHERE id = $1", 4)
assert.NoError(t, err)
assert.False(t, rows.Next())
})
}
func Test_if_not_accept_should_throw_timeout(t *testing.T) {
tb := newFakeTB(t)
s := NewSnapWithConfig(tb, addr, Config{
TestTimeout: 10 * time.Millisecond,
})
defer s.Finish()
time.Sleep(100 * time.Millisecond)
assert.True(t, tb.FailNowCalled.Load())
}
type fakeTB struct {
testing.TB
ErrorMessages []string
FailNowCalled *atomic.Bool
}
func newFakeTB(t testing.TB) *fakeTB {
return &fakeTB{
TB: t,
FailNowCalled: &atomic.Bool{},
}
}
func (f *fakeTB) Error(args ...interface{}) {
f.ErrorMessages = append(f.ErrorMessages, fmt.Sprintln(args...))
}
func (f *fakeTB) Errorf(format string, args ...interface{}) {
f.ErrorMessages = append(f.ErrorMessages, fmt.Sprintf(format, args...))
}
func (f *fakeTB) FailNow() {
f.FailNowCalled.Store(true)
}