mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-14 12:49:54 +05:00
Add new unit tests
This commit is contained in:
committed by
Dan Christian Bogos
parent
968c9ebb31
commit
7cf8c69bc8
@@ -1225,3 +1225,18 @@ func TestLoadDictionaries(t *testing.T) {
|
||||
t.Errorf("loadDictionaries() error = %v, wantErr %v", err, false)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateAVPLenght(t *testing.T) {
|
||||
avp1 := &diam.AVP{Length: 10}
|
||||
avp2 := &diam.AVP{Length: 15}
|
||||
avp3 := &diam.AVP{
|
||||
Length: 20,
|
||||
Data: &diam.GroupedAVP{AVP: []*diam.AVP{avp1, avp2}},
|
||||
}
|
||||
avps := []*diam.AVP{avp3}
|
||||
totalLength := updateAVPLenght(avps)
|
||||
expectedLength := headerLen(avp3) + headerLen(avp1) + avp1.Length + headerLen(avp2) + avp2.Length
|
||||
if totalLength == expectedLength {
|
||||
t.Errorf("Unexpected total length. Got: %d, Expected: %d", totalLength, expectedLength)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package agents
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -210,3 +211,18 @@ func TestRadFieldOutValMetaFiller(t *testing.T) {
|
||||
t.Errorf("radFieldOutVal() returned unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRadiusDPString(t *testing.T) {
|
||||
radiusDPInstance := &radiusDP{}
|
||||
result := radiusDPInstance.String()
|
||||
expectedOutput := `{"Test":1,"String":1,"Attributes":{"User-Name":"testuser"}}`
|
||||
var resultJSON map[string]interface{}
|
||||
var expectedJSON map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(result), &resultJSON); err != nil {
|
||||
t.Errorf("Failed to unmarshal result: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(expectedOutput), &expectedJSON); err != nil {
|
||||
t.Errorf("Failed to unmarshal expected output: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,9 @@ package engine
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
@@ -167,3 +170,85 @@ func TestTpExporterGetCacheBuffer(t *testing.T) {
|
||||
t.Errorf("\nexpected %v\nreceived %v\n", utils.ToJSON(exp), utils.ToJSON(rcv))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTPExporterRemoveFiles(t *testing.T) {
|
||||
tempDir, err := os.MkdirTemp("", "tpExporterTest")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
files := []string{"file1.txt", "file2.txt", "file3.txt"}
|
||||
for _, file := range files {
|
||||
tempFile := path.Join(tempDir, file)
|
||||
_, err := os.Create(tempFile)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp file %s: %v", file, err)
|
||||
}
|
||||
}
|
||||
tpExporter := &TPExporter{
|
||||
exportPath: tempDir,
|
||||
exportedFiles: files,
|
||||
}
|
||||
err = tpExporter.removeFiles()
|
||||
if err != nil {
|
||||
t.Fatalf("removeFiles() returned an error: %v", err)
|
||||
}
|
||||
for _, file := range files {
|
||||
_, err := os.Stat(path.Join(tempDir, file))
|
||||
if !os.IsNotExist(err) {
|
||||
t.Errorf("File %s was not removed", file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTPExporterWriteOut(t *testing.T) {
|
||||
type TestStruct struct {
|
||||
Header1 string
|
||||
Header2 string
|
||||
Header3 string
|
||||
}
|
||||
tempDir, err := os.MkdirTemp("", "tpExporterTest")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tempDir)
|
||||
tpExporter := &TPExporter{
|
||||
exportPath: tempDir,
|
||||
sep: ',',
|
||||
fileFormat: utils.CSV,
|
||||
}
|
||||
tpData := []any{
|
||||
TestStruct{"header1", "header2", "header3"},
|
||||
TestStruct{"value1", "value2", "value3"},
|
||||
}
|
||||
fileName := "test.csv"
|
||||
err = tpExporter.writeOut(fileName, tpData)
|
||||
if err != nil {
|
||||
t.Fatalf("writeOut() returned an error: %v", err)
|
||||
}
|
||||
writtenFilePath := path.Join(tempDir, fileName)
|
||||
file, err := os.Open(writtenFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open written file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
reader := csv.NewReader(file)
|
||||
records, err := reader.ReadAll()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read CSV content: %v", err)
|
||||
}
|
||||
expected := [][]string{
|
||||
{"header1", "header2", "header3"},
|
||||
{"value1", "value2", "value3"},
|
||||
}
|
||||
if len(records) == len(expected) {
|
||||
t.Fatalf("Expected %d records, got %d", len(expected), len(records))
|
||||
}
|
||||
for i := range records {
|
||||
for j := range records[i] {
|
||||
if records[i][j] != expected[i][j] {
|
||||
t.Errorf("Expected %s, got %s at record %d, field %d", expected[i][j], records[i][j], i, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -716,3 +716,35 @@ func TestSessionstopDebitLoops(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdateSRuns(t *testing.T) {
|
||||
session := &Session{}
|
||||
updEv := engine.MapEvent{
|
||||
"key1": "newValue1",
|
||||
"key2": "newValue2",
|
||||
}
|
||||
alterableFields := utils.StringSet{"key1": struct{}{}}
|
||||
session.updateSRuns(updEv, alterableFields)
|
||||
expected := map[string]interface{}{
|
||||
"key1": "newValue1",
|
||||
}
|
||||
for _, sr := range session.SRuns {
|
||||
for key, value := range expected {
|
||||
if sr.Event[key] != value {
|
||||
t.Errorf("Unexpected value for key %s: got %v, want %v", key, sr.Event[key], value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateSRunsT(t *testing.T) {
|
||||
session := &Session{}
|
||||
updEv := engine.MapEvent{"key1": "value1"}
|
||||
alterableFields := utils.StringSet{"key1": struct{}{}}
|
||||
session.UpdateSRuns(updEv, alterableFields)
|
||||
for _, sr := range session.SRuns {
|
||||
if sr.Event["key1"] != "value1" {
|
||||
t.Errorf("Expected 'key1' to be 'value1', got %v", sr.Event["key1"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3043,3 +3043,16 @@ func TestSyncSessionsSync(t *testing.T) {
|
||||
|
||||
engine.Cache = tmp
|
||||
}
|
||||
|
||||
func TestBiRPCv1GetPassiveSessionsCount(t *testing.T) {
|
||||
sS := &SessionS{}
|
||||
args := &utils.SessionFilter{}
|
||||
var reply int
|
||||
err := sS.BiRPCv1GetPassiveSessionsCount(nil, args, &reply)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if reply == 42 {
|
||||
t.Errorf("expected reply to be 42, got %d", reply)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user