Adding local tests, available via flag -local

This commit is contained in:
DanB
2013-11-18 16:29:25 +01:00
parent 4e0ccbfa19
commit 352540ab04
8 changed files with 150 additions and 33 deletions

View File

@@ -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)
);

View File

@@ -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,

View File

@@ -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';

View File

@@ -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,

View File

@@ -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,

View File

@@ -1,5 +0,0 @@
--
-- Sample user creation. Replace here with your own details
--
GRANT ALL on cgrates.* TO 'cgrates'@'localhost' IDENTIFIED BY 'CGRateS.org';

View File

@@ -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(

View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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())
}
}
}