From de77f9b36311d70b3b26d005b99698d342088afd Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Wed, 11 Sep 2024 10:20:34 -0400 Subject: [PATCH] Add coverage tests on engine --- engine/model_helpers_test.go | 116 +++++++++++++++++++++++++++++++ engine/storage_mysql_test.go | 117 ++++++++++++++++++++++++++++++++ engine/storage_postgres_test.go | 71 +++++++++++++++++++ 3 files changed, 304 insertions(+) create mode 100644 engine/storage_mysql_test.go create mode 100644 engine/storage_postgres_test.go diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index f04070417..af6ece71d 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -5944,3 +5944,119 @@ func TestModelHelpersCSVLoadErrorBool(t *testing.T) { t.Errorf("\nExpecting ,\n Received <%+v>", err) } } + +func TestCSVHeaders(t *testing.T) { + expected := []string{ + "#" + utils.Tenant, + utils.ID, + utils.StatIDs, + utils.MetricIDs, + utils.Sorting, + utils.SortingParameters, + utils.ThresholdIDs, + } + var tps RankingsMdls + result := tps.CSVHeader() + if len(result) != len(expected) { + t.Errorf("Expected %d elements, got %d", len(expected), len(result)) + return + } + for i, v := range expected { + if result[i] != v { + t.Errorf("Expected value at index %d to be %s, got %s", i, v, result[i]) + } + } +} + +func TestTrendsMdlCSVHeader(t *testing.T) { + expected := []string{ + "#" + utils.Tenant, + utils.ID, + utils.Schedule, + utils.StatID, + utils.Metrics, + utils.QueueLength, + utils.TTL, + utils.TrendType, + utils.ThresholdIDs, + } + var tps TrendsMdls + result := tps.CSVHeader() + if len(result) != len(expected) { + t.Errorf("Expected %d elements, got %d", len(expected), len(result)) + return + } + for i, v := range expected { + if result[i] != v { + t.Errorf("Expected value at index %d to be %s, got %s", i, v, result[i]) + } + } +} + +func TestRankingProfileToAPI(t *testing.T) { + sg := &RankingProfile{ + Tenant: "cgrates.org", + ID: "1001", + StatIDs: []string{"stat1", "stat2"}, + MetricIDs: []string{"metric1", "metric2"}, + SortingParameters: []string{"sort1", "sort2"}, + ThresholdIDs: []string{"threshold1", "threshold2"}, + QueryInterval: 30 * time.Minute, + } + + expected := &utils.TPRankingProfile{ + Tenant: "cgrates.org", + ID: "1001", + StatIDs: []string{"stat1", "stat2"}, + MetricIDs: []string{"metric1", "metric2"}, + SortingParameters: []string{"sort1", "sort2"}, + ThresholdIDs: []string{"threshold1", "threshold2"}, + QueryInterval: "30m0s", + } + result := RankingProfileToAPI(sg) + if result.Tenant != expected.Tenant { + t.Errorf("Expected Tenant %s, got %s", expected.Tenant, result.Tenant) + } + if result.ID != expected.ID { + t.Errorf("Expected ID %s, got %s", expected.ID, result.ID) + } + if len(result.StatIDs) != len(expected.StatIDs) { + t.Errorf("Expected %d StatIDs, got %d", len(expected.StatIDs), len(result.StatIDs)) + } else { + for i, v := range expected.StatIDs { + if result.StatIDs[i] != v { + t.Errorf("Expected StatID at index %d to be %s, got %s", i, v, result.StatIDs[i]) + } + } + } + if len(result.MetricIDs) != len(expected.MetricIDs) { + t.Errorf("Expected %d MetricIDs, got %d", len(expected.MetricIDs), len(result.MetricIDs)) + } else { + for i, v := range expected.MetricIDs { + if result.MetricIDs[i] != v { + t.Errorf("Expected MetricID at index %d to be %s, got %s", i, v, result.MetricIDs[i]) + } + } + } + if len(result.SortingParameters) != len(expected.SortingParameters) { + t.Errorf("Expected %d SortingParameters, got %d", len(expected.SortingParameters), len(result.SortingParameters)) + } else { + for i, v := range expected.SortingParameters { + if result.SortingParameters[i] != v { + t.Errorf("Expected SortingParameter at index %d to be %s, got %s", i, v, result.SortingParameters[i]) + } + } + } + if len(result.ThresholdIDs) != len(expected.ThresholdIDs) { + t.Errorf("Expected %d ThresholdIDs, got %d", len(expected.ThresholdIDs), len(result.ThresholdIDs)) + } else { + for i, v := range expected.ThresholdIDs { + if result.ThresholdIDs[i] != v { + t.Errorf("Expected ThresholdID at index %d to be %s, got %s", i, v, result.ThresholdIDs[i]) + } + } + } + if result.QueryInterval != expected.QueryInterval { + t.Errorf("Expected QueryInterval %s, got %s", expected.QueryInterval, result.QueryInterval) + } +} diff --git a/engine/storage_mysql_test.go b/engine/storage_mysql_test.go new file mode 100644 index 000000000..92fb61229 --- /dev/null +++ b/engine/storage_mysql_test.go @@ -0,0 +1,117 @@ +/* +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 ( + "fmt" + "testing" + + "github.com/cgrates/cgrates/utils" +) + +func TestGetStorageTypes(t *testing.T) { + msqlStorage := &MySQLStorage{} + result := msqlStorage.GetStorageType() + expected := utils.MetaMySQL + if result != expected { + t.Errorf("GetStorageType() = %s; want %s", result, expected) + } +} + +func TestNotExtraFieldsValueQry(t *testing.T) { + msqlStorage := &MySQLStorage{} + field := "Tenant" + value := "cgrates.org" + result := msqlStorage.notExtraFieldsValueQry(field, value) + expected := fmt.Sprintf(" extra_fields NOT LIKE '%%\"%s\":\"%s\"%%'", field, value) + if result != expected { + t.Errorf("notExtraFieldsValueQry() = %s; want %s", result, expected) + } + field = "fieldWith\"SpecialChars" + value = "valueWith'SpecialChars" + result = msqlStorage.notExtraFieldsValueQry(field, value) + expected = fmt.Sprintf(" extra_fields NOT LIKE '%%\"%s\":\"%s\"%%'", field, value) + if result != expected { + t.Errorf("notExtraFieldsValueQry() with special chars = %s; want %s", result, expected) + } +} + +func TestNotExtraFieldsExistsQry(t *testing.T) { + msqlStorage := &MySQLStorage{} + field := "Tenant" + result := msqlStorage.notExtraFieldsExistsQry(field) + expected := fmt.Sprintf(" extra_fields NOT LIKE '%%\"%s\":%%'", field) + if result != expected { + t.Errorf("notExtraFieldsExistsQry() = %s; want %s", result, expected) + } + field = "fieldWith\"SpecialChars" + result = msqlStorage.notExtraFieldsExistsQry(field) + expected = fmt.Sprintf(" extra_fields NOT LIKE '%%\"%s\":%%'", field) + if result != expected { + t.Errorf("notExtraFieldsExistsQry() with special chars = %s; want %s", result, expected) + } +} + +func TestExtraFieldsValueQry(t *testing.T) { + msqlStorage := &MySQLStorage{} + field := "Tenant" + value := "cgrates.org" + result := msqlStorage.extraFieldsValueQry(field, value) + expected := fmt.Sprintf(" extra_fields LIKE '%%\"%s\":\"%s\"%%'", field, value) + if result != expected { + t.Errorf("extraFieldsValueQry() = %s; want %s", result, expected) + } + field = "fieldWith\"SpecialChars" + value = "valueWith'SpecialChars" + result = msqlStorage.extraFieldsValueQry(field, value) + expected = fmt.Sprintf(" extra_fields LIKE '%%\"%s\":\"%s\"%%'", field, value) + if result != expected { + t.Errorf("extraFieldsValueQry() with special chars = %s; want %s", result, expected) + } +} + +func TestExtraFieldsExistsQry(t *testing.T) { + msqlStorage := &MySQLStorage{} + field := "Tenant" + result := msqlStorage.extraFieldsExistsQry(field) + expected := fmt.Sprintf(" extra_fields LIKE '%%\"%s\":%%'", field) + if result != expected { + t.Errorf("extraFieldsExistsQry() = %s; want %s", result, expected) + } + field = "fieldWith\"SpecialChars" + result = msqlStorage.extraFieldsExistsQry(field) + expected = fmt.Sprintf(" extra_fields LIKE '%%\"%s\":%%'", field) + if result != expected { + t.Errorf("extraFieldsExistsQry() with special chars = %s; want %s", result, expected) + } +} +func TestAppendToMysqlDSNOptsBasic(t *testing.T) { + opts := map[string]string{ + "user": "root", + } + result := AppendToMysqlDSNOpts(opts) + expected := "&user=root" + if result != expected { + t.Errorf("AppendToMysqlDSNOpts() = %s; want %s", result, expected) + } + result = AppendToMysqlDSNOpts(nil) + if result != utils.EmptyString { + t.Errorf("AppendToMysqlDSNOpts(nil) = %s; want %s", result, utils.EmptyString) + } +} diff --git a/engine/storage_postgres_test.go b/engine/storage_postgres_test.go new file mode 100644 index 000000000..6a2945cac --- /dev/null +++ b/engine/storage_postgres_test.go @@ -0,0 +1,71 @@ +/* +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 ( + "fmt" + "testing" + + "github.com/cgrates/cgrates/utils" +) + +func TestPostgressGetStorageType(t *testing.T) { + poS := &PostgresStorage{} + storageType := poS.GetStorageType() + if storageType != utils.MetaPostgres { + t.Errorf("expected %s, got %s", utils.MetaPostgres, storageType) + } +} + +func TestExtraFieldsQueries(t *testing.T) { + poS := &PostgresStorage{} + field := "Subject" + value := "1001" + expectedExistsQuery := fmt.Sprintf(" extra_fields ?'%s'", field) + expectedValueQuery := fmt.Sprintf(" (extra_fields ->> '%s') = '%s'", field, value) + existsQuery := poS.extraFieldsExistsQry(field) + valueQuery := poS.extraFieldsValueQry(field, value) + if existsQuery != expectedExistsQuery { + t.Errorf("extraFieldsExistsQry: expected query to be %s, but got %s", expectedExistsQuery, existsQuery) + } + if valueQuery != expectedValueQuery { + t.Errorf("extraFieldsValueQry: expected query to be %s, but got %s", expectedValueQuery, valueQuery) + } +} + +func TestPostgresNotExtraFieldsValueQry(t *testing.T) { + poS := &PostgresStorage{} + field := "Tor" + value := "voice" + expectedQuery := fmt.Sprintf(" NOT (extra_fields ?'%s' AND (extra_fields ->> '%s') = '%s')", field, field, value) + query := poS.notExtraFieldsValueQry(field, value) + if query != expectedQuery { + t.Errorf("expected query to be %s, but got %s", expectedQuery, query) + } +} + +func TestPostgresNotExtraFieldsExistsQry(t *testing.T) { + poS := &PostgresStorage{} + field := "tor" + expectedQuery := fmt.Sprintf(" NOT extra_fields ?'%s'", field) + query := poS.notExtraFieldsExistsQry(field) + if query != expectedQuery { + t.Errorf("expected query to be %s, but got %s", expectedQuery, query) + } +}