mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Completed tests for utils/struct.go
This commit is contained in:
committed by
Dan Christian Bogos
parent
0bbcdfa250
commit
93baba1be1
@@ -51,11 +51,16 @@ func MissingMapFields(s map[string]interface{}, mandatories []string) []string {
|
||||
} else {
|
||||
fld := reflect.ValueOf(fldval)
|
||||
// sanitize the string fields before checking
|
||||
if fld.Kind() == reflect.String && fld.CanSet() {
|
||||
fld.SetString(strings.TrimSpace(fld.String()))
|
||||
if fld.Kind() == reflect.String {
|
||||
str := strings.TrimSpace(fld.String())
|
||||
s[fieldName] = str
|
||||
if len(str) == 0 {
|
||||
missing = append(missing, fieldName)
|
||||
}
|
||||
continue
|
||||
//fld.SetString(strings.TrimSpace(fld.String()))
|
||||
}
|
||||
if (fld.Kind() == reflect.String && fld.String() == "") ||
|
||||
((fld.Kind() == reflect.Slice || fld.Kind() == reflect.Map) && fld.Len() == 0) ||
|
||||
if ((fld.Kind() == reflect.Slice || fld.Kind() == reflect.Map) && fld.Len() == 0) ||
|
||||
(fld.Kind() == reflect.Int && fld.Int() == 0) {
|
||||
missing = append(missing, fieldName)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/cmplx"
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
@@ -99,3 +100,139 @@ func TestMissingMapFields(t *testing.T) {
|
||||
t.Errorf("Expected %s ,received: %s", expected, missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingStructFieldsAppend(t *testing.T) {
|
||||
var attr = struct {
|
||||
Tenant string
|
||||
Account string
|
||||
Type string
|
||||
ActionTimingsID string
|
||||
}{"", "", META_PREPAID, ""}
|
||||
missing := MissingStructFields(&attr,
|
||||
[]string{"Tenant", "Account", "Type", "ActionTimingsID"})
|
||||
if len(missing) == 0 {
|
||||
t.Error("Required missing field not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingMapFieldsTrim(t *testing.T) {
|
||||
var attr = map[string]interface{}{
|
||||
"Tenant": "cgrates.org",
|
||||
"Account": "1001",
|
||||
}
|
||||
if missing := MissingMapFields(attr,
|
||||
[]string{"Tenant", "Account"}); len(missing) != 0 {
|
||||
t.Error("Found missing field on correct struct", missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingMapFieldsMissing(t *testing.T) {
|
||||
var attr = map[string]interface{}{
|
||||
"Tenant": 0,
|
||||
"Account": 0,
|
||||
}
|
||||
missing := MissingMapFields(attr, []string{"Tenant", "Account"})
|
||||
if len(missing) == 0 {
|
||||
t.Error("Required missing field not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateStructWithIfaceMapValEmpty(t *testing.T) {
|
||||
type myStruct struct {
|
||||
String string
|
||||
Bool bool
|
||||
Float float64
|
||||
Int int64
|
||||
}
|
||||
s := new(myStruct)
|
||||
mp := map[string]interface{}{
|
||||
"String": "",
|
||||
"Bool": "",
|
||||
"Float": "",
|
||||
"Int": "",
|
||||
}
|
||||
expectedStruct := &myStruct{
|
||||
String: "",
|
||||
Bool: false,
|
||||
Float: 0,
|
||||
Int: 0,
|
||||
}
|
||||
UpdateStructWithIfaceMap(s, mp)
|
||||
if !reflect.DeepEqual(s, expectedStruct) {
|
||||
t.Errorf("Expected <%+v> ,received: <%+v>", expectedStruct, s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateStructWithIfaceMapErrorBol(t *testing.T) {
|
||||
type myStruct struct {
|
||||
String string
|
||||
Bool bool
|
||||
Float float64
|
||||
Int int64
|
||||
}
|
||||
s := new(myStruct)
|
||||
mp := map[string]interface{}{
|
||||
"String": "string",
|
||||
"Bool": "cat",
|
||||
"Float": 1.2,
|
||||
"Int": 1,
|
||||
}
|
||||
err := UpdateStructWithIfaceMap(s, mp)
|
||||
if err == nil || err.Error() != "strconv.ParseBool: parsing \"cat\": invalid syntax" {
|
||||
t.Errorf("Expected <strconv.ParseBool: parsing \"cat\": invalid syntax> ,received: <%+v>", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateStructWithIfaceMapErrorInt(t *testing.T) {
|
||||
type myStruct struct {
|
||||
String string
|
||||
Bool bool
|
||||
Float float64
|
||||
Int int64
|
||||
}
|
||||
s := new(myStruct)
|
||||
mp := map[string]interface{}{
|
||||
"String": "string",
|
||||
"Bool": true,
|
||||
"Float": 1.2,
|
||||
"Int": "cat",
|
||||
}
|
||||
err := UpdateStructWithIfaceMap(s, mp)
|
||||
if err == nil || err.Error() != "strconv.ParseInt: parsing \"cat\": invalid syntax" {
|
||||
t.Errorf("Expected <strconv.ParseInt: parsing \"cat\": invalid syntax> ,received: <%+v>", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateStructWithIfaceMapErrorFloat(t *testing.T) {
|
||||
type myStruct struct {
|
||||
String string
|
||||
Bool bool
|
||||
Float float64
|
||||
Int int64
|
||||
}
|
||||
s := new(myStruct)
|
||||
mp := map[string]interface{}{
|
||||
"String": "string",
|
||||
"Bool": true,
|
||||
"Float": "cat",
|
||||
"Int": 2,
|
||||
}
|
||||
err := UpdateStructWithIfaceMap(s, mp)
|
||||
if err == nil || err.Error() != "strconv.ParseFloat: parsing \"cat\": invalid syntax" {
|
||||
t.Errorf("Expected <strconv.ParseFloat: parsing \"cat\": invalid syntax> ,received: <%+v>", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateStructWithIfaceMapErrorDefault(t *testing.T) {
|
||||
type myStruct struct {
|
||||
wrongField1 complex128
|
||||
}
|
||||
s := new(myStruct)
|
||||
mp := map[string]interface{}{
|
||||
"wrongField1": cmplx.Sqrt(-5 + 12i),
|
||||
}
|
||||
err := UpdateStructWithIfaceMap(s, mp)
|
||||
if err == nil || err.Error() != "cannot update unsupported struct field: (0+0i)" {
|
||||
t.Errorf("Expected <cannot update unsupported struct field: (0+0i)> ,received: <%+v>", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user