mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Add coverage tests on ers and config
This commit is contained in:
committed by
Dan Christian Bogos
parent
03e5ee65ac
commit
7560328388
@@ -21,6 +21,7 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -5848,3 +5849,46 @@ func TestV1GetConfigRANKINGS_JSON(t *testing.T) {
|
||||
t.Errorf("Expected %+v \n, received %+v", utils.ToJSON(expected), utils.ToJSON(reply))
|
||||
}
|
||||
}
|
||||
|
||||
func TestV1ReloadConfig(t *testing.T) {
|
||||
cfg := &CGRConfig{}
|
||||
ctx := context.TODO()
|
||||
missingPathArgs := &ReloadArgs{
|
||||
APIOpts: map[string]any{},
|
||||
Tenant: "cgrates.org",
|
||||
Section: "section",
|
||||
DryRun: false,
|
||||
}
|
||||
var reply string
|
||||
err := cfg.V1ReloadConfig(ctx, missingPathArgs, &reply)
|
||||
if err == nil {
|
||||
t.Errorf("Expected an error for missing 'Path' field, but got none")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestLoadConfigFromFolder(t *testing.T) {
|
||||
cfg := &CGRConfig{}
|
||||
tmpDir, err := os.MkdirTemp("", "configtest")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp directory: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
err = cfg.loadConfigFromFolder(tmpDir, nil, false)
|
||||
if err == nil || err.Error() != "No config file found on path "+tmpDir {
|
||||
t.Errorf("Expected error for no config files, but got: %v", err)
|
||||
}
|
||||
validJson := `{"Subject": "1001"}`
|
||||
filePath := filepath.Join(tmpDir, "config.json")
|
||||
err = os.WriteFile(filePath, []byte(validJson), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to write test JSON file: %v", err)
|
||||
}
|
||||
loadFunc := func(jsnCfg *CgrJsonCfg) error {
|
||||
return nil
|
||||
}
|
||||
err = cfg.loadConfigFromFolder(tmpDir, []func(jsnCfg *CgrJsonCfg) error{loadFunc}, false)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package config
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -112,3 +115,44 @@ func TestConfigSCfgClone(t *testing.T) {
|
||||
t.Errorf("Expected clone to not modify the cloned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleConfigSFile(t *testing.T) {
|
||||
|
||||
tmpFile, err := os.CreateTemp("", "testfile-*.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp file: %v", err)
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
|
||||
content := "content"
|
||||
if _, err := tmpFile.WriteString(content); err != nil {
|
||||
t.Fatalf("Failed to write to temp file: %v", err)
|
||||
}
|
||||
tmpFile.Close()
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handleConfigSFile(tmpFile.Name(), rr)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("Expected status code %d, got %d", http.StatusOK, rr.Code)
|
||||
}
|
||||
|
||||
if rr.Body.String() != content {
|
||||
t.Errorf("Expected response body %q, got %q", content, rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleConfigSFileFileReadError(t *testing.T) {
|
||||
|
||||
nonExistentFilePath := "non-existent-file.txt"
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handleConfigSFile(nonExistentFilePath, rr)
|
||||
|
||||
if rr.Code != http.StatusInternalServerError {
|
||||
t.Errorf("Expected status code %d, got %d", http.StatusInternalServerError, rr.Code)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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