-
Notifications
You must be signed in to change notification settings - Fork 2
/
expectations_test.go
117 lines (99 loc) · 3.32 KB
/
expectations_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
// 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 mockhouse
import (
"errors"
"fmt"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestExpectationsExpectedQuery(t *testing.T) {
ex := ExpectedQuery{}
ex.WillDelayFor(1).RowsWillBeClosed()
assert.True(t, ex.delay > 0)
assert.True(t, ex.rowsMustBeClosed)
}
func TestExpectationsExpectedPrepareBatch(t *testing.T) {
ex := ExpectedPrepareBatch{}
ex.ExpectAbort().WillReturnError(fmt.Errorf("error"))
assert.True(t, ex.abortErr != nil)
}
func TestExpectedClose(t *testing.T) {
t.Parallel()
expectedClose := &ExpectedClose{}
// Test WillReturnError method
err := errors.New("some error")
expectedClose.WillReturnError(err)
if expectedClose.err != err {
t.Errorf("Expected error to be set to %v, got %v", err, expectedClose.err)
}
// Test String method
expectedString := "ExpectedClose => expecting database Close, which should return error: some error"
if expectedClose.String() != expectedString {
t.Errorf("Expected string representation to be %v, got %v", expectedString, expectedClose.String())
}
}
func TestExpectedQuery(t *testing.T) {
t.Parallel()
expectedQuery := &ExpectedQuery{
queryBasedExpectation: queryBasedExpectation{
expectSQL: "SELECT * FROM table",
},
}
// Test WithArgs method
args := []any{1, "test"}
expectedQuery.WithArgs(args...)
if !reflect.DeepEqual(expectedQuery.args, args) {
t.Errorf("Expected args to be %v, got %v", args, expectedQuery.args)
}
// Test WillReturnError method
err := errors.New("some error")
expectedQuery.WillReturnError(err)
if expectedQuery.err != err {
t.Errorf("Expected error to be %v, got %v", err, expectedQuery.err)
}
// Test WillDelayFor method
duration := time.Second * 5
expectedQuery.WillDelayFor(duration)
if expectedQuery.delay != duration {
t.Errorf("Expected delay to be %v, got %v", duration, expectedQuery.delay)
}
// Test WillReturnRows method
rows := &Rows{}
expectedQuery.WillReturnRows(rows)
if expectedQuery.rows != rows {
t.Errorf("Expected rows to be %v, got %v", rows, expectedQuery.rows)
}
}
func TestExpectedPing(t *testing.T) {
t.Parallel()
expectedPing := &ExpectedPing{}
// Test WillDelayFor method
duration := time.Second * 5
expectedPing.WillDelayFor(duration)
if expectedPing.delay != duration {
t.Errorf("Expected delay to be %v, got %v", duration, expectedPing.delay)
}
// Test WillReturnError method
err := errors.New("some error")
expectedPing.WillReturnError(err)
if expectedPing.err != err {
t.Errorf("Expected error to be %v, got %v", err, expectedPing.err)
}
// Test String method
expectedString := "ExpectedPing => expecting database Ping, which should return error: some error"
if expectedPing.String() != expectedString {
t.Errorf("Expected string representation to be %v, got %v", expectedString, expectedPing.String())
}
}