Cover test for account in apis

This commit is contained in:
andronache
2021-05-28 14:01:33 +03:00
committed by Dan Christian Bogos
parent ad0f291d26
commit af7ec4a84c

View File

@@ -20,6 +20,7 @@ package apis
import (
"reflect"
"sort"
"testing"
"github.com/cgrates/birpc/context"
@@ -407,7 +408,7 @@ func TestAccountSetGetAccountErrorBadFilter(t *testing.T) {
}
}
func TestAccountSetGetAccountError(t *testing.T) {
func TestAccountSetGetAccountErrorSetLoadIDs(t *testing.T) {
cacheInit := engine.Cache
cfg := config.NewDefaultCGRConfig()
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
@@ -462,3 +463,281 @@ func TestAccountSetGetAccountError(t *testing.T) {
}
engine.Cache = cacheInit
}
func TestAccountSetGetAccountErrorCallCache(t *testing.T) {
cacheInit := engine.Cache
cfg := config.NewDefaultCGRConfig()
cfg.GeneralCfg().DefaultCaching = "123"
cfg.AdminSCfg().CachesConns = []string{}
connMgr := engine.NewConnManager(cfg, nil)
dataDB := &engine.DataDBMock{
SetAccountDrvF: func(ctx *context.Context, profile *utils.Account) error {
return nil
},
GetAccountDrvF: func(ctx *context.Context, str1 string, str2 string) (*utils.Account, error) {
return nil, nil
},
SetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx string, indexes map[string]utils.StringSet, commit bool, transactionID string) (err error) {
return nil
},
GetIndexesDrvF: func(ctx *context.Context, idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) {
return nil, nil
},
SetLoadIDsDrvF: func(ctx *context.Context, loadIDs map[string]int64) error {
return nil
},
}
dm := engine.NewDataManager(dataDB, nil, connMgr)
admS := NewAdminSv1(cfg, dm, connMgr)
newCache := engine.NewCacheS(cfg, dm, nil)
engine.Cache = newCache
ext := &APIAccountWithAPIOpts{
APIAccount: &utils.APIAccount{
Tenant: "",
ID: "test_ID1",
Weights: ";10",
Opts: map[string]interface{}{},
Balances: map[string]*utils.APIBalance{
"VoiceBalance": {
ID: "VoiceBalance",
Weights: ";12",
Type: "*abstract",
Opts: map[string]interface{}{
"Destination": 10,
},
Units: 0,
},
},
ThresholdIDs: nil,
},
APIOpts: nil,
}
var setRply string
err := admS.SetAccount(context.Background(), ext, &setRply)
if err == nil || err.Error() != "SERVER_ERROR: MANDATORY_IE_MISSING: [connIDs]" {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", "SERVER_ERROR: MANDATORY_IE_MISSING: [connIDs]", err)
}
if !reflect.DeepEqual(setRply, "") {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", "", utils.ToJSON(setRply))
}
engine.Cache = cacheInit
}
func TestAccountSetGetAccountIDs(t *testing.T) {
cfg := config.NewDefaultCGRConfig()
cfg.GeneralCfg().DefaultCaching = utils.MetaNone
connMgr := engine.NewConnManager(cfg, nil)
dataDB := engine.NewInternalDB(nil, nil, true)
dm := engine.NewDataManager(dataDB, nil, connMgr)
admS := NewAdminSv1(cfg, dm, connMgr)
ext := &APIAccountWithAPIOpts{
APIAccount: &utils.APIAccount{
Tenant: "testTenant",
ID: "test_ID1",
Opts: map[string]interface{}{},
Balances: map[string]*utils.APIBalance{
"VoiceBalance": {
ID: "VoiceBalance",
FilterIDs: []string{"*string:~*req.Account:1001"},
Weights: ";12",
Type: "*abstract",
Opts: map[string]interface{}{
"Destination": 10,
},
Units: 0,
},
},
Weights: ";10",
},
APIOpts: nil,
}
var setRply string
err := admS.SetAccount(context.Background(), ext, &setRply)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(setRply, `OK`) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", `OK`, utils.ToJSON(setRply))
}
var getRply []string
err = admS.GetAccountIDs(context.Background(),
&utils.PaginatorWithTenant{
Tenant: "testTenant",
Paginator: utils.Paginator{},
APIOpts: nil,
}, &getRply)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
expectedGet := []string{"test_ID1"}
if !reflect.DeepEqual(getRply, expectedGet) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expectedGet), utils.ToJSON(getRply))
}
var getRplyCount int
err = admS.GetAccountIDsCount(context.Background(),
&utils.TenantWithAPIOpts{
Tenant: "testTenant",
APIOpts: nil,
}, &getRplyCount)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(getRplyCount, 1) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", "1", utils.ToJSON(getRplyCount))
}
ext2 := &APIAccountWithAPIOpts{
APIAccount: &utils.APIAccount{
Tenant: "testTenant",
ID: "test_ID2",
Opts: map[string]interface{}{},
Balances: map[string]*utils.APIBalance{
"VoiceBalance": {
ID: "VoiceBalance",
FilterIDs: []string{"*string:~*req.Account:1001"},
Weights: ";12",
Type: "*abstract",
Opts: map[string]interface{}{
"Destination": 10,
},
Units: 0,
},
},
Weights: ";10",
},
APIOpts: nil,
}
var setRply2 string
err = admS.SetAccount(context.Background(), ext2, &setRply2)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(setRply2, `OK`) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", `OK`, utils.ToJSON(setRply2))
}
var getRply3 []string
err = admS.GetAccountIDs(context.Background(),
&utils.PaginatorWithTenant{
Tenant: "testTenant",
Paginator: utils.Paginator{},
APIOpts: nil,
}, &getRply3)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
expectedGet3 := []string{"test_ID1", "test_ID2"}
sort.Strings(getRply3)
if !reflect.DeepEqual(getRply3, expectedGet3) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expectedGet3), utils.ToJSON(getRply3))
}
var getRply4 int
err = admS.GetAccountIDsCount(context.Background(),
&utils.TenantWithAPIOpts{
Tenant: "testTenant",
APIOpts: nil,
}, &getRply4)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(getRply4, 2) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", "2", utils.ToJSON(getRply4))
}
var getRplyRmv string
err = admS.RemoveAccount(context.Background(),
&utils.TenantIDWithAPIOpts{
TenantID: &utils.TenantID{
Tenant: "testTenant",
ID: "test_ID2",
},
APIOpts: nil,
}, &getRplyRmv)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(getRplyRmv, `OK`) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", `OK`, utils.ToJSON(getRplyRmv))
}
var getRplyCount2 int
err = admS.GetAccountIDsCount(context.Background(),
&utils.TenantWithAPIOpts{
Tenant: "testTenant",
APIOpts: nil,
}, &getRplyCount2)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(getRplyCount2, 1) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", "1", utils.ToJSON(getRplyCount2))
}
var getRplyID []string
err = admS.GetAccountIDs(context.Background(),
&utils.PaginatorWithTenant{
Tenant: "testTenant",
Paginator: utils.Paginator{},
APIOpts: nil,
}, &getRplyID)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
expectedID := []string{"test_ID1"}
if !reflect.DeepEqual(getRplyID, expectedID) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expectedID), utils.ToJSON(getRplyID))
}
var getNtf utils.Account
err = admS.GetAccount(context.Background(),
&utils.TenantIDWithAPIOpts{
TenantID: &utils.TenantID{
Tenant: "",
ID: "test_ID2",
},
APIOpts: nil,
}, &getNtf)
if err == nil || err != utils.ErrNotFound {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ErrNotFound, err)
}
var getRplyRmv2 string
err = admS.RemoveAccount(context.Background(),
&utils.TenantIDWithAPIOpts{
TenantID: &utils.TenantID{
Tenant: "testTenant",
ID: "test_ID1",
},
APIOpts: nil,
}, &getRplyRmv2)
if err != nil {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err)
}
if !reflect.DeepEqual(getRplyRmv2, `OK`) {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", `OK`, utils.ToJSON(getRplyRmv2))
}
var getRplyCount3 int
err = admS.GetAccountIDsCount(context.Background(),
&utils.TenantWithAPIOpts{
Tenant: "testTenant",
APIOpts: nil,
}, &getRplyCount3)
if err == nil || err != utils.ErrNotFound {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ErrNotFound, err)
}
var getRplyID2 []string
err = admS.GetAccountIDs(context.Background(),
&utils.PaginatorWithTenant{
Tenant: "testTenant",
Paginator: utils.Paginator{},
APIOpts: nil,
}, &getRplyID2)
if err == nil || err != utils.ErrNotFound {
t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ErrNotFound, err)
}
}