/* * Copyright (c) 2020 Alex aka mailoman * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @author Alex aka mailoman * @copyright Copyright (c) 2020 Alex aka mailoman * @since 14.01.2020 * */ package fields_test import ( "testing" "github.com/satori/go.uuid" "github.com/stretchr/testify/assert" "git.webz.asia/go-convert/fields" ) type FromStruct struct { ID string `conv:"new,update"` Field1 string `conv:"new,update"` Field2 string `conv:"update"` Field3 string `conv:"update,omit"` Field4 string `conv:"category-1,omitempty"` } type FromStructNone struct { ID string `conv1:"new,update"` Field1 string `conv2:"new,update"` Field2 string `conv3:"update"` } type ToStruct struct { ID string Field1 string Field2 string Field3 string Field4 string } type ToStruct2 struct { Id string Field1 string Field2 string Field3 string Field4 string } type FromStructCustom struct { ID string `copy:"new,update"` Field1 string `copy:"new,update"` Field2 string `copy:"update"` Field3 string `copy:"update,ignore"` Field4 string `copy:"category-1,noempty"` } type FromStructBase struct { ID string `conv:"new,update"` Field1 string `conv:"new,update"` } type FromStructExtraBase struct { ID uuid.UUID `conv:"new,update"` Field1 string `conv:"new,update"` } type FromStructCombined struct { FromStructBase `conv:"new,update"` Field2 string `conv:"update"` Field3 string `conv:"update,omit"` Field4 string `conv:"category-1,omitempty"` } type FromStructCombinedExtra struct { FromStructExtraBase `conv:"new,update"` Field2 uuid.UUID `conv:"update"` Field3 string `conv:"update,omit"` Field4 string `conv:"category-1,omitempty"` } var ( // testsNormal provides normal test cases testsNormal = []struct { from FromStruct kind string to ToStruct expectedTo ToStruct }{ // case 1.1 { FromStruct{ "1", "field 1 value", "field 2 value", "omitting", "", }, "new", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "1", "field 1 value", "old field 2 value", "old field 3 value", "", }, }, // case 1.2 { FromStruct{ "1", "field 1 value", "field 2 value", "omitting", "", }, "update", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "1", "field 1 value", "field 2 value", "old field 3 value", "", }, }, // case 1.3.1 { FromStruct{ "1", "field 1 value", "field 2 value", "omitting", "", }, "category-1", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, }, // case 1.3.2 { FromStruct{ "1", "field 1 value", "field 2 value", "omitting", "not empty", }, "category-1", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "not empty", }, }, } // testsNone provides not applicable test cases testsNone = []struct { from FromStructNone kind string to ToStruct expectedTo ToStruct }{ // case 1.1 { FromStructNone{ "1", "field 1 value", "field 2 value", }, "new", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, }, // case 1.2 { FromStructNone{ "1", "field 1 value", "field 2 value", }, "update", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, }, } // testsNegative provides failing test cases testsNegative = []struct { from interface{} kind string to ToStruct expectedTo ToStruct }{ // case 1 { "string value", "new", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, }, // case 2 { 1, "update", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, }, // case 3 { nil, "update", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, }, } // testsCustomNormal provides normal test cases with custom init config testsCustomNormal = []struct { from FromStructCustom kind string to ToStruct expectedTo ToStruct }{ // case 1.1 { FromStructCustom{ "1", "field 1 value", "field 2 value", "omitting", "", }, "new", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "1", "field 1 value", "old field 2 value", "old field 3 value", "", }, }, // case 1.2 { FromStructCustom{ "1", "field 1 value", "field 2 value", "omitting", "", }, "update", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "1", "field 1 value", "field 2 value", "old field 3 value", "", }, }, // case 1.3.1 { FromStructCustom{ "1", "field 1 value", "field 2 value", "omitting", "", }, "category-1", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, }, // case 1.3.2 { FromStructCustom{ "1", "field 1 value", "field 2 value", "omitting", "not empty", }, "category-1", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "not empty", }, }, } // testsCombinedNormal provides normal test cases with embedded structs testsCombinedNormal = []struct { from FromStructCombined kind string to ToStruct expectedTo ToStruct }{ // case 3.1 { FromStructCombined{ FromStructBase{"1", "field 1 value"}, "field 2 value", "omitting", "", }, "new", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "1", "field 1 value", "old field 2 value", "old field 3 value", "", }, }, // case 3.2 { FromStructCombined{ FromStructBase{"1", "field 1 value"}, "field 2 value", "omitting", "", }, "update", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct{ "1", "field 1 value", "field 2 value", "old field 3 value", "", }, }, // case 3.3.1 { FromStructCombined{ FromStructBase{"1", "field 1 value"}, "field 2 value", "omitting", "", }, "category-1", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, }, // case 3.3.2 { FromStructCombined{ FromStructBase{"1", "field 1 value"}, "field 2 value", "omitting", "not empty", }, "category-1", ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, ToStruct{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "not empty", }, }, } // testsCombinedExtra provides normal test cases with embedded structs with more features testsCombinedExtra = []struct { from FromStructCombinedExtra kind string to ToStruct2 expectedTo ToStruct2 }{ // case 4.1 { FromStructCombinedExtra{ FromStructExtraBase{uuid.FromStringOrNil("bb9f2a39-1dbd-4929-8e87-4863696d9cb3"), "field 1 value"}, uuid.FromStringOrNil("d039089d-27f5-438b-8b70-90eb3df0d79b"), "omitting", "", }, "new", ToStruct2{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct2{ "bb9f2a39-1dbd-4929-8e87-4863696d9cb3", "field 1 value", "old field 2 value", "old field 3 value", "", }, }, // case 4.2 { FromStructCombinedExtra{ FromStructExtraBase{uuid.FromStringOrNil("bb9f2a39-1dbd-4929-8e87-4863696d9cb3"), "field 1 value"}, uuid.FromStringOrNil("d039089d-27f5-438b-8b70-90eb3df0d79b"), "omitting", "", }, "update", ToStruct2{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "", }, ToStruct2{ "bb9f2a39-1dbd-4929-8e87-4863696d9cb3", "field 1 value", "d039089d-27f5-438b-8b70-90eb3df0d79b", "old field 3 value", "", }, }, // case 4.3.1 { FromStructCombinedExtra{ FromStructExtraBase{uuid.FromStringOrNil("bb9f2a39-1dbd-4929-8e87-4863696d9cb3"), "field 1 value"}, uuid.FromStringOrNil("d039089d-27f5-438b-8b70-90eb3df0d79b"), "omitting", "", }, "category-1", ToStruct2{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, ToStruct2{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, }, // case 4.3.2 { FromStructCombinedExtra{ FromStructExtraBase{uuid.FromStringOrNil("bb9f2a39-1dbd-4929-8e87-4863696d9cb3"), "field 1 value"}, uuid.FromStringOrNil("d039089d-27f5-438b-8b70-90eb3df0d79b"), "omitting", "not empty", }, "category-1", ToStruct2{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "old field 4 value", }, ToStruct2{ "old val 1", "old field 1 value", "old field 2 value", "old field 3 value", "not empty", }, }, } ) func TestConvert_Normal(t *testing.T) { for _, test := range testsNormal { to := &ToStruct{} *to = test.to fields.Сonvert(test.from, test.kind, to) assert.NotEmpty(t, *to) assert.Equal(t, test.expectedTo, *to) } } func TestConvert_None(t *testing.T) { for _, test := range testsNone { to := &ToStruct{} *to = test.to fields.Сonvert(test.from, test.kind, to) assert.NotEmpty(t, *to) assert.Equal(t, test.expectedTo, *to) } } func TestConvert_Negative(t *testing.T) { for _, test := range testsNegative { to := &ToStruct{} *to = test.to fields.Сonvert(test.from, test.kind, to) assert.NotEmpty(t, *to) assert.Equal(t, test.expectedTo, *to) } } func TestConvert_Custom(t *testing.T) { fields.SetTagName("copy") fields.SetOmitTagName("ignore") fields.SetOmitEmptyTagName("noempty") for _, test := range testsCustomNormal { to := &ToStruct{} *to = test.to fields.Сonvert(test.from, test.kind, to) assert.NotEmpty(t, *to) assert.Equal(t, test.expectedTo, *to) } } func TestSetDefaults(t *testing.T) { // not default values after previous tests assert.NotEqual(t, "conv", fields.ConvertTagName()) assert.NotEqual(t, "omit", fields.OmitTagName()) assert.NotEqual(t, "omitempty", fields.OmitEmptyTagName()) fields.SetDefaults() assert.Equal(t, "conv", fields.ConvertTagName()) assert.Equal(t, "omit", fields.OmitTagName()) assert.Equal(t, "omitempty", fields.OmitEmptyTagName()) } func TestConvertTagName(t *testing.T) { defaultValue := fields.ConvertTagName() assert.Equal(t, "conv", defaultValue) newValue := "newTagName" fields.SetTagName(newValue) assert.Equal(t, newValue, fields.ConvertTagName()) } func TestOmitTagName(t *testing.T) { defaultValue := fields.OmitTagName() assert.Equal(t, defaultValue, "omit") newValue := "newOmitTagName" fields.SetOmitTagName(newValue) assert.Equal(t, newValue, fields.OmitTagName()) } func TestOmitEmptyTagName(t *testing.T) { defaultValue := fields.OmitEmptyTagName() assert.Equal(t, defaultValue, "omitempty") newValue := "newOmitEmptyTagName" fields.SetOmitEmptyTagName(newValue) assert.Equal(t, newValue, fields.OmitEmptyTagName()) } func TestConvert_CombinedNormal(t *testing.T) { fields.SetDefaults() for _, test := range testsCombinedNormal { to := &ToStruct{} *to = test.to fields.Сonvert(test.from, test.kind, to) assert.NotEmpty(t, *to) assert.Equal(t, test.expectedTo, *to) } } func TestConvert_CombinedExtra(t *testing.T) { fields.SetDefaults() for _, test := range testsCombinedExtra { to := &ToStruct2{} *to = test.to fields.Сonvert(test.from, test.kind, to) assert.NotEmpty(t, *to) assert.Equal(t, test.expectedTo, *to) } }