From 4e56cd69de25f39fac4dd2da783779ae76e77016 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Mon, 19 Apr 2021 17:38:24 +0300 Subject: [PATCH] Cover funcs in engine/chargers.go --- engine/z_chargers_test.go | 143 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 engine/z_chargers_test.go diff --git a/engine/z_chargers_test.go b/engine/z_chargers_test.go new file mode 100644 index 000000000..a8c5bfe3c --- /dev/null +++ b/engine/z_chargers_test.go @@ -0,0 +1,143 @@ +/* +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 +*/ + +package engine + +import ( + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" +) + +func TestChargersmatchingChargerProfilesForEventErrPass(t *testing.T) { + Cache.Clear(nil) + defaultCfg := config.NewDefaultCGRConfig() + defaultCfg.ChargerSCfg().IndexedSelects = false + + dbm := &DataDBMock{ + GetChargerProfileDrvF: func(s1, s2 string) (*ChargerProfile, error) { + return &ChargerProfile{ + Tenant: s1, + ID: s2, + RunID: utils.MetaDefault, + FilterIDs: []string{"fltr1"}, + }, nil + }, + GetKeysForPrefixF: func(s string) ([]string, error) { + return []string{s + "cgrates.org:chr1"}, nil + }, + GetFilterDrvF: func(s1, s2 string) (*Filter, error) { + return nil, utils.ErrNotImplemented + }, + } + dmFilter := NewDataManager(dbm, defaultCfg.CacheCfg(), nil) + cS := &ChargerService{ + dm: dmFilter, + filterS: &FilterS{ + dm: dmFilter, + cfg: defaultCfg, + }, + cfg: defaultCfg, + } + cgrEvTm := time.Date(2021, 4, 19, 12, 0, 0, 0, time.UTC) + cgrEv := &utils.CGREvent{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: "cgrEvID", + Event: map[string]interface{}{ + "Charger": "ChargerProfile1", + utils.AnswerTime: time.Date(2021, 4, 19, 12, 0, 0, 0, time.UTC), + "UsageInterval": "10s", + utils.Weight: "10.0", + }, + APIOpts: map[string]interface{}{ + utils.Subsys: utils.MetaChargers, + }, + Time: &cgrEvTm, + } + + experr := utils.ErrNotImplemented + rcv, err := cS.matchingChargerProfilesForEvent(cgrEv.Tenant, cgrEv) + + if err == nil || err != experr { + t.Fatalf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err) + } + + if rcv != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) + } +} + +func TestChargersmatchingChargerProfilesForEventNotActive(t *testing.T) { + Cache.Clear(nil) + defaultCfg := config.NewDefaultCGRConfig() + defaultCfg.ChargerSCfg().IndexedSelects = false + + dbm := &DataDBMock{ + GetChargerProfileDrvF: func(s1, s2 string) (*ChargerProfile, error) { + return &ChargerProfile{ + Tenant: s1, + ID: s2, + RunID: utils.MetaDefault, + FilterIDs: []string{"fltr1"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2021, 4, 19, 17, 0, 0, 0, time.UTC), + }, + }, nil + }, + GetKeysForPrefixF: func(s string) ([]string, error) { + return []string{s + "cgrates.org:chr1"}, nil + }, + GetFilterDrvF: func(s1, s2 string) (*Filter, error) { + return nil, utils.ErrNotImplemented + }, + } + dmFilter := NewDataManager(dbm, defaultCfg.CacheCfg(), nil) + cS := &ChargerService{ + dm: dmFilter, + filterS: &FilterS{ + dm: dmFilter, + cfg: defaultCfg, + }, + cfg: defaultCfg, + } + cgrEvTm := time.Date(2021, 4, 19, 12, 0, 0, 0, time.UTC) + cgrEv := &utils.CGREvent{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: "cgrEvID", + Event: map[string]interface{}{ + "Charger": "ChargerProfile1", + utils.AnswerTime: time.Date(2021, 4, 19, 12, 0, 0, 0, time.UTC), + "UsageInterval": "10s", + utils.Weight: "10.0", + }, + APIOpts: map[string]interface{}{ + utils.Subsys: utils.MetaChargers, + }, + Time: &cgrEvTm, + } + + experr := utils.ErrNotFound + rcv, err := cS.matchingChargerProfilesForEvent(cgrEv.Tenant, cgrEv) + + if err == nil || err != experr { + t.Fatalf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err) + } + + if rcv != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) + } +}