Skip to content
Snippets Groups Projects
Commit ea087660 authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #818 from TrangPham/testing-databases

Enable testing different database environments
parents 718e487d d56db2dc
No related branches found
No related tags found
No related merge requests found
...@@ -4,3 +4,4 @@ rdoc/* ...@@ -4,3 +4,4 @@ rdoc/*
.bundle .bundle
Gemfile.lock Gemfile.lock
api_keys.yml api_keys.yml
test/geocoder_test.sqlite
language: ruby language: ruby
env:
- DB=
- DB=sqlite
- DB=postgres
- DB=mysql
rvm: rvm:
- 1.9.3 - 1.9.3
- 2.0.0 - 2.0.0
......
...@@ -22,4 +22,19 @@ group :development, :test do ...@@ -22,4 +22,19 @@ group :development, :test do
end end
end end
group :test do
gem 'sqlite3', :platform => [:ruby, :mswin, :mingw]
platforms :ruby do
gem 'pg'
gem 'mysql2'
end
platforms :jruby do
gem 'jdbc-mysql'
gem 'jdbc-sqlite3'
gem 'activerecord-jdbcpostgresql-adapter'
end
end
gemspec gemspec
require 'bundler' require 'bundler'
Bundler::GemHelper.install_tasks Bundler::GemHelper.install_tasks
ACCEPTED_DB_VALUES = %w(sqlite postgres mysql)
DATABASE_CONFIG_FILE = 'test/database.yml'
def config
require 'yaml'
YAML.load(File.open(DATABASE_CONFIG_FILE))
end
namespace :db do
task :create do
if ACCEPTED_DB_VALUES.include? ENV['DB']
Rake::Task["db:#{ENV['DB']}:create"].invoke
end
end
task :drop do
if ACCEPTED_DB_VALUES.include? ENV['DB']
Rake::Task["db:#{ENV['DB']}:drop"].invoke
end
end
task :reset => [:drop, :create]
namespace :mysql do
desc 'Create the MySQL test databases'
task :create do
`mysql --user=#{config['mysql']['username']} -e "create DATABASE #{config['mysql']['database']} DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci "`
end
desc 'Drop the MySQL test databases'
task :drop do
`mysqladmin --user=#{config['mysql']['username']} -f drop #{config['mysql']['database']}`
end
end
namespace :postgres do
desc 'Create the PostgreSQL test databases'
task :create do
`createdb -E UTF8 -T template0 #{config['postgres']['database']}`
end
desc 'Drop the PostgreSQL test databases'
task :drop do
`dropdb #{config['postgres']['database']}`
end
end
namespace :sqlite do
task :drop
task :create
end
end
require 'rake/testtask' require 'rake/testtask'
desc "Use DB to test with #{ACCEPTED_DB_VALUES}, otherwise test standalone"
Rake::TestTask.new(:test) do |test| Rake::TestTask.new(:test) do |test|
Rake::Task['db:reset'].invoke if ACCEPTED_DB_VALUES.include? ENV['DB']
test.libs << 'lib' << 'test' test.libs << 'lib' << 'test'
test.pattern = 'test/unit/**/*_test.rb' test.pattern = 'test/unit/**/*_test.rb'
test.verbose = true test.verbose = true
......
...@@ -7,9 +7,13 @@ group :development, :test do ...@@ -7,9 +7,13 @@ group :development, :test do
gem 'geoip' gem 'geoip'
gem 'rubyzip' gem 'rubyzip'
gem 'rails' gem 'rails'
gem 'sqlite3'
gem 'pg'
gem 'mysql2'
# i18n gem >=0.7.0 does not work with Ruby 1.9.2 # i18n gem >=0.7.0 does not work with Ruby 1.9.2
gem 'i18n', '0.6.1' gem 'i18n', '0.6.1'
gem 'test-unit' # needed for Ruby >=2.2.0
gem 'debugger' gem 'debugger'
......
# test/database.yml
sqlite:
adapter: sqlite3
database: ":memory:"
timeout: 500
mysql:
adapter: mysql2
database: geocoder_test
username: travis
encoding: utf8
postgres:
adapter: postgresql
database: geocoder_test
username:
# CreateTestSchema creates the tables used in test_helper.rb
class CreateTestSchema < ActiveRecord::Migration
def self.up
[
:places,
:place_reverse_geocodeds
].each do |table|
create_table table do |t|
t.column :name, :string
t.column :address, :string
t.column :latitude, :decimal, :precision => 16, :scale => 6
t.column :longitude, :decimal, :precision => 16, :scale => 6
end
end
[
:place_with_custom_lookup_procs,
:place_with_custom_lookups,
:place_reverse_geocoded_with_custom_lookups
].each do |table|
create_table table do |t|
t.column :name, :string
t.column :address, :string
t.column :latitude, :decimal, :precision => 16, :scale => 6
t.column :longitude, :decimal, :precision => 16, :scale => 6
t.column :result_class, :string
end
end
create_table :place_with_forward_and_reverse_geocodings do |t|
t.column :name, :string
t.column :location, :string
t.column :lat, :decimal, :precision => 16, :scale => 6
t.column :lon, :decimal, :precision => 16, :scale => 6
t.column :address, :string
end
create_table :place_reverse_geocoded_with_custom_results_handlings do |t|
t.column :name, :string
t.column :address, :string
t.column :latitude, :decimal, :precision => 16, :scale => 6
t.column :longitude, :decimal, :precision => 16, :scale => 6
t.column :country, :string
end
create_table :place_with_custom_results_handlings do |t|
t.column :name, :string
t.column :address, :string
t.column :latitude, :decimal, :precision => 16, :scale => 6
t.column :longitude, :decimal, :precision => 16, :scale => 6
t.column :coords_string, :string
end
end
end
...@@ -4,58 +4,73 @@ require 'test/unit' ...@@ -4,58 +4,73 @@ require 'test/unit'
$LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
class MysqlConnection require 'yaml'
def adapter_name configs = YAML.load_file('test/database.yml')
"mysql"
end
end
## if configs.keys.include? ENV['DB']
# Simulate enough of ActiveRecord::Base that objects can be used for testing. require 'active_record'
#
module ActiveRecord
class Base
def initialize # Establish a database connection
@attributes = {} ActiveRecord::Base.configurations = configs
end
def read_attribute(attr_name) db_name = ENV['DB']
@attributes[attr_name.to_sym] ActiveRecord::Base.establish_connection(db_name)
end ActiveRecord::Base.default_timezone = :utc
def write_attribute(attr_name, value) ActiveRecord::Migrator.migrate('test/db/migrate', nil)
@attributes[attr_name.to_sym] = value else
class MysqlConnection
def adapter_name
"mysql"
end end
end
def update_attribute(attr_name, value) ##
write_attribute(attr_name.to_sym, value) # Simulate enough of ActiveRecord::Base that objects can be used for testing.
end #
module ActiveRecord
class Base
def self.scope(*args); end def initialize
@attributes = {}
end
def self.connection def read_attribute(attr_name)
MysqlConnection.new @attributes[attr_name.to_sym]
end end
def method_missing(name, *args, &block) def write_attribute(attr_name, value)
if name.to_s[-1..-1] == "=" @attributes[attr_name.to_sym] = value
write_attribute name.to_s[0...-1], *args
else
read_attribute name
end end
end
class << self def update_attribute(attr_name, value)
def table_name write_attribute(attr_name.to_sym, value)
'test_table_name'
end end
def primary_key def self.scope(*args); end
:id
def self.connection
MysqlConnection.new
end end
end
def method_missing(name, *args, &block)
if name.to_s[-1..-1] == "="
write_attribute name.to_s[0...-1], *args
else
read_attribute name
end
end
class << self
def table_name
'test_table_name'
end
def primary_key
:id
end
end
end
end end
end end
...@@ -65,7 +80,7 @@ end ...@@ -65,7 +80,7 @@ end
# Require Geocoder after ActiveRecord simulator. # Require Geocoder after ActiveRecord simulator.
require 'geocoder' require 'geocoder'
require "geocoder/lookups/base" require 'geocoder/lookups/base'
# and initialize Railtie manually (since Rails::Railtie doesn't exist) # and initialize Railtie manually (since Rails::Railtie doesn't exist)
Geocoder::Railtie.insert Geocoder::Railtie.insert
......
...@@ -5,16 +5,23 @@ require 'test_helper' ...@@ -5,16 +5,23 @@ require 'test_helper'
class NearTest < GeocoderTestCase class NearTest < GeocoderTestCase
def test_near_scope_options_without_sqlite_includes_bounding_box_condition def test_near_scope_options_without_sqlite_includes_bounding_box_condition
omit("Not applicable to SQLite") if ENV['DB'] == 'sqlite'
result = PlaceWithCustomResultsHandling.send(:near_scope_options, 1.0, 2.0, 5) result = PlaceWithCustomResultsHandling.send(:near_scope_options, 1.0, 2.0, 5)
assert_match(/test_table_name.latitude BETWEEN 0.9276\d* AND 1.0723\d* AND test_table_name.longitude BETWEEN 1.9276\d* AND 2.0723\d* AND /, result[:conditions][0]) table_name = PlaceWithCustomResultsHandling.table_name
assert_match(/#{table_name}.latitude BETWEEN 0.9276\d* AND 1.0723\d* AND #{table_name}.longitude BETWEEN 1.9276\d* AND 2.0723\d* AND /, result[:conditions][0])
end end
def test_near_scope_options_without_sqlite_includes_radius_condition def test_near_scope_options_without_sqlite_includes_radius_condition
omit("Not applicable to SQLite") if ENV['DB'] == 'sqlite'
result = Place.send(:near_scope_options, 1.0, 2.0, 5) result = Place.send(:near_scope_options, 1.0, 2.0, 5)
assert_match(/BETWEEN \? AND \?$/, result[:conditions][0]) assert_match(/BETWEEN \? AND \?$/, result[:conditions][0])
end end
def test_near_scope_options_without_sqlite_includes_radius_default_min_radius def test_near_scope_options_without_sqlite_includes_radius_default_min_radius
omit("Not applicable to SQLite") if ENV['DB'] == 'sqlite'
result = Place.send(:near_scope_options, 1.0, 2.0, 5) result = Place.send(:near_scope_options, 1.0, 2.0, 5)
assert_equal(0, result[:conditions][1]) assert_equal(0, result[:conditions][1])
...@@ -22,6 +29,8 @@ class NearTest < GeocoderTestCase ...@@ -22,6 +29,8 @@ class NearTest < GeocoderTestCase
end end
def test_near_scope_options_without_sqlite_includes_radius_custom_min_radius def test_near_scope_options_without_sqlite_includes_radius_custom_min_radius
omit("Not applicable to SQLite") if ENV['DB'] == 'sqlite'
result = Place.send(:near_scope_options, 1.0, 2.0, 5, :min_radius => 3) result = Place.send(:near_scope_options, 1.0, 2.0, 5, :min_radius => 3)
assert_equal(3, result[:conditions][1]) assert_equal(3, result[:conditions][1])
...@@ -29,6 +38,8 @@ class NearTest < GeocoderTestCase ...@@ -29,6 +38,8 @@ class NearTest < GeocoderTestCase
end end
def test_near_scope_options_without_sqlite_includes_radius_bogus_min_radius def test_near_scope_options_without_sqlite_includes_radius_bogus_min_radius
omit("Not applicable to SQLite") if ENV['DB'] == 'sqlite'
result = Place.send(:near_scope_options, 1.0, 2.0, 5, :min_radius => 'bogus') result = Place.send(:near_scope_options, 1.0, 2.0, 5, :min_radius => 'bogus')
assert_equal(0, result[:conditions][1]) assert_equal(0, result[:conditions][1])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment