Passed
Push — main ( 2a46cd...12059c )
by Igor
01:41
created

test/validate_iterable_test.go   A

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 49
dl 0
loc 74
c 0
b 0
f 0
rs 10
1
package test
2
3
import (
4
	"testing"
5
6
	"github.com/muonsoft/validation"
7
	"github.com/muonsoft/validation/code"
8
	"github.com/muonsoft/validation/it"
9
	"github.com/muonsoft/validation/validationtest"
10
	"github.com/muonsoft/validation/validator"
11
	"github.com/stretchr/testify/assert"
12
)
13
14
func TestValidateIterable_WhenSliceOfValidatable_ExpectViolationsWithValidPaths(t *testing.T) {
15
	strings := []mockValidatableString{{value: ""}}
16
17
	err := validator.ValidateValue(strings)
18
19
	validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool {
20
		t.Helper()
21
		if assert.Len(t, violations, 1) {
22
			assert.Equal(t, code.NotBlank, violations[0].Code())
23
			assert.Equal(t, "[0].value", violations[0].PropertyPath().String())
24
		}
25
		return true
26
	})
27
}
28
29
func TestValidateIterable_WhenSliceOfValidatableWithConstraints_ExpectCollectionViolationsWithValidPaths(t *testing.T) {
30
	strings := []mockValidatableString{{value: ""}}
31
32
	err := validator.ValidateValue(strings, it.HasMinCount(2))
33
34
	validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool {
35
		t.Helper()
36
		if assert.Len(t, violations, 2) {
37
			assert.Equal(t, code.CountTooFew, violations[0].Code())
38
			assert.Equal(t, "", violations[0].PropertyPath().String())
39
			assert.Equal(t, code.NotBlank, violations[1].Code())
40
			assert.Equal(t, "[0].value", violations[1].PropertyPath().String())
41
		}
42
		return true
43
	})
44
}
45
46
func TestValidateIterable_WhenMapOfValidatable_ExpectViolationsWithValidPaths(t *testing.T) {
47
	strings := map[string]mockValidatableString{"key": {value: ""}}
48
49
	err := validator.ValidateValue(strings)
50
51
	validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool {
52
		t.Helper()
53
		if assert.Len(t, violations, 1) {
54
			assert.Equal(t, code.NotBlank, violations[0].Code())
55
			assert.Equal(t, "key.value", violations[0].PropertyPath().String())
56
		}
57
		return true
58
	})
59
}
60
61
func TestValidateIterable_WhenMapOfValidatableWithConstraints_ExpectCollectionViolationsWithValidPaths(t *testing.T) {
62
	strings := map[string]mockValidatableString{"key": {value: ""}}
63
64
	err := validator.ValidateValue(strings, it.HasMinCount(2))
65
66
	validationtest.AssertIsViolationList(t, err, func(t *testing.T, violations validation.ViolationList) bool {
67
		t.Helper()
68
		if assert.Len(t, violations, 2) {
69
			assert.Equal(t, code.CountTooFew, violations[0].Code())
70
			assert.Equal(t, "", violations[0].PropertyPath().String())
71
			assert.Equal(t, code.NotBlank, violations[1].Code())
72
			assert.Equal(t, "key.value", violations[1].PropertyPath().String())
73
		}
74
		return true
75
	})
76
}
77