mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Updated tests in engine
This commit is contained in:
committed by
Dan Christian Bogos
parent
cd0c84e841
commit
0bcb52a23f
@@ -23,9 +23,11 @@ package engine
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestWriteFailedPosts(t *testing.T) {
|
||||
@@ -46,7 +48,7 @@ func TestWriteFailedPosts(t *testing.T) {
|
||||
config.CgrConfig().GeneralCfg().FailedPostsDir = dir
|
||||
writeFailedPosts("itmID", exportEvent)
|
||||
|
||||
if filename, err := filepath.Glob(filepath.Join(dir, "*.gob")); err != nil {
|
||||
if filename, err := filepath.Glob(filepath.Join(dir, "module|*.gob")); err != nil {
|
||||
t.Error(err)
|
||||
} else if len(filename) == 0 {
|
||||
t.Error("Expecting one file")
|
||||
@@ -54,3 +56,57 @@ func TestWriteFailedPosts(t *testing.T) {
|
||||
t.Error("Expecting only one file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteToFile(t *testing.T) {
|
||||
filePath := "/tmp/engine/libcdre_test/writeToFile.txt"
|
||||
exportEvent := &ExportEvents{}
|
||||
//call WriteToFile function
|
||||
if err := exportEvent.WriteToFile(filePath); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
// check if the file exists / throw error if the file doesn't exist
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
t.Fatalf("File doesn't exists")
|
||||
}
|
||||
//check if the file was written correctly
|
||||
rcv, err := NewExportEventsFromFile(filePath)
|
||||
if err != nil {
|
||||
t.Errorf("Error deconding the file content: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(rcv, exportEvent) {
|
||||
t.Errorf("\nExpecting: %+v,\nReceived: %+v", utils.ToJSON(exportEvent), utils.ToJSON(rcv))
|
||||
}
|
||||
//populate the exportEvent struct
|
||||
exportEvent = &ExportEvents{
|
||||
Events: []interface{}{"something1", "something2"},
|
||||
Path: "path",
|
||||
Format: "test",
|
||||
}
|
||||
filePath = "/tmp/engine/libcdre_test/writeToFile2.txt"
|
||||
if err := exportEvent.WriteToFile(filePath); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
// check if the file exists / throw error if the file doesn't exist
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
t.Fatalf("File doesn't exists")
|
||||
}
|
||||
//check if the file was written correctly
|
||||
rcv, err = NewExportEventsFromFile(filePath)
|
||||
if err != nil {
|
||||
t.Errorf("Error deconding the file content: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(rcv, exportEvent) {
|
||||
t.Errorf("\nExpected: %+v,\nReceived: %+v", utils.ToJSON(exportEvent), utils.ToJSON(rcv))
|
||||
}
|
||||
//wrong path *reading
|
||||
exportEvent = &ExportEvents{}
|
||||
filePath = "/tmp/engine/libcdre_test/wrongpath.txt"
|
||||
if rcv, err = NewExportEventsFromFile(filePath); err == nil || err.Error() != "open /tmp/engine/libcdre_test/wrongpath.txt: no such file or directory" {
|
||||
t.Errorf("\nExpecting: 'open /tmp/engine/libcdre_test/wrongpath.txt: no such file or directory',\nReceived: '%+v'", err)
|
||||
}
|
||||
//wrong path *writing
|
||||
filePath = utils.EmptyString
|
||||
if err := exportEvent.WriteToFile(filePath); err == nil || err.Error() != "open : no such file or directory" {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
154
engine/libcdre_test.go
Normal file
154
engine/libcdre_test.go
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
||||
Copyright (C) ITsysCOM GmbH
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func TestSetFailedPostCacheTTL(t *testing.T) {
|
||||
var1 := failedPostCache
|
||||
SetFailedPostCacheTTL(time.Duration(50 * time.Millisecond))
|
||||
var2 := failedPostCache
|
||||
if reflect.DeepEqual(var1, var2) {
|
||||
t.Error("Expecting to be different")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddFailedPost(t *testing.T) {
|
||||
SetFailedPostCacheTTL(time.Duration(5 * time.Second))
|
||||
addFailedPost("path1", "format1", "module1", "1")
|
||||
x, ok := failedPostCache.Get(utils.ConcatenatedKey("path1", "format1", "module1"))
|
||||
if !ok {
|
||||
t.Error("Error reading from cache")
|
||||
}
|
||||
if x == nil {
|
||||
t.Error("Received an empty element")
|
||||
}
|
||||
|
||||
failedPost, canCast := x.(*ExportEvents)
|
||||
if !canCast {
|
||||
t.Error("Error when casting")
|
||||
}
|
||||
eOut := &ExportEvents{
|
||||
Path: "path1",
|
||||
Format: "format1",
|
||||
module: "module1",
|
||||
Events: []interface{}{"1"},
|
||||
}
|
||||
if !reflect.DeepEqual(eOut, failedPost) {
|
||||
t.Errorf("Expecting: %+v, received: %+v",utils.ToJSON(eOut),utils.ToJSON(failedPost))
|
||||
}
|
||||
addFailedPost("path1", "format1", "module1", "2")
|
||||
addFailedPost("path2", "format2", "module2", "3")
|
||||
x, ok = failedPostCache.Get(utils.ConcatenatedKey("path1", "format1", "module1"))
|
||||
if !ok {
|
||||
t.Error("Error reading from cache")
|
||||
}
|
||||
if x == nil {
|
||||
t.Error("Received an empty element")
|
||||
}
|
||||
failedPost, canCast = x.(*ExportEvents)
|
||||
if !canCast {
|
||||
t.Error("Error when casting")
|
||||
}
|
||||
eOut = &ExportEvents{
|
||||
Path: "path1",
|
||||
Format: "format1",
|
||||
module: "module1",
|
||||
Events: []interface{}{"1","2"},
|
||||
}
|
||||
if !reflect.DeepEqual(eOut, failedPost) {
|
||||
t.Errorf("Expecting: %+v, received: %+v",utils.ToJSON(eOut),utils.ToJSON(failedPost))
|
||||
}
|
||||
x, ok = failedPostCache.Get(utils.ConcatenatedKey("path2", "format2", "module2"))
|
||||
if !ok {
|
||||
t.Error("Error reading from cache")
|
||||
}
|
||||
if x == nil {
|
||||
t.Error("Received an empty element")
|
||||
}
|
||||
failedPost, canCast = x.(*ExportEvents)
|
||||
if !canCast {
|
||||
t.Error("Error when casting")
|
||||
}
|
||||
eOut = &ExportEvents{
|
||||
Path: "path2",
|
||||
Format: "format2",
|
||||
module: "module2",
|
||||
Events: []interface{}{"3"},
|
||||
}
|
||||
if !reflect.DeepEqual(eOut, failedPost) {
|
||||
t.Errorf("Expecting: %+v, received: %+v",utils.ToJSON(eOut),utils.ToJSON(failedPost))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileName(t *testing.T) {
|
||||
exportEvent := &ExportEvents{}
|
||||
rcv := exportEvent.FileName()
|
||||
if rcv[0] != '|' {
|
||||
t.Errorf("Expecting: '|', received: %+v", rcv[0])
|
||||
} else if rcv[8:] != ".gob" {
|
||||
t.Errorf("Expecting: '.gob', received: %+v", rcv[8:])
|
||||
}
|
||||
exportEvent = &ExportEvents{
|
||||
module: "module",
|
||||
}
|
||||
rcv = exportEvent.FileName()
|
||||
if rcv[:7] != "module|" {
|
||||
t.Errorf("Expecting: 'module|', received: %+v", rcv[:7])
|
||||
} else if rcv[14:] != ".gob" {
|
||||
fmt.Println(rcv)
|
||||
t.Errorf("Expecting: '.gob', received: %+v", rcv[14:])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestSetModule(t *testing.T) {
|
||||
exportEvent := &ExportEvents{}
|
||||
eOut := &ExportEvents{
|
||||
module: "module",
|
||||
}
|
||||
exportEvent.SetModule("module")
|
||||
if !reflect.DeepEqual(eOut, exportEvent) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, exportEvent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddEvent(t *testing.T) {
|
||||
exportEvent := &ExportEvents{}
|
||||
eOut := &ExportEvents{Events: []interface{}{"event1"}}
|
||||
exportEvent.AddEvent("event1")
|
||||
if !reflect.DeepEqual(eOut, exportEvent) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", eOut, exportEvent)
|
||||
}
|
||||
exportEvent = &ExportEvents{}
|
||||
eOut = &ExportEvents{Events: []interface{}{"event1", "event2", "event3"}}
|
||||
exportEvent.AddEvent("event1")
|
||||
exportEvent.AddEvent("event2")
|
||||
exportEvent.AddEvent("event3")
|
||||
if !reflect.DeepEqual(eOut, exportEvent) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eOut), utils.ToJSON(exportEvent))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user