Improving coverage for unit tests

This commit is contained in:
gezimbll
2023-04-28 09:46:54 -04:00
committed by Dan Christian Bogos
parent a86d01be6b
commit f62dea0768
3 changed files with 246 additions and 25 deletions

View File

@@ -18,6 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"errors"
"net/http"
"net/url"
"reflect"
"testing"
"time"
@@ -97,3 +100,58 @@ func TestReplicatedCgrCdrAsCDR(t *testing.T) {
t.Errorf("Expecting %v, received: %v", expctRtCdr, CDR)
}
}
func TestNewCgrCdrFromHttpReq(t *testing.T) {
testCases := []struct {
name string
httpRequest *http.Request
timezone string
expectedCgrCdr CgrCdr
expectedError error
}{
{
name: "Valid request",
httpRequest: &http.Request{
RemoteAddr: "127.0.0.1:2040",
Form: url.Values{
"key1": {"value1"},
"key2": {"value2"},
},
},
timezone: "UTC",
expectedCgrCdr: CgrCdr{
utils.Source: "127.0.0.1:2040",
"key1": "value1",
"key2": "value2",
},
expectedError: nil,
},
{
name: "Error parsing form",
httpRequest: &http.Request{
RemoteAddr: "127.0.0.1",
Form: nil,
},
timezone: "UTC",
expectedCgrCdr: nil,
expectedError: errors.New("unable to parse form"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
httpRequestCopy := new(http.Request)
*httpRequestCopy = *tc.httpRequest
result, err := NewCgrCdrFromHttpReq(httpRequestCopy, tc.timezone)
if err != nil {
if tc.expectedError.Error() != err.Error() {
t.Error(err)
}
} else if tc.expectedCgrCdr != nil {
if !reflect.DeepEqual(tc.expectedCgrCdr, result) {
t.Errorf("Expected %v,Received %v", tc.expectedCgrCdr, result)
}
}
})
}
}

View File

@@ -803,8 +803,10 @@ func TestDMReplicateMultipleIds(t *testing.T) {
for i, acc := range accs {
dm.SetAccount(acc)
connIds[i] = acc.ID
//UpdateReplicationFilters(utils.ACCOUNT_PREFIX, connIds[i], utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1))
}
if err := replicateMultipleIDs(connMgr, connIds, true, utils.ACCOUNT_PREFIX, connIds, utils.ReplicatorSv1RemoveAccount, "cgrates.org"); err != nil {
if err := replicateMultipleIDs(connMgr, []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)}, true, utils.ACCOUNT_PREFIX, connIds, utils.ReplicatorSv1RemoveAccount, "cgrates.org"); err != nil {
t.Error(err)
} else if ids, err := dm.DataDB().GetKeysForPrefix("cgrates"); len(ids) > 0 || err != nil {
t.Error(err)
@@ -1668,28 +1670,51 @@ func TestDmRemoveFilter(t *testing.T) {
}
}
// func TestDMGetSupplierProfile(t *testing.T) {
// cfg, _ := config.NewDefaultCGRConfig()
// cfg.DataDbCfg().Items[utils.MetaSupplierProfiles].Remote = true
// cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)}
// defer func() {
// cfg2, _ := config.NewDefaultCGRConfig()
// config.SetCgrConfig(cfg2)
// }()
// clientConn := make(chan rpcclient.ClientConnector, 1)
// clientConn <- clMock(func(serviceMethod string, _, _ interface{}) error {
// if serviceMethod == utils.ReplicatorSv1GetSupplierProfile {
// return nil
// }
// return utils.ErrNotFound
// })
// db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
// dm := NewDataManager(db, cfg.CacheCfg(), NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{
// utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn,
// }))
// config.SetCgrConfig(cfg)
// if _, err := dm.GetSupplierProfile("cgrates.org", "SPL1", false, false, ""); err != nil {
// t.Error(err)
// }
func TestDMGetSupplierProfile(t *testing.T) {
cfg, _ := config.NewDefaultCGRConfig()
Cache.Clear(nil)
cfg.DataDbCfg().Items[utils.MetaSupplierProfiles].Remote = true
cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)}
defer func() {
cfg2, _ := config.NewDefaultCGRConfig()
config.SetCgrConfig(cfg2)
}()
clientConn := make(chan rpcclient.ClientConnector, 1)
clientConn <- clMock(func(serviceMethod string, _, reply interface{}) error {
if serviceMethod == utils.ReplicatorSv1GetSupplierProfile {
rpl := &SupplierProfile{}
*reply.(**SupplierProfile) = rpl
return nil
}
return utils.ErrNotFound
})
db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
dm := NewDataManager(db, cfg.CacheCfg(), NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{
utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn,
}))
config.SetCgrConfig(cfg)
if _, err := dm.GetSupplierProfile("cgrates.org", "SPL1", false, false, ""); err != nil {
t.Error(err)
}
}
// }
func TestConnManagerCallWithConnIDs(t *testing.T) {
cfg, _ := config.NewDefaultCGRConfig()
connId := utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)
cfg.RPCConns()[connId] = &config.RPCConn{
Conns: []*config.RemoteHost{
{
ID: connId,
Address: "127.0.0.1:2012",
Transport: utils.MetaJSON,
TLS: true,
},
},
}
connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{})
if err := connMgr.CallWithConnIDs([]string{connId}, utils.StringSet{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): {}}, utils.ReplicatorSv1GetAccount, nil, nil); err == nil {
t.Error(err)
}
}

View File

@@ -18,7 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"bytes"
"fmt"
"log"
"os"
"reflect"
"testing"
"time"
@@ -1310,3 +1313,138 @@ func TestTpRLoadAll(t *testing.T) {
t.Error(err)
}
}
func TestTpReaderIsValid(t *testing.T) {
cfg, _ := config.NewDefaultCGRConfig()
dataDb := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
storDb := NewInternalDB(nil, nil, false, cfg.StorDbCfg().Items)
tpId := "TP1"
tpr, err := NewTpReader(dataDb, storDb, tpId, "UTC", nil, nil)
if err != nil {
t.Error(err)
}
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
dests := []*utils.TPDestination{
{
TPid: tpId,
ID: "DEST",
Prefixes: []string{
"1001", "1002", "1003",
},
},
}
dest := &Destination{
Id: "DEST",
Prefixes: []string{
"1001", "1002", "1003",
},
}
rates := []*utils.TPRate{
{
TPid: tpId,
ID: "RATE1",
RateSlots: []*utils.RateSlot{
{ConnectFee: 12,
Rate: 3,
RateUnit: "4s",
RateIncrement: "6s",
GroupIntervalStart: "1s"},
},
}}
destRates := []*utils.TPDestinationRate{
{
TPid: tpId,
ID: "DR_FREESWITCH_USERS",
DestinationRates: []*utils.DestinationRate{
{
DestinationId: "DEST",
RateId: "RATE1",
RoundingMethod: "*up",
RoundingDecimals: 4},
},
},
}
timings := []*utils.ApierTPTiming{
{
TPid: tpId,
ID: "ALWAYS",
Years: "*any",
Months: "*any",
MonthDays: "*any",
WeekDays: "*any",
Time: "00:00:00",
},
}
ratingPlans := []*utils.TPRatingPlan{
{
TPid: tpId,
ID: "RP_1",
RatingPlanBindings: []*utils.TPRatingPlanBinding{
{
DestinationRatesId: "DR_FREESWITCH_USERS",
TimingId: "ALWAYS",
Weight: 10,
},
},
},
}
if err := storDb.SetTPDestinations(dests); err != nil {
t.Error(err)
}
if err := dataDb.SetDestinationDrv(dest, utils.NonTransactional); err != nil {
t.Error(err)
}
if err := storDb.SetTPRates(rates); err != nil {
t.Error(err)
}
if err := storDb.SetTPDestinationRates(destRates); err != nil {
t.Error(err)
}
if err := storDb.SetTPTimings(timings); err != nil {
t.Error(err)
}
if err := storDb.SetTPRatingPlans(ratingPlans); err != nil {
t.Error(err)
}
if err := tpr.LoadAll(); err != nil {
t.Error(err)
}
if valid := tpr.IsValid(); !valid {
t.Error("RatingPlan is not continous")
}
timings = []*utils.ApierTPTiming{
{
TPid: tpId,
ID: "ALWAYS",
Years: "*any",
Months: "*any",
MonthDays: "*any",
WeekDays: "1;2;3;4;5",
Time: "00:00:00",
},
}
if err := storDb.SetTPTimings(timings); err != nil {
t.Error(err)
}
if err := tpr.LoadAll(); err != nil {
t.Error(err)
}
if valid := tpr.IsValid(); !valid {
t.Error("RatingPlan is not continous")
}
}