From c773c8256791234390e90532eef8be9185532471 Mon Sep 17 00:00:00 2001 From: DanB Date: Sat, 8 Nov 2014 20:13:23 +0100 Subject: [PATCH] Diferential SQL testing for MySQL and PostgreSQL --- data/storage/postgres/create_db_with_users.sh | 2 +- .../postgres/create_tariffplan_tables.sql | 4 +- data/storage/postgres/setup_cgr_db.sh | 7 ++- engine/loader_csv.go | 2 +- ...al_test.go => storage_mysql_local_test.go} | 19 +------ engine/storage_postgres.go | 6 +- engine/storage_psql_local_test.go | 57 +++++++++++++++++++ 7 files changed, 71 insertions(+), 26 deletions(-) rename engine/{storage_sql_local_test.go => storage_mysql_local_test.go} (98%) create mode 100644 engine/storage_psql_local_test.go diff --git a/data/storage/postgres/create_db_with_users.sh b/data/storage/postgres/create_db_with_users.sh index 1f7ff45b1..199b84dcf 100755 --- a/data/storage/postgres/create_db_with_users.sh +++ b/data/storage/postgres/create_db_with_users.sh @@ -5,5 +5,5 @@ sudo -u postgres dropdb -e cgrates sudo -u postgres dropuser -e cgrates -sudo -u postgres createuser -S -D -R -e cgrates +sudo -u postgres psql -c "CREATE USER cgrates password 'CGRateS.org';" sudo -u postgres createdb -e -O cgrates cgrates diff --git a/data/storage/postgres/create_tariffplan_tables.sql b/data/storage/postgres/create_tariffplan_tables.sql index f73fb4fa6..c770fe196 100644 --- a/data/storage/postgres/create_tariffplan_tables.sql +++ b/data/storage/postgres/create_tariffplan_tables.sql @@ -41,7 +41,7 @@ CREATE TABLE tp_rates ( rate_unit VARCHAR(16) NOT NULL, rate_increment VARCHAR(16) NOT NULL, group_interval_start VARCHAR(16) NOT NULL, - UNIQUE (tpid, tag, group_interval_start), + UNIQUE (tpid, tag, group_interval_start) ); -- @@ -56,7 +56,7 @@ CREATE TABLE tp_destination_rates ( destinations_tag VARCHAR(64) NOT NULL, rates_tag VARCHAR(64) NOT NULL, rounding_method VARCHAR(255) NOT NULL, - rounding_decimals SMALLINT(4) NOT NULL, + rounding_decimals SMALLINT NOT NULL, UNIQUE (tpid, tag , destinations_tag) ); diff --git a/data/storage/postgres/setup_cgr_db.sh b/data/storage/postgres/setup_cgr_db.sh index c3ccce9ff..ad1d7324a 100755 --- a/data/storage/postgres/setup_cgr_db.sh +++ b/data/storage/postgres/setup_cgr_db.sh @@ -1,9 +1,10 @@ #! /usr/bin/env sh +export PGPASSWORD="CGRateS.org" user=$1 if [ -z "$1" ]; then - user="postgres" + user="cgrates" fi host=$2 @@ -13,9 +14,9 @@ fi ./create_db_with_users.sh -sudo -u $user psql -d cgrates -f create_cdrs_tables.sql +psql -U $user -h $host -d cgrates -f create_cdrs_tables.sql cdrt=$? -sudo -u $user psql -d cgrates -f create_tariffplan_tables.sql +psql -U $user -h $host -d cgrates -f create_tariffplan_tables.sql tpt=$? if [ $cdrt = 0 ] && [ $tpt = 0 ]; then diff --git a/engine/loader_csv.go b/engine/loader_csv.go index 9d24c04e9..25bf54319 100644 --- a/engine/loader_csv.go +++ b/engine/loader_csv.go @@ -420,7 +420,7 @@ func (csvr *CSVReader) LoadRates() (err error) { func (csvr *CSVReader) LoadDestinationRates() (err error) { csvReader, fp, err := csvr.readerFunc(csvr.destinationratesFn, csvr.sep, utils.DESTINATION_RATES_NRCOLS) if err != nil { - log.Print("Could not load rates file: ", err) + log.Print("Could not load destination_rates file: ", err) // allow writing of the other values return nil } diff --git a/engine/storage_sql_local_test.go b/engine/storage_mysql_local_test.go similarity index 98% rename from engine/storage_sql_local_test.go rename to engine/storage_mysql_local_test.go index b18726b1a..5140e6a14 100644 --- a/engine/storage_sql_local_test.go +++ b/engine/storage_mysql_local_test.go @@ -1,6 +1,6 @@ /* Real-time Charging System for Telecom & ISP environments -Copyright (C) 2012-2014 ITsysCOM GmbH +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 @@ -29,22 +29,9 @@ import ( "github.com/cgrates/cgrates/utils" ) -/* -README: - - Enable these tests by passing '-local' to the go test command - - Only database supported for now is mysqlDb. 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: - mysqlDb -pyourrootpwd < /usr/share/cgrates/storage/mysqlDb/create_mysqlDb_with_users.sql -*/ - var mysqlDb *MySQLStorage -func TestCreateTables(t *testing.T) { +func TestMySQLCreateTables(t *testing.T) { if !*testLocal { return } @@ -57,7 +44,7 @@ func TestCreateTables(t *testing.T) { mysqlDb = d.(*MySQLStorage) } for _, scriptName := range []string{CREATE_CDRS_TABLES_SQL, CREATE_TARIFFPLAN_TABLES_SQL} { - if err := mysqlDb.CreateTablesFromScript(path.Join(*dataDir, "storage", "mysql", scriptName)); err != nil { + if err := mysqlDb.CreateTablesFromScript(path.Join(*dataDir, "storage", utils.MYSQL, scriptName)); err != nil { t.Error("Error on mysqlDb creation: ", err.Error()) return // No point in going further } diff --git a/engine/storage_postgres.go b/engine/storage_postgres.go index 2bd8a3b39..685230124 100644 --- a/engine/storage_postgres.go +++ b/engine/storage_postgres.go @@ -32,7 +32,7 @@ type PostgresStorage struct { *SQLStorage } -func NewPostgresStorage(host, port, name, user, password string) (Storage, error) { +func NewPostgresStorage(host, port, name, user, password string, maxConn, maxIdleConn int) (Storage, error) { connectString := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=disable", host, port, name, user, password) db, err := gorm.Open("postgres", connectString) if err != nil { @@ -42,8 +42,8 @@ func NewPostgresStorage(host, port, name, user, password string) (Storage, error if err != nil { return nil, err } - db.DB().SetMaxIdleConns(10) - db.DB().SetMaxOpenConns(100) + db.DB().SetMaxIdleConns(maxIdleConn) + db.DB().SetMaxOpenConns(maxConn) //db.LogMode(true) return &PostgresStorage{&SQLStorage{Db: db.DB(), db: db}}, nil diff --git a/engine/storage_psql_local_test.go b/engine/storage_psql_local_test.go new file mode 100644 index 000000000..f405c14e7 --- /dev/null +++ b/engine/storage_psql_local_test.go @@ -0,0 +1,57 @@ +/* +Real-time Charging System 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 +*/ + +package engine + +import ( + "fmt" + "path" + //"reflect" + "testing" + //"time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" +) + +var psqlDb *PostgresStorage + +func TestPSQLCreateTables(t *testing.T) { + if !*testLocal { + return + } + cgrConfig, _ := config.NewDefaultCGRConfig() + if d, err := NewPostgresStorage("localhost", "5432", cgrConfig.StorDBName, cgrConfig.StorDBUser, cgrConfig.StorDBPass, + cgrConfig.StorDBMaxOpenConns, cgrConfig.StorDBMaxIdleConns); err != nil { + t.Error("Error on opening database connection: ", err) + return + } else { + psqlDb = d.(*PostgresStorage) + } + for _, scriptName := range []string{CREATE_CDRS_TABLES_SQL, CREATE_TARIFFPLAN_TABLES_SQL} { + if err := psqlDb.CreateTablesFromScript(path.Join(*dataDir, "storage", utils.POSTGRES, scriptName)); err != nil { + t.Error("Error on psqlDb creation: ", err.Error()) + return // No point in going further + } + } + for _, tbl := range []string{utils.TBL_CDRS_PRIMARY, utils.TBL_CDRS_EXTRA} { + if _, err := psqlDb.Db.Query(fmt.Sprintf("SELECT 1 from %s", tbl)); err != nil { + t.Error(err.Error()) + } + } +}