|
| 1 | +// Copyright 2024 chenmingyong0423 |
| 2 | + |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package validator |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "log" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/go-playground/validator/v10" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + |
| 28 | + "github.com/chenmingyong0423/go-mongox/operation" |
| 29 | +) |
| 30 | + |
| 31 | +func TestExecute(t *testing.T) { |
| 32 | + type User struct { |
| 33 | + Name string `bson:"name"` |
| 34 | + Age int `bson:"age" validate:"gte=0,lte=150"` |
| 35 | + } |
| 36 | + |
| 37 | + testCases := []struct { |
| 38 | + name string |
| 39 | + |
| 40 | + ctx context.Context |
| 41 | + doc *operation.OpContext |
| 42 | + opType operation.OpType |
| 43 | + |
| 44 | + errFunc require.ErrorAssertionFunc |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "unaccepted operation type", |
| 48 | + ctx: context.Background(), |
| 49 | + doc: operation.NewOpContext(nil, operation.WithDoc(&User{Name: "Mingyong Chen", Age: 18})), |
| 50 | + opType: operation.OpTypeAfterInsert, |
| 51 | + errFunc: require.NoError, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "nil value", |
| 55 | + ctx: context.Background(), |
| 56 | + doc: operation.NewOpContext(nil), |
| 57 | + opType: operation.OpTypeBeforeInsert, |
| 58 | + errFunc: require.NoError, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "unsupported type", |
| 62 | + ctx: context.Background(), |
| 63 | + doc: operation.NewOpContext(nil, operation.WithDoc(6)), |
| 64 | + opType: operation.OpTypeBeforeInsert, |
| 65 | + errFunc: require.NoError, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "unsupported point type", |
| 69 | + ctx: context.Background(), |
| 70 | + doc: operation.NewOpContext(nil, operation.WithDoc(func() *int { |
| 71 | + i := 6 |
| 72 | + return &i |
| 73 | + }())), |
| 74 | + opType: operation.OpTypeBeforeInsert, |
| 75 | + errFunc: require.NoError, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "special unsupported type - time.Time{}", |
| 79 | + ctx: context.Background(), |
| 80 | + doc: operation.NewOpContext(nil, operation.WithDoc(&time.Time{})), |
| 81 | + opType: operation.OpTypeBeforeInsert, |
| 82 | + errFunc: require.NoError, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "*User(nil)", |
| 86 | + ctx: context.Background(), |
| 87 | + doc: operation.NewOpContext(nil, operation.WithDoc((*User)(nil))), |
| 88 | + opType: operation.OpTypeBeforeInsert, |
| 89 | + errFunc: require.NoError, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "fails to validate struct in case of BeforeInsert", |
| 93 | + ctx: context.Background(), |
| 94 | + doc: operation.NewOpContext(nil, operation.WithDoc(&User{Age: -1})), |
| 95 | + opType: operation.OpTypeBeforeInsert, |
| 96 | + errFunc: func(t require.TestingT, err error, i ...interface{}) { |
| 97 | + var e validator.ValidationErrors |
| 98 | + if !errors.As(err, &e) { |
| 99 | + log.Fatal(err) |
| 100 | + } |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "fails to validate struct in case of BeforeUpsert", |
| 105 | + ctx: context.Background(), |
| 106 | + doc: operation.NewOpContext(nil, operation.WithReplacement(&User{Age: -1})), |
| 107 | + opType: operation.OpTypeBeforeUpsert, |
| 108 | + errFunc: func(t require.TestingT, err error, i ...interface{}) { |
| 109 | + var e validator.ValidationErrors |
| 110 | + if !errors.As(err, &e) { |
| 111 | + log.Fatal(err) |
| 112 | + } |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "fails to validate slice in case of BeforeInsert", |
| 117 | + ctx: context.Background(), |
| 118 | + doc: operation.NewOpContext(nil, operation.WithDoc([]*User{ |
| 119 | + {Age: -1}, |
| 120 | + {Age: 18}, |
| 121 | + })), |
| 122 | + opType: operation.OpTypeBeforeInsert, |
| 123 | + errFunc: func(t require.TestingT, err error, i ...interface{}) { |
| 124 | + var e validator.ValidationErrors |
| 125 | + if !errors.As(err, &e) { |
| 126 | + log.Fatal(err) |
| 127 | + } |
| 128 | + }, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "validate struct successfully in case of BeforeInsert", |
| 132 | + ctx: context.Background(), |
| 133 | + doc: operation.NewOpContext(nil, operation.WithDoc(&User{Age: 18})), |
| 134 | + opType: operation.OpTypeBeforeInsert, |
| 135 | + errFunc: require.NoError, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "validate slice successfully in case of BeforeInsert", |
| 139 | + ctx: context.Background(), |
| 140 | + doc: operation.NewOpContext(nil, operation.WithDoc([]*User{ |
| 141 | + {Age: 18}, |
| 142 | + {Age: 20}, |
| 143 | + })), |
| 144 | + opType: operation.OpTypeBeforeInsert, |
| 145 | + errFunc: require.NoError, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "validate struct successfully in case of BeforeUpsert", |
| 149 | + ctx: context.Background(), |
| 150 | + doc: operation.NewOpContext(nil, operation.WithReplacement(&User{Age: 18})), |
| 151 | + opType: operation.OpTypeBeforeUpsert, |
| 152 | + errFunc: require.NoError, |
| 153 | + }, |
| 154 | + } |
| 155 | + |
| 156 | + for _, tc := range testCases { |
| 157 | + t.Run(tc.name, func(t *testing.T) { |
| 158 | + err := Execute(tc.ctx, tc.doc, tc.opType) |
| 159 | + tc.errFunc(t, err) |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
0 commit comments