forked from go-gorp/gorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
120 lines (100 loc) · 2.97 KB
/
errors_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
// Copyright 2012 James Cooper. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gorp_test
import (
"errors"
"testing"
"github.com/go-gorp/gorp/v3"
"github.com/stretchr/testify/suite"
)
type ErrorsTestSuite struct {
suite.Suite
}
func TestErrorsTestSuite(t *testing.T) {
suite.Run(t, new(ErrorsTestSuite))
}
func (s *ErrorsTestSuite) TestNoFieldInTypeError() {
// Test with single missing column
err1 := &gorp.NoFieldInTypeError{
TypeName: "User",
MissingColNames: []string{"age"},
}
s.Equal("gorp: no fields [age] in type User", err1.Error())
// Test with multiple missing columns
err2 := &gorp.NoFieldInTypeError{
TypeName: "Product",
MissingColNames: []string{"price", "quantity", "category"},
}
s.Equal("gorp: no fields [price quantity category] in type Product", err2.Error())
// Test with empty missing columns
err3 := &gorp.NoFieldInTypeError{
TypeName: "Empty",
MissingColNames: []string{},
}
s.Equal("gorp: no fields [] in type Empty", err3.Error())
}
func (s *ErrorsTestSuite) TestInvalidDialectError() {
// Test with standard SQL dialect
err1 := &gorp.InvalidDialectError{
Dialect: "mysql",
}
s.Equal("gorp: invalid dialect specified: mysql", err1.Error())
// Test with empty dialect
err2 := &gorp.InvalidDialectError{
Dialect: "",
}
s.Equal("gorp: invalid dialect specified: ", err2.Error())
// Test with non-standard dialect
err3 := &gorp.InvalidDialectError{
Dialect: "custom-dialect",
}
s.Equal("gorp: invalid dialect specified: custom-dialect", err3.Error())
}
func (s *ErrorsTestSuite) TestTableNotFoundError() {
// Test without schema
err1 := &gorp.TableNotFoundError{
Table: "users",
}
s.Equal("gorp: table users not found", err1.Error())
// Test with schema
err2 := &gorp.TableNotFoundError{
Schema: "app",
Table: "products",
}
s.Equal("gorp: table app.products not found", err2.Error())
// Test with empty table name
err3 := &gorp.TableNotFoundError{
Table: "",
}
s.Equal("gorp: table not found", err3.Error())
// Test with empty schema but table name
err4 := &gorp.TableNotFoundError{
Schema: "",
Table: "orders",
}
s.Equal("gorp: table orders not found", err4.Error())
}
func (s *ErrorsTestSuite) TestNonFatalError() {
// Test nil error
s.False(gorp.NonFatalError(nil))
// Test NoFieldInTypeError (should be non-fatal)
noFieldErr := &gorp.NoFieldInTypeError{
TypeName: "User",
MissingColNames: []string{"age"},
}
s.True(gorp.NonFatalError(noFieldErr))
// Test TableNotFoundError (should be non-fatal)
tableNotFoundErr := &gorp.TableNotFoundError{
Table: "users",
}
s.True(gorp.NonFatalError(tableNotFoundErr))
// Test InvalidDialectError (should be fatal)
dialectErr := &gorp.InvalidDialectError{
Dialect: "invalid",
}
s.False(gorp.NonFatalError(dialectErr))
// Test standard error (should be fatal)
standardErr := errors.New("standard error")
s.False(gorp.NonFatalError(standardErr))
}