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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
package client
import (
"testing"
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testValidationModel struct {
Name string `validate:"min=1,max=64"`
Age int `validate:"min=0,max=150"`
Email string `validate:"omitempty,email"`
Tags []string `validate:"dive,max=10"`
Score *int `validate:"omitempty,min=0,max=100"`
FloodVLANs []int `validate:"max=4096,dive,min=0,max=4095"`
Protocols []string `validate:"dive,oneof='OpenFlow10' 'OpenFlow11' 'OpenFlow12' 'OpenFlow13' 'OpenFlow14' 'OpenFlow15'"`
Mappings map[int]int `validate:"dive,keys,min=0,max=16777215,endkeys,min=0,max=4095"`
Config map[string]string `validate:"dive,keys,min=1,max=32,endkeys,min=1,max=256"`
KeyConfig map[int]int `validate:"dive,keys,min=0,max=10"`
ValueConfig map[string]int `validate:"dive,min=0,max=10"`
IsActive bool
}
type testValidationNestedModel struct {
ID string `validate:"uuid"`
Detail testValidationModel `validate:"required"`
}
func intPtr(i int) *int {
return &i
}
func TestValidateModel_Nil(t *testing.T) {
err := validateModel(nil)
require.Error(t, err)
assert.ErrorContains(t, err, "model cannot be nil")
}
func TestValidateModel_Valid(t *testing.T) {
model := testValidationModel{
Name: "Test Name",
Age: 30,
Email: "test@example.com",
Tags: []string{"tag1", "tag2"},
Score: intPtr(85),
FloodVLANs: []int{100, 200, 4095},
Protocols: []string{"OpenFlow13", "OpenFlow15"},
Mappings: map[int]int{100: 500, 200: 1000, 16777215: 4095},
Config: map[string]string{"key1": "value1", "key2": "value2"},
KeyConfig: map[int]int{1: 1, 10: 10},
ValueConfig: map[string]int{"key1": 1, "key2": 9},
IsActive: true,
}
err := validateModel(&model)
require.NoError(t, err)
}
func TestValidateModel_Invalid(t *testing.T) {
tests := []struct {
name string
model testValidationModel
expectedError string
}{
{
name: "invalid empty name",
model: testValidationModel{
Name: "",
Age: 30,
},
expectedError: "field 'testValidationModel.Name' (value: '') failed on rule 'min' (param: 1)",
},
{
name: "invalid age",
model: testValidationModel{
Name: "Test Name",
Age: -5,
},
expectedError: "field 'testValidationModel.Age' (value: '-5') failed on rule 'min' (param: 0)",
},
{
name: "invalid email",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Email: "not-an-email",
},
expectedError: "field 'testValidationModel.Email' (value: 'not-an-email') failed on rule 'email'",
},
{
name: "invalid score",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Score: intPtr(150),
},
expectedError: "field 'testValidationModel.Score' (value: '150') failed on rule 'max' (param: 100)",
},
{
name: "invalid FloodVLAN value (too high)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
FloodVLANs: []int{100, 4096},
},
expectedError: "field 'testValidationModel.FloodVLANs[1]' (value: '4096') failed on rule 'max' (param: 4095)",
},
{
name: "invalid FloodVLAN value (too low)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
FloodVLANs: []int{-1, 100},
},
expectedError: "field 'testValidationModel.FloodVLANs[0]' (value: '-1') failed on rule 'min' (param: 0)",
},
{
name: "invalid Protocol value",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Protocols: []string{"OpenFlow13", "InvalidProtocol"},
},
expectedError: "field 'testValidationModel.Protocols[1]' (value: 'InvalidProtocol') failed on rule 'oneof' (param: 'OpenFlow10' 'OpenFlow11' 'OpenFlow12' 'OpenFlow13' 'OpenFlow14' 'OpenFlow15')",
},
{
name: "invalid Mappings key (too high)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Mappings: map[int]int{16777216: 1000},
},
expectedError: "field 'testValidationModel.Mappings[16777216]' (value: '16777216') failed on rule 'max' (param: 16777215)",
},
{
name: "invalid Mappings key (too low)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Mappings: map[int]int{-1: 1000},
},
expectedError: "field 'testValidationModel.Mappings[-1]' (value: '-1') failed on rule 'min' (param: 0)",
},
{
name: "invalid Mappings value (too high)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Mappings: map[int]int{100: 4096},
},
expectedError: "field 'testValidationModel.Mappings[100]' (value: '4096') failed on rule 'max' (param: 4095)",
},
{
name: "invalid Mappings value (too low)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Mappings: map[int]int{100: -1},
},
expectedError: "field 'testValidationModel.Mappings[100]' (value: '-1') failed on rule 'min' (param: 0)",
},
{
name: "invalid Config key (too short)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Config: map[string]string{"": "value1"},
},
expectedError: "field 'testValidationModel.Config[]' (value: '') failed on rule 'min' (param: 1)",
},
{
name: "invalid Config key (too long)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Config: map[string]string{"verylongkeythatexceedsthirtytwocharacters": "value1"},
},
expectedError: "field 'testValidationModel.Config[verylongkeythatexceedsthirtytwocharacters]' (value: 'verylongkeythatexceedsthirtytwocharacters') failed on rule 'max' (param: 32)",
},
{
name: "invalid Config value (too short)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Config: map[string]string{"key1": ""},
},
expectedError: "field 'testValidationModel.Config[key1]' (value: '') failed on rule 'min' (param: 1)",
},
{
name: "invalid Config value (too long)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
Config: map[string]string{"key1": "verylongvaluethatexceedstwohundredandfiftysixcharacterslimitverylongvaluethatexceedstwohundredandfiftysixcharacterslimitverylongvaluethatexceedstwohundredandfiftysixcharacterslimitverylongvaluethatexceedstwohundredandfiftysixcharacterslimitverylongvaluethatexceedstwohundredandfiftysixcharacterslimit"},
},
expectedError: "field 'testValidationModel.Config[key1]' (value: 'verylongvaluethatexceedstwohundredandfiftysixcharacterslimitverylongvaluethatexceedstwohundredandfiftysixcharacterslimitverylongvaluethatexceedstwohundredandfiftysixcharacterslimitverylongvaluethatexceedstwohundredandfiftysixcharacterslimitverylongvaluethatexceedstwohundredandfiftysixcharacterslimit') failed on rule 'max' (param: 256)",
},
{
name: "invalid KeyConfig key (too large)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
KeyConfig: map[int]int{11: 999},
},
expectedError: "field 'testValidationModel.KeyConfig[11]' (value: '11') failed on rule 'max' (param: 10)",
},
{
name: "invalid ValueConfig value (too large)",
model: testValidationModel{
Name: "Test Name",
Age: 30,
ValueConfig: map[string]int{"key1": 11, "key2": 10},
},
expectedError: "field 'testValidationModel.ValueConfig[key1]' (value: '11') failed on rule 'max' (param: 10)",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := validateModel(&tc.model)
require.Error(t, err)
require.ErrorContains(t, err, "model validation failed:")
if tc.expectedError != "" {
require.ErrorContains(t, err, tc.expectedError)
}
var valErrs validator.ValidationErrors
require.ErrorAs(t, err, &valErrs)
assert.NotEmpty(t, valErrs)
})
}
}
func TestValidateModel_NestedStruct(t *testing.T) {
validNestedModel := testValidationNestedModel{
ID: "550e8400-e29b-41d4-a716-446655440000",
Detail: testValidationModel{
Name: "Test Name",
Age: 30,
FloodVLANs: []int{100, 200},
Protocols: []string{"OpenFlow13"},
Mappings: map[int]int{100: 500},
Config: map[string]string{"key1": "value1"},
},
}
err := validateModel(&validNestedModel)
require.NoError(t, err)
invalidNestedModel := testValidationNestedModel{
ID: "invalid-uuid",
Detail: testValidationModel{
Name: "Test Name",
Age: 30,
},
}
err = validateModel(&invalidNestedModel)
require.Error(t, err)
require.ErrorContains(t, err, "model validation failed:")
require.ErrorContains(t, err, "field 'testValidationNestedModel.ID' (value: 'invalid-uuid') failed on rule 'uuid'")
var valErrs validator.ValidationErrors
require.ErrorAs(t, err, &valErrs)
assert.NotEmpty(t, valErrs)
}
func TestValidateModel_SliceValidation(t *testing.T) {
model := testValidationModel{
Name: "Test Name",
Age: 30,
Tags: []string{"this-tag-is-more-than-ten-characters-long", "valid-tag"},
}
err := validateModel(&model)
require.Error(t, err)
require.ErrorContains(t, err, "model validation failed:")
require.ErrorContains(t, err, "field 'testValidationModel.Tags[0]' (value: 'this-tag-is-more-than-ten-characters-long') failed on rule 'max' (param: 10)")
var valErrs validator.ValidationErrors
require.ErrorAs(t, err, &valErrs)
assert.NotEmpty(t, valErrs)
}
func TestFormatValidationErrors(t *testing.T) {
// Test the formatValidationErrors function directly
model := testValidationModel{
Name: "",
Age: -5,
}
err := validate.Struct(&model)
require.Error(t, err)
var valErrs validator.ValidationErrors
require.ErrorAs(t, err, &valErrs)
// Test without context
formatted := formatValidationErrors("client.testValidationModel", "", valErrs)
assert.Contains(t, formatted, "validation error for model client.testValidationModel")
assert.Contains(t, formatted, "field 'testValidationModel.Name' (value: '') failed on rule 'min' (param: 1)")
assert.Contains(t, formatted, "field 'testValidationModel.Age' (value: '-5') failed on rule 'min' (param: 0)")
// Test with context
formatted = formatValidationErrors("client.testValidationModel", "mutation on column name", valErrs)
assert.Contains(t, formatted, "validation error for model client.testValidationModel: mutation on column name")
assert.Contains(t, formatted, "field 'testValidationModel.Name' (value: '') failed on rule 'min' (param: 1)")
}
|