Added tests in config

This commit is contained in:
andronache
2020-12-07 17:49:13 +02:00
committed by Dan Christian Bogos
parent 22e0c16e6d
commit 530ad1e541
3 changed files with 154 additions and 0 deletions

View File

@@ -17,3 +17,86 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package config
import (
"reflect"
"testing"
"github.com/cgrates/cgrates/utils"
)
func TestActionSCfgLoadFromJSONCfg(t *testing.T) {
jsonCfg := &ActionSJsonCfg{
Enabled: utils.BoolPointer(true),
Indexed_selects: utils.BoolPointer(false),
String_indexed_fields: &[]string{"*req.index1"},
Prefix_indexed_fields: &[]string{"*req.index1", "*req.index2"},
Suffix_indexed_fields: &[]string{"*req.index1"},
Nested_fields: utils.BoolPointer(true),
}
expected := &ActionSCfg{
Enabled: true,
IndexedSelects: false,
StringIndexedFields: &[]string{"*req.index1"},
PrefixIndexedFields: &[]string{"*req.index1", "*req.index2"},
SuffixIndexedFields: &[]string{"*req.index1"},
NestedFields: true,
}
jsnCfg := NewDefaultCGRConfig()
if err = jsnCfg.actionSCfg.loadFromJSONCfg(jsonCfg); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(expected, jsnCfg.actionSCfg) {
t.Errorf("\nExpecting <%+v>,\n Received <%+v>", utils.ToJSON(expected), utils.ToJSON(jsnCfg.actionSCfg))
}
}
func TestActionSCfgAsMapInterface(t *testing.T) {
cfgJSONStr := `{
"actions": {
"enabled": true,
"Indexed_selects": false,
"String_indexed_fields": ["*req.index1"],
"Prefix_indexed_fields": ["*req.index1","*req.index2"],
"Suffix_indexed_fields": ["*req.index1"],
"Nested_fields": true,
},
}`
eMap := map[string]interface{}{
utils.EnabledCfg: true,
utils.IndexedSelectsCfg: false,
utils.StringIndexedFieldsCfg: []string{"*req.index1"},
utils.PrefixIndexedFieldsCfg: []string{"*req.index1", "*req.index2"},
utils.SuffixIndexedFieldsCfg: []string{"*req.index1"},
utils.NestedFieldsCfg: true,
}
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgJSONStr); err != nil {
t.Error(err)
} else if rcv := cgrCfg.actionSCfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) {
t.Errorf("Expected: %+v\n Received: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv))
}
}
func TestActionSCfgClone(t *testing.T) {
ban := &ActionSCfg{
Enabled: true,
IndexedSelects: false,
StringIndexedFields: &[]string{"*req.index1"},
PrefixIndexedFields: &[]string{"*req.index1", "*req.index2"},
SuffixIndexedFields: &[]string{"*req.index1"},
NestedFields: true,
}
rcv := ban.Clone()
if !reflect.DeepEqual(ban, rcv) {
t.Errorf("\nExpected: %+v\nReceived: %+v", utils.ToJSON(ban), utils.ToJSON(rcv))
}
if (*rcv.StringIndexedFields)[0] = ""; (*ban.StringIndexedFields)[0] != "*req.index1" {
t.Errorf("Expected clone to not modify the cloned")
}
if (*rcv.PrefixIndexedFields)[0] = ""; (*ban.PrefixIndexedFields)[0] != "*req.index1" {
t.Errorf("Expected clone to not modify the cloned")
}
if (*rcv.SuffixIndexedFields)[0] = ""; (*ban.SuffixIndexedFields)[0] != "*req.index1" {
t.Errorf("Expected clone to not modify the cloned")
}
}

View File

@@ -2162,3 +2162,23 @@ func TestDfTemplateSJsonCfg(t *testing.T) {
t.Errorf("Expected: %+v \n,received: %+v", utils.ToJSON(eCfg), utils.ToJSON(cfg))
}
}
func TestDfActionSJsonCfg(t *testing.T) {
eCfg := &ActionSJsonCfg{
Enabled: utils.BoolPointer(false),
Indexed_selects: utils.BoolPointer(true),
String_indexed_fields: nil,
Prefix_indexed_fields: &[]string{},
Suffix_indexed_fields: &[]string{},
Nested_fields: utils.BoolPointer(false),
}
dfCgrJSONCfg, err := NewCgrJsonCfgFromBytes([]byte(CGRATES_CFG_JSON))
if err != nil {
t.Error(err)
}
if cfg, err := dfCgrJSONCfg.ActionSCfgJson(); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(eCfg, cfg) {
t.Error("Received: ", utils.ToJSON(cfg))
}
}

View File

@@ -5868,3 +5868,54 @@ func TestCGRConfigClone(t *testing.T) {
t.Errorf("Expected: %+v\nReceived: %+v", utils.ToJSON(cfg.coreSCfg), utils.ToJSON(rcv.coreSCfg))
}
}
func TestActionSConfig(t *testing.T) {
expected := &ActionSCfg{
Enabled: false,
IndexedSelects: true,
StringIndexedFields: nil,
PrefixIndexedFields: &[]string{},
SuffixIndexedFields: &[]string{},
NestedFields: false,
}
cgrConfig := NewDefaultCGRConfig()
newConfig := cgrConfig.ActionSCfg()
if !reflect.DeepEqual(expected, newConfig) {
t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expected), utils.ToJSON(newConfig))
}
}
func TestV1GetConfigSectionActionSJson(t *testing.T) {
var reply map[string]interface{}
expected := map[string]interface{}{
ActionSJson: map[string]interface{}{
utils.EnabledCfg: false,
utils.IndexedSelectsCfg: true,
utils.PrefixIndexedFieldsCfg: []string{},
utils.SuffixIndexedFieldsCfg: []string{},
utils.NestedFieldsCfg: false,
},
}
cfgCgr := NewDefaultCGRConfig()
if err := cfgCgr.V1GetConfig(&SectionWithOpts{Section: ActionSJson}, &reply); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(reply, expected) {
t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expected), utils.ToJSON(reply))
}
}
func TestLoadActionSCfgError(t *testing.T) {
cfgJSONStr := `{
"actions": {
"string_indexed_fields": "*req.index",
}
}`
expected := "json: cannot unmarshal string into Go struct field ActionSJsonCfg.String_indexed_fields of type []string"
cgrConfig := NewDefaultCGRConfig()
if cgrCfgJSON, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil {
t.Error(err)
} else if err := cgrConfig.loadActionSCfg(cgrCfgJSON); err == nil || err.Error() != expected {
t.Errorf("Expected %+v, received %+v", expected, err)
}
}