From 6c02605cfdb5490f4c7aa95fe950d36cfa3d0307 Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 9 Oct 2014 14:11:58 +0200 Subject: [PATCH] ApierV2.RemTP --- engine/storage_sql.go | 12 ++++++++ engine/storage_sql_local_test.go | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 07ed44975..1c3d6d6e4 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -177,6 +177,18 @@ func (self *SQLStorage) SetTPTiming(tpid string, tm *utils.TPTiming) error { } func (self *SQLStorage) RemTPData(table, tpid string, args ...string) error { + if len(table) == 0 { // Remove tpid out of all tables + tx := self.db.Begin() + for _, tblName := range []string{utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_RATES, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_RATING_PLANS, utils.TBL_TP_RATE_PROFILES, + utils.TBL_TP_SHARED_GROUPS, utils.TBL_TP_CDR_STATS, utils.TBL_TP_LCRS, utils.TBL_TP_ACTIONS, utils.TBL_TP_ACTION_PLANS, utils.TBL_TP_ACTION_TRIGGERS, utils.TBL_TP_ACCOUNT_ACTIONS, utils.TBL_TP_DERIVED_CHARGERS} { + if err := tx.Table(tblName).Where("tpid = ?", tpid).Delete(nil).Error; err != nil { + tx.Rollback() + return err + } + } + tx.Commit() + return nil + } q := fmt.Sprintf("DELETE FROM %s WHERE tpid='%s' AND id='%s'", table, tpid, args[0]) switch table { case utils.TBL_TP_RATE_PROFILES: diff --git a/engine/storage_sql_local_test.go b/engine/storage_sql_local_test.go index eb532ec0a..62b23ad36 100644 --- a/engine/storage_sql_local_test.go +++ b/engine/storage_sql_local_test.go @@ -133,6 +133,53 @@ func TestRemoveData(t *testing.T) { } else if err.Error() != "Record Not Found" { t.Error(err.Error()) } + // Create again so we can test complete TP removal + if err := mysqlDb.SetTPTiming(TEST_SQL, tm); err != nil { + t.Error(err.Error()) + } + if tmgs, err := mysqlDb.GetTpTimings(TEST_SQL, tm.Id); err != nil { + t.Error(err.Error()) + } else if len(tmgs) == 0 { + t.Error("Could not store TPTiming") + } + // Create RatingProfile + if err := mysqlDb.SetTPRatingProfiles(TEST_SQL, map[string]*utils.TPRatingProfile{rp.KeyId(): rp}); err != nil { + t.Error(err.Error()) + } + if rps, err := mysqlDb.GetTpRatingProfiles(rp); err != nil { + t.Error(err.Error()) + } else if len(rps) == 0 { + t.Error("Could not store TPRatingProfile") + } + // Create AccountActions + if err := mysqlDb.SetTPAccountActions(aa.TPid, map[string]*utils.TPAccountActions{aa.KeyId(): aa}); err != nil { + t.Error(err.Error()) + } + if aas, err := mysqlDb.GetTpAccountActions(aa); err != nil { + t.Error(err.Error()) + } else if len(aas) == 0 { + t.Error("Could not create TPAccountActions") + } + // Remove TariffPlan completely + if err := mysqlDb.RemTPData("", TEST_SQL); err != nil { + t.Error(err.Error()) + } + // Make sure we have removed it + if _, err := mysqlDb.GetTpTimings(TEST_SQL, tm.Id); err == nil { + t.Error("Should report error on querying here") + } else if err.Error() != "Record Not Found" { + t.Error(err.Error()) + } + if _, err := mysqlDb.GetTpRatingProfiles(rp); err == nil { + t.Error("Should return error in case of record not found from ORM") + } else if err.Error() != "Record Not Found" { + t.Error(err.Error()) + } + if _, err := mysqlDb.GetTpAccountActions(aa); err == nil { + t.Error("Should receive error in case of not found") + } else if err.Error() != "Record Not Found" { + t.Error(err.Error()) + } } func TestSetCdr(t *testing.T) {