github.com/go-generalize/volcago@v1.7.0/generator/testfiles/misc/misc_test.go (about) 1 package misc 2 3 import ( 4 "testing" 5 "time" 6 7 "cloud.google.com/go/firestore" 8 "github.com/google/go-cmp/cmp" 9 "github.com/google/go-cmp/cmp/cmpopts" 10 "google.golang.org/genproto/googleapis/type/latlng" 11 ) 12 13 type Meta struct { 14 CreatedAt time.Time `json:"createdAt" firestore:"createdAt"` 15 CreatedBy string `json:"createdBy" firestore:"createdBy"` 16 UpdatedAt time.Time `json:"updatedAt" firestore:"updatedAt"` 17 UpdatedBy string `json:"updatedBy" firestore:"updatedBy"` 18 DeletedAt *time.Time `json:"deletedAt" firestore:"deletedAt"` 19 DeletedBy string `json:"deletedBy" firestore:"deletedBy"` 20 Version int `json:"version" firestore:"version"` 21 } 22 23 type article struct { 24 User user `json:"user" firestore:"user"` 25 Page string `json:"page" firestore:"page"` 26 Published bool `json:"published" firestore:"published"` 27 Meta 28 } 29 30 type user struct { 31 Name string `json:"name" firestore:"name"` 32 Age int `json:"age" firestore:"age"` 33 BirthDay *time.Time `json:"birthDay" firestore:"birthDay"` 34 IsAdult bool `json:"isAdult" firestore:"isAdult"` 35 Address address `json:"address" firestore:"address"` 36 } 37 38 type address struct { 39 LatLng *latlng.LatLng `json:"latLng"` 40 } 41 42 type articleUpdateParam struct { 43 User interface{} 44 Page interface{} 45 Published interface{} 46 CreatedAt interface{} 47 CreatedBy interface{} 48 UpdatedAt interface{} 49 UpdatedBy interface{} 50 DeletedAt interface{} 51 DeletedBy interface{} 52 Version interface{} 53 } 54 55 func Test_updateBuilder(t *testing.T) { 56 type args struct { 57 v interface{} 58 param *articleUpdateParam 59 } 60 61 unix := time.Unix(0, 0) 62 age := time.Now().Year() - unix.Year() 63 latLng := &latlng.LatLng{ 64 Latitude: 35.678803, 65 Longitude: 139.756263, 66 } 67 68 tests := []struct { 69 name string 70 args args 71 want map[string]firestore.Update 72 }{ 73 { 74 args: args{ 75 v: article{}, 76 param: &articleUpdateParam{ 77 User: user{ 78 Name: "john", 79 Age: age, 80 BirthDay: &unix, 81 IsAdult: false, 82 Address: address{ 83 LatLng: latLng, 84 }, 85 }, 86 Page: "section", 87 Published: false, 88 CreatedAt: unix, 89 CreatedBy: "operator", 90 UpdatedAt: unix, 91 Version: 1, 92 }, 93 }, 94 want: map[string]firestore.Update{ 95 "user.Name": {FieldPath: firestore.FieldPath{"user", "name"}, Value: "john"}, 96 "user.Age": {FieldPath: firestore.FieldPath{"user", "age"}, Value: age}, 97 "user.BirthDay": {FieldPath: firestore.FieldPath{"user", "birthDay"}, Value: &unix}, 98 "user.address.LatLng": {FieldPath: firestore.FieldPath{"user", "address", "LatLng"}, Value: latLng}, 99 "Page": {FieldPath: firestore.FieldPath{"page"}, Value: "section"}, 100 "Published": {FieldPath: firestore.FieldPath{"published"}, Value: false}, 101 "CreatedAt": {FieldPath: firestore.FieldPath{"createdAt"}, Value: unix}, 102 "CreatedBy": {FieldPath: firestore.FieldPath{"createdBy"}, Value: "operator"}, 103 "UpdatedAt": {FieldPath: firestore.FieldPath{"updatedAt"}, Value: unix}, 104 "Version": {FieldPath: firestore.FieldPath{"version"}, Value: 1}, 105 }, 106 }, 107 } 108 109 for _, tt := range tests { 110 tt := tt // escape: Using the variable on range scope `tt` in loop literal 111 t.Run(tt.name, func(t *testing.T) { 112 got := updateBuilder(tt.args.v, tt.args.param) 113 if diff := cmp.Diff(got, tt.want, cmpopts.IgnoreUnexported(latlng.LatLng{})); diff != "" { 114 t.Errorf("updateBuilder() = %v, want %v\n%s", got, tt.want, diff) 115 } 116 }) 117 } 118 } 119 120 func Test_updater(t *testing.T) { 121 type args struct { 122 v interface{} 123 param *articleUpdateParam 124 } 125 126 unix := time.Unix(0, 0) 127 age := time.Now().Year() - unix.Year() 128 latLng := &latlng.LatLng{ 129 Latitude: 35.678803, 130 Longitude: 139.756263, 131 } 132 133 tests := []struct { 134 name string 135 args args 136 want []firestore.Update 137 }{ 138 { 139 args: args{ 140 v: article{}, 141 param: &articleUpdateParam{ 142 User: user{ 143 Name: "john", 144 Age: age, 145 BirthDay: &unix, 146 IsAdult: false, 147 Address: address{ 148 LatLng: latLng, 149 }, 150 }, 151 Page: "section", 152 Published: false, 153 CreatedAt: unix, 154 CreatedBy: "operator", 155 UpdatedAt: unix, 156 Version: 1, 157 }, 158 }, 159 want: []firestore.Update{ 160 {FieldPath: firestore.FieldPath{"createdAt"}, Value: unix}, 161 {FieldPath: firestore.FieldPath{"createdBy"}, Value: "operator"}, 162 {FieldPath: firestore.FieldPath{"page"}, Value: "section"}, 163 {FieldPath: firestore.FieldPath{"published"}, Value: false}, 164 {FieldPath: firestore.FieldPath{"updatedAt"}, Value: unix}, 165 {FieldPath: firestore.FieldPath{"user", "address", "LatLng"}, Value: latLng}, 166 {FieldPath: firestore.FieldPath{"user", "age"}, Value: age}, 167 {FieldPath: firestore.FieldPath{"user", "birthDay"}, Value: &unix}, 168 {FieldPath: firestore.FieldPath{"user", "name"}, Value: "john"}, 169 {FieldPath: firestore.FieldPath{"version"}, Value: 1}, 170 }, 171 }, 172 { 173 args: args{ 174 v: article{}, 175 param: &articleUpdateParam{ 176 Page: "section", 177 }, 178 }, 179 want: []firestore.Update{ 180 {FieldPath: firestore.FieldPath{"page"}, Value: "section"}, 181 }, 182 }, 183 } 184 185 for _, tt := range tests { 186 tt := tt // escape: Using the variable on range scope `tt` in loop literal 187 t.Run(tt.name, func(t *testing.T) { 188 got := updater(tt.args.v, tt.args.param) 189 if diff := cmp.Diff(got, tt.want, cmpopts.IgnoreUnexported(latlng.LatLng{})); diff != "" { 190 t.Errorf("updater() = %v, want %v\n%s", got, tt.want, diff) 191 } 192 }) 193 } 194 } 195 196 func Test_tagMap(t *testing.T) { 197 type args struct { 198 v interface{} 199 } 200 201 tests := []struct { 202 name string 203 args args 204 want map[string]string 205 }{ 206 { 207 args: args{ 208 v: article{}, 209 }, 210 want: map[string]string{ 211 "CreatedAt": "createdAt", 212 "CreatedBy": "createdBy", 213 "DeletedAt": "deletedAt", 214 "DeletedBy": "deletedBy", 215 "Page": "page", 216 "Published": "published", 217 "UpdatedAt": "updatedAt", 218 "UpdatedBy": "updatedBy", 219 "Version": "version", 220 "user.Age": "user.age", 221 "user.BirthDay": "user.birthDay", 222 "user.IsAdult": "user.isAdult", 223 "user.Name": "user.name", 224 "user.address.LatLng": "user.address.LatLng", 225 }, 226 }, 227 } 228 229 for _, tt := range tests { 230 tt := tt // escape: Using the variable on range scope `tt` in loop literal 231 t.Run(tt.name, func(t *testing.T) { 232 got := tagMap(tt.args.v) 233 if diff := cmp.Diff(got, tt.want); diff != "" { 234 t.Errorf("tagMap() = %v, want %v\n%s", got, tt.want, diff) 235 } 236 }) 237 } 238 }