mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 10:06:24 +05:00
Add coverage tests on ers and config
This commit is contained in:
committed by
Dan Christian Bogos
parent
03e5ee65ac
commit
7560328388
@@ -20,7 +20,11 @@ package ers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func testCreateDirs(t *testing.T) {
|
||||
@@ -61,3 +65,58 @@ func testCleanupFiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessReaderDir(t *testing.T) {
|
||||
dir, err := os.MkdirTemp("", "testProcessReaderDir")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
file1 := filepath.Join(dir, "file1.csv")
|
||||
file2 := filepath.Join(dir, "file2.csv")
|
||||
file3 := filepath.Join(dir, "file3.txt")
|
||||
|
||||
if err := os.WriteFile(file1, []byte("data"), 0644); err != nil {
|
||||
t.Fatalf("Failed to create file1: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(file2, []byte("data"), 0644); err != nil {
|
||||
t.Fatalf("Failed to create file2: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(file3, []byte("data"), 0644); err != nil {
|
||||
t.Fatalf("Failed to create file3: %v", err)
|
||||
}
|
||||
|
||||
var processedFiles []string
|
||||
var mu sync.Mutex
|
||||
mockFunc := func(fn string) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
processedFiles = append(processedFiles, fn)
|
||||
return nil
|
||||
}
|
||||
|
||||
processReaderDir(dir, ".csv", mockFunc)
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(processedFiles) != 2 {
|
||||
t.Errorf("Expected 2 files to be processed, got %d", len(processedFiles))
|
||||
}
|
||||
|
||||
expectedFiles := []string{"file1.csv", "file2.csv"}
|
||||
for _, expected := range expectedFiles {
|
||||
found := false
|
||||
for _, processed := range processedFiles {
|
||||
if strings.HasSuffix(processed, expected) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("Expected file %s to be processed, but it was not", expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user