Add coverage tests for models.go and model_helpers.go

This commit is contained in:
NikolasPetriti
2023-08-03 16:59:57 +02:00
committed by Dan Christian Bogos
parent 9079248d4e
commit 4c544f427c
2 changed files with 386 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package engine
import (
"errors"
"reflect"
"sort"
"strings"
@@ -2708,3 +2709,237 @@ func TestModelHelpersAPItoModelTPAttribute(t *testing.T) {
t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv))
}
}
func TestModelHelpersmodelEqual(t *testing.T) {
type Test struct {
Fl float64
Nm int
Bl bool
Str string
}
tst := Test{
Fl: fl,
Nm: nm,
Bl: bl,
Str: str,
}
type args struct {
this any
other any
}
tests := []struct {
name string
args args
exp bool
}{
{
name: "true return",
args: args{tst, tst},
exp: true,
},
{
name: "false return",
args: args{nm, str},
exp: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rcv := modelEqual(tt.args.this, tt.args.other)
if rcv != tt.exp {
t.Errorf("received %v, expected %v", rcv, tt.exp)
}
})
}
}
func TestModelHelpersparamsToString(t *testing.T) {
rcv := paramsToString([]any{str, str})
if rcv != "test;test" {
t.Error(rcv)
}
}
func TestModelHelpersAPItoModelTPDispatcherProfile(t *testing.T) {
tpDPP := &utils.TPDispatcherProfile{
TPid: str,
Tenant: str,
ID: str,
Subsystems: slc,
FilterIDs: slc,
ActivationInterval: &utils.TPActivationInterval{
ActivationTime: str,
ExpiryTime: str,
},
Strategy: str,
StrategyParams: []any{str, str},
Weight: fl,
Hosts: []*utils.TPDispatcherHostProfile{},
}
rcv := APItoModelTPDispatcherProfile(tpDPP)
exp := TPDispatcherProfiles{{
Tpid: str,
Tenant: str,
ID: str,
Subsystems: str,
FilterIDs: str,
ActivationInterval: "test;test",
Strategy: str,
StrategyParameters: "test;test",
ConnID: "",
ConnFilterIDs: "",
ConnWeight: 0,
ConnBlocker: false,
ConnParameters: "",
Weight: fl,
}}
if !reflect.DeepEqual(rcv, exp) {
t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv))
}
rcv = APItoModelTPDispatcherProfile(nil)
if rcv != nil {
t.Error(rcv)
}
}
func TestModelHelpersAsTPDispatcherHosts(t *testing.T) {
tps := TPDispatcherHosts{{
Tpid: str,
Tenant: str,
ID: str,
Address: utils.EmptyString,
Transport: str,
TLS: bl,
}, {
Tpid: str,
Tenant: str,
ID: str,
Address: str,
Transport: utils.EmptyString,
TLS: bl,
}}
rcv := tps.AsTPDispatcherHosts()
exp := []*utils.TPDispatcherHost{{
TPid: str,
Tenant: str,
ID: str,
Conns: []*utils.TPDispatcherHostConn{{
Address: str,
Transport: "*json",
TLS: true,
}},
}}
if !reflect.DeepEqual(rcv, exp) {
t.Errorf("expected %v, received %v", utils.ToJSON(exp), utils.ToJSON(rcv))
}
}
func TestModelHelpersAPItoResource(t *testing.T) {
errStr := "test`"
tpRL := &utils.TPResourceProfile{
TPid: str,
Tenant: str,
ID: str,
FilterIDs: slc,
ActivationInterval: &utils.TPActivationInterval{
ActivationTime: str,
ExpiryTime: str,
},
UsageTTL: errStr,
Limit: str,
AllocationMessage: str,
Blocker: bl,
Stored: bl,
Weight: fl,
ThresholdIDs: slc,
}
_, err := APItoResource(tpRL, "")
if err != nil {
if err.Error() != errors.New("time: invalid duration "+`"`+errStr+`"`).Error() {
t.Error(err)
}
}
tpRL.UsageTTL = utils.EmptyString
_, err = APItoResource(tpRL, "test")
if err != nil {
if err.Error() != "unknown time zone test" {
t.Error(err)
}
}
tpRL.ActivationInterval = nil
_, err = APItoResource(tpRL, "")
if err != nil {
if err.Error() != `strconv.ParseFloat: parsing "test": invalid syntax` {
t.Error(err)
}
}
}
func TestModelHelpersAPItoStats(t *testing.T) {
errStr := "test`"
tpST := &utils.TPStatProfile{
TTL: errStr,
}
_, err := APItoStats(tpST, "")
if err != nil {
if err.Error() != errors.New("time: invalid duration "+`"`+errStr+`"`).Error() {
t.Error(err)
}
}
tpST.TTL = utils.EmptyString
_, err = APItoStats(tpST, "test")
if err != nil {
if err.Error() != "unknown time zone test" {
t.Error(err)
}
}
}
func TestModelHelpersAPItoThresholdProfile(t *testing.T) {
errStr := "test`"
tpST := &utils.TPThresholdProfile{
MinSleep: errStr,
ActivationInterval: &utils.TPActivationInterval{
ActivationTime: str,
ExpiryTime: str,
},
}
_, err := APItoThresholdProfile(tpST, "")
if err != nil {
if err.Error() != errors.New("time: invalid duration "+`"`+errStr+`"`).Error() {
t.Error(err)
}
}
tpST.MinSleep = ""
_, err = APItoThresholdProfile(tpST, "test")
if err != nil {
if err.Error() != "unknown time zone test" {
t.Error(err)
}
}
}

151
engine/models_test.go Normal file
View File

@@ -0,0 +1,151 @@
/*
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 (
"reflect"
"testing"
"github.com/cgrates/cgrates/utils"
)
func TestModelsSetAccountActionId(t *testing.T) {
aa := &TpAccountAction{}
err := aa.SetAccountActionId(str)
if err != nil {
if err.Error() != "Wrong TP Account Action Id: test" {
t.Error(err)
}
}
err = aa.SetAccountActionId("test:test:test")
if err != nil {
t.Error(err)
}
if aa.Loadid != str && aa.Tenant != str && aa.Account != str {
t.Error("didn't set account action id")
}
}
func TestModelsGetAccountActionId(t *testing.T) {
aa := &TpAccountAction{}
err = aa.SetAccountActionId("test:test:test")
if err != nil {
t.Error(err)
}
rcv := aa.GetAccountActionId()
exp := "test:test"
if rcv != exp {
t.Errorf("received %s, expected %s", rcv, exp)
}
}
func TestModelsTableName(t *testing.T) {
tn := CDRsql{}
rcv := tn.TableName()
if rcv != utils.CDRsTBL {
t.Error(rcv)
}
}
func TestModelsAsMapStringInterface(t *testing.T) {
tn := CDRsql{
ID: 1,
Cgrid: str,
RunID: str,
OriginHost: str,
Source: str,
OriginID: str,
TOR: str,
RequestType: str,
Tenant: str,
Category: str,
Account: str,
Subject: str,
Destination: str,
SetupTime: tm,
AnswerTime: tm,
Usage: 1,
ExtraFields: str,
CostSource: str,
Cost: fl,
CostDetails: str,
ExtraInfo: str,
CreatedAt: tm,
UpdatedAt: tm,
DeletedAt: &tm,
}
rcv := tn.AsMapStringInterface()
exp := make(map[string]any)
exp["cgrid"] = tn.Cgrid
exp["run_id"] = tn.RunID
exp["origin_host"] = tn.OriginHost
exp["source"] = tn.Source
exp["origin_id"] = tn.OriginID
exp["tor"] = tn.TOR
exp["request_type"] = tn.RequestType
exp["tenant"] = tn.Tenant
exp["category"] = tn.Category
exp["account"] = tn.Account
exp["subject"] = tn.Subject
exp["destination"] = tn.Destination
exp["setup_time"] = tn.SetupTime
exp["answer_time"] = tn.AnswerTime
exp["usage"] = tn.Usage
exp["extra_fields"] = tn.ExtraFields
exp["cost_source"] = tn.CostSource
exp["cost"] = tn.Cost
exp["cost_details"] = tn.CostDetails
exp["extra_info"] = tn.ExtraInfo
exp["created_at"] = tn.CreatedAt
exp["updated_at"] = tn.UpdatedAt
if !reflect.DeepEqual(rcv, exp) {
t.Errorf("received %s, expected %s", rcv, exp)
}
}
func TestModelsSessionCostsSQLTableName(t *testing.T) {
tn := SessionCostsSQL{}
rcv := tn.TableName()
if rcv != utils.SessionCostsTBL {
t.Error(rcv)
}
}
func TestModelsTBLVersionTableName(t *testing.T) {
tn := TBLVersion{}
rcv := tn.TableName()
if rcv != utils.TBLVersions {
t.Error(rcv)
}
}