mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-23 08:08:45 +05:00
Add coverage tests for utils
This commit is contained in:
committed by
Dan Christian Bogos
parent
b5358d1cea
commit
94abb7d7d0
@@ -1735,3 +1735,5 @@ func TestDfEventReaderCfg(t *testing.T) {
|
||||
t.Errorf("Expected: %+v, \nreceived: %+v", utils.ToJSON(eCfg), utils.ToJSON(cfg))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
@@ -1947,3 +1948,233 @@ func TestGeneralCfg(t *testing.T) {
|
||||
t.Errorf("Expected: %+v\nReceived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigDBFields(t *testing.T) {
|
||||
d := dbDefaults{}
|
||||
str := "test"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
rcv any
|
||||
exp any
|
||||
}{
|
||||
{
|
||||
name: "dbName diff from dynamic",
|
||||
rcv: d.dbName(str, str),
|
||||
exp: str,
|
||||
},
|
||||
{
|
||||
name: "dbUser diff from dynamic",
|
||||
rcv: d.dbUser(str, str),
|
||||
exp: str,
|
||||
},
|
||||
{
|
||||
name: "dbHost diff from dynamic",
|
||||
rcv: d.dbHost(str, str),
|
||||
exp: str,
|
||||
},
|
||||
{
|
||||
name: "dbPass diff from dynamic",
|
||||
rcv: d.dbPass(str, str),
|
||||
exp: str,
|
||||
},
|
||||
{
|
||||
name: "isHidden true",
|
||||
rcv: isHidden(".test"),
|
||||
exp: true,
|
||||
},
|
||||
{
|
||||
name: "isHidden false",
|
||||
rcv: isHidden("."),
|
||||
exp: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if tt.rcv != tt.exp {
|
||||
t.Errorf("Expected: %+v\nReceived: %+v", tt.exp, tt.rcv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigLoadErrors(t *testing.T) {
|
||||
c := CGRConfig{}
|
||||
js := json.RawMessage([]byte(`test`))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
rcv error
|
||||
exp string
|
||||
}{
|
||||
{
|
||||
name: "load general cfg error check",
|
||||
rcv: c.loadGeneralCfg(&CgrJsonCfg{GENERAL_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load cache cfg error check",
|
||||
rcv: c.loadCacheCfg(&CgrJsonCfg{CACHE_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load listen cfg error check",
|
||||
rcv: c.loadListenCfg(&CgrJsonCfg{LISTEN_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load HTTP cfg error check",
|
||||
rcv: c.loadHTTPCfg(&CgrJsonCfg{HTTP_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load StorDB cfg error check",
|
||||
rcv: c.loadStorDBCfg(&CgrJsonCfg{STORDB_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load FilterS cfg error check",
|
||||
rcv: c.loadFilterSCfg(&CgrJsonCfg{FilterSjsn: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load RalS cfg error check",
|
||||
rcv: c.loadRalSCfg(&CgrJsonCfg{RALS_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load Scheduler cfg error check",
|
||||
rcv: c.loadSchedulerCfg(&CgrJsonCfg{SCHEDULER_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load drs cfg error check",
|
||||
rcv: c.loadCdrsCfg(&CgrJsonCfg{CDRS_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load SessionS cfg error check",
|
||||
rcv: c.loadSessionSCfg(&CgrJsonCfg{SessionSJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load KamAgent cfg error check",
|
||||
rcv: c.loadKamAgentCfg(&CgrJsonCfg{KamailioAgentJSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load FreeswitchAgent cfg error check",
|
||||
rcv: c.loadFreeswitchAgentCfg(&CgrJsonCfg{FreeSWITCHAgentJSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load AsteriskAgent cfg error check",
|
||||
rcv: c.loadAsteriskAgentCfg(&CgrJsonCfg{AsteriskAgentJSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load DiameterAgent cfg error check",
|
||||
rcv: c.loadDiameterAgentCfg(&CgrJsonCfg{DA_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load RadiusAgent cfg error check",
|
||||
rcv: c.loadRadiusAgentCfg(&CgrJsonCfg{RA_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load DNSAgent cfg error check",
|
||||
rcv: c.loadDNSAgentCfg(&CgrJsonCfg{DNSAgentJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load HttpAgent cfg error check",
|
||||
rcv: c.loadHttpAgentCfg(&CgrJsonCfg{HttpAgentJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load AttributeS cfg error check",
|
||||
rcv: c.loadAttributeSCfg(&CgrJsonCfg{ATTRIBUTE_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load ChargerS cfg error check",
|
||||
rcv: c.loadChargerSCfg(&CgrJsonCfg{ChargerSCfgJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load ResourceS cfg error check",
|
||||
rcv: c.loadResourceSCfg(&CgrJsonCfg{RESOURCES_JSON: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load StatS cfg error check",
|
||||
rcv: c.loadStatSCfg(&CgrJsonCfg{STATS_JSON: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load ThresholdS cfg error check",
|
||||
rcv: c.loadThresholdSCfg(&CgrJsonCfg{THRESHOLDS_JSON: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load SupplierS cfg error check",
|
||||
rcv: c.loadSupplierSCfg(&CgrJsonCfg{SupplierSJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load Mailer cfg error check",
|
||||
rcv: c.loadMailerCfg(&CgrJsonCfg{MAILER_JSN: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load SureTax cfg error check",
|
||||
rcv: c.loadSureTaxCfg(&CgrJsonCfg{SURETAX_JSON: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load DispatcherS cfg error check",
|
||||
rcv: c.loadDispatcherSCfg(&CgrJsonCfg{DispatcherSJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load MigratorCgr cfg error check",
|
||||
rcv: c.loadMigratorCgrCfg(&CgrJsonCfg{CgrMigratorCfgJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load TlsCgr cfg error check",
|
||||
rcv: c.loadTlsCgrCfg(&CgrJsonCfg{TlsCfgJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load AnalyzerCgr cfg error check",
|
||||
rcv: c.loadAnalyzerCgrCfg(&CgrJsonCfg{AnalyzerCfgJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load Apier cfg error check",
|
||||
rcv: c.loadApierCfg(&CgrJsonCfg{ApierS: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load Apier cfg error check",
|
||||
rcv: c.loadApierCfg(&CgrJsonCfg{ApierS: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
{
|
||||
name: "load Ers cfg error check",
|
||||
rcv: c.loadErsCfg(&CgrJsonCfg{ERsJson: &js}),
|
||||
exp: "invalid character 'e' in literal true (expecting 'r')",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if tt.rcv != nil {
|
||||
if tt.rcv.Error() != tt.exp {
|
||||
t.Errorf("Expected: %+v\nReceived: %+v", tt.exp, tt.rcv)
|
||||
}
|
||||
} else {
|
||||
t.Error("was expecting an error")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,3 +97,33 @@ func TestHTTPCfgAsMapInterface(t *testing.T) {
|
||||
t.Errorf("Expected: %+v ,\n received: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPCfgAsMapInterface2(t *testing.T) {
|
||||
str := "test"
|
||||
bl := true
|
||||
mp := map[string]string{"test1": "val1", "test": "val2"}
|
||||
|
||||
httpcfg := HTTPCfg{
|
||||
HTTPJsonRPCURL: str,
|
||||
HTTPWSURL: str,
|
||||
HTTPFreeswitchCDRsURL: str,
|
||||
HTTPCDRsURL: str,
|
||||
HTTPUseBasicAuth: bl,
|
||||
HTTPAuthUsers: mp,
|
||||
}
|
||||
|
||||
exp := map[string]any{
|
||||
utils.HTTPJsonRPCURLCfg: httpcfg.HTTPJsonRPCURL,
|
||||
utils.HTTPWSURLCfg: httpcfg.HTTPWSURL,
|
||||
utils.HTTPFreeswitchCDRsURLCfg: httpcfg.HTTPFreeswitchCDRsURL,
|
||||
utils.HTTPCDRsURLCfg: httpcfg.HTTPCDRsURL,
|
||||
utils.HTTPUseBasicAuthCfg: httpcfg.HTTPUseBasicAuth,
|
||||
utils.HTTPAuthUsersCfg: map[string]any{"test1": "val1", "test": "val2"},
|
||||
}
|
||||
|
||||
rcv := httpcfg.AsMapInterface()
|
||||
|
||||
if !reflect.DeepEqual(rcv, exp) {
|
||||
t.Errorf("Expected: %+v ,\n received: %+v", exp, rcv)
|
||||
}
|
||||
}
|
||||
@@ -151,3 +151,30 @@ func TestKamAgentCfgAsMapInterface(t *testing.T) {
|
||||
t.Errorf("\nExpected: %+v\nReceived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
func TestKamAgentCfgloadFromJsonCfg2(t *testing.T) {
|
||||
nm := 1
|
||||
str := "test"
|
||||
self := KamConnCfg{}
|
||||
|
||||
js := KamConnJsonCfg{
|
||||
Alias: &str,
|
||||
Address: &str,
|
||||
Reconnects: &nm,
|
||||
}
|
||||
|
||||
exp := KamConnCfg{
|
||||
Alias: str,
|
||||
Address: str,
|
||||
Reconnects: nm,
|
||||
}
|
||||
|
||||
err := self.loadFromJsonCfg(&js)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(self, exp) {
|
||||
t.Errorf("\nExpected: %+v\nReceived: %+v", exp, self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ package config
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestListenCfgloadFromJsonCfg(t *testing.T) {
|
||||
@@ -62,3 +64,31 @@ func TestListenCfgloadFromJsonCfg(t *testing.T) {
|
||||
t.Errorf("Expected: %+v , received: %+v", expected, lstcfg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListenCfgAsMapInterface(t *testing.T) {
|
||||
str := "test"
|
||||
|
||||
lstcfg := ListenCfg{
|
||||
RPCJSONListen: str,
|
||||
RPCGOBListen: str,
|
||||
HTTPListen: str,
|
||||
RPCJSONTLSListen: str,
|
||||
RPCGOBTLSListen: str,
|
||||
HTTPTLSListen: str,
|
||||
}
|
||||
|
||||
exp := map[string]any{
|
||||
utils.RPCJSONListenCfg: lstcfg.RPCJSONListen,
|
||||
utils.RPCGOBListenCfg: lstcfg.RPCGOBListen,
|
||||
utils.HTTPListenCfg: lstcfg.HTTPListen,
|
||||
utils.RPCJSONTLSListenCfg: lstcfg.RPCJSONTLSListen,
|
||||
utils.RPCGOBTLSListenCfg: lstcfg.RPCGOBTLSListen,
|
||||
utils.HTTPTLSListenCfg: lstcfg.HTTPTLSListen,
|
||||
}
|
||||
|
||||
rcv := lstcfg.AsMapInterface()
|
||||
|
||||
if !reflect.DeepEqual(rcv, exp) {
|
||||
t.Errorf("Expected: %+v , received: %+v", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,3 +106,40 @@ func TestLoaderCgrCfgAsMapInterface(t *testing.T) {
|
||||
t.Errorf("Expected: %+v, Received: %+v, at field: %s", utils.ToJSON(eMap["scheduler_conns"]), utils.ToJSON(rcv["scheduler_conns"]), "scheduler_conns")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoaderCgrCfgloadFromJsonCfg2(t *testing.T) {
|
||||
str := "test"
|
||||
bl := false
|
||||
slc := []string{"val1", utils.MetaInternal}
|
||||
ld := LoaderCgrCfg{}
|
||||
|
||||
js := LoaderCfgJson{
|
||||
Tpid: &str,
|
||||
Data_path: &str,
|
||||
Disable_reverse: &bl,
|
||||
Field_separator: &str,
|
||||
Caches_conns: &slc,
|
||||
Scheduler_conns: &slc,
|
||||
}
|
||||
|
||||
slc2 := []string{"val1", utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCaches)}
|
||||
slc3 := []string{"val1", utils.ConcatenatedKey(utils.MetaInternal, utils.MetaScheduler)}
|
||||
|
||||
exp := LoaderCgrCfg{
|
||||
TpID: str,
|
||||
DataPath: str,
|
||||
DisableReverse: bl,
|
||||
FieldSeparator: 't',
|
||||
CachesConns: slc2,
|
||||
SchedulerConns: slc3,
|
||||
}
|
||||
|
||||
err := ld.loadFromJsonCfg(&js)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(ld, exp) {
|
||||
t.Errorf("Expected: %+v , received: %+v", exp, ld)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,3 +124,70 @@ func TestResourceSConfigAsMapInterface(t *testing.T) {
|
||||
t.Errorf("\nExpected: %+v\nReceived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourcesCfgloadFromJson(t *testing.T) {
|
||||
str := "err"
|
||||
s := ResourceSConfig{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arg *ResourceSJsonCfg
|
||||
err string
|
||||
}{
|
||||
{
|
||||
name: "conns different from *internal",
|
||||
arg: &ResourceSJsonCfg{Thresholds_conns: &[]string{str}},
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "conns different from *internal",
|
||||
arg: &ResourceSJsonCfg{Store_interval: &str},
|
||||
err: `time: invalid duration "err"`,
|
||||
},
|
||||
{
|
||||
name: "conns different from *internal",
|
||||
arg: &ResourceSJsonCfg{String_indexed_fields: &[]string{str}},
|
||||
err: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
err := s.loadFromJsonCfg(tt.arg)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != tt.err {
|
||||
t.Errorf("\nExpected: %+v\nReceived: %+v", tt.err, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourcesCfgAsMapInterface(t *testing.T) {
|
||||
slc := []string{"test"}
|
||||
|
||||
rlcfg := ResourceSConfig{
|
||||
Enabled: true,
|
||||
IndexedSelects: true,
|
||||
ThresholdSConns: slc,
|
||||
StoreInterval: 1 * time.Second,
|
||||
StringIndexedFields: &slc,
|
||||
PrefixIndexedFields: &slc,
|
||||
NestedFields: true,
|
||||
}
|
||||
|
||||
exp := map[string]any{
|
||||
utils.EnabledCfg: rlcfg.Enabled,
|
||||
utils.IndexedSelectsCfg: rlcfg.IndexedSelects,
|
||||
utils.ThresholdSConnsCfg: slc,
|
||||
utils.StoreIntervalCfg: "1s",
|
||||
utils.StringIndexedFieldsCfg: slc,
|
||||
utils.PrefixIndexedFieldsCfg: slc,
|
||||
utils.NestedFieldsCfg: rlcfg.NestedFields,
|
||||
}
|
||||
|
||||
rcv := rlcfg.AsMapInterface()
|
||||
|
||||
if !reflect.DeepEqual(rcv, exp) {
|
||||
t.Errorf("\nExpected: %+v\nReceived: %+v", exp, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,3 +81,41 @@ func TestSchedulerCfgAsMapInterface(t *testing.T) {
|
||||
t.Errorf("\nExpected: %+v\nReceived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchedulerCfgloadFromJsonCfg2(t *testing.T) {
|
||||
s := SchedulerCfg{}
|
||||
|
||||
tests := []struct{
|
||||
name string
|
||||
arg *SchedulerJsonCfg
|
||||
err string
|
||||
}{
|
||||
{
|
||||
name: "cdrs conns diff from *internal",
|
||||
arg: &SchedulerJsonCfg{Cdrs_conns: &[]string{"test"}},
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "cdrs conns equal to *internal",
|
||||
arg: &SchedulerJsonCfg{Cdrs_conns: &[]string{"*internal"}},
|
||||
err: "",
|
||||
},
|
||||
{
|
||||
name: "filers with value",
|
||||
arg: &SchedulerJsonCfg{Filters: &[]string{"test"}},
|
||||
err: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := s.loadFromJsonCfg(tt.arg)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() != tt.err {
|
||||
t.Errorf("\nExpected: %+v\nReceived: %+v", tt.err, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user