mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
114 lines
3.7 KiB
Go
114 lines
3.7 KiB
Go
/*
|
|
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
|
Copyright (C) ITsysCOM GmbH
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
*/
|
|
package engine
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
var (
|
|
matchEV map[string]interface{}
|
|
dmMatch *DataManager
|
|
stringIndexedFields *[]string
|
|
prefixIndexedFields *[]string
|
|
)
|
|
|
|
func TestFilterMatchingItemIDsForEvent(t *testing.T) {
|
|
var stringFilter, prefixFilter, defaultFilter []*FilterRule
|
|
stringFilterID := "stringFilterID"
|
|
prefixFilterID := "prefixFilterID"
|
|
defaultFilterID := "defaultFilterID"
|
|
data, _ := NewMapStorage()
|
|
dmMatch = NewDataManager(data)
|
|
context := utils.MetaRating
|
|
x, err := NewFilterRule(MetaString, "Field", []string{"profile"})
|
|
if err != nil {
|
|
t.Errorf("Error: %+v", err)
|
|
}
|
|
stringFilter = append(stringFilter, x)
|
|
attribStringF := &Filter{Tenant: config.CgrConfig().DefaultTenant,
|
|
ID: "stringFilter", Rules: stringFilter}
|
|
dmMatch.SetFilter(attribStringF)
|
|
x, err = NewFilterRule(MetaPrefix, "Field", []string{"profilePrefix"})
|
|
if err != nil {
|
|
t.Errorf("Error: %+v", err)
|
|
}
|
|
prefixFilter = append(prefixFilter, x)
|
|
attribPrefF := &Filter{Tenant: config.CgrConfig().DefaultTenant,
|
|
ID: "prefFilter", Rules: prefixFilter}
|
|
dmMatch.SetFilter(attribPrefF)
|
|
x, err = NewFilterRule(MetaGreaterOrEqual, "Weight", []string{"200.00"})
|
|
if err != nil {
|
|
t.Errorf("Error: %+v", err)
|
|
}
|
|
defaultFilter = append(defaultFilter, x)
|
|
attribDefaultF := &Filter{Tenant: config.CgrConfig().DefaultTenant,
|
|
ID: "defaultFilter", Rules: defaultFilter}
|
|
dmMatch.SetFilter(attribDefaultF)
|
|
prefix := utils.ConcatenatedKey(config.CgrConfig().DefaultTenant, context)
|
|
atrRFI := NewFilterIndexer(dmMatch, utils.AttributeProfilePrefix, prefix)
|
|
atrRFI.IndexTPFilter(FilterToTPFilter(attribStringF), stringFilterID)
|
|
atrRFI.IndexTPFilter(FilterToTPFilter(attribPrefF), prefixFilterID)
|
|
atrRFI.IndexTPFilter(FilterToTPFilter(attribDefaultF), defaultFilterID)
|
|
err = atrRFI.StoreIndexes(true, utils.NonTransactional)
|
|
if err != nil {
|
|
t.Errorf("Error: %+v", err)
|
|
}
|
|
matchEV = map[string]interface{}{
|
|
utils.AnswerTime: time.Date(2014, 7, 14, 14, 30, 0, 0, time.UTC),
|
|
"Field": "profile",
|
|
}
|
|
aPrflIDs, err := matchingItemIDsForEvent(matchEV, nil, nil,
|
|
dmMatch, utils.CacheAttributeFilterIndexes, prefix)
|
|
if err != nil {
|
|
t.Errorf("Error: %+v", err)
|
|
}
|
|
_, has := aPrflIDs[stringFilterID]
|
|
if !has {
|
|
t.Errorf("Expecting: %+v, received: %+v", stringFilterID, aPrflIDs)
|
|
}
|
|
matchEV = map[string]interface{}{
|
|
"Field": "profilePrefix",
|
|
}
|
|
aPrflIDs, err = matchingItemIDsForEvent(matchEV, nil, nil,
|
|
dmMatch, utils.CacheAttributeFilterIndexes, prefix)
|
|
if err != nil {
|
|
t.Errorf("Error: %+v", err)
|
|
}
|
|
_, has = aPrflIDs[prefixFilterID]
|
|
if !has {
|
|
t.Errorf("Expecting: %+v, received: %+v", prefixFilterID, aPrflIDs)
|
|
}
|
|
matchEV = map[string]interface{}{
|
|
"Weight": "200",
|
|
}
|
|
aPrflIDs, err = matchingItemIDsForEvent(matchEV, nil, nil,
|
|
dmMatch, utils.CacheAttributeFilterIndexes, prefix)
|
|
if err != nil {
|
|
t.Errorf("Error: %+v", err)
|
|
}
|
|
_, has = aPrflIDs[defaultFilterID]
|
|
if !has {
|
|
t.Errorf("Expecting: %+v, received: %+v", defaultFilterID, aPrflIDs)
|
|
}
|
|
}
|