11package changeset
22
33import (
4+ "fmt"
45 "testing"
6+ "time"
57
8+ "github.com/Fs02/grimoire/params"
69 "github.com/stretchr/testify/assert"
710)
811
@@ -21,6 +24,22 @@ func TestValidateRequired(t *testing.T) {
2124 assert .Nil (t , ch .Errors ())
2225}
2326
27+ func TestValidateRequired_cast (t * testing.T ) {
28+ type customString string
29+ type customType struct {
30+ Field1 customString
31+ Field2 customString
32+ Field3 customString
33+ FieldTime time.Time
34+ }
35+
36+ ct := customType {Field1 : "field1" , FieldTime : time .Now ()}
37+ ch := Cast (ct , params.Map {"field2" : "field2" , "field3" : "field3" }, []string {"field1" , "field2" , "field3" , "field_time" })
38+ ValidateRequired (ch , []string {"field1" , "field2" , "field3" , "field_time" })
39+
40+ assert .Nil (t , ch .Errors ())
41+ }
42+
2443func TestValidateRequired_error (t * testing.T ) {
2544 ch := & Changeset {
2645 changes : map [string ]interface {}{
@@ -34,6 +53,7 @@ func TestValidateRequired_error(t *testing.T) {
3453 assert .Equal (t , 3 , len (ch .Errors ()))
3554 assert .Equal (t , "field1 is required" , ch .Errors ()[0 ].Error ())
3655 assert .Equal (t , "field2 is required" , ch .Errors ()[1 ].Error ())
56+ assert .Equal (t , "field3 is required" , ch .Errors ()[2 ].Error ())
3757
3858 // empty struct
3959 ch = & Changeset {
@@ -53,3 +73,23 @@ func TestValidateRequired_error(t *testing.T) {
5373 assert .Equal (t , "field2 is required" , ch .Errors ()[0 ].Error ())
5474 assert .Equal (t , "field3 is required" , ch .Errors ()[1 ].Error ())
5575}
76+
77+ func TestValidateRequired_cast_error (t * testing.T ) {
78+ type customString string
79+ type customType struct {
80+ Field1 customString
81+ Field2 customString
82+ Field3 customString
83+ FieldTime time.Time
84+ }
85+
86+ ct := customType {Field1 : "field1" }
87+ fmt .Println (ct .FieldTime )
88+ ch := Cast (ct , params.Map {"field2" : "field2" }, []string {"field1" , "field2" , "field3" , "field_time" })
89+ ValidateRequired (ch , []string {"field1" , "field2" , "field3" , "field_time" })
90+
91+ assert .NotNil (t , ch .Errors ())
92+ assert .Equal (t , 2 , len (ch .Errors ()))
93+ assert .Equal (t , "field3 is required" , ch .Errors ()[0 ].Error ())
94+ assert .Equal (t , "field_time is required" , ch .Errors ()[1 ].Error ())
95+ }
0 commit comments