mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add coverage tests for balances.go, resources.go and storage_utils.go
This commit is contained in:
committed by
Dan Christian Bogos
parent
710a8d78be
commit
df9947730c
@@ -436,3 +436,115 @@ func TestBalancePublish(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestBalancesMatchFilter(t *testing.T) {
|
||||
b := Balance{
|
||||
ID: "test",
|
||||
}
|
||||
|
||||
rcv := b.MatchFilter(nil, false, false)
|
||||
|
||||
if rcv != true {
|
||||
t.Error(rcv)
|
||||
}
|
||||
|
||||
str := "test"
|
||||
bf := BalanceFilter{
|
||||
ID: &str,
|
||||
Uuid: &str,
|
||||
}
|
||||
|
||||
rcv = b.MatchFilter(&bf, false, false)
|
||||
|
||||
if rcv != false {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalancesHardMatchFilter(t *testing.T) {
|
||||
b := Balance{
|
||||
ID: "test",
|
||||
}
|
||||
|
||||
rcv := b.HardMatchFilter(nil, false)
|
||||
|
||||
if rcv != true {
|
||||
t.Error(rcv)
|
||||
}
|
||||
|
||||
str := "test"
|
||||
bf := BalanceFilter{
|
||||
ID: &str,
|
||||
Uuid: &str,
|
||||
}
|
||||
|
||||
rcv = b.HardMatchFilter(&bf, false)
|
||||
|
||||
if rcv != false {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalancesIsActive(t *testing.T) {
|
||||
b := Balance{
|
||||
Disabled: true,
|
||||
}
|
||||
|
||||
tm := time.Now()
|
||||
rcv := b.IsActiveAt(tm)
|
||||
|
||||
if rcv != false {
|
||||
t.Error(rcv)
|
||||
}
|
||||
|
||||
b2 := Balance{
|
||||
Disabled: false,
|
||||
Timings: []*RITiming{
|
||||
{
|
||||
Years: utils.Years{2020, 2022},
|
||||
Months: utils.Months{time.Now().Month()},
|
||||
MonthDays: utils.MonthDays{time.Now().Day()},
|
||||
WeekDays: utils.WeekDays{time.Now().Weekday()},
|
||||
StartTime: "00:00:00",
|
||||
EndTime: "00:00:01",
|
||||
cronString: "test",
|
||||
tag: "test",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
tm2 := time.Now()
|
||||
rcv = b2.IsActiveAt(tm2)
|
||||
|
||||
if rcv != false {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalancesHasDestination(t *testing.T) {
|
||||
b := Balance{
|
||||
DestinationIDs: utils.StringMap{"*any": false},
|
||||
}
|
||||
|
||||
rcv := b.HasDestination()
|
||||
|
||||
if rcv != true {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalancesMatchDestination(t *testing.T) {
|
||||
|
||||
b := Balance{
|
||||
DestinationIDs: utils.StringMap{
|
||||
"*any": false,
|
||||
"test": true,
|
||||
},
|
||||
}
|
||||
|
||||
rcv := b.MatchDestination("test")
|
||||
|
||||
if rcv != true {
|
||||
t.Error(rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1489,3 +1489,115 @@ func TestRSProcessThreshold(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourcesLockUnlock(t *testing.T) {
|
||||
rp := ResourceProfile{
|
||||
Tenant: "",
|
||||
ID: "",
|
||||
}
|
||||
|
||||
rp.lock("")
|
||||
|
||||
if rp.lkID == "" {
|
||||
t.Error("didn't lock")
|
||||
}
|
||||
|
||||
rp.lkID = ""
|
||||
|
||||
rp.unlock()
|
||||
}
|
||||
|
||||
func TestResourcesLockUnlock2(t *testing.T) {
|
||||
rp := Resource{
|
||||
Tenant: "",
|
||||
ID: "",
|
||||
}
|
||||
|
||||
rp.lock("")
|
||||
|
||||
if rp.lkID == "" {
|
||||
t.Error("didn't lock")
|
||||
}
|
||||
|
||||
rp.lkID = ""
|
||||
|
||||
rp.unlock()
|
||||
}
|
||||
|
||||
func TestResourcesRecordUsage(t *testing.T) {
|
||||
tm := 0 * time.Millisecond
|
||||
r := Resource{
|
||||
ttl: &tm,
|
||||
}
|
||||
|
||||
ru := ResourceUsage{}
|
||||
|
||||
err := r.recordUsage(&ru)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourcesrecordUsage(t *testing.T) {
|
||||
r := Resource{
|
||||
Usages: map[string]*ResourceUsage{"tes": {ID: "tes"}},
|
||||
}
|
||||
r2 := Resource{
|
||||
Usages: map[string]*ResourceUsage{"test": {ID: "test"}},
|
||||
}
|
||||
r3 := Resource{
|
||||
Usages: map[string]*ResourceUsage{"test": {ID: "test"}},
|
||||
}
|
||||
|
||||
rs := Resources{&r, &r2, &r3}
|
||||
ru := ResourceUsage{ID: "test"}
|
||||
|
||||
err := rs.recordUsage(&ru)
|
||||
if err.Error() != "duplicate resource usage with id: :test" {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourcesClearUsage(t *testing.T) {
|
||||
tm := 1 * time.Millisecond
|
||||
r := Resource{
|
||||
ttl: &tm,
|
||||
}
|
||||
rs := Resources{&r}
|
||||
|
||||
err := rs.clearUsage("test")
|
||||
|
||||
if err.Error() != "cannot find usage record with id: test" {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourcesAllocateReource(t *testing.T) {
|
||||
rs := Resources{}
|
||||
|
||||
rcv, err := rs.allocateResource(nil, false)
|
||||
|
||||
if err.Error() != utils.ErrResourceUnavailable.Error() {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rcv != "" {
|
||||
t.Errorf("expected %s\n, recived %s\n", rcv, "")
|
||||
}
|
||||
|
||||
r := Resource{}
|
||||
rs2 := Resources{&r}
|
||||
ru := ResourceUsage{
|
||||
ID: "test",
|
||||
}
|
||||
|
||||
rcv, err = rs2.allocateResource(&ru, true)
|
||||
|
||||
if err.Error() != "empty configuration for resourceID: :" {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if rcv != "" {
|
||||
t.Errorf("expected %s\n, recived %s\n", rcv, "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1478,3 +1478,132 @@ func TestTpReaderIsValid(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestStorageUtilsNewDataDBConn(t *testing.T) {
|
||||
str := "test"
|
||||
strn := "1"
|
||||
|
||||
type args struct {
|
||||
dbType, host, port, name, user,
|
||||
pass, marshaler, sentinelName string
|
||||
itemsCacheCfg map[string]*config.ItemOpt
|
||||
}
|
||||
|
||||
ms, _ := NewMongoStorage(str, str, str, str, str, str, nil, true)
|
||||
|
||||
type exp struct {
|
||||
d DataDB
|
||||
err string
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
exp exp
|
||||
}{
|
||||
{
|
||||
name: "default error",
|
||||
args: args{str, str, str, str, str, str, str, str, map[string]*config.ItemOpt{}},
|
||||
exp: exp{nil, "unsupported db_type <test>"},
|
||||
},
|
||||
{
|
||||
name: "atoi error",
|
||||
args: args{"*redis", str, str, str, str, str, str, str, map[string]*config.ItemOpt{}},
|
||||
exp: exp{nil, `strconv.Atoi: parsing "test": invalid syntax`},
|
||||
},
|
||||
{
|
||||
name: "meta mongo case",
|
||||
args: args{"*mongo", strn, strn, strn, str, str, str, str, map[string]*config.ItemOpt{}},
|
||||
exp: exp{ms, "Unsupported marshaler: test"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rcv, err := NewDataDBConn(tt.args.dbType, tt.args.host, tt.args.port, tt.args.name, tt.args.user,
|
||||
tt.args.pass, tt.args.marshaler, tt.args.sentinelName, tt.args.itemsCacheCfg)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != tt.exp.err {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(rcv, tt.exp.d) {
|
||||
t.Errorf("received %v, expected %v", rcv, tt.exp.d)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageUtilsNewStorDBConn(t *testing.T) {
|
||||
str := "test"
|
||||
|
||||
type args struct {
|
||||
dbType, host, port, name, user, pass, marshaler, sslmode string
|
||||
maxConn, maxIdleConn, connMaxLifetime int
|
||||
stringIndexedFields, prefixIndexedFields []string
|
||||
itemsCacheCfg map[string]*config.ItemOpt
|
||||
}
|
||||
|
||||
type exp struct {
|
||||
db StorDB
|
||||
err string
|
||||
}
|
||||
|
||||
db, err := NewMongoStorage("1", "1", "1", str, str, str, []string{"test"}, false)
|
||||
db2, err2 := NewPostgresStorage("1", "1", "1", str, str, str, 1, 1, 1)
|
||||
db3, err3 := NewMySQLStorage("1", "1", "1", str, str, 1, 1, 1)
|
||||
|
||||
tests := []struct{
|
||||
name string
|
||||
args args
|
||||
exp exp
|
||||
}{
|
||||
{
|
||||
name: "case mongo",
|
||||
args: args{"*mongo", "1", "1", "1", str, str, str, str, 1, 1, 1, []string{"test"}, []string{"test2"}, map[string]*config.ItemOpt{}},
|
||||
exp: exp{db, err.Error()},
|
||||
},
|
||||
{
|
||||
name: "case postgres",
|
||||
args: args{"*postgres", "1", "1", "1", str, str, str, str, 1, 1, 1, []string{"test"}, []string{"test2"}, map[string]*config.ItemOpt{}},
|
||||
exp: exp{db2, err2.Error()},
|
||||
},
|
||||
{
|
||||
name: "case MySQL",
|
||||
args: args{"*mysql", "1", "1", "1", str, str, str, str, 1, 1, 1, []string{"test"}, []string{"test2"}, map[string]*config.ItemOpt{}},
|
||||
exp: exp{db3, err3.Error()},
|
||||
},
|
||||
{
|
||||
name: "case default",
|
||||
args: args{"test", "1", "1", "1", str, str, str, str, 1, 1, 1, []string{"test"}, []string{"test2"}, map[string]*config.ItemOpt{}},
|
||||
exp: exp{nil, "unknown db 'test' valid options are [*mysql, *mongo, *postgres, *internal]"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rcv, err := NewStorDBConn(tt.args.dbType, tt.args.host, tt.args.port, tt.args.name, tt.args.user,
|
||||
tt.args.pass, tt.args.marshaler, tt.args.sslmode, tt.args.maxConn,
|
||||
tt.args.maxIdleConn, tt.args.connMaxLifetime, tt.args.stringIndexedFields,
|
||||
tt.args.prefixIndexedFields, tt.args.itemsCacheCfg)
|
||||
|
||||
if err.Error() != tt.exp.err {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(rcv, tt.exp.db) {
|
||||
t.Errorf("received %v, expected %v", rcv, tt.exp.db)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageUtilsBuildURL(t *testing.T) {
|
||||
s := "test"
|
||||
_, err := buildURL(s, "!£%$%$£&$%/&%(/&)&)", s, s, s, s)
|
||||
if err.Error() != `parse "//!£%$%$£&$%/&%(/&)&)": invalid URL escape "%$%"` {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user