From 352540ab045cdd9d3fc823f05817c27139c4be51 Mon Sep 17 00:00:00 2001 From: DanB Date: Mon, 18 Nov 2013 16:29:25 +0100 Subject: [PATCH] Adding local tests, available via flag -local --- data/storage/mysql/create_cdrs_tables.sql | 54 ++++++------- .../mysql/create_costdetails_tables.sql | 2 + data/storage/mysql/create_db_with_users.sql | 9 +++ data/storage/mysql/create_mediator_tables.sql | 1 + .../mysql/create_tariffplan_tables.sql | 10 +++ data/storage/mysql/create_users.sql | 5 -- engine/storage_sql.go | 21 +++++ engine/storage_sql_test.go | 81 +++++++++++++++++++ 8 files changed, 150 insertions(+), 33 deletions(-) create mode 100644 data/storage/mysql/create_db_with_users.sql delete mode 100644 data/storage/mysql/create_users.sql create mode 100644 engine/storage_sql_test.go diff --git a/data/storage/mysql/create_cdrs_tables.sql b/data/storage/mysql/create_cdrs_tables.sql index 89ca6f467..b5f87fbe1 100644 --- a/data/storage/mysql/create_cdrs_tables.sql +++ b/data/storage/mysql/create_cdrs_tables.sql @@ -1,31 +1,29 @@ --- --- Table structure for table `cdrs_primary` --- -CREATE TABLE `cdrs_primary` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `cgrid` char(40) NOT NULL, - `accid` varchar(64) NOT NULL, - `cdrhost` varchar(64) NOT NULL, - `reqtype` varchar(24) NOT NULL, - `direction` varchar(8) NOT NULL, - `tenant` varchar(64) NOT NULL, - `tor` varchar(16) NOT NULL, - `account` varchar(128) NOT NULL, - `subject` varchar(128) NOT NULL, - `destination` varchar(128) NOT NULL, - `answer_timestamp` int NOT NULL, - `duration` int NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `cgrid` (`cgrid`) +DROP TABLE IF EXISTS cdrs_primary; + +CREATE TABLE cdrs_primary ( + id int(11) NOT NULL AUTO_INCREMENT, + cgrid char(40) NOT NULL, + accid varchar(64) NOT NULL, + cdrhost varchar(64) NOT NULL, + reqtype varchar(24) NOT NULL, + direction varchar(8) NOT NULL, + tenant varchar(64) NOT NULL, + tor varchar(16) NOT NULL, + account varchar(128) NOT NULL, + subject varchar(128) NOT NULL, + destination varchar(128) NOT NULL, + answer_timestamp int NOT NULL, + duration int NOT NULL, + PRIMARY KEY (id), + UNIQUE KEY cgrid (cgrid) ); --- --- Table structure for table cdrs_extra --- -CREATE TABLE `cdrs_extra` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `cgrid` char(40) NOT NULL, - `extra_fields` text NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `cgrid` (`cgrid`) +DROP TABLE IF EXISTS cdrs_extra; + +CREATE TABLE cdrs_extra ( + id int(11) NOT NULL AUTO_INCREMENT, + cgrid char(40) NOT NULL, + extra_fields text NOT NULL, + PRIMARY KEY (id), + UNIQUE KEY cgrid (cgrid) ); diff --git a/data/storage/mysql/create_costdetails_tables.sql b/data/storage/mysql/create_costdetails_tables.sql index 6db95465d..6c64918db 100644 --- a/data/storage/mysql/create_costdetails_tables.sql +++ b/data/storage/mysql/create_costdetails_tables.sql @@ -2,6 +2,8 @@ -- -- Table structure for table `cost_details` -- + +DROP TABLE IF EXISTS `cost_details`; CREATE TABLE `cost_details` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cgrid` char(40) NOT NULL, diff --git a/data/storage/mysql/create_db_with_users.sql b/data/storage/mysql/create_db_with_users.sql new file mode 100644 index 000000000..31746b604 --- /dev/null +++ b/data/storage/mysql/create_db_with_users.sql @@ -0,0 +1,9 @@ + +-- +-- Sample db and users creation. Replace here with your own details +-- + +DROP DATABASE IF EXISTS cgrates; +CREATE DATABASE cgrates; + +GRANT ALL on cgrates.* TO 'cgrates'@'localhost' IDENTIFIED BY 'CGRateS.org'; diff --git a/data/storage/mysql/create_mediator_tables.sql b/data/storage/mysql/create_mediator_tables.sql index 4b6d4611b..d5c98120a 100644 --- a/data/storage/mysql/create_mediator_tables.sql +++ b/data/storage/mysql/create_mediator_tables.sql @@ -2,6 +2,7 @@ -- -- Table structure for table `rater_cdrs` -- +DROP TABLE IF EXISTS `rated_cdrs`; CREATE TABLE `rated_cdrs` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cgrid` char(40) NOT NULL, diff --git a/data/storage/mysql/create_tariffplan_tables.sql b/data/storage/mysql/create_tariffplan_tables.sql index f88123c91..b53403b45 100644 --- a/data/storage/mysql/create_tariffplan_tables.sql +++ b/data/storage/mysql/create_tariffplan_tables.sql @@ -1,6 +1,7 @@ -- -- Table structure for table `tp_timings` -- +DROP TABLE IF EXISTS `tp_timings`; CREATE TABLE `tp_timings` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -20,6 +21,7 @@ CREATE TABLE `tp_timings` ( -- Table structure for table `tp_destinations` -- +DROP TABLE IF EXISTS `tp_destinations`; CREATE TABLE `tp_destinations` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -35,6 +37,7 @@ CREATE TABLE `tp_destinations` ( -- Table structure for table `tp_rates` -- +DROP TABLE IF EXISTS `tp_rates`; CREATE TABLE `tp_rates` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -56,6 +59,7 @@ CREATE TABLE `tp_rates` ( -- Table structure for table `destination_rates` -- +DROP TABLE IF EXISTS `tp_destination_rates`; CREATE TABLE `tp_destination_rates` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -72,6 +76,7 @@ CREATE TABLE `tp_destination_rates` ( -- Table structure for table `tp_rating_plans` -- +DROP TABLE IF EXISTS `tp_rating_plans`; CREATE TABLE `tp_rating_plans` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -89,6 +94,7 @@ CREATE TABLE `tp_rating_plans` ( -- Table structure for table `tp_rate_profiles` -- +DROP TABLE IF EXISTS `tp_rating_profiles`; CREATE TABLE `tp_rating_profiles` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -109,6 +115,7 @@ CREATE TABLE `tp_rating_profiles` ( -- Table structure for table `tp_actions` -- +DROP TABLE IF EXISTS `tp_actions`; CREATE TABLE `tp_actions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -132,6 +139,7 @@ CREATE TABLE `tp_actions` ( -- Table structure for table `tp_action_timings` -- +DROP TABLE IF EXISTS `tp_action_timings`; CREATE TABLE `tp_action_timings` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -148,6 +156,7 @@ CREATE TABLE `tp_action_timings` ( -- Table structure for table `tp_action_triggers` -- +DROP TABLE IF EXISTS `tp_action_triggers`; CREATE TABLE `tp_action_triggers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, @@ -168,6 +177,7 @@ CREATE TABLE `tp_action_triggers` ( -- Table structure for table `tp_account_actions` -- +DROP TABLE IF EXISTS `tp_account_actions`; CREATE TABLE `tp_account_actions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` varchar(64) NOT NULL, diff --git a/data/storage/mysql/create_users.sql b/data/storage/mysql/create_users.sql deleted file mode 100644 index 149a38db1..000000000 --- a/data/storage/mysql/create_users.sql +++ /dev/null @@ -1,5 +0,0 @@ - --- --- Sample user creation. Replace here with your own details --- -GRANT ALL on cgrates.* TO 'cgrates'@'localhost' IDENTIFIED BY 'CGRateS.org'; diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 753760fb5..c80f57aef 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -24,6 +24,8 @@ import ( "fmt" "github.com/cgrates/cgrates/utils" "time" + "io/ioutil" + "strings" ) type SQLStorage struct { @@ -38,6 +40,25 @@ func (self *SQLStorage) Flush() (err error) { return } +func (self *SQLStorage) CreateTablesFromScript( scriptPath string) error { + fileContent, err := ioutil.ReadFile(scriptPath) + if err != nil { + return err + } + qries := strings.Split(string(fileContent), ";") // Script has normally multiple queries separate by ';' go driver does not understand this so we handle it here + for _,qry := range qries { + qry = strings.TrimSpace(qry) // Avoid empty queries + if len(qry) == 0 { + continue + } + if _, err := self.Db.Exec(qry); err != nil { + return err + } + } + return nil +} + + // Return a list with all TPids defined in the system, even if incomplete, isolated in some table. func (self *SQLStorage) GetTPIds() ([]string, error) { rows, err := self.Db.Query( diff --git a/engine/storage_sql_test.go b/engine/storage_sql_test.go new file mode 100644 index 000000000..ae35a8355 --- /dev/null +++ b/engine/storage_sql_test.go @@ -0,0 +1,81 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2013 ITsysCOM + +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 +*/ + +package engine + +import ( + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/cgrates/config" + "testing" + "path" + "fmt" + "flag" +) + +/* +README: + + Enable these tests by passing '-local' to the go test command + + Only database supported for now is MySQL. Postgres could be easily extended if needed. + + It is expected that the data folder of CGRateS exists at path /usr/share/cgrates/data. + + Prior running the tests, create database and users by running: + mysql -pyourrootpwd < /usr/share/cgrates/data/storage/mysql/create_db_with_users.sql +*/ + +const ( + SQL_SCRIPTS_PATH = "/usr/share/cgrates/data/storage" + MYSQL = "mysql" + CREATE_CDRS_TABLES_SQL = "create_cdrs_tables.sql" + CREATE_COSTDETAILS_TABLES_SQL = "create_costdetails_tables.sql" + CREATE_MEDIATOR_TABLES_SQL = "create_mediator_tables.sql" + CREATE_TARIFFPLAN_TABLES_SQL = "create_tariffplan_tables.sql" +) + +var db *MySQLStorage +var testLocal = flag.Bool("local", false, "Perform the tests only on local test environment, not by default.") // This flag will be passed here via "go test -local" args + + +func TestCreateTables(t *testing.T) { + if !*testLocal { + return + } + cgrConfig, _ := config.NewDefaultCGRConfig() + if d, err := NewMySQLStorage(cgrConfig.StorDBHost, cgrConfig.StorDBPort, cgrConfig.StorDBName, cgrConfig.StorDBUser, cgrConfig.StorDBPass); err != nil { + t.Error("Error on opening database connection: ",err) + return + } else { + db = d.(*MySQLStorage) + } + for _, scriptName := range []string{CREATE_CDRS_TABLES_SQL, CREATE_COSTDETAILS_TABLES_SQL, CREATE_MEDIATOR_TABLES_SQL, CREATE_TARIFFPLAN_TABLES_SQL} { + if err := db.CreateTablesFromScript(path.Join(SQL_SCRIPTS_PATH, MYSQL, scriptName)); err != nil { + t.Error("Error on db creation: ", err.Error()) + return // No point in going further + } + } + for _, tbl := range []string{utils.TBL_CDRS_PRIMARY, utils.TBL_CDRS_EXTRA} { + if _, err := db.Db.Query(fmt.Sprintf("SELECT 1 from %s", tbl)); err != nil { + t.Error(err.Error()) + } + } +} + + +