Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v4.2.x] Fix connection string parsing #23

Open
wants to merge 2 commits into
base: v4.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,73 @@ language: ruby
cache: bundler
matrix:
include:
- rvm: 2.3.1
- rvm: 2.3.7
env:
- DB=mysql
- CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;'
addons:
mysql: "5.5"
apt:
packages:
- unixodbc
- unixodbc-dev
- libmyodbc
- mysql-client
- rvm: 2.3.1
- mysql-client-5.6
services:
- mysql
- rvm: 2.3.7
env:
- DB=postgresql
- CONN_STR='DRIVER={PostgreSQL ANSI};SERVER=localhost;PORT=5432;DATABASE=odbc_test;UID=postgres;'
addons:
postgresql: "9.1"
postgresql: "9.2"
apt:
packages:
- unixodbc
- unixodbc-dev
- odbc-postgresql
- rvm: 2.4.4
env:
- DB=mysql
- CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;'
addons:
apt:
packages:
- unixodbc
- unixodbc-dev
- libmyodbc
- mysql-client-5.6
services:
- mysql
- rvm: 2.4.4
env:
- DB=postgresql
- CONN_STR='DRIVER={PostgreSQL ANSI};SERVER=localhost;PORT=5432;DATABASE=odbc_test;UID=postgres;'
addons:
postgresql: "9.2"
apt:
packages:
- unixodbc
- unixodbc-dev
- odbc-postgresql
- rvm: 2.5.1
env:
- DB=mysql
- CONN_STR='DRIVER=MySQL;SERVER=localhost;DATABASE=odbc_test;USER=root;PASSWORD=;'
addons:
apt:
packages:
- unixodbc
- unixodbc-dev
- libmyodbc
- mysql-client-5.6
services:
- mysql
- rvm: 2.5.1
env:
- DB=postgresql
- CONN_STR='DRIVER={PostgreSQL ANSI};SERVER=localhost;PORT=5432;DATABASE=odbc_test;UID=postgres;'
addons:
postgresql: "9.2"
apt:
packages:
- unixodbc
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/connection_adapters/odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def odbc_conn_str_connection(config)
driver.attrs = {}

connstr_keyval_pairs.each do |pair|
keyval = pair.split('=')
keyval = pair.split('=', 2)
driver.attrs[keyval[0]] = keyval[1] if keyval.length == 2
end

Expand Down
84 changes: 84 additions & 0 deletions test/connection_string_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'test_helper'

class ConnectionStringTest < Minitest::Test
def setup
end

def teardown
end

# Make sure that the connection string is parsed properly when it has an equals sign
def test_odbc_conn_str_connection_with_equals
conn_str = "Foo=Bar;Foo2=Something=with=equals"

odbc_driver_attrs = {}
odbc_driver_instance_mock = Minitest::Mock.new
odbc_database_instance_mock = Minitest::Mock.new
odbc_connection_instance_mock = Minitest::Mock.new

# Setup ODBC::Driver instance mocks
odbc_driver_instance_mock.expect(:name=, nil, ['odbc'])
odbc_driver_instance_mock.expect(:attrs=, nil, [{}])
odbc_driver_instance_mock.expect(:attrs, odbc_driver_attrs)
odbc_driver_instance_mock.expect(:attrs, odbc_driver_attrs) # must be called twice

# Setup ODBC::Database instance mocks
odbc_database_instance_mock.expect(:drvconnect, odbc_connection_instance_mock, [odbc_driver_instance_mock])

# Stub ODBC::Driver.new
ODBC::Driver.stub :new, odbc_driver_instance_mock do
# Stub ODBC::Database.new
ODBC::Database.stub :new, odbc_database_instance_mock do
# Run under our stubs/mocks
ActiveRecord::Base.__send__(:odbc_conn_str_connection, conn_str: conn_str)
end
end

# Assert we set up the driver properly
assert_equal odbc_driver_attrs['Foo'], 'Bar'
assert_equal odbc_driver_attrs['Foo2'], 'Something=with=equals'

# make sure we called the methods we expected
odbc_driver_instance_mock.verify
odbc_database_instance_mock.verify
odbc_connection_instance_mock.verify
end

# Make sure that the connection string is parsed properly when it doesn't have an
# equals sign
def test_odbc_conn_str_connection_without_equals
conn_str = "Foo=Bar;Foo2=Something without equals"

odbc_driver_attrs = {}
odbc_driver_instance_mock = Minitest::Mock.new
odbc_database_instance_mock = Minitest::Mock.new
odbc_connection_instance_mock = Minitest::Mock.new

# Setup ODBC::Driver instance mocks
odbc_driver_instance_mock.expect(:name=, nil, ['odbc'])
odbc_driver_instance_mock.expect(:attrs=, nil, [{}])
odbc_driver_instance_mock.expect(:attrs, odbc_driver_attrs)
odbc_driver_instance_mock.expect(:attrs, odbc_driver_attrs) # must be called twice

# Setup ODBC::Database instance mocks
odbc_database_instance_mock.expect(:drvconnect, odbc_connection_instance_mock, [odbc_driver_instance_mock])

# Stub ODBC::Driver.new
ODBC::Driver.stub :new, odbc_driver_instance_mock do
# Stub ODBC::Database.new
ODBC::Database.stub :new, odbc_database_instance_mock do
# Run under our stubs/mocks
ActiveRecord::Base.__send__(:odbc_conn_str_connection, conn_str: conn_str)
end
end

# Assert we set up the driver properly
assert_equal odbc_driver_attrs['Foo'], 'Bar'
assert_equal odbc_driver_attrs['Foo2'], 'Something without equals'

# make sure we called the methods we expected
odbc_driver_instance_mock.verify
odbc_database_instance_mock.verify
odbc_connection_instance_mock.verify
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'odbc_adapter'

require 'minitest/autorun'
require 'minitest/mock'
require 'pry'

options = { adapter: 'odbc' }
Expand Down