From c66f4f2710a47efd9f3d8617ae39e8fce431ef1d Mon Sep 17 00:00:00 2001 From: DanB Date: Sun, 16 Mar 2014 18:49:05 +0100 Subject: [PATCH] Local tests for SetCdr, SetRatedCdr and GetStoredCdrs --- engine/storage_sql.go | 6 +- engine/storage_sql_local_test.go | 174 +++++++++++++++++++++++++++++++ utils/cgrcdr_test.go | 13 +-- 3 files changed, 184 insertions(+), 9 deletions(-) diff --git a/engine/storage_sql.go b/engine/storage_sql.go index a754c4369..e19f9532c 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -632,19 +632,19 @@ func (self *SQLStorage) GetStoredCdrs(runId, cdrHost, cdrSource, reqType, direct if len(fltr) != 0 { fltr += " AND " } - fltr += fmt.Sprintf(" destination LIKE '%s%'", destPrefix) + fltr += fmt.Sprintf(" destination LIKE '%s%%'", destPrefix) } if !timeStart.IsZero() { if len(fltr) != 0 { fltr += " AND " } - fltr += fmt.Sprintf(" answer_time>='%d'", timeStart) + fltr += fmt.Sprintf(" answer_time>='%s'", timeStart) } if !timeEnd.IsZero() { if len(fltr) != 0 { fltr += " AND " } - fltr += fmt.Sprintf(" answer_time<'%d'", timeEnd) + fltr += fmt.Sprintf(" answer_time<'%s'", timeEnd) } if ignoreRated { if len(fltr) != 0 { diff --git a/engine/storage_sql_local_test.go b/engine/storage_sql_local_test.go index 0d9e6b8fa..e10b5b0c6 100644 --- a/engine/storage_sql_local_test.go +++ b/engine/storage_sql_local_test.go @@ -24,6 +24,7 @@ import ( "github.com/cgrates/cgrates/utils" "path" "testing" + "time" ) /* @@ -130,3 +131,176 @@ func TestRemoveData(t *testing.T) { t.Error("Did not remove TPAccountActions") } } + +func TestSetCdr(t *testing.T) { + if !*testLocal { + return + } + cgrCdr1 := &utils.CgrCdr{"accid": "aaa1", "cdrhost": "192.168.1.1", "reqtype": "rated", "direction": "*out", "tenant": "cgrates.org", "tor": "call", + "account": "1001", "subject": "1001", "destination": "1002", "setup_time": "2013-11-08T08:42:20Z", "answer_time": "2013-11-08T08:42:26Z", "duration": "10s", + "field_extr1": "val_extr1", "fieldextr2": "valextr2", "cdrsource": TEST_SQL} + cgrCdr2 := &utils.CgrCdr{"accid": "aaa2", "cdrhost": "192.168.1.1", "reqtype": "prepaid", "direction": "*out", "tenant": "cgrates.org", "tor": "call", + "account": "1001", "subject": "1001", "destination": "1002", "setup_time": "2013-11-08T08:42:22Z", "answer_time": "2013-11-08T08:42:26Z", "duration": "20", + "field_extr1": "val_extr1", "fieldextr2": "valextr2", "cdrsource": TEST_SQL} + cgrCdr3 := &utils.CgrCdr{"accid": "aaa3", "cdrhost": "192.168.1.1", "reqtype": "rated", "direction": "*out", "tenant": "cgrates.org", "tor": "premium_call", + "account": "1002", "subject": "1002", "destination": "1001", "setup_time": "2013-11-07T08:42:24Z", "answer_time": "2013-11-07T08:42:26Z", "duration": "60s", + "field_extr1": "val_extr1", "fieldextr2": "valextr2", "cdrsource": TEST_SQL} + cgrCdr4 := &utils.CgrCdr{"accid": "aaa4", "cdrhost": "192.168.1.2", "reqtype": "pseudoprepaid", "direction": "*out", "tenant": "itsyscom.com", "tor": "call", + "account": "1001", "subject": "1001", "destination": "+4986517174964", "setup_time": "2013-11-07T08:42:21Z", "answer_time": "2013-11-07T08:42:26Z", "duration": "1m2s", + "field_extr1": "val_extr1", "fieldextr2": "valextr2", "cdrsource": TEST_SQL} + cgrCdr5 := &utils.CgrCdr{"accid": "aaa5", "cdrhost": "192.168.1.2", "reqtype": "postpaid", "direction": "*out", "tenant": "itsyscom.com", "tor": "call", + "account": "1002", "subject": "1002", "destination": "+4986517174963", "setup_time": "2013-11-07T08:42:25Z", "answer_time": "2013-11-07T08:42:26Z", "duration": "15s", + "field_extr1": "val_extr1", "fieldextr2": "valextr2", "cdrsource": TEST_SQL} + for _, cdr := range []*utils.CgrCdr{cgrCdr1, cgrCdr2, cgrCdr3, cgrCdr4, cgrCdr5} { + if err := mysql.SetCdr(cdr); err != nil { + t.Error(err.Error()) + } + } + strCdr1 := &utils.StoredCdr{CgrId: utils.FSCgrId("bbb1"), AccId: "bbb1", CdrHost: "192.168.1.1", CdrSource: "UNKNOWN", ReqType: "rated", + Direction: "*out", Tenant: "cgrates.org", TOR: "call", Account: "1001", Subject: "1001", Destination: "1002", + SetupTime: time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC), AnswerTime: time.Date(2013, 12, 7, 8, 42, 26, 0, time.UTC), + Duration: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + MediationRunId: utils.DEFAULT_RUNID, Cost: 1.201} + strCdr2 := &utils.StoredCdr{CgrId: utils.FSCgrId("bbb2"), AccId: "bbb2", CdrHost: "192.168.1.2", CdrSource: TEST_SQL, ReqType: "prepaid", + Direction: "*out", Tenant: "cgrates.org", TOR: "call", Account: "1001", Subject: "1001", Destination: "1002", + SetupTime: time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC), AnswerTime: time.Date(2013, 12, 7, 8, 42, 26, 0, time.UTC), + Duration: time.Duration(12) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + MediationRunId: utils.DEFAULT_RUNID, Cost: 0.201} + strCdr3 := &utils.StoredCdr{CgrId: utils.FSCgrId("bbb3"), AccId: "bbb3", CdrHost: "192.168.1.1", CdrSource: TEST_SQL, ReqType: "rated", + Direction: "*out", Tenant: "itsyscom.com", TOR: "call", Account: "1002", Subject: "1000", Destination: "+4986517174963", + SetupTime: time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC), AnswerTime: time.Date(2013, 12, 7, 8, 42, 26, 0, time.UTC), + Duration: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + MediationRunId: utils.DEFAULT_RUNID, Cost: 1.201} + + for _, cdr := range []*utils.StoredCdr{strCdr1, strCdr2, strCdr3} { + if err := mysql.SetCdr(cdr); err != nil { + t.Error(err.Error()) + } + } +} + +func TestSetRatedCdr(t *testing.T) { + if !*testLocal { + return + } + strCdr1 := &utils.StoredCdr{CgrId: utils.FSCgrId("bbb1"), AccId: "bbb1", CdrHost: "192.168.1.1", CdrSource: "UNKNOWN", ReqType: "rated", + Direction: "*out", Tenant: "cgrates.org", TOR: "call", Account: "1001", Subject: "1001", Destination: "1002", + SetupTime: time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC), AnswerTime: time.Date(2013, 12, 7, 8, 42, 26, 0, time.UTC), + Duration: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + MediationRunId: utils.DEFAULT_RUNID, Cost: 1.201} + strCdr2 := &utils.StoredCdr{CgrId: utils.FSCgrId("bbb2"), AccId: "bbb2", CdrHost: "192.168.1.2", CdrSource: TEST_SQL, ReqType: "prepaid", + Direction: "*out", Tenant: "cgrates.org", TOR: "call", Account: "1001", Subject: "1001", Destination: "1002", + SetupTime: time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC), AnswerTime: time.Date(2013, 12, 7, 8, 42, 26, 0, time.UTC), + Duration: time.Duration(12) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + MediationRunId: utils.DEFAULT_RUNID, Cost: 0.201} + strCdr3 := &utils.StoredCdr{CgrId: utils.FSCgrId("bbb3"), AccId: "bbb3", CdrHost: "192.168.1.1", CdrSource: TEST_SQL, ReqType: "rated", + Direction: "*out", Tenant: "itsyscom.com", TOR: "call", Account: "1002", Subject: "1002", Destination: "+4986517174963", + SetupTime: time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC), AnswerTime: time.Date(2013, 12, 7, 8, 42, 26, 0, time.UTC), + Duration: time.Duration(10) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + MediationRunId: "wholesale_run", Cost: 1.201} + + for _, cdr := range []*utils.StoredCdr{strCdr1, strCdr2, strCdr3} { + if err := mysql.SetRatedCdr(cdr, ""); err != nil { + t.Error(err.Error()) + } + } +} + +func TestGetStoredCdrs(t *testing.T) { + if !*testLocal { + return + } + var timeStart, timeEnd time.Time + // All CDRs, no filter + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 8 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on runId + if storedCdrs, err := mysql.GetStoredCdrs(utils.DEFAULT_RUNID, "", "", "", "", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 2 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on cdrHost + if storedCdrs, err := mysql.GetStoredCdrs("", "192.168.1.2", "", "", "", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 3 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on cdrSource + if storedCdrs, err := mysql.GetStoredCdrs("", "", "UNKNOWN", "", "", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 1 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on reqType + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "prepaid", "", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 2 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on direction + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "*out", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 8 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on tenant + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "itsyscom.com", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 3 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on tor + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "", "premium_call", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 1 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on account + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "", "", "1002", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 3 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on subject + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "", "", "", "1000", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 1 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on ignoreErr + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "", "", "", "", "", timeStart, timeEnd, true, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 8 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on ignoreRated + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "", "", "", "", "", timeStart, timeEnd, false, true); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 5 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on timeStart + timeStart = time.Date(2013, 11, 8, 8, 0, 0, 0, time.UTC) + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 5 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Filter on timeStart and timeEnd + timeEnd = time.Date(2013, 12, 1, 8, 0, 0, 0, time.UTC) + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "", "", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 2 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } + // Combined filter + if storedCdrs, err := mysql.GetStoredCdrs("", "", "", "rated", "", "", "", "", "", "", timeStart, timeEnd, false, false); err != nil { + t.Error(err.Error()) + } else if len(storedCdrs) != 1 { + t.Error("Unexpected number of StoredCdrs returned: ", storedCdrs) + } +} diff --git a/utils/cgrcdr_test.go b/utils/cgrcdr_test.go index 5b0ed6bfb..2e1c48d87 100644 --- a/utils/cgrcdr_test.go +++ b/utils/cgrcdr_test.go @@ -30,7 +30,8 @@ curl --data "accid=asbfdsaf&cdrhost=192.168.1.1&reqtype=rated&direction=*out&ten func TestCgrCdrFields(t *testing.T) { cgrCdr := CgrCdr{"accid": "dsafdsaf", "cdrhost": "192.168.1.1", "reqtype": "rated", "direction": "*out", "tenant": "cgrates.org", "tor": "call", - "account": "1001", "subject": "1001", "destination": "1002", "answer_time": "2013-11-07T08:42:26Z", "duration": "10", "field_extr1": "val_extr1", "fieldextr2": "valextr2"} + "account": "1001", "subject": "1001", "destination": "1002", "answer_time": "2013-11-07T08:42:26Z", "duration": "10", + "field_extr1": "val_extr1", "fieldextr2": "valextr2"} if cgrCdr.GetCgrId() != FSCgrId("dsafdsaf") { t.Error("Error parsing cdr: ", cgrCdr) } @@ -82,32 +83,32 @@ func TestCgrCdrAsStoredCdr(t *testing.T) { cgrCdr := &CgrCdr{"accid": "dsafdsaf", "cdrhost": "192.168.1.1", "cdrsource": "source_test", "reqtype": "rated", "direction": "*out", "tenant": "cgrates.org", "tor": "call", "account": "1001", "subject": "1001", "destination": "1002", "setup_time": "2013-11-07T08:42:24Z", "answer_time": "2013-11-07T08:42:26Z", "duration": "10", "field_extr1": "val_extr1", "fieldextr2": "valextr2"} - rtCdrOut, err := cgrCdr.AsStoredCdr("wholesale_run", "reqtype", "direction", "tenant", "tor", "account", "subject", "destination", "setup_time", "answer_time", "duration", + rtCdrOut, err := cgrCdr.AsStoredCdr("wholesale_run", "reqtype", "direction", "tenant", "tor", "account", "subject", "destination", "setup_time", "answer_time", "duration", []string{"field_extr1", "fieldextr2"}, true) if err != nil { t.Error("Unexpected error received", err) } expctRatedCdr := &StoredCdr{CgrId: FSCgrId("dsafdsaf"), AccId: "dsafdsaf", CdrHost: "192.168.1.1", CdrSource: "source_test", ReqType: "rated", - Direction: "*out", Tenant: "cgrates.org", TOR: "call", Account: "1001", Subject: "1001", Destination: "1002", + Direction: "*out", Tenant: "cgrates.org", TOR: "call", Account: "1001", Subject: "1001", Destination: "1002", SetupTime: time.Unix(1383813744, 0).UTC(), AnswerTime: time.Unix(1383813746, 0).UTC(), Duration: 10000000000, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, MediationRunId: "wholesale_run", Cost: -1} if !reflect.DeepEqual(rtCdrOut, expctRatedCdr) { t.Errorf("Received: %v, expected: %v", rtCdrOut, expctRatedCdr) } - rtCdrOut2, err := cgrCdr.AsStoredCdr("wholesale_run", "^postpaid", "^*in", "^cgrates.com", "^premium_call", "^first_account", "^first_subject", "destination", + rtCdrOut2, err := cgrCdr.AsStoredCdr("wholesale_run", "^postpaid", "^*in", "^cgrates.com", "^premium_call", "^first_account", "^first_subject", "destination", "^2013-12-07T08:42:24Z", "^2013-12-07T08:42:26Z", "^12s", []string{"field_extr1", "fieldextr2"}, true) if err != nil { t.Error("Unexpected error received", err) } expctRatedCdr2 := &StoredCdr{CgrId: FSCgrId("dsafdsaf"), AccId: "dsafdsaf", CdrHost: "192.168.1.1", CdrSource: "source_test", ReqType: "postpaid", Direction: "*in", Tenant: "cgrates.com", TOR: "premium_call", Account: "first_account", Subject: "first_subject", Destination: "1002", - SetupTime: time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC), + SetupTime: time.Date(2013, 12, 7, 8, 42, 24, 0, time.UTC), AnswerTime: time.Date(2013, 12, 7, 8, 42, 26, 0, time.UTC), Duration: time.Duration(12) * time.Second, ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, MediationRunId: "wholesale_run", Cost: -1} if !reflect.DeepEqual(rtCdrOut2, expctRatedCdr2) { t.Errorf("Received: %v, expected: %v", rtCdrOut2, expctRatedCdr2) } - _, err = cgrCdr.AsStoredCdr("wholesale_run", "dummy_header", "direction", "tenant", "tor", "account", "subject", "destination", "setup_time", "answer_time", "duration", + _, err = cgrCdr.AsStoredCdr("wholesale_run", "dummy_header", "direction", "tenant", "tor", "account", "subject", "destination", "setup_time", "answer_time", "duration", []string{"field_extr1", "fieldextr2"}, true) if err == nil { t.Error("Failed to detect missing header")