Files
cgrates/engine/aliases_test.go
Radu Ioan Fericean b0fe280b34 improve reverse aliases
2015-11-23 14:23:34 +02:00

112 lines
2.9 KiB
Go

package engine
import (
"testing"
"github.com/cgrates/cgrates/cache2go"
"github.com/cgrates/cgrates/utils"
)
func init() {
aliasService = NewAliasHandler(accountingStorage)
}
func TestAliasesGetAlias(t *testing.T) {
alias := Alias{}
err := aliasService.GetAlias(Alias{
Direction: "*out",
Tenant: "cgrates.org",
Category: "call",
Account: "dan",
Subject: "dan",
Context: "*rating",
}, &alias)
if err != nil ||
len(alias.Values) != 2 ||
len(alias.Values[0].Pairs) != 2 {
t.Error("Error getting alias: ", err, alias)
}
}
func TestAliasesGetMatchingAlias(t *testing.T) {
var response string
err := aliasService.GetMatchingAlias(AttrMatchingAlias{
Direction: "*out",
Tenant: "cgrates.org",
Category: "call",
Account: "dan",
Subject: "dan",
Context: "*rating",
Destination: "444",
Target: "Subject",
Original: "rif",
}, &response)
if err != nil || response != "rif1" {
t.Error("Error getting alias: ", err, response)
}
}
func TestAliasesLoadAlias(t *testing.T) {
var response string
cd := &CallDescriptor{
Direction: "*out",
Tenant: "cgrates.org",
Category: "call",
Account: "rif",
Subject: "rif",
Destination: "444",
ExtraFields: map[string]string{
"Cli": "0723",
"Other": "stuff",
},
}
err := LoadAlias(
&AttrMatchingAlias{
Direction: "*out",
Tenant: "cgrates.org",
Category: "call",
Account: "dan",
Subject: "dan",
Context: "*rating",
Destination: "444",
}, cd, "ExtraFields")
if err != nil || cd == nil {
t.Error("Error getting alias: ", err, response)
}
if cd.Subject != "rif1" ||
cd.ExtraFields["Cli"] != "0724" {
t.Errorf("Aliases failed to change interface: %+v", cd)
}
}
func TestAliasesCache(t *testing.T) {
key := "*out:cgrates.org:call:remo:remo:*rating"
a, err := cache2go.Get(utils.ALIASES_PREFIX + key)
if err != nil || a == nil {
//log.Printf("Test: %+v", cache2go.GetEntriesKeys(utils.REVERSE_ALIASES_PREFIX))
t.Error("Error getting alias from cache: ", err, a)
}
rKey1 := "minuAccount*rating"
ra1, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + rKey1)
if err != nil || len(ra1.(map[interface{}]struct{})) != 2 {
t.Error("Error getting reverse alias 1: ", ra1)
}
rKey2 := "minuSubject*rating"
ra2, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + rKey2)
if err != nil || len(ra2.(map[interface{}]struct{})) != 2 {
t.Error("Error getting reverse alias 2: ", ra2)
}
accountingStorage.RemoveAlias(key)
a, err = cache2go.Get(utils.ALIASES_PREFIX + key)
if err == nil {
t.Error("Error getting alias from cache: ", err)
}
ra1, err = cache2go.Get(utils.REVERSE_ALIASES_PREFIX + rKey1)
if err != nil || len(ra1.(map[interface{}]struct{})) != 1 {
t.Error("Error getting reverse alias 1: ", ra1)
}
ra2, err = cache2go.Get(utils.REVERSE_ALIASES_PREFIX + rKey2)
if err != nil || len(ra2.(map[interface{}]struct{})) != 1 {
t.Error("Error getting reverse alias 2: ", ra2)
}
}