1
- package schema
1
+ package schema_test
2
2
3
3
import (
4
4
"errors"
@@ -7,21 +7,23 @@ import (
7
7
"testing"
8
8
9
9
"github.com/stretchr/testify/assert"
10
+
11
+ "github.com/rs/rest-layer/schema"
10
12
)
11
13
12
14
func TestFloatQueryValidator (t * testing.T ) {
13
15
cases := []struct {
14
16
name string
15
- field Float
17
+ field schema. Float
16
18
input , expect interface {}
17
19
err error
18
20
}{
19
- {`Float.ValidateQuery(float64)` , Float {}, 1.2 , 1.2 , nil },
20
- {`Float.ValidateQuery(int)` , Float {}, 1 , nil , errors .New ("not a float" )},
21
- {`Float.ValidateQuery(string)` , Float {}, "1.2" , nil , errors .New ("not a float" )},
22
- {"Float.ValidateQuery(float64)-out of range above" , Float {Boundaries : & Boundaries {Min : 0 , Max : 2 }}, 3.1 , 3.1 , nil },
23
- {"Float.ValidateQuery(float64)-in range" , Float {Boundaries : & Boundaries {Min : 0 , Max : 2 }}, 1.1 , 1.1 , nil },
24
- {"Float.ValidateQuery(float64)-out of range below" , Float {Boundaries : & Boundaries {Min : 2 , Max : 10 }}, 1.1 , 1.1 , nil },
21
+ {`Float.ValidateQuery(float64)` , schema. Float {}, 1.2 , 1.2 , nil },
22
+ {`Float.ValidateQuery(int)` , schema. Float {}, 1 , nil , errors .New ("not a float" )},
23
+ {`Float.ValidateQuery(string)` , schema. Float {}, "1.2" , nil , errors .New ("not a float" )},
24
+ {"Float.ValidateQuery(float64)-out of range above" , schema. Float {Boundaries : & schema. Boundaries {Min : 0 , Max : 2 }}, 3.1 , 3.1 , nil },
25
+ {"Float.ValidateQuery(float64)-in range" , schema. Float {Boundaries : & schema. Boundaries {Min : 0 , Max : 2 }}, 1.1 , 1.1 , nil },
26
+ {"Float.ValidateQuery(float64)-out of range below" , schema. Float {Boundaries : & schema. Boundaries {Min : 2 , Max : 10 }}, 1.1 , 1.1 , nil },
25
27
}
26
28
for i := range cases {
27
29
tt := cases [i ]
@@ -39,104 +41,53 @@ func TestFloatQueryValidator(t *testing.T) {
39
41
}
40
42
41
43
func TestFloatValidator (t * testing.T ) {
42
- s , err := Float {}.Validate (1.2 )
44
+ s , err := schema. Float {}.Validate (1.2 )
43
45
assert .NoError (t , err )
44
46
assert .Equal (t , 1.2 , s )
45
- s , err = Float {}.Validate (1 )
47
+ s , err = schema. Float {}.Validate (1 )
46
48
assert .EqualError (t , err , "not a float" )
47
49
assert .Nil (t , s )
48
- s , err = Float {}.Validate ("1.2" )
50
+ s , err = schema. Float {}.Validate ("1.2" )
49
51
assert .EqualError (t , err , "not a float" )
50
52
assert .Nil (t , s )
51
- s , err = Float {Boundaries : & Boundaries {Min : 0 , Max : 2 }}.Validate (3.1 )
53
+ s , err = schema. Float {Boundaries : & schema. Boundaries {Min : 0 , Max : 2 }}.Validate (3.1 )
52
54
assert .EqualError (t , err , "is greater than 2.00" )
53
55
assert .Nil (t , s )
54
- s , err = Float {Boundaries : & Boundaries {Min : 0 , Max : 2 }}.Validate (1.1 )
56
+ s , err = schema. Float {Boundaries : & schema. Boundaries {Min : 0 , Max : 2 }}.Validate (1.1 )
55
57
assert .NoError (t , err )
56
58
assert .Equal (t , 1.1 , s )
57
- s , err = Float {Boundaries : & Boundaries {Min : 2 , Max : 10 }}.Validate (1.1 )
59
+ s , err = schema. Float {Boundaries : & schema. Boundaries {Min : 2 , Max : 10 }}.Validate (1.1 )
58
60
assert .EqualError (t , err , "is lower than 2.00" )
59
61
assert .Nil (t , s )
60
- s , err = Float {Boundaries : & Boundaries {Min : 2 , Max : 10 }}.Validate (3.1 )
62
+ s , err = schema. Float {Boundaries : & schema. Boundaries {Min : 2 , Max : 10 }}.Validate (3.1 )
61
63
assert .NoError (t , err )
62
64
assert .Equal (t , 3.1 , s )
63
- s , err = Float {Boundaries : & Boundaries {Min : math .Inf (- 1 ), Max : 10 }}.Validate (3.1 )
65
+ s , err = schema. Float {Boundaries : & schema. Boundaries {Min : math .Inf (- 1 ), Max : 10 }}.Validate (3.1 )
64
66
assert .NoError (t , err )
65
67
assert .Equal (t , 3.1 , s )
66
- s , err = Float {Boundaries : & Boundaries {Min : math .NaN (), Max : 10 }}.Validate (3.1 )
68
+ s , err = schema. Float {Boundaries : & schema. Boundaries {Min : math .NaN (), Max : 10 }}.Validate (3.1 )
67
69
assert .NoError (t , err )
68
70
assert .Equal (t , 3.1 , s )
69
- s , err = Float {Boundaries : & Boundaries {Min : 2 , Max : math .Inf (1 )}}.Validate (3.1 )
71
+ s , err = schema. Float {Boundaries : & schema. Boundaries {Min : 2 , Max : math .Inf (1 )}}.Validate (3.1 )
70
72
assert .NoError (t , err )
71
73
assert .Equal (t , 3.1 , s )
72
- s , err = Float {Boundaries : & Boundaries {Min : 2 , Max : math .NaN ()}}.Validate (3.1 )
74
+ s , err = schema. Float {Boundaries : & schema. Boundaries {Min : 2 , Max : math .NaN ()}}.Validate (3.1 )
73
75
assert .NoError (t , err )
74
76
assert .Equal (t , 3.1 , s )
75
- s , err = Float {Boundaries : & Boundaries {}}.Validate (1.1 )
77
+ s , err = schema. Float {Boundaries : & schema. Boundaries {}}.Validate (1.1 )
76
78
assert .EqualError (t , err , "is greater than 0.00" )
77
79
assert .Nil (t , s )
78
- s , err = Float {Boundaries : & Boundaries {}}.Validate (- 1.1 )
80
+ s , err = schema. Float {Boundaries : & schema. Boundaries {}}.Validate (- 1.1 )
79
81
assert .EqualError (t , err , "is lower than 0.00" )
80
82
assert .Nil (t , s )
81
- s , err = Float {Allowed : []float64 {.1 , .2 , .3 }}.Validate (.4 )
83
+ s , err = schema. Float {Allowed : []float64 {.1 , .2 , .3 }}.Validate (.4 )
82
84
assert .EqualError (t , err , "not one of the allowed values" )
83
85
assert .Nil (t , s )
84
- s , err = Float {Allowed : []float64 {.1 , .2 , .3 }}.Validate (.2 )
86
+ s , err = schema. Float {Allowed : []float64 {.1 , .2 , .3 }}.Validate (.2 )
85
87
assert .NoError (t , err )
86
88
assert .Equal (t , .2 , s )
87
89
}
88
90
89
- func TestFloatParse (t * testing.T ) {
90
- cases := []struct {
91
- name string
92
- input , expect interface {}
93
- err error
94
- }{
95
- {`Float.parse(float64)` , 1.2 , 1.2 , nil },
96
- {`Float.parse(int)` , 1 , nil , errors .New ("not a float" )},
97
- {`Float.parse(string)` , "1.2" , nil , errors .New ("not a float" )},
98
- }
99
- for i := range cases {
100
- tt := cases [i ]
101
- t .Run (tt .name , func (t * testing.T ) {
102
- t .Parallel ()
103
- got , err := Float {}.parse (tt .input )
104
- if ! reflect .DeepEqual (err , tt .err ) {
105
- t .Errorf ("unexpected error for `%v`\n got: %v\n want: %v" , tt .input , err , tt .err )
106
- }
107
- if ! reflect .DeepEqual (got , tt .expect ) {
108
- t .Errorf ("invalid output for `%v`:\n got: %#v\n want: %#v" , tt .input , got , tt .expect )
109
- }
110
- })
111
- }
112
- }
113
-
114
- func TestFloatGet (t * testing.T ) {
115
- cases := []struct {
116
- name string
117
- field Float
118
- input , expect interface {}
119
- err error
120
- }{
121
- {`Float.get(float64)` , Float {}, 1.2 , 1.2 , nil },
122
- {`Float.get(int)` , Float {}, 1 , 0.0 , errors .New ("not a float" )},
123
- {`Float.get(string)` , Float {}, "1.2" , 0.0 , errors .New ("not a float" )},
124
- }
125
- for i := range cases {
126
- tt := cases [i ]
127
- t .Run (tt .name , func (t * testing.T ) {
128
- t .Parallel ()
129
- got , err := (tt .field ).get (tt .input )
130
- if ! reflect .DeepEqual (err , tt .err ) {
131
- t .Errorf ("unexpected error for `%v`\n got: %v\n want: %v" , tt .input , err , tt .err )
132
- }
133
- if ! reflect .DeepEqual (got , tt .expect ) {
134
- t .Errorf ("invalid output for `%v`:\n got: %#v\n want: %#v" , tt .input , got , tt .expect )
135
- }
136
- })
137
- }
138
- }
139
-
140
91
func TestFloatLesser (t * testing.T ) {
141
92
cases := []struct {
142
93
name string
@@ -148,11 +99,13 @@ func TestFloatLesser(t *testing.T) {
148
99
{`Float.Less(2.0,1.0)` , 2.0 , 1.0 , false },
149
100
{`Float.Less(1.0,"2.0")` , 1.0 , "2.0" , false },
150
101
}
102
+ lessFunc := schema.Float {}.LessFunc ()
103
+
151
104
for i := range cases {
152
105
tt := cases [i ]
153
106
t .Run (tt .name , func (t * testing.T ) {
154
107
t .Parallel ()
155
- got := Float {}. Less (tt .value , tt .other )
108
+ got := lessFunc (tt .value , tt .other )
156
109
if got != tt .expected {
157
110
t .Errorf ("output for `%v`\n got: %v\n want: %v" , tt .name , got , tt .expected )
158
111
}
0 commit comments