Skip to content

Commit

Permalink
Improve DSN coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamed2 committed Nov 6, 2015
1 parent 081fcc2 commit 24ee667
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 22 deletions.
11 changes: 7 additions & 4 deletions src/dbapi/dsn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ function generate_dsn(params::DSNConnectionParameters)
end

if num_slashes < 2
throw(PostgreSQLConnectionError("Invalid connection URI: $dsn"))
throw(
PostgreSQLConnectionError("Invalid connection URI: \"$dsn_param\"")
)
end

has_params_already = findnext(dsn_param, '=', i) > 0
end_index = endof(dsn_param)

print(dsn, dsn_param)

if nparams > 0
if has_params_already
print(dsn, '&')
if last(dsn_param) != '&'
print(dsn, '&')
end
else
# we have to make sure the first part of the dsn is properly terminated
if num_slashes == 2
Expand All @@ -53,7 +56,7 @@ function generate_dsn(params::DSNConnectionParameters)
end

# leave out the dsn in dbname (always first parameter)
for i = 2:(nparams - 1)
for i = 2:nparams
print_param(dsn, params, i)

print(dsn, '&')
Expand Down
103 changes: 85 additions & 18 deletions test/dbapi/dsn.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
facts("DSN") do
ConnectionParameters = PostgreSQL.PostgreSQLDBAPI.PostgreSQLDBAPIBase.ConnectionParameters
DSNConnectionParameters = PostgreSQL.PostgreSQLDBAPI.PostgreSQLDBAPIBase.DSNConnectionParameters
generate_dsn = PostgreSQL.PostgreSQLDBAPI.DSN.generate_dsn
PostgreSQLConnectionError = PostgreSQL.PostgreSQLDBAPI.PostgreSQLDBAPIBase.PostgreSQLConnectionError

context("Invalid") do
dsn = "postgresql:/"
params = DSNConnectionParameters(["dbname"], [dsn], dsn)
@fact_throws PostgreSQLConnectionError generate_dsn(params)

dsn = ""
params = DSNConnectionParameters(["dbname"], [dsn], dsn)
@fact_throws PostgreSQLConnectionError generate_dsn(params)
end

context("Expand DBNAME") do
dsn = "postgresql://postgres@localhost:5432"
Expand All @@ -19,27 +31,82 @@ facts("DSN") do
end
end

context("No parameters") do
params = ConnectionParameters()
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "postgresql:///"
end
context("No DSN") do
context("No parameters") do
params = ConnectionParameters()
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "postgresql:///"
end

context("One parameter") do
params = ConnectionParameters(; host="localhost")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "postgresql:///?host=localhost"
end
context("One parameter") do
params = ConnectionParameters(; host="localhost")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "postgresql:///?host=localhost"
end

context("Two parameters") do
params = ConnectionParameters(; host="localhost", port="5432")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "postgresql:///?host=localhost&port=5432"
end

context("Two parameters") do
params = ConnectionParameters(; host="localhost", port="5432")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "postgresql:///?host=localhost&port=5432"
context("Three parameters") do
params = ConnectionParameters(; host="localhost", port="5432", user="postgres")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "postgresql:///?host=localhost&port=5432&user=postgres"
end
end

context("Three parameters") do
params = ConnectionParameters(; host="localhost", port="5432", user="postgres")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "postgresql:///?host=localhost&port=5432&user=postgres"
context("Has DSN") do
context("No parameters") do
context("Two slashes") do
dsn = "postgresql://postgres@localhost:5432"
params = ConnectionParameters(dsn)
generated_dsn = generate_dsn(params)
@fact generated_dsn --> dsn
end

context("Three slashes") do
dsn = "postgresql://postgres@localhost:5432/postgres?host=localhost"
params = ConnectionParameters(dsn)
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "$dsn"
end

context("Trailing &") do
dsn = "postgresql://postgres@localhost:5432/postgres?host=localhost&"
params = ConnectionParameters(dsn)
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "$dsn"
end
end

context("Trailing &") do
dsn = "postgresql://postgres@localhost:5432/postgres?host=localhost&"
params = ConnectionParameters(dsn; host="localhost")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "$(dsn)host=localhost"
end

context("One parameter") do
dsn = "postgresql://postgres@localhost:5432/postgres?host=localhost"
params = ConnectionParameters(dsn; host="localhost")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "$dsn&host=localhost"
end

context("Two parameters") do
dsn = "postgresql://postgres@localhost:5432/postgres?host=localhost"
params = ConnectionParameters(dsn; host="localhost", dbname="postgres")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "$dsn&host=localhost&dbname=postgres"
end

context("Three parameters") do
dsn = "postgresql://postgres@localhost:5432/postgres?host=localhost"
params = ConnectionParameters(dsn; host="localhost", dbname="postgres", port="5432")
generated_dsn = generate_dsn(params)
@fact generated_dsn --> "$dsn&host=localhost&dbname=postgres&port=5432"
end
end
end

0 comments on commit 24ee667

Please sign in to comment.