Add test for - Special characters are not working in postgres password(for generator)

https://github.com/go-jet/jet/issues/95
This commit is contained in:
go-jet 2021-10-22 18:08:05 +02:00
parent f2e4b8551c
commit a50d89ff9d
3 changed files with 32 additions and 10 deletions

View file

@ -9,8 +9,6 @@ import (
mysqlgen "github.com/go-jet/jet/v2/generator/mysql" mysqlgen "github.com/go-jet/jet/v2/generator/mysql"
postgresgen "github.com/go-jet/jet/v2/generator/postgres" postgresgen "github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/mysql"
"github.com/go-jet/jet/v2/postgres"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -89,10 +87,10 @@ Usage:
Example commands: Example commands:
$ jet -source=PostgreSQL -dbname=jetdb -host=localhost -port=5432 -user=jet -password=jet -schema=./dvds $ jet -source=PostgreSQL -dbname=jetdb -host=localhost -port=5432 -user=jet -password=jet -schema=dvds -path=./gen
$ jet -dsn=postgresql://jet:jet@localhost:5432/jetdb -schema=./dvds $ jet -dsn=postgresql://jet:jet@localhost:5432/jetdb -schema=dvds -path=./gen
$ jet -source=postgres -dsn="user=jet password=jet host=localhost port=5432 dbname=jetdb" -schema=./dvds $ jet -source=postgres -dsn="user=jet password=jet host=localhost port=5432 dbname=jetdb" -schema=dvds -path=./gen
$ jet -source=sqlite -dsn="file://path/to/sqlite/database/file" -schema=./dvds $ jet -source=sqlite -dsn="file://path/to/sqlite/database/file" -schema=dvds -path=./gen
`) `)
} }
@ -158,7 +156,7 @@ Example commands:
} }
err = sqlitegen.GenerateDSN(dsn, destDir) err = sqlitegen.GenerateDSN(dsn, destDir)
default: default:
printErrorAndExit("ERROR: unsupported source " + source + ". " + postgres.Dialect.Name() + " and " + mysql.Dialect.Name() + " are currently supported.") printErrorAndExit("ERROR: unknown data source " + source + ". Only postgres, mysql, mariadb and sqlite are supported.")
} }
if err != nil { if err != nil {
@ -178,5 +176,12 @@ func detectSchema(dsn string) string {
if len(match) < 2 { // not found if len(match) < 2 { // not found
return "" return ""
} }
protocol := match[0]
if protocol == "file" {
return "sqlite"
}
return match[0] return match[0]
} }

View file

@ -31,11 +31,11 @@ type DBConnection struct {
// Generate generates jet files at destination dir from database connection details // Generate generates jet files at destination dir from database connection details
func Generate(destDir string, dbConn DBConnection, genTemplate ...template.Template) (err error) { func Generate(destDir string, dbConn DBConnection, genTemplate ...template.Template) (err error) {
dsn := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=%s", dsn := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=%s",
url.QueryEscape(dbConn.User), url.PathEscape(dbConn.User),
url.QueryEscape(dbConn.Password), url.PathEscape(dbConn.Password),
dbConn.Host, dbConn.Host,
strconv.Itoa(dbConn.Port), strconv.Itoa(dbConn.Port),
url.QueryEscape(dbConn.DBName), url.PathEscape(dbConn.DBName),
dbConn.SslMode, dbConn.SslMode,
) )

View file

@ -125,6 +125,23 @@ func TestGenerator(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
func TestGeneratorSpecialCharacters(t *testing.T) {
t.SkipNow()
err := postgres.Generate(genTestDir2, postgres.DBConnection{
Host: dbconfig.PgHost,
Port: dbconfig.PgPort,
User: "!@#$%^&* () {}[];+-",
Password: "!@#$%^&* () {}[];+-",
SslMode: "disable",
Params: "",
DBName: "!@#$%^&* () {}[];+-",
SchemaName: "!@#$%^&* () {}[];+-",
})
require.NoError(t, err)
}
func assertGeneratedFiles(t *testing.T) { func assertGeneratedFiles(t *testing.T) {
// Table SQL Builder files // Table SQL Builder files
tableSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/table") tableSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/table")