mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Removed error from decimal convert func + tests in utils
This commit is contained in:
committed by
Dan Christian Bogos
parent
db7f982169
commit
5a3d48a9ea
@@ -168,6 +168,6 @@ func (cB *concreteBalance) debitUnits(dUnts *utils.Decimal, incrm *utils.Decimal
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("failed representing decimal <%s> as float64", rmain)
|
||||
}
|
||||
cB.blnCfg.Units, err = utils.NewDecimalFromFloat64(rmainFlt64)
|
||||
cB.blnCfg.Units = utils.NewDecimalFromFloat64(rmainFlt64)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -3084,12 +3084,8 @@ func APItoRateProfile(tpRp *utils.TPRateProfile, timezone string) (rp *RateProfi
|
||||
RoundingDecimals: tpRp.RoundingDecimals,
|
||||
MaxCostStrategy: tpRp.MaxCostStrategy,
|
||||
Rates: make(map[string]*Rate),
|
||||
}
|
||||
if rp.MinCost, err = utils.NewDecimalFromFloat64(tpRp.MinCost); err != nil {
|
||||
return
|
||||
}
|
||||
if rp.MaxCost, err = utils.NewDecimalFromFloat64(tpRp.MaxCost); err != nil {
|
||||
return
|
||||
MinCost: utils.NewDecimalFromFloat64(tpRp.MinCost),
|
||||
MaxCost: utils.NewDecimalFromFloat64(tpRp.MaxCost),
|
||||
}
|
||||
for i, stp := range tpRp.FilterIDs {
|
||||
rp.FilterIDs[i] = stp
|
||||
@@ -3113,12 +3109,8 @@ func APItoRateProfile(tpRp *utils.TPRateProfile, timezone string) (rp *RateProfi
|
||||
if rp.Rates[key].IntervalRates[i].IntervalStart, err = utils.ParseDurationWithNanosecs(iRate.IntervalStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rp.Rates[key].IntervalRates[i].FixedFee, err = utils.NewDecimalFromFloat64(iRate.FixedFee); err != nil {
|
||||
return
|
||||
}
|
||||
if rp.Rates[key].IntervalRates[i].RecurrentFee, err = utils.NewDecimalFromFloat64(iRate.RecurrentFee); err != nil {
|
||||
return
|
||||
}
|
||||
rp.Rates[key].IntervalRates[i].FixedFee = utils.NewDecimalFromFloat64(iRate.FixedFee)
|
||||
rp.Rates[key].IntervalRates[i].RecurrentFee = utils.NewDecimalFromFloat64(iRate.RecurrentFee)
|
||||
if rp.Rates[key].IntervalRates[i].Unit, err = utils.NewDecimalFromUnit(iRate.Unit); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -3663,9 +3655,7 @@ func APItoAccountProfile(tpAp *utils.TPAccountProfile, timezone string) (ap *uti
|
||||
Weight: bal.Weight,
|
||||
Blocker: bal.Blocker,
|
||||
Type: bal.Type,
|
||||
}
|
||||
if ap.Balances[id].Units, err = utils.NewDecimalFromFloat64(bal.Units); err != nil {
|
||||
return
|
||||
Units: utils.NewDecimalFromFloat64(bal.Units),
|
||||
}
|
||||
if bal.Opts != utils.EmptyString {
|
||||
ap.Balances[id].Opts = make(map[string]interface{})
|
||||
@@ -3685,19 +3675,13 @@ func APItoAccountProfile(tpAp *utils.TPAccountProfile, timezone string) (ap *uti
|
||||
FilterIDs: costIncrement.FilterIDs,
|
||||
}
|
||||
if costIncrement.Increment != nil {
|
||||
if ap.Balances[id].CostIncrements[j].Increment, err = utils.NewDecimalFromFloat64(*costIncrement.Increment); err != nil {
|
||||
return
|
||||
}
|
||||
ap.Balances[id].CostIncrements[j].Increment = utils.NewDecimalFromFloat64(*costIncrement.Increment)
|
||||
}
|
||||
if costIncrement.FixedFee != nil {
|
||||
if ap.Balances[id].CostIncrements[j].FixedFee, err = utils.NewDecimalFromFloat64(*costIncrement.FixedFee); err != nil {
|
||||
return
|
||||
}
|
||||
ap.Balances[id].CostIncrements[j].FixedFee = utils.NewDecimalFromFloat64(*costIncrement.FixedFee)
|
||||
}
|
||||
if costIncrement.RecurrentFee != nil {
|
||||
if ap.Balances[id].CostIncrements[j].RecurrentFee, err = utils.NewDecimalFromFloat64(*costIncrement.RecurrentFee); err != nil {
|
||||
return
|
||||
}
|
||||
ap.Balances[id].CostIncrements[j].RecurrentFee = utils.NewDecimalFromFloat64(*costIncrement.RecurrentFee)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3712,9 +3696,7 @@ func APItoAccountProfile(tpAp *utils.TPAccountProfile, timezone string) (ap *uti
|
||||
for j, unitFactor := range bal.UnitFactors {
|
||||
ap.Balances[id].UnitFactors[j] = &utils.UnitFactor{
|
||||
FilterIDs: unitFactor.FilterIDs,
|
||||
}
|
||||
if ap.Balances[id].UnitFactors[j].Factor, err = utils.NewDecimalFromFloat64(unitFactor.Factor); err != nil {
|
||||
return
|
||||
Factor: utils.NewDecimalFromFloat64(unitFactor.Factor),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,9 +252,7 @@ func (ext *APIAccountProfile) AsAccountProfile() (profile *AccountProfile, err e
|
||||
if len(ext.Balances) != 0 {
|
||||
profile.Balances = make(map[string]*Balance, len(ext.Balances))
|
||||
for i, bal := range ext.Balances {
|
||||
if profile.Balances[i], err = bal.AsBalance(); err != nil {
|
||||
return
|
||||
}
|
||||
profile.Balances[i] = bal.AsBalance()
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -275,7 +273,7 @@ type APIBalance struct {
|
||||
}
|
||||
|
||||
// AsBalance convert APIBalance struct to Balance struct
|
||||
func (ext *APIBalance) AsBalance() (balance *Balance, err error) {
|
||||
func (ext *APIBalance) AsBalance() (balance *Balance) {
|
||||
balance = &Balance{
|
||||
ID: ext.ID,
|
||||
FilterIDs: ext.FilterIDs,
|
||||
@@ -284,26 +282,20 @@ func (ext *APIBalance) AsBalance() (balance *Balance, err error) {
|
||||
Type: ext.Type,
|
||||
Opts: ext.Opts,
|
||||
CostAttributes: ext.CostAttributes,
|
||||
Units: NewDecimalFromFloat64(ext.Units),
|
||||
}
|
||||
if len(ext.CostIncrements) != 0 {
|
||||
balance.CostIncrements = make([]*CostIncrement, len(ext.CostIncrements))
|
||||
for i, cIncr := range ext.CostIncrements {
|
||||
if balance.CostIncrements[i], err = cIncr.AsCostIncrement(); err != nil {
|
||||
return
|
||||
}
|
||||
balance.CostIncrements[i] = cIncr.AsCostIncrement()
|
||||
}
|
||||
}
|
||||
if len(ext.UnitFactors) != 0 {
|
||||
balance.UnitFactors = make([]*UnitFactor, len(ext.UnitFactors))
|
||||
for i, uFct := range ext.UnitFactors {
|
||||
if balance.UnitFactors[i], err = uFct.AsUnitFactor(); err != nil {
|
||||
return
|
||||
}
|
||||
balance.UnitFactors[i] = uFct.AsUnitFactor()
|
||||
}
|
||||
}
|
||||
if balance.Units, err = NewDecimalFromFloat64(ext.Units); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
@@ -317,24 +309,18 @@ type APICostIncrement struct {
|
||||
}
|
||||
|
||||
// AsCostIncrement convert APICostIncrement struct to CostIncrement struct
|
||||
func (ext *APICostIncrement) AsCostIncrement() (cIncr *CostIncrement, err error) {
|
||||
func (ext *APICostIncrement) AsCostIncrement() (cIncr *CostIncrement) {
|
||||
cIncr = &CostIncrement{
|
||||
FilterIDs: ext.FilterIDs,
|
||||
}
|
||||
if ext.Increment != nil {
|
||||
if cIncr.Increment, err = NewDecimalFromFloat64(*ext.Increment); err != nil {
|
||||
return
|
||||
}
|
||||
cIncr.Increment = NewDecimalFromFloat64(*ext.Increment)
|
||||
}
|
||||
if ext.FixedFee != nil {
|
||||
if cIncr.FixedFee, err = NewDecimalFromFloat64(*ext.FixedFee); err != nil {
|
||||
return
|
||||
}
|
||||
cIncr.FixedFee = NewDecimalFromFloat64(*ext.FixedFee)
|
||||
}
|
||||
if ext.RecurrentFee != nil {
|
||||
if cIncr.RecurrentFee, err = NewDecimalFromFloat64(*ext.RecurrentFee); err != nil {
|
||||
return
|
||||
}
|
||||
cIncr.RecurrentFee = NewDecimalFromFloat64(*ext.RecurrentFee)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -346,13 +332,9 @@ type APIUnitFactor struct {
|
||||
}
|
||||
|
||||
// AsUnitFactor convert APIUnitFactor struct to UnitFactor struct
|
||||
func (ext *APIUnitFactor) AsUnitFactor() (uFac *UnitFactor, err error) {
|
||||
uFac = &UnitFactor{
|
||||
func (ext *APIUnitFactor) AsUnitFactor() *UnitFactor {
|
||||
return &UnitFactor{
|
||||
FilterIDs: ext.FilterIDs,
|
||||
Factor: NewDecimalFromFloat64(ext.Factor),
|
||||
}
|
||||
if uFac.Factor, err = NewDecimalFromFloat64(ext.Factor); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
@@ -134,6 +134,7 @@ func TestAccountProfileAsAccountProfile(t *testing.T) {
|
||||
Units: 0,
|
||||
},
|
||||
},
|
||||
Weight: 10,
|
||||
}
|
||||
expected := &AccountProfile{
|
||||
Tenant: "cgrates.org",
|
||||
@@ -151,10 +152,68 @@ func TestAccountProfileAsAccountProfile(t *testing.T) {
|
||||
Units: NewDecimal(0, 0),
|
||||
},
|
||||
},
|
||||
Weight: 10,
|
||||
}
|
||||
if rcv, err := apiAccPrf.AsAccountProfile(); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected, rcv) {
|
||||
t.Errorf("Expected %+v, received %+v", ToJSON(expected), ToJSON(rcv))
|
||||
}
|
||||
|
||||
accPrfList := AccountProfiles{}
|
||||
accPrfList = append(accPrfList, expected)
|
||||
accPrfList.Sort()
|
||||
if !reflect.DeepEqual(accPrfList[0], expected) {
|
||||
t.Errorf("Expected %+v \n, received %+v", expected, accPrfList[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIBalanceAsBalance(t *testing.T) {
|
||||
blc := &APIBalance{
|
||||
ID: "VoiceBalance",
|
||||
CostIncrements: []*APICostIncrement{
|
||||
{
|
||||
FilterIDs: []string{"*string:~*req.Account:1001"},
|
||||
Increment: Float64Pointer(1),
|
||||
FixedFee: Float64Pointer(10),
|
||||
RecurrentFee: Float64Pointer(35),
|
||||
},
|
||||
},
|
||||
Weight: 10,
|
||||
UnitFactors: []*APIUnitFactor{
|
||||
{
|
||||
FilterIDs: []string{"*string:~*req.Account:1001"},
|
||||
Factor: 20,
|
||||
},
|
||||
},
|
||||
}
|
||||
expected := &Balance{
|
||||
ID: "VoiceBalance",
|
||||
CostIncrements: []*CostIncrement{
|
||||
{
|
||||
FilterIDs: []string{"*string:~*req.Account:1001"},
|
||||
Increment: NewDecimal(1, 0),
|
||||
FixedFee: NewDecimal(10, 0),
|
||||
RecurrentFee: NewDecimal(35, 0),
|
||||
},
|
||||
},
|
||||
Weight: 10,
|
||||
UnitFactors: []*UnitFactor{
|
||||
{
|
||||
FilterIDs: []string{"*string:~*req.Account:1001"},
|
||||
Factor: NewDecimal(20, 0),
|
||||
},
|
||||
},
|
||||
Units: NewDecimal(0, 0),
|
||||
}
|
||||
if rcv := blc.AsBalance(); !reflect.DeepEqual(rcv, expected) {
|
||||
t.Errorf("Expected %+v \n, received %+v", ToJSON(expected), ToJSON(rcv))
|
||||
}
|
||||
|
||||
blcList := Balances{}
|
||||
blcList = append(blcList, expected)
|
||||
blcList.Sort()
|
||||
if !reflect.DeepEqual(blcList[0], expected) {
|
||||
t.Errorf("Expected %+v \n, received %+v", expected, blcList[0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -462,3 +462,17 @@ func TestCGREventFieldAsInt64(t *testing.T) {
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
func TestCGREventWithOptsCache(t *testing.T) {
|
||||
event := &CGREventWithOpts{}
|
||||
event.CacheInit()
|
||||
event.CacheSet("testKey", "string_for_test")
|
||||
if rcv, _ := event.CacheGet("testKey"); rcv != "string_for_test" {
|
||||
t.Errorf("Expected %+v, received %+v", rcv, event.cache["testKey"])
|
||||
}
|
||||
event.CacheRemove("testKey")
|
||||
if event.cache["testKey"] != nil {
|
||||
t.Errorf("Expectred nil")
|
||||
}
|
||||
event.CacheClear()
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -55,12 +54,9 @@ func SubstractDecimal(x, y *Decimal) *Decimal {
|
||||
|
||||
// NewDecimalFromFloat64 is a constructor for Decimal out of float64
|
||||
// passing through string is necessary due to differences between decimal and binary representation of float64
|
||||
func NewDecimalFromFloat64(f float64) (*Decimal, error) {
|
||||
d, canSet := new(decimal.Big).SetString(strconv.FormatFloat(f, 'f', -1, 64))
|
||||
if !canSet {
|
||||
return nil, fmt.Errorf("cannot convert float64 to Decimal")
|
||||
}
|
||||
return &Decimal{d}, nil
|
||||
func NewDecimalFromFloat64(f float64) *Decimal {
|
||||
d, _ := new(decimal.Big).SetString(strconv.FormatFloat(f, 'f', -1, 64))
|
||||
return &Decimal{d}
|
||||
}
|
||||
|
||||
// NewDecimalFromUnit is a constructor for Decimal out of unit represents as string
|
||||
|
||||
@@ -54,3 +54,28 @@ func TestNewDecimalAdd(t *testing.T) {
|
||||
t.Errorf("Expecting: <%+v>, received: <%+v>", expected, received)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDecimalFromUnit(t *testing.T) {
|
||||
if val, err := NewDecimalFromUnit("1ns"); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(val, NewDecimal(1, 0)) {
|
||||
t.Errorf("Expected %+v, received %+v", NewDecimal(1, 0), val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalMarshalBinary(t *testing.T) {
|
||||
dec := &Decimal{}
|
||||
expected := NewDecimal(10, 0)
|
||||
if err := dec.UnmarshalBinary([]byte(`10`)); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected.Big, dec.Big) {
|
||||
t.Errorf("Expected %T, received %T", expected, dec.Big)
|
||||
}
|
||||
|
||||
expected2 := []byte(`10`)
|
||||
if rcv, err := dec.MarshalBinary(); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(expected2, rcv) {
|
||||
t.Errorf("Expected %+v, received %+v", expected2, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -939,3 +939,16 @@ func TestOrderedNavigableMapRemove2(t *testing.T) {
|
||||
t.Errorf("Expected error: %s,received: %v", expErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrderedNavigableMapOrderedFields(t *testing.T) {
|
||||
nm := NewOrderedNavigableMap()
|
||||
nm.Set(&FullPath{
|
||||
PathItems: PathItems{{Field: "Field1"}, {Field: "Field2", Index: StringPointer("0")}},
|
||||
Path: "Field1.Field2[0]",
|
||||
}, NewNMData("1003"))
|
||||
rcv := nm.OrderedFields()
|
||||
newRcv := rcv[0].(string)
|
||||
if newRcv != "1003" {
|
||||
t.Errorf("Expected %+v, received %+v", "1003", newRcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,3 +163,23 @@ func TestPathItemsSlice(t *testing.T) {
|
||||
t.Errorf("Expected: %q, received: %q", expected, rply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewFullPath(t *testing.T) {
|
||||
expected := &FullPath{
|
||||
PathItems: []PathItem{
|
||||
{
|
||||
Field: EmptyString,
|
||||
},
|
||||
{
|
||||
Field: "test",
|
||||
},
|
||||
{
|
||||
Field: "path",
|
||||
},
|
||||
},
|
||||
Path: "/test/path",
|
||||
}
|
||||
if rcv := NewFullPath("/test/path", Slash); !reflect.DeepEqual(rcv, expected) {
|
||||
t.Errorf("Expected %+v \n, received %+v", ToJSON(expected), ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,4 +229,9 @@ func TestGetOne(t *testing.T) {
|
||||
t.Errorf("Expected %+v, received %+v", expected, value)
|
||||
}
|
||||
|
||||
set = StringSet{}
|
||||
value = set.GetOne()
|
||||
if value != EmptyString {
|
||||
t.Errorf("Expected %+v, received %+v", EmptyString, value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user