fields/fields_test.go

360 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2020 Alex aka mailoman <alex@webz.asia>
*
* 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 <alex@webz.asia>
* @copyright Copyright (c) 2020 Alex aka mailoman <alex@webz.asia>
* @since 14.01.2020
*
*/
package fields_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/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 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"`
}
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",
},
},
}
)
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())
}