mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Started to fixed test in accounts
This commit is contained in:
committed by
Dan Christian Bogos
parent
eff17ed7bb
commit
ab6726f7fd
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -28,7 +29,6 @@ import (
|
||||
"github.com/ericlagergren/decimal"
|
||||
)
|
||||
|
||||
/*
|
||||
func TestABDebitUsageFromConcretes(t *testing.T) {
|
||||
aB := &abstractBalance{
|
||||
cncrtBlncs: []*concreteBalance{
|
||||
@@ -44,7 +44,7 @@ func TestABDebitUsageFromConcretes(t *testing.T) {
|
||||
Factor: utils.NewDecimal(100, 0), // EuroCents
|
||||
},
|
||||
},
|
||||
Units: utils.NewDecimal(500, 0), // 500 EURcents
|
||||
Units: utils.NewDecimal(500, 0), // 500 EuroCents
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -57,75 +57,54 @@ func TestABDebitUsageFromConcretes(t *testing.T) {
|
||||
Units: utils.NewDecimal(125, 2),
|
||||
},
|
||||
},
|
||||
}}
|
||||
expectedEvCharg := &utils.EventCharges{
|
||||
Concretes: utils.NewDecimal(5, 0),
|
||||
Accounting: make(map[string]*utils.AccountCharge),
|
||||
UnitFactors: make(map[string]*utils.UnitFactor),
|
||||
Rating: make(map[string]*utils.RateSInterval),
|
||||
},
|
||||
}
|
||||
|
||||
// consume only from first balance
|
||||
if evCh, err := debitAbstractsFromConcretes(aB.cncrtBlncs,
|
||||
decimal.New(int64(time.Duration(5*time.Minute)), 0),
|
||||
&utils.CostIncrement{
|
||||
Increment: utils.NewDecimal(int64(time.Duration(time.Minute)), 0),
|
||||
RecurrentFee: utils.NewDecimal(1, 0)},
|
||||
new(utils.CGREvent), nil, nil, nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(0, 0)) != 0 {
|
||||
if _, err := debitConcreteUnits(decimal.New(int64(time.Duration(5*time.Minute)), 0),
|
||||
utils.EmptyString, aB.cncrtBlncs, new(utils.CGREvent)); err == nil || err != utils.ErrInsufficientCredit {
|
||||
t.Errorf("Expected %+v, received %+v", utils.ErrInsufficientCredit, err)
|
||||
} else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(500, 0)) != 0 {
|
||||
t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[0].blnCfg.Units)
|
||||
} else if aB.cncrtBlncs[1].blnCfg.Units.Compare(utils.NewDecimal(125, 2)) != 0 {
|
||||
t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[1].blnCfg.Units)
|
||||
} else if !reflect.DeepEqual(evCh, expectedEvCharg) {
|
||||
t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedEvCharg), utils.ToJSON(evCh))
|
||||
}
|
||||
|
||||
// consume from second also, remaining in second
|
||||
aB.cncrtBlncs[0].blnCfg.Units = utils.NewDecimal(500, 0)
|
||||
aB.cncrtBlncs[1].blnCfg.Units = utils.NewDecimal(125, 2)
|
||||
expectedEvCharg = &utils.EventCharges{
|
||||
Concretes: utils.NewDecimal(9, 0),
|
||||
Accounting: make(map[string]*utils.AccountCharge),
|
||||
UnitFactors: make(map[string]*utils.UnitFactor),
|
||||
Rating: make(map[string]*utils.RateSInterval),
|
||||
}
|
||||
|
||||
if evCh, err := debitAbstractsFromConcretes(aB.cncrtBlncs,
|
||||
decimal.New(int64(time.Duration(9*time.Minute)), 0),
|
||||
&utils.CostIncrement{
|
||||
Increment: utils.NewDecimal(int64(time.Duration(time.Minute)), 0),
|
||||
RecurrentFee: utils.NewDecimal(1, 0)},
|
||||
new(utils.CGREvent), nil, nil, nil); err != nil {
|
||||
t.Error(err)
|
||||
} else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(-200, 0)) != 0 {
|
||||
t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[0].blnCfg.Units)
|
||||
} else if aB.cncrtBlncs[1].blnCfg.Units.Compare(utils.NewDecimal(-75, 2)) != 0 {
|
||||
t.Errorf("Unexpected units in second balance: %s", aB.cncrtBlncs[1].blnCfg.Units)
|
||||
} else if !reflect.DeepEqual(evCh, expectedEvCharg) {
|
||||
t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedEvCharg), utils.ToJSON(evCh))
|
||||
}
|
||||
|
||||
// not enough balance
|
||||
aB.cncrtBlncs[0].blnCfg.Units = utils.NewDecimal(500, 0)
|
||||
aB.cncrtBlncs[1].blnCfg.Units = utils.NewDecimal(125, 2)
|
||||
expectedEvCharg = nil
|
||||
if _, err := debitConcreteUnits(decimal.New(int64(time.Duration(9*time.Minute)), 0),
|
||||
utils.EmptyString, aB.cncrtBlncs, new(utils.CGREvent)); err == nil || err != utils.ErrInsufficientCredit {
|
||||
t.Errorf("Expected %+v, received %+v", utils.ErrInsufficientCredit, err)
|
||||
} else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(500, 0)) != 0 {
|
||||
t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[0].blnCfg.Units)
|
||||
} else if aB.cncrtBlncs[1].blnCfg.Units.Compare(utils.NewDecimal(125, 2)) != 0 {
|
||||
t.Errorf("Unexpected units in second balance: %s", aB.cncrtBlncs[1].blnCfg.Units)
|
||||
}
|
||||
|
||||
if evCh, err := debitAbstractsFromConcretes(aB.cncrtBlncs,
|
||||
decimal.New(int64(time.Duration(10*time.Minute)), 0),
|
||||
&utils.CostIncrement{
|
||||
Increment: utils.NewDecimal(int64(time.Duration(time.Minute)), 0),
|
||||
RecurrentFee: utils.NewDecimal(1, 0)},
|
||||
new(utils.CGREvent), nil, nil, nil); err == nil || err != utils.ErrInsufficientCredit {
|
||||
if _, err := debitConcreteUnits(decimal.New(int64(time.Duration(10*time.Minute)), 0),
|
||||
utils.EmptyString, aB.cncrtBlncs, new(utils.CGREvent)); err == nil || err != utils.ErrInsufficientCredit {
|
||||
t.Error(err)
|
||||
} else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(500, 0)) != 0 {
|
||||
t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[0].blnCfg.Units)
|
||||
} else if aB.cncrtBlncs[1].blnCfg.Units.Compare(utils.NewDecimal(125, 2)) != 0 {
|
||||
t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[1].blnCfg.Units)
|
||||
t.Errorf("Unexpected units in second balance: %s", aB.cncrtBlncs[1].blnCfg.Units)
|
||||
}
|
||||
|
||||
expectedEvCharg := &utils.EventCharges{
|
||||
Concretes: utils.NewDecimal(925, 2),
|
||||
Accounting: make(map[string]*utils.AccountCharge),
|
||||
UnitFactors: make(map[string]*utils.UnitFactor),
|
||||
Rating: make(map[string]*utils.RateSInterval),
|
||||
}
|
||||
if evCh, err := debitConcreteUnits(decimal.New(925, 2),
|
||||
utils.EmptyString, aB.cncrtBlncs, new(utils.CGREvent)); err != nil {
|
||||
t.Error(err)
|
||||
} else if aB.cncrtBlncs[0].blnCfg.Units.Compare(utils.NewDecimal(-200, 0)) != 0 {
|
||||
t.Errorf("Unexpected units in first balance: %s", aB.cncrtBlncs[0].blnCfg.Units)
|
||||
} else if aB.cncrtBlncs[1].blnCfg.Units.Compare(utils.NewDecimal(-1, 0)) != 0 {
|
||||
t.Errorf("Unexpected units in second balance: %s", aB.cncrtBlncs[1].blnCfg.Units)
|
||||
} else if !reflect.DeepEqual(evCh, expectedEvCharg) {
|
||||
t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedEvCharg), utils.ToJSON(evCh))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
func TestABDebitAbstracts(t *testing.T) {
|
||||
aB := &abstractBalance{
|
||||
|
||||
@@ -436,6 +436,15 @@ func TestAccountsDebit(t *testing.T) {
|
||||
t.Errorf("Expected %+v, received %+v", utils.ToJSON(expectedUsage), utils.ToJSON(evCh.Concretes))
|
||||
}
|
||||
|
||||
var err error
|
||||
utils.Logger, err = utils.Newlogger(utils.MetaStdLog, utils.EmptyString)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
utils.Logger.SetLogLevel(7)
|
||||
buff := new(bytes.Buffer)
|
||||
log.SetOutput(buff)
|
||||
|
||||
accntsPrf[0].Balances["ConcreteBalance2"].Units = &utils.Decimal{decimal.New(213, 0)}
|
||||
accnts.dm = nil
|
||||
expected = "NO_DATA_BASE_CONNECTION"
|
||||
@@ -443,6 +452,12 @@ func TestAccountsDebit(t *testing.T) {
|
||||
t.Errorf("Expected %+v, received %+v", expected, err)
|
||||
}
|
||||
|
||||
subString := "<AccountS> error <NO_DATA_BASE_CONNECTION> restoring account <cgrates.org:TestAccountsDebit>"
|
||||
if rcv := buff.String(); !strings.Contains(rcv, subString) {
|
||||
t.Errorf("Expected %+q, received %+q", subString, rcv)
|
||||
}
|
||||
|
||||
log.SetOutput(os.Stderr)
|
||||
}
|
||||
|
||||
func TestV1AccountProfilesForEvent(t *testing.T) {
|
||||
|
||||
@@ -19,7 +19,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -421,11 +425,13 @@ func TestRestoreAccount(t *testing.T) { //coverage purpose
|
||||
if err := dm.SetAccountProfile(acntPrf, false); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
restoreAccounts(dm, []*utils.AccountProfileWithWeight{
|
||||
{acntPrf, 0, utils.EmptyString},
|
||||
}, []utils.AccountBalancesBackup{
|
||||
map[string]*decimal.Big{"CB2": decimal.New(100, 0)},
|
||||
})
|
||||
|
||||
if rcv, err := dm.GetAccountProfile("cgrates.org", "1001"); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(rcv.Balances) != 2 {
|
||||
@@ -459,12 +465,27 @@ func TestRestoreAccount2(t *testing.T) { //coverage purpose
|
||||
},
|
||||
},
|
||||
}
|
||||
var err error
|
||||
utils.Logger, err = utils.Newlogger(utils.MetaStdLog, utils.EmptyString)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
utils.Logger.SetLogLevel(7)
|
||||
buff := new(bytes.Buffer)
|
||||
log.SetOutput(buff)
|
||||
|
||||
restoreAccounts(dm, []*utils.AccountProfileWithWeight{
|
||||
{acntPrf, 0, utils.EmptyString},
|
||||
}, []utils.AccountBalancesBackup{
|
||||
map[string]*decimal.Big{"CB1": decimal.New(100, 0)},
|
||||
})
|
||||
|
||||
subString := "<AccountS> error <NOT_IMPLEMENTED> restoring account <cgrates.org:1001>"
|
||||
if rcv := buff.String(); !strings.Contains(rcv, subString) {
|
||||
t.Errorf("Expected %+q, received %+q", subString, rcv)
|
||||
}
|
||||
|
||||
log.SetOutput(os.Stderr)
|
||||
}
|
||||
|
||||
func TestRestoreAccount3(t *testing.T) { //coverage purpose
|
||||
@@ -491,10 +512,10 @@ func TestRestoreAccount3(t *testing.T) { //coverage purpose
|
||||
if err := dm.SetAccountProfile(acntPrf, false); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
restoreAccounts(dm, []*utils.AccountProfileWithWeight{
|
||||
{acntPrf, 0, utils.EmptyString},
|
||||
}, []utils.AccountBalancesBackup{
|
||||
nil,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// Newlogger creates the Loggerr object
|
||||
// Newlogger creates the Logger object
|
||||
func Newlogger(loggertype, id string) (lgr LoggerInterface, err error) {
|
||||
lgr = &StdLogger{nodeID: id}
|
||||
switch loggertype {
|
||||
|
||||
Reference in New Issue
Block a user