mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add coverage tests on engine && utils
This commit is contained in:
committed by
Dan Christian Bogos
parent
9d05e66d2e
commit
63320b71f0
@@ -1454,3 +1454,46 @@ func TestRatingMatchedFiltersFieldAsInterface(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldAsInterfaces(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
accounting Accounting
|
||||
fldPath []string
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
name: "Non-existent path",
|
||||
accounting: Accounting{"balance1": &BalanceCharge{AccountID: "1"}},
|
||||
fldPath: []string{"balance2"},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "Nil value in map",
|
||||
accounting: Accounting{"balance1": nil},
|
||||
fldPath: []string{"balance1"},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "Valid key with non-nil value",
|
||||
accounting: Accounting{"balance1": &BalanceCharge{AccountID: "1"}},
|
||||
fldPath: []string{"balance1"},
|
||||
expectErr: false,
|
||||
},
|
||||
{
|
||||
name: "Nil Accounting map",
|
||||
accounting: nil,
|
||||
fldPath: []string{"balance1"},
|
||||
expectErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := tt.accounting.FieldAsInterface(tt.fldPath)
|
||||
if (err != nil) != tt.expectErr {
|
||||
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1027,3 +1027,156 @@ func TestNewAttrReloadCacheWithOptsFromMap(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestIsActiveAt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
timing TPTiming
|
||||
checkTime time.Time
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "Active timing",
|
||||
timing: TPTiming{
|
||||
Years: Years{2024},
|
||||
Months: Months{time.January},
|
||||
MonthDays: MonthDays{15},
|
||||
WeekDays: WeekDays{time.Monday},
|
||||
StartTime: "09:00:00",
|
||||
EndTime: "17:00:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Inactive year",
|
||||
timing: TPTiming{
|
||||
Years: Years{2023},
|
||||
Months: Months{time.January},
|
||||
MonthDays: MonthDays{15},
|
||||
WeekDays: WeekDays{time.Monday},
|
||||
StartTime: "09:00:00",
|
||||
EndTime: "17:00:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Inactive month",
|
||||
timing: TPTiming{
|
||||
Years: Years{2024},
|
||||
Months: Months{time.February},
|
||||
MonthDays: MonthDays{15},
|
||||
WeekDays: WeekDays{time.Monday},
|
||||
StartTime: "09:00:00",
|
||||
EndTime: "17:00:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Inactive day",
|
||||
timing: TPTiming{
|
||||
Years: Years{2024},
|
||||
Months: Months{time.January},
|
||||
MonthDays: MonthDays{16},
|
||||
WeekDays: WeekDays{time.Monday},
|
||||
StartTime: "09:00:00",
|
||||
EndTime: "17:00:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Inactive weekday",
|
||||
timing: TPTiming{
|
||||
Years: Years{2024},
|
||||
Months: Months{time.January},
|
||||
MonthDays: MonthDays{15},
|
||||
WeekDays: WeekDays{time.Wednesday},
|
||||
StartTime: "09:00:00",
|
||||
EndTime: "17:00:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Before start time",
|
||||
timing: TPTiming{
|
||||
Years: Years{2024},
|
||||
Months: Months{time.January},
|
||||
MonthDays: MonthDays{15},
|
||||
WeekDays: WeekDays{time.Monday},
|
||||
StartTime: "12:00:00",
|
||||
EndTime: "17:00:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 11, 0, 0, 0, time.UTC),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "After end time",
|
||||
timing: TPTiming{
|
||||
Years: Years{2024},
|
||||
Months: Months{time.January},
|
||||
MonthDays: MonthDays{15},
|
||||
WeekDays: WeekDays{time.Monday},
|
||||
StartTime: "09:00:00",
|
||||
EndTime: "12:00:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 13, 0, 0, 0, time.UTC),
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := tt.timing.IsActiveAt(tt.checkTime)
|
||||
if result != tt.expected {
|
||||
t.Errorf("expected %v, got %v", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRightMargin(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
timing TPTiming
|
||||
checkTime time.Time
|
||||
expected time.Time
|
||||
}{
|
||||
{
|
||||
name: "With specific end time",
|
||||
timing: TPTiming{
|
||||
EndTime: "15:30:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC),
|
||||
expected: time.Date(2024, time.January, 15, 15, 30, 0, 0, time.UTC),
|
||||
},
|
||||
{
|
||||
name: "With default end of the day",
|
||||
timing: TPTiming{
|
||||
EndTime: "",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC),
|
||||
expected: time.Date(2024, time.January, 15, 23, 59, 59, 0, time.UTC).Add(time.Second),
|
||||
},
|
||||
{
|
||||
name: "With second specific end time",
|
||||
timing: TPTiming{
|
||||
EndTime: "12:00:00",
|
||||
},
|
||||
checkTime: time.Date(2024, time.January, 15, 10, 0, 0, 0, time.UTC),
|
||||
expected: time.Date(2024, time.January, 15, 12, 0, 0, 0, time.UTC),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := tt.timing.getRightMargin(tt.checkTime)
|
||||
if result != tt.expected {
|
||||
t.Errorf("expected %v, got %v", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,11 @@ package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -113,3 +117,86 @@ func TestGetReaderFromPathError(t *testing.T) {
|
||||
t.Errorf("Expected <open string: no such file or directory>, received <%v>", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetReaderFromPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
timeout time.Duration
|
||||
expectErr bool
|
||||
expectData string
|
||||
}{
|
||||
{
|
||||
name: "Valid file path",
|
||||
path: "testfile.txt",
|
||||
timeout: 1 * time.Second,
|
||||
expectErr: false,
|
||||
expectData: "This is a test file.",
|
||||
},
|
||||
{
|
||||
name: "Invalid file path",
|
||||
path: "invalidfile.txt",
|
||||
timeout: 1 * time.Second,
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "Valid HTTP URL",
|
||||
path: "http://cgrates.com",
|
||||
timeout: 1 * time.Second,
|
||||
expectErr: false,
|
||||
},
|
||||
{
|
||||
name: "HTTP URL returns non-200 status",
|
||||
path: "http://cgrates.com/non200",
|
||||
timeout: 1 * time.Second,
|
||||
expectErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
testFileName := "testfile.txt"
|
||||
err := os.WriteFile(testFileName, []byte("This is a test file."), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
}
|
||||
defer os.Remove(testFileName)
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/non200" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Write([]byte("test"))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.path == "http://cgrates.com" {
|
||||
tt.path = ts.URL
|
||||
} else if tt.path == "http://cgrates.com/non200" {
|
||||
tt.path = ts.URL + "/non200"
|
||||
}
|
||||
|
||||
reader, err := GetReaderFromPath(tt.path, tt.timeout)
|
||||
|
||||
if (err != nil) != tt.expectErr {
|
||||
t.Errorf("expected error: %v, got: %v", tt.expectErr, err)
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.expectErr && reader != nil {
|
||||
defer reader.Close()
|
||||
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
t.Errorf("failed to read from reader: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if string(data) != tt.expectData && tt.expectData != "" {
|
||||
t.Errorf("expected data: %s, got: %s", tt.expectData, string(data))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user