Diferential SQL testing for MySQL and PostgreSQL

This commit is contained in:
DanB
2014-11-08 20:13:23 +01:00
parent 1e9f1131ba
commit c773c82567
7 changed files with 71 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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