Added unit tests for account set method

This commit is contained in:
Trial97
2021-11-26 13:52:12 +02:00
committed by Dan Christian Bogos
parent fca363623e
commit d1eb628fc1
7 changed files with 278 additions and 30 deletions

View File

@@ -237,21 +237,6 @@ func (ar *record) Remove(fullPath *utils.FullPath) error {
}
}
// ParseField outputs the value based on the template item
func (ar *record) ParseField(
cfgFld *config.FCTemplate) (out interface{}, err error) {
if err != nil &&
!strings.HasPrefix(err.Error(), "Could not find") {
return
}
if utils.StringTmplType.Has(cfgFld.Type) { // format the string additionally with fmtFieldWidth
out, err = utils.FmtFieldWidth(cfgFld.Tag, out.(string), cfgFld.Width,
cfgFld.Strip, cfgFld.Padding, cfgFld.Mandatory)
}
return
}
// Append sets the value at the given path
// this used with full path and the processed path to not calculate them for every set
func (ar *record) Append(fullPath *utils.FullPath, val *utils.DataLeaf) (err error) {

View File

@@ -78,7 +78,6 @@ func TestNewRecord(t *testing.T) {
if !reflect.DeepEqual(r, exp) {
t.Errorf("Expected %+v, received %+q", exp, r)
}
}
func TestNewRecordWithCahe(t *testing.T) {

View File

@@ -204,6 +204,17 @@ func TestDryRunWithModelsErrors(t *testing.T) {
if _, err := testDryRunWithData(utils.MetaDispatcherHosts, []*utils.OrderedNavigableMap{newOrderNavMap(utils.MapStorage{utils.Tenant: "cgrates.org", utils.ID: "ID", "ReplyTimeout": "float", "Address": "127.0.0.1"})}); err == nil || err.Error() != expErrMsg {
t.Errorf("Expeceted: %v, received: %v", expErrMsg, err)
}
if _, err := testDryRunWithData(utils.MetaFilters, []*utils.OrderedNavigableMap{newOrderNavMap(utils.MapStorage{utils.Tenant: "cgrates.org", utils.ID: "ID", "ReplyTimeout": "float"})}); err != utils.ErrWrongPath {
t.Errorf("Expeceted: %v, received: %v", expErrMsg, err)
}
expErrMsg = `emtpy RSRParser in rule: <>`
data := newOrderNavMap(utils.MapStorage{utils.Tenant: "cgrates.org", utils.ID: "ID"})
data.SetAsSlice(utils.NewFullPath("Rules.Type"), []*utils.DataNode{utils.NewLeafNode("*no")})
if _, err := testDryRunWithData(utils.MetaFilters, []*utils.OrderedNavigableMap{data}); err == nil || err.Error() != expErrMsg {
t.Errorf("Expeceted: %v, received: %v", expErrMsg, err)
}
}
func TestSetToDBWithUpdateStructErrors(t *testing.T) {
@@ -262,6 +273,20 @@ func TestSetToDBWithModelsErrors(t *testing.T) {
if err := setToDB(context.Background(), nil, utils.MetaDispatcherHosts, utils.InfieldSep, utils.NewTenantID("cgrates.org:ID"), []*utils.OrderedNavigableMap{newOrderNavMap(utils.MapStorage{utils.Tenant: "cgrates.org", utils.ID: "ID", "ReplyTimeout": "float", "Address": "127.0.0.1"})}, true, false); err == nil || err.Error() != expErrMsg {
t.Errorf("Expeceted: %v, received: %v", expErrMsg, err)
}
if err := setToDB(context.Background(), nil, utils.MetaFilters, utils.InfieldSep, utils.NewTenantID("cgrates.org:ID"), []*utils.OrderedNavigableMap{newOrderNavMap(utils.MapStorage{utils.Tenant: "cgrates.org", utils.ID: "ID", "ReplyTimeout": "float"})}, true, false); err != utils.ErrWrongPath {
t.Errorf("Expeceted: %v, received: %v", expErrMsg, err)
}
expErrMsg = `emtpy RSRParser in rule: <>`
data := newOrderNavMap(utils.MapStorage{utils.Tenant: "cgrates.org", utils.ID: "ID"})
data.SetAsSlice(utils.NewFullPath("Rules.Type"), []*utils.DataNode{utils.NewLeafNode("*no")})
if err := setToDB(context.Background(), nil, utils.MetaFilters, utils.InfieldSep, utils.NewTenantID("cgrates.org:ID"), []*utils.OrderedNavigableMap{data}, true, false); err == nil || err.Error() != expErrMsg {
t.Errorf("Expeceted: %v, received: %v", expErrMsg, err)
}
if err := setToDB(context.Background(), nil, utils.EmptyString, utils.InfieldSep, utils.NewTenantID("cgrates.org:ID"), []*utils.OrderedNavigableMap{newOrderNavMap(utils.MapStorage{utils.Tenant: "cgrates.org", utils.ID: "ID"})}, true, false); err != nil {
t.Error(err)
}
}
func TestSetToDBWithDBError(t *testing.T) {
@@ -430,6 +455,16 @@ func TestSetToDB(t *testing.T) {
} else if !reflect.DeepEqual(v12, prf) {
t.Errorf("Expeceted: %v, received: %v", utils.ToJSON(v12), utils.ToJSON(prf))
}
if err := setToDB(context.Background(), dm, utils.MetaRateProfiles, utils.InfieldSep, utils.NewTenantID("cgrates.org:ID"), []*utils.OrderedNavigableMap{newOrderNavMap(utils.MapStorage{utils.Tenant: "cgrates.org", utils.ID: "ID"})}, true, true); err != nil {
t.Fatal(err)
}
v13 := &utils.RateProfile{Tenant: "cgrates.org", ID: "ID", Rates: map[string]*utils.Rate{}, MinCost: utils.NewDecimal(0, 0), MaxCost: utils.NewDecimal(0, 0)}
if prf, err := dm.GetRateProfile(context.Background(), "cgrates.org", "ID", true, true, utils.NonTransactional); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(v13, prf) {
t.Errorf("Expeceted: %v, received: %v", utils.ToJSON(v13), utils.ToJSON(prf))
}
}
func TestLoaderProcess(t *testing.T) {

View File

@@ -653,15 +653,16 @@ func (ap *Account) Set(path []string, val interface{}, newBranch bool, _ string)
if strings.HasPrefix(path[0], Opts) &&
path[0][4] == '[' && path[0][len(path[0])-1] == ']' {
ap.Opts[path[0][5:len(path[0])-1]] = val
return
}
if strings.HasPrefix(path[0], Balances) &&
path[0][8] == '[' && path[0][len(path[0])-1] == ']' {
id := path[0][9 : len(path[0])-1]
if _, has := ap.Balances[id]; !has {
ap.Balances[id] = &Balance{ID: id, Opts: make(map[string]interface{}), Units: NewDecimal(0, 0)}
}
return ap.Balances[id].Set(path[1:], val, newBranch)
}
// if strings.HasPrefix(path[0], Balances) &&
// path[0][8] == '[' && path[0][len(path[0])-1] == ']' {
// id := path[0][9 : len(path[0])-1]
// if _, has := ap.Balances[id]; !has {
// ap.Balances[id] = &Balance{ID: id, Opts: make(map[string]interface{}), Units: NewDecimal(0, 0)}
// }
// return ap.Balances[id].Set(path[1:], val, newBranch)
// }
return ErrWrongPath
case Tenant:
ap.Tenant = IfaceAsString(val)
@@ -683,13 +684,13 @@ func (ap *Account) Set(path []string, val interface{}, newBranch bool, _ string)
return
default:
}
if path[0] == Opts {
return MapStorage(ap.Opts).Set(path[1:], val)
}
if strings.HasPrefix(path[0], Opts) &&
path[0][4] == '[' && path[0][len(path[0])-1] == ']' {
return MapStorage(ap.Opts).Set(append([]string{path[0][5 : len(path[0])-1]}, path[1:]...), val)
}
if path[0] == Opts {
return MapStorage(ap.Opts).Set(path[1:], val)
}
var id string
if path[0] == Balances {
id = path[1]
@@ -718,6 +719,7 @@ func (bL *Balance) Set(path []string, val interface{}, newBranch bool) (err erro
if strings.HasPrefix(path[0], Opts) &&
path[0][4] == '[' && path[0][len(path[0])-1] == ']' {
bL.Opts[path[0][5:len(path[0])-1]] = val
return
}
return ErrWrongPath
case ID:
@@ -853,12 +855,12 @@ func (bL *Balance) Set(path []string, val interface{}, newBranch bool) (err erro
}
}
if path[0] == Opts {
return MapStorage(bL.Opts).Set(path[1:], val)
}
if strings.HasPrefix(path[0], Opts) &&
path[0][4] == '[' && path[0][len(path[0])-1] == ']' {
return MapStorage(bL.Opts).Set(append([]string{path[0][5 : len(path[0])-1]}, path[1:]...), val)
}
if path[0] == Opts {
return MapStorage(bL.Opts).Set(path[1:], val)
}
return ErrWrongPath
}

View File

@@ -1092,3 +1092,101 @@ func TestAccountClone(t *testing.T) {
t.Error(err)
}
}
func TestAccountSet(t *testing.T) {
acc := Account{Balances: map[string]*Balance{}}
exp := Account{
Tenant: "cgrates.org",
ID: "ID",
FilterIDs: []string{"fltr1", "*string:~*req.Account:1001"},
Weights: DynamicWeights{{}},
ThresholdIDs: []string{"TH1"},
Opts: map[string]interface{}{
"bal": "val",
"bal2": "val2",
"bal3": "val2",
"bal4": "val2",
"bal5": MapStorage{"bal6": "val3"},
},
Balances: map[string]*Balance{
"bal1": {
ID: "bal1",
Type: MetaConcrete,
Opts: map[string]interface{}{
"bal7": "val3",
"bal8": MapStorage{"bal9": "val3"},
"bal10": "val3",
},
Units: NewDecimal(0, 0),
},
},
}
if err := acc.Set([]string{}, "", false, EmptyString); err != ErrWrongPath {
t.Error(err)
}
if err := acc.Set([]string{"NotAField"}, "", false, EmptyString); err != ErrWrongPath {
t.Error(err)
}
if err := acc.Set([]string{"NotAField", "1"}, "", false, EmptyString); err != ErrWrongPath {
t.Error(err)
}
expErr := `malformed map pair: <"bal">`
if err := acc.Set([]string{Opts}, "bal", false, EmptyString); err == nil || err.Error() != expErr {
t.Error(err)
}
if err := acc.Set([]string{Opts}, "bal:val;bal2:val2", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Opts, "bal3"}, "val2", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Opts + "[bal4]"}, "val2", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Opts + "[bal5]", "bal6"}, "val3", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Tenant}, "cgrates.org", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{ID}, "ID", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{FilterIDs}, "fltr1;*string:~*req.Account:1001", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{ThresholdIDs}, "TH1", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Weights}, "", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Balances + "[bal1]", ID}, "bal1", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Balances, "bal1", Type}, MetaConcrete, false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Balances, "bal1", Opts}, "", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Balances, "bal1", Opts + "bal7]"}, "val3", false, EmptyString); err != ErrWrongPath {
t.Error(err)
}
if err := acc.Set([]string{Balances, "bal1", Opts + "bal7]", ""}, "val3", false, EmptyString); err != ErrWrongPath {
t.Error(err)
}
if err := acc.Set([]string{Balances, "bal1", Opts + "[bal7]"}, "val3", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Balances, "bal1", Opts + "[bal8]", "bal9"}, "val3", false, EmptyString); err != nil {
t.Error(err)
}
if err := acc.Set([]string{Balances, "bal1", Opts, "bal10"}, "val3", false, EmptyString); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(exp, acc) {
t.Errorf("Expected %v \n but received \n %v", ToJSON(exp), ToJSON(acc))
}
}

View File

@@ -1107,6 +1107,11 @@ func TestNewTenantID(t *testing.T) {
if rcv := NewTenantID("cgrates.org:id"); *rcv != *eOut {
t.Errorf("Expecting: %+v, received %+v", eOut, rcv)
}
eOut = &TenantID{Tenant: "cgrates.org", ID: "id"}
if rcv := NewTenantID("cgrates.org:id"); !eOut.Equal(rcv) {
t.Errorf("Expecting: %+v, received %+v", eOut, rcv)
}
}
func TestTenantID(t *testing.T) {

View File

@@ -1133,6 +1133,103 @@ func TestLenTimeConverter3(t *testing.T) {
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]string{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]interface{}{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]bool{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]int{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]int8{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]int16{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]int32{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]int64{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]uint{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]uint8{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]uint16{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]uint32{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]uint64{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]uintptr{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]float32{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]float64{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]complex64{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]complex128{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
if rcv, err := cnv.Convert(map[string]Account{}); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, rcv) {
t.Errorf("Expecting: %+v, received: %+v", expected, rcv)
}
}
func TestFloat64Converter(t *testing.T) {
@@ -1274,4 +1371,31 @@ func TestJoinConverter(t *testing.T) {
} else if expVal != i {
t.Errorf("expecting: %q, received: %q", expVal, i)
}
expErr := `cannot convert field: 5 to []string`
if _, err := d.Convert(5); err == nil || err.Error() != expErr {
t.Error(err)
}
}
func TestSplitConverter(t *testing.T) {
d, err := NewDataConverter(MetaSplit)
if err != nil {
t.Fatal(err)
}
expVal := []string{"1", "2", "3", "5"}
if i, err := d.Convert("1,2,3,5"); err != nil {
t.Error(err.Error())
} else if !reflect.DeepEqual(expVal, i) {
t.Errorf("expecting: %q, received: %q", expVal, i)
}
d, err = NewDataConverter(MetaSplit + ":|")
if err != nil {
t.Fatal(err)
}
if i, err := d.Convert("1|2|3|5"); err != nil {
t.Error(err.Error())
} else if !reflect.DeepEqual(expVal, i) {
t.Errorf("expecting: %q, received: %q", expVal, i)
}
}