forked from cockroachdb/copyist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyist_test.go
155 lines (129 loc) · 3.88 KB
/
copyist_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2020 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package copyist
import (
"bytes"
"database/sql"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
// TestOpenWithoutRegister tests that copyist.Open panics if copyist.Register
// was never called.
func TestOpenWithoutRegister(t *testing.T) {
require.False(t, IsOpen())
require.PanicsWithError(t, "Register was not called", func() {
Open(t)
})
}
// TestMultipleRegisterCalls tests that copyist.Register is an error when called
// multiple times. Test that there is no error when calling with a different
// driver.
func TestMultipleRegisterCalls(t *testing.T) {
Register("multiple-register-driver-1")
require.PanicsWithError(t, "Register called twice for driver multiple-register-driver-1", func() {
Register("multiple-register-driver-1")
})
// Should be no error.
Register("multiple-register-driver-2")
}
// TestUnknownDriver tests that copyist.Driver.Open returns an error when an
// unknown driver name is passed to copyist.Register.
func TestUnknownDriver(t *testing.T) {
// Force recording mode.
*recordFlag = true
visitedRecording = true
registered = nil
Register("unknown")
Open(t)
db, err := sql.Open("copyist_unknown", "")
require.NoError(t, err)
_, err = db.Query("SELECT 1")
require.Error(t, err, `sql: unknown driver "unknown"`)
}
// TestRecordingNotFound tests that copyist panics when trying to playback a
// recording that does not exist.
func TestRecordingNotFound(t *testing.T) {
// Enter playback mode.
*recordFlag = false
visitedRecording = true
registered = nil
Register("postgres")
Open(t)
db, err := sql.Open("copyist_postgres", "")
require.NoError(t, err)
require.PanicsWithError(
t,
`no recording exists with this name: TestRecordingNotFound`,
func() { db.Query("SELECT 1") },
)
}
type mockTestingT struct {
*testing.T
buf bytes.Buffer
}
func (t *mockTestingT) Fatalf(format string, args ...interface{}) {
fmt.Fprintf(&t.buf, format, args...)
}
func TestSessionFailuresAreFatalfd(t *testing.T) {
// Enter playback mode.
*recordFlag = false
visitedRecording = true
registered = nil
Register("postgres2")
m := &mockTestingT{T: t}
defer func() {
require.Equal(t, "no recording exists with this name: TestSessionFailuresAreFatalfd\n",
m.buf.String())
}()
defer Open(m).Close()
db, err := sql.Open("copyist_postgres2", "")
require.NoError(t, err)
// NB: This will return an error, but the the copyist closer will track the
// first error and convert it into a call to testing.T.Fatalf.
db.Query("SELECT 1")
}
func TestNonSessionPanicsAreNotCaught(t *testing.T) {
// Enter playback mode.
*recordFlag = false
visitedRecording = true
registered = nil
Register("postgres3")
defer func() {
require.Equal(t, recover(), "test panic")
}()
defer Open(t).Close()
panic("test panic")
}
// TestCopyistEnvVar tests that copyist respects the COPYIST_RECORD environment
// variable.
func TestCopyistEnvVar(t *testing.T) {
// Enter playback mode.
require.NoError(t, os.Setenv("COPYIST_RECORD", "TRUE"))
*recordFlag = false
visitedRecording = false
require.True(t, IsRecording())
}
// TestFindTestFile tests that copyist finds the top-level *_test.go file.
func TestFindTestFile(t *testing.T) {
require.Equal(t, "copyist_test.go", filepath.Base(indirectFindTestFile()))
}
func ignorePanic(f func()) {
defer func() {
recover()
}()
f()
}