github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/ev/validator_dep_test.go (about) 1 package ev 2 3 import ( 4 "github.com/go-email-validator/go-email-validator/pkg/ev/evmail" 5 "github.com/go-email-validator/go-email-validator/pkg/ev/evtests" 6 mockevmail "github.com/go-email-validator/go-email-validator/test/mock/ev/evmail" 7 "github.com/stretchr/testify/require" 8 "reflect" 9 "testing" 10 "time" 11 ) 12 13 type testSleep struct { 14 sleep time.Duration 15 mockValidator 16 deps []ValidatorName 17 } 18 19 func (t testSleep) GetDeps() []ValidatorName { 20 return t.deps 21 } 22 23 func (t testSleep) Validate(_ Input, results ...ValidationResult) ValidationResult { 24 time.Sleep(t.sleep) 25 26 var isValid = true 27 for _, result := range results { 28 if !result.IsValid() { 29 isValid = false 30 break 31 } 32 } 33 34 return NewDepValidatorResult(isValid && t.result, nil) 35 } 36 37 func TestDepValidator_Validate_Independent(t *testing.T) { 38 email := mockevmail.GetValidTestEmail() 39 strings := emptyDeps 40 41 depValidator := NewDepValidator( 42 map[ValidatorName]Validator{ 43 "test1": &testSleep{ 44 0, 45 newMockValidator(true), 46 strings, 47 }, 48 "test2": &testSleep{ 49 0, 50 newMockValidator(true), 51 strings, 52 }, 53 "test3": &testSleep{ 54 0, 55 newMockValidator(false), 56 strings, 57 }, 58 }, 59 ) 60 61 v := depValidator.Validate(NewInput(email)) 62 require.False(t, v.IsValid()) 63 } 64 65 func TestDepValidator_Validate_Dependent(t *testing.T) { 66 email := mockevmail.GetValidTestEmail() 67 strings := emptyDeps 68 69 depValidator := NewDepValidator(map[ValidatorName]Validator{ 70 "test1": &testSleep{ 71 100 * time.Millisecond, 72 newMockValidator(true), 73 strings, 74 }, 75 "test2": &testSleep{ 76 100 * time.Millisecond, 77 newMockValidator(true), 78 strings, 79 }, 80 "test3": &testSleep{ 81 100 * time.Millisecond, 82 newMockValidator(true), 83 []ValidatorName{"test1", "test2"}, 84 }, 85 }, 86 ) 87 88 v := depValidator.Validate(NewInput(email)) 89 require.True(t, v.IsValid()) 90 } 91 92 func TestDepValidator_Validate_Full(t *testing.T) { 93 evtests.FunctionalSkip(t) 94 95 email := evmail.FromString(mockevmail.ValidEmailString) 96 depValidator := NewDepBuilder(nil).Build() 97 98 v := depValidator.Validate(NewInput(email)) 99 require.True(t, v.IsValid()) 100 } 101 102 func Test_depValidationResult_Errors(t *testing.T) { 103 type fields struct { 104 isValid bool 105 results DepResult 106 } 107 tests := []struct { 108 name string 109 fields fields 110 want []error 111 }{ 112 { 113 name: "with Errors", 114 fields: fields{ 115 isValid: false, 116 results: DepResult{ 117 mockValidatorName: mockValidationResult{errs: []error{errorSimple, errorSimple2}}, 118 SyntaxValidatorName: mockValidationResult{errs: []error{errorSimple2, errorSimple}}, 119 }, 120 }, 121 want: []error{errorSimple, errorSimple2, errorSimple2, errorSimple}, 122 }, 123 { 124 name: "without Errors", 125 fields: fields{ 126 isValid: false, 127 results: DepResult{ 128 mockValidatorName: mockValidationResult{}, 129 SyntaxValidatorName: mockValidationResult{}, 130 }, 131 }, 132 want: nil, 133 }, 134 } 135 for _, tt := range tests { 136 t.Run(tt.name, func(t *testing.T) { 137 d := NewDepValidatorResult(tt.fields.isValid, tt.fields.results) 138 139 got := sortErrors(d.Errors()) 140 tt.want = sortErrors(tt.want) 141 142 if !reflect.DeepEqual(got, tt.want) { 143 t.Errorf("Errors() = %v, want %v", got, tt.want) 144 } 145 }) 146 } 147 } 148 149 func Test_depValidationResult_Warnings(t *testing.T) { 150 type fields struct { 151 isValid bool 152 results DepResult 153 } 154 tests := []struct { 155 name string 156 fields fields 157 wantWarnings []error 158 }{ 159 { 160 name: "with Warnings", 161 fields: fields{ 162 isValid: false, 163 results: DepResult{ 164 mockValidatorName: mockValidationResult{warns: []error{errorSimple, errorSimple2}}, 165 SyntaxValidatorName: mockValidationResult{warns: []error{errorSimple2, errorSimple}}, 166 }, 167 }, 168 wantWarnings: []error{errorSimple, errorSimple2, errorSimple2, errorSimple}, 169 }, 170 { 171 name: "without Warnings", 172 fields: fields{ 173 isValid: false, 174 results: DepResult{ 175 mockValidatorName: mockValidationResult{}, 176 SyntaxValidatorName: mockValidationResult{}, 177 }, 178 }, 179 wantWarnings: nil, 180 }, 181 } 182 for _, tt := range tests { 183 t.Run(tt.name, func(t *testing.T) { 184 d := NewDepValidatorResult(tt.fields.isValid, tt.fields.results) 185 186 gotWarnings := sortErrors(d.Warnings()) 187 tt.wantWarnings = sortErrors(tt.wantWarnings) 188 189 if !reflect.DeepEqual(gotWarnings, tt.wantWarnings) { 190 t.Errorf("Warnings() = %v, want %v", gotWarnings, tt.wantWarnings) 191 } 192 }) 193 } 194 }