From 49f0fa439c5f7fbce450575c6dde892bbdcb2b30 Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Wed, 13 Sep 2023 17:07:18 +0200 Subject: [PATCH] Add coverage tests for engine --- engine/ratingplan_test.go | 53 ++++++++++++ engine/suretax_test.go | 115 ++++++++++++++++++++++++++ engine/tpexporter_test.go | 169 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 337 insertions(+) create mode 100644 engine/tpexporter_test.go diff --git a/engine/ratingplan_test.go b/engine/ratingplan_test.go index 37d8971ab..c9ef5e8b2 100644 --- a/engine/ratingplan_test.go +++ b/engine/ratingplan_test.go @@ -537,3 +537,56 @@ func TestRatingPlanEqual(t *testing.T) { t.Error(rcv) } } + +func TestRAtingPlangetFirstUnsaneRating(t *testing.T) { + str := "test" + rt := &RITiming{ + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + StartTime: "00:00:00", + EndTime: "00:00:00", + cronString: str, + tag: str, + } + + rr := &RIRate{ + ConnectFee: fl, + RoundingMethod: str, + RoundingDecimals: nm, + MaxCost: fl, + MaxCostStrategy: str, + Rates: RateGroups{{ + GroupIntervalStart: 0 * time.Second, + Value: fl, + RateIncrement: 0 * time.Second, + RateUnit: 0 * time.Second, + }, { + GroupIntervalStart: 1 * time.Second, + Value: fl, + RateIncrement: 1 * time.Second, + RateUnit: 1 * time.Second, + }}, + tag: "TestTag", + } + + rl := RPRateList{{ + Timing: str, + Rating: str, + Weight: fl, + }} + + rp := &RatingPlan{ + Id: "test2", + Timings: map[string]*RITiming{str: rt}, + Ratings: map[string]*RIRate{str: rr}, + DestinationRates: map[string]RPRateList{str: rl}, + } + + rcv := rp.getFirstUnsaneRating() + + if rcv != "TestTag" { + t.Error(rcv) + } +} diff --git a/engine/suretax_test.go b/engine/suretax_test.go index 51ed72ba3..2c1e850e2 100644 --- a/engine/suretax_test.go +++ b/engine/suretax_test.go @@ -99,3 +99,118 @@ func TestSureTaxNewSureTaxRequest(t *testing.T) { t.Error(rcv) } } + +func TestSureTaxSureTaxProcessCdr(t *testing.T) { + str := "test" + var nm int64 = 1 + fl := 1.2 + bl := false + td := 1 * time.Second + tm := time.Date(2023, 8, 17, 20, 34, 58, 651387237, time.UTC) + tm2 := time.Date(2023, 8, 17, 20, 34, 59, 651387237, time.UTC) + nm2 := 1 + ct := &ChargedTiming{ + Years: utils.Years{1, 2}, + Months: utils.Months{2, 3}, + MonthDays: utils.MonthDays{4, 5}, + WeekDays: utils.WeekDays{2, 3}, + StartTime: "Time", + } + r := &Rate{ + GroupIntervalStart: td, + Value: fl, + RateIncrement: td, + RateUnit: td, + } + rg := RateGroups{r} + rm := RatingMatchedFilters{str: nm2} + bc := &BalanceCharge{ + AccountID: str, + BalanceUUID: str, + RatingID: str, + Units: fl, + ExtraChargeID: str, + } + ru := &RatingUnit{ + ConnectFee: fl, + RoundingMethod: str, + RoundingDecimals: nm2, + MaxCost: fl, + MaxCostStrategy: str, + TimingID: str, + RatesID: str, + RatingFiltersID: str, + } + cdr := &CDR{ + CGRID: str, + RunID: str, + OrderID: nm, + OriginHost: str, + Source: str, + OriginID: str, + ToR: str, + RequestType: str, + Tenant: str, + Category: str, + Account: str, + Subject: str, + Destination: str, + SetupTime: tm, + AnswerTime: tm2, + Usage: td, + ExtraFields: map[string]string{str: str}, + ExtraInfo: str, + Partial: bl, + PreRated: bl, + CostSource: str, + Cost: fl, + CostDetails: &EventCost{ + CGRID: str, + RunID: str, + StartTime: tm, + Usage: &td, + Cost: &fl, + Charges: []*ChargingInterval{{ + RatingID: str, + Increments: []*ChargingIncrement{{ + Usage: td, + Cost: fl, + AccountingID: str, + CompressFactor: nm2, + }}, + CompressFactor: nm2, + usage: &td, + ecUsageIdx: &td, + cost: &fl, + }}, + AccountSummary: &AccountSummary{ + Tenant: str, + ID: str, + BalanceSummaries: BalanceSummaries{{ + UUID: str, + ID: str, + Type: str, + Value: fl, + Disabled: bl, + }}, + AllowNegative: bl, + Disabled: bl, + }, + Rating: Rating{str: ru}, + Accounting: Accounting{str: bc}, + RatingFilters: RatingFilters{str: rm}, + Rates: ChargedRates{str: rg}, + Timings: ChargedTimings{str: ct}, + }, + } + + err := SureTaxProcessCdr(cdr) + + if err != nil { + if err.Error() != `Post "": unsupported protocol scheme ""` { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } +} diff --git a/engine/tpexporter_test.go b/engine/tpexporter_test.go new file mode 100644 index 000000000..b1c334e78 --- /dev/null +++ b/engine/tpexporter_test.go @@ -0,0 +1,169 @@ +/* +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 ( + "archive/zip" + "bytes" + "reflect" + "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" +) + +func TestTpExporterNewTPExporter(t *testing.T) { + str := "test" + bl := false + sSCfg, _ := config.NewDefaultCGRConfig() + mpStr := NewInternalDB(nil, nil, true, sSCfg.DataDbCfg().Items) + rcv, err := NewTPExporter(mpStr, str, str, str, str, false) + + if err != nil { + if err.Error() != "Unsupported file format" { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } + + if rcv != nil { + t.Error(rcv) + } + + rcv, err = NewTPExporter(mpStr, "", str, utils.CSV, str, false) + + if err != nil { + if err.Error() != "Missing TPid" { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } + + if rcv != nil { + t.Error(rcv) + } + + rcv, err = NewTPExporter(mpStr, str, str, utils.CSV, str, bl) + if err != nil { + t.Error(err) + } + + exp := &TPExporter{ + storDb: mpStr, + tpID: str, + exportPath: str, + fileFormat: utils.CSV, + sep: 't', + compress: bl, + cacheBuff: new(bytes.Buffer), + } + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("\nexpected %v\nreceived %v\n", exp, rcv) + } + + rcv, err = NewTPExporter(mpStr, str, str, utils.CSV, "", bl) + + if err != nil { + if err.Error() != "Invalid field separator: " { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } + + if rcv != nil { + t.Error(rcv) + } + + rcv, err = NewTPExporter(mpStr, str, str, utils.CSV, str, true) + + if err != nil { + if err.Error() != "open test/tpexport.zip: no such file or directory" { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } + + if rcv != nil { + t.Error(rcv) + } + + rcv, err = NewTPExporter(mpStr, str, "", utils.CSV, str, true) + if err != nil { + t.Error(err) + } + + exp2 := &TPExporter{ + storDb: mpStr, + tpID: str, + exportPath: "", + fileFormat: utils.CSV, + sep: 't', + compress: true, + cacheBuff: exp.cacheBuff, + zipWritter: zip.NewWriter(exp.cacheBuff), + } + + if !reflect.DeepEqual(rcv, exp2) { + t.Errorf("\nexpected %v\nreceived %v\n", exp2, rcv) + } +} + +func TestTpExporterExportStats(t *testing.T) { + str := "test" + bl := false + sSCfg, _ := config.NewDefaultCGRConfig() + mpStr := NewInternalDB(nil, nil, true, sSCfg.DataDbCfg().Items) + self, err := NewTPExporter(mpStr, str, str, utils.CSV, str, bl) + if err != nil { + t.Error(err) + } + + rcv := self.ExportStats() + exp := &utils.ExportedTPStats{ + ExportPath: str, + Compressed: bl, + } + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("\nexpected %v\nreceived %v\n", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +} + +func TestTpExporterGetCacheBuffer(t *testing.T) { + str := "test" + bl := false + sSCfg, _ := config.NewDefaultCGRConfig() + mpStr := NewInternalDB(nil, nil, true, sSCfg.DataDbCfg().Items) + self, err := NewTPExporter(mpStr, str, str, utils.CSV, str, bl) + if err != nil { + t.Error(err) + } + + rcv := self.GetCacheBuffer() + exp := new(bytes.Buffer) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("\nexpected %v\nreceived %v\n", utils.ToJSON(exp), utils.ToJSON(rcv)) + } +}