From 29524f9be4e503d94259ab114f1bff6e70319167 Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Wed, 31 Jul 2024 11:42:17 -0400 Subject: [PATCH] Add new unit tests on migrator --- migrator/accounts_test.go | 32 ++++++++++++ migrator/derived_chargers_test.go | 19 +++++++ migrator/session_costs_test.go | 86 +++++++++++++++++++++++++++++++ migrator/thresholds_test.go | 58 +++++++++++++++++++++ 4 files changed, 195 insertions(+) diff --git a/migrator/accounts_test.go b/migrator/accounts_test.go index a41d7d659..70ffff4a0 100644 --- a/migrator/accounts_test.go +++ b/migrator/accounts_test.go @@ -121,3 +121,35 @@ func TestV1AccountAsAccount(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", testAccount.BalanceMap["*data"][0], newAcc.BalanceMap["*data"][0]) } } + +func TestV2toV3Account(t *testing.T) { + v2Acc := v2Account{ + ID: "test-account", + AllowNegative: true, + Disabled: false, + } + result := v2Acc.V2toV3Account() + if result.ID != v2Acc.ID { + t.Errorf("expected ID %v, got %v", v2Acc.ID, result.ID) + } + if result.AllowNegative != v2Acc.AllowNegative { + t.Errorf("expected AllowNegative %v, got %v", v2Acc.AllowNegative, result.AllowNegative) + } + if result.Disabled != v2Acc.Disabled { + t.Errorf("expected Disabled %v, got %v", v2Acc.Disabled, result.Disabled) + } + if len(result.BalanceMap) != len(v2Acc.BalanceMap) { + t.Errorf("expected %d balances, got %d", len(v2Acc.BalanceMap), len(result.BalanceMap)) + } + if resultBalance, ok := result.BalanceMap["balance1"]; ok { + if len(resultBalance) != 1 { + t.Errorf("expected 1 balance in balance1, got %d", len(resultBalance)) + } + if resultBalance[0].ID != v2Acc.BalanceMap["balance1"][0].ID { + t.Errorf("expected Balance ID %v, got %v", v2Acc.BalanceMap["balance1"][0].ID, resultBalance[0].ID) + } + if resultBalance[0].Value != v2Acc.BalanceMap["balance1"][0].Value { + t.Errorf("expected Balance Value %v, got %v", v2Acc.BalanceMap["balance1"][0].Value, resultBalance[0].Value) + } + } +} diff --git a/migrator/derived_chargers_test.go b/migrator/derived_chargers_test.go index 931bae270..34d5de7ff 100644 --- a/migrator/derived_chargers_test.go +++ b/migrator/derived_chargers_test.go @@ -256,3 +256,22 @@ func TestDerivedChargers2Charger(t *testing.T) { } } } +func TestMigrateFilterV3(t *testing.T) { + v1flt := &v1Filter{ + Tenant: "cgrates.org", + ID: "test-id", + Rules: []*v1FilterRule{{Type: "type1", FieldName: "field1", Values: []string{"value1"}}}, + ActivationInterval: nil, + } + result := migrateFilterV3(v1flt) + + if result.Tenant != v1flt.Tenant { + t.Errorf("expected Tenant %v, got %v", v1flt.Tenant, result.Tenant) + } + if result.ID != v1flt.ID { + t.Errorf("expected ID %v, got %v", v1flt.ID, result.ID) + } + if len(result.Rules) != len(v1flt.Rules) { + t.Errorf("expected %d rules, got %d", len(v1flt.Rules), len(result.Rules)) + } +} diff --git a/migrator/session_costs_test.go b/migrator/session_costs_test.go index 57c4d4882..f1cde0be7 100644 --- a/migrator/session_costs_test.go +++ b/migrator/session_costs_test.go @@ -19,6 +19,7 @@ along with this program. If not, see package migrator import ( + "encoding/json" "reflect" "testing" "time" @@ -93,3 +94,88 @@ func TestV2toV3Cost(t *testing.T) { t.Errorf("Expected: %s ,received: %s", utils.ToJSON(sv3), utils.ToJSON(rply)) } } + +func TestAsSessionsCostSql(t *testing.T) { + + costDetails := &engine.CallCost{Cost: 100.0} + v2Cost := &v2SessionsCost{ + CGRID: "test-cgrid", + RunID: "test-runid", + OriginHost: "test-originhost", + OriginID: "test-originid", + CostSource: "test-costsource", + CostDetails: costDetails, + Usage: 5 * time.Second, + } + + result := v2Cost.AsSessionsCostSql() + + if result.Cgrid != v2Cost.CGRID { + t.Errorf("expected Cgrid %v, got %v", v2Cost.CGRID, result.Cgrid) + } + if result.RunID != v2Cost.RunID { + t.Errorf("expected RunID %v, got %v", v2Cost.RunID, result.RunID) + } + if result.OriginHost != v2Cost.OriginHost { + t.Errorf("expected OriginHost %v, got %v", v2Cost.OriginHost, result.OriginHost) + } + if result.OriginID != v2Cost.OriginID { + t.Errorf("expected OriginID %v, got %v", v2Cost.OriginID, result.OriginID) + } + if result.CostSource != v2Cost.CostSource { + t.Errorf("expected CostSource %v, got %v", v2Cost.CostSource, result.CostSource) + } + expectedCostDetails := utils.ToJSON(v2Cost.CostDetails) + if result.CostDetails != expectedCostDetails { + t.Errorf("expected CostDetails %v, got %v", expectedCostDetails, result.CostDetails) + } + if result.Usage != v2Cost.Usage.Nanoseconds() { + t.Errorf("expected Usage %v, got %v", v2Cost.Usage.Nanoseconds(), result.Usage) + } + +} + +func TestNewV2SessionsCostFromSessionsCostSql(t *testing.T) { + + costDetails := &engine.CallCost{Cost: 100.0} + costDetailsJson, _ := json.Marshal(costDetails) + smSql := &engine.SessionCostsSQL{ + Cgrid: "test-cgrid", + RunID: "test-runid", + OriginHost: "test-originhost", + OriginID: "test-originid", + CostSource: "test-costsource", + CostDetails: string(costDetailsJson), + Usage: int64(5 * time.Second), + CreatedAt: time.Now(), + } + + result, err := NewV2SessionsCostFromSessionsCostSql(smSql) + + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if result.CGRID != smSql.Cgrid { + t.Errorf("expected CGRID %v, got %v", smSql.Cgrid, result.CGRID) + } + if result.RunID != smSql.RunID { + t.Errorf("expected RunID %v, got %v", smSql.RunID, result.RunID) + } + if result.OriginHost != smSql.OriginHost { + t.Errorf("expected OriginHost %v, got %v", smSql.OriginHost, result.OriginHost) + } + if result.OriginID != smSql.OriginID { + t.Errorf("expected OriginID %v, got %v", smSql.OriginID, result.OriginID) + } + if result.CostSource != smSql.CostSource { + t.Errorf("expected CostSource %v, got %v", smSql.CostSource, result.CostSource) + } + if result.Usage != time.Duration(smSql.Usage) { + t.Errorf("expected Usage %v, got %v", time.Duration(smSql.Usage), result.Usage) + } + if result.CostDetails == nil { + t.Fatalf("expected CostDetails not nil, got nil") + } else if result.CostDetails.Cost != costDetails.Cost { + t.Errorf("expected Cost %v, got %v", costDetails.Cost, result.CostDetails.Cost) + } +} diff --git a/migrator/thresholds_test.go b/migrator/thresholds_test.go index 0c64e5d8c..6afd3279f 100644 --- a/migrator/thresholds_test.go +++ b/migrator/thresholds_test.go @@ -104,3 +104,61 @@ func TestV2ActionTriggerAsThreshold(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", filter, fltr) } } +func TestV2toV3Threshold(t *testing.T) { + + v2T := v2Threshold{ + Tenant: "test-tenant", + ID: "test-id", + FilterIDs: []string{"filter1", "filter2"}, + ActivationInterval: &utils.ActivationInterval{}, + Recurrent: true, + MinHits: 5, + MinSleep: 10 * time.Second, + Blocker: true, + Weight: 2.5, + ActionIDs: []string{"action1", "action2"}, + Async: true, + } + + result := v2T.V2toV3Threshold() + + if result.Tenant != v2T.Tenant { + t.Errorf("expected Tenant %v, got %v", v2T.Tenant, result.Tenant) + } + if result.ID != v2T.ID { + t.Errorf("expected ID %v, got %v", v2T.ID, result.ID) + } + if result.FilterIDs[0] != v2T.FilterIDs[0] || result.FilterIDs[1] != v2T.FilterIDs[1] { + t.Errorf("expected FilterIDs %v, got %v", v2T.FilterIDs, result.FilterIDs) + } + if result.ActivationInterval != v2T.ActivationInterval { + t.Errorf("expected ActivationInterval %v, got %v", v2T.ActivationInterval, result.ActivationInterval) + } + if result.MinHits != v2T.MinHits { + t.Errorf("expected MinHits %v, got %v", v2T.MinHits, result.MinHits) + } + if result.MinSleep != v2T.MinSleep { + t.Errorf("expected MinSleep %v, got %v", v2T.MinSleep, result.MinSleep) + } + if result.Blocker != v2T.Blocker { + t.Errorf("expected Blocker %v, got %v", v2T.Blocker, result.Blocker) + } + if result.Weight != v2T.Weight { + t.Errorf("expected Weight %v, got %v", v2T.Weight, result.Weight) + } + if result.ActionIDs[0] != v2T.ActionIDs[0] || result.ActionIDs[1] != v2T.ActionIDs[1] { + t.Errorf("expected ActionIDs %v, got %v", v2T.ActionIDs, result.ActionIDs) + } + if result.Async != v2T.Async { + t.Errorf("expected Async %v, got %v", v2T.Async, result.Async) + } + if result.MaxHits != -1 { + t.Errorf("expected MaxHits %v, got %v", -1, result.MaxHits) + } + + v2T.Recurrent = false + result = v2T.V2toV3Threshold() + if result.MaxHits != 1 { + t.Errorf("expected MaxHits %v, got %v", 1, result.MaxHits) + } +}