Skip to content

Commit

Permalink
Support testing in sqlite by default, rather than forcing the user
Browse files Browse the repository at this point in the history
to configure a database.yml.
  • Loading branch information
mperham committed Nov 22, 2008
1 parent 01a675c commit 64b7cc9
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 31 deletions.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

42 changes: 28 additions & 14 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'rubygems'
require 'echoe'

#gem 'rails', '=2.0.2'

require File.dirname(__FILE__) << "/lib/data_fabric/version"

Echoe.new 'data_fabric' do |p|
Expand Down Expand Up @@ -33,7 +35,7 @@ def load_database_yml
if !File.exist?(filename)
STDERR.puts "\n*** ERROR ***:\n" <<
"You must have a 'test/database.yml' file in order to create the test database. " <<
"An example is provided in 'test/database.yml.example'.\n\n"
"An example is provided in 'test/database.yml.mysql'.\n\n"
exit 1
end
YAML::load(ERB.new(IO.read(filename)).result)
Expand All @@ -42,12 +44,12 @@ end
def setup_connection
require 'active_record'
ActiveRecord::Base.configurations = load_database_yml
ActiveRecord::Base.establish_connection('fiveruns_city_austin_test_master')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::DEBUG
end

def using_connection(database_identifier, &block)
ActiveRecord::Base.establish_connection(database_identifier)
ActiveRecord::Base.connection.instance_eval(&block)
end

Expand All @@ -56,18 +58,30 @@ def setup(create = false)

ActiveRecord::Base.configurations.each_pair do |identifier, config|
using_connection(identifier) do
db_name = config['database']
if create
execute "drop database if exists #{db_name}"
execute "create database #{db_name}"
end
execute "use #{db_name}"
execute "drop table if exists the_whole_burritos"
execute "drop table if exists enchiladas"
execute "create table enchiladas (id integer not null auto_increment, name varchar(30) not null, primary key(id))"
execute "insert into enchiladas (id, name) values (1, '#{db_name}')"
execute "create table the_whole_burritos (id integer not null auto_increment, name varchar(30) not null, primary key(id))"
execute "insert into the_whole_burritos (id, name) values (1, '#{db_name}')"
send("create_#{config['adapter']}", create, config['database'])
end
end
end

def create_sqlite3(create, db_name)
execute "drop table if exists the_whole_burritos"
execute "drop table if exists enchiladas"
execute "create table enchiladas (id integer not null primary key, name varchar(30) not null)"
execute "insert into enchiladas (id, name) values (1, '#{db_name}')"
execute "create table the_whole_burritos (id integer not null primary key, name varchar(30) not null)"
execute "insert into the_whole_burritos (id, name) values (1, '#{db_name}')"
end

def create_mysql(create, db_name)
if create
execute "drop database if exists #{db_name}"
execute "create database #{db_name}"
end
execute "use #{db_name}"
execute "drop table if exists the_whole_burritos"
execute "drop table if exists enchiladas"
execute "create table enchiladas (id integer not null auto_increment, name varchar(30) not null, primary key(id))"
execute "insert into enchiladas (id, name) values (1, '#{db_name}')"
execute "create table the_whole_burritos (id integer not null auto_increment, name varchar(30) not null, primary key(id))"
execute "insert into the_whole_burritos (id, name) values (1, '#{db_name}')"
end
12 changes: 5 additions & 7 deletions TESTING.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ data_fabric has two layers of tests: unit tests and integration tests.
== Running the Unit Tests

The unit tests test both with and without an actual database. test/database_test.rb
tests against a MySQL database. The other unit tests mock AR so no actual database is
required. You must customize test/database.yml to point to your MySQL database.
"rake pretest" will set up MySQL to run the tests but you will need to customize
the connection settings in the Rakefile pretest task.
tests against a database. The other unit tests mock AR so no actual database is
required. You can use the standard test/database.yml which tests against SQLite3
or customize the provided test/database.yml.mysql. The "rake create_db" task will
set up the necessary databases and tables.


== Running the Integration Tests
Expand All @@ -27,6 +27,4 @@ then be able to run the example application's tests.

== Submitting Bugs

If you think you've found a problem with data_fabric, please write a unit or integration test
which reproduces the problem. This should make tracking down and fixing the bug relatively
easy and provides a regression test for future releases.
If you think you've found a problem with data_fabric, contact me at mperham AT gmail.com.
8 changes: 4 additions & 4 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
require 'flexmock/test_unit'

class PrefixModel < ActiveRecord::Base
connection_topology :prefix => 'prefix'
data_fabric :prefix => 'prefix'
end

class ShardModel < ActiveRecord::Base
connection_topology :shard_by => :city
data_fabric :shard_by => :city
end

class TheWholeEnchilada < ActiveRecord::Base
connection_topology :prefix => 'fiveruns', :replicated => true, :shard_by => :city
data_fabric :prefix => 'fiveruns', :replicated => true, :shard_by => :city
end

class AdapterMock < ActiveRecord::ConnectionAdapters::AbstractAdapter
Expand Down Expand Up @@ -43,7 +43,7 @@ def method_missing(name, *args)
class ConnectionTest < Test::Unit::TestCase

def test_should_install_into_arbase
assert PrefixModel.methods.include?('connection_topology')
assert PrefixModel.methods.include?('data_fabric')
end

def test_prefix_connection_name
Expand Down
24 changes: 24 additions & 0 deletions test/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The unit tests make use of the data populated in these databases.
#
# Notes:
# - The database identifiers (e.g. "fiveruns_city_austin_test_master") MUST NOT
# be changed! Everything else may be changed.
# - The user defined for "fiveruns_city_austin_test_master" MUST have the
# privilege to create and drop databases and tables.


fiveruns_city_austin_test_master:
adapter: sqlite3
database: test/vr_austin_master.db

fiveruns_city_austin_test_slave:
adapter: sqlite3
database: test/vr_austin_slave.db

fiveruns_city_dallas_test_master:
adapter: sqlite3
database: test/vr_dallas_master.db

fiveruns_city_dallas_test_slave:
adapter: sqlite3
database: test/vr_dallas_slave.db
File renamed without changes.
8 changes: 4 additions & 4 deletions test/database_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'erb'

class TheWholeBurrito < ActiveRecord::Base
connection_topology :prefix => 'fiveruns', :replicated => true, :shard_by => :city
data_fabric :prefix => 'fiveruns', :replicated => true, :shard_by => :city
end

class DatabaseTest < Test::Unit::TestCase
Expand All @@ -18,19 +18,19 @@ def test_live_burrito

# Should use the slave
burrito = TheWholeBurrito.find(1)
assert_equal 'vr_dallas_slave', burrito.name
assert_match 'vr_dallas_slave', burrito.name

# Should use the master
burrito.reload
assert_equal 'vr_dallas_master', burrito.name
assert_match 'vr_dallas_master', burrito.name

# ...but immediately set it back to default to the slave
assert_equal 'fiveruns_city_dallas_test_slave', TheWholeBurrito.connection.connection_name

# Should use the master
TheWholeBurrito.transaction do
burrito = TheWholeBurrito.find(1)
assert_equal 'vr_dallas_master', burrito.name
assert_match 'vr_dallas_master', burrito.name
burrito.save!
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/thread_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_concurrency_not_allowed
class ThreadedEnchilada < ActiveRecord::Base
self.allow_concurrency = true
set_table_name :enchiladas
connection_topology :prefix => 'fiveruns', :replicated => true, :shard_by => :city
data_fabric :prefix => 'fiveruns', :replicated => true, :shard_by => :city
end
}
end
Expand Down

0 comments on commit 64b7cc9

Please sign in to comment.