Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support testing in sqlite by default, rather than forcing the user

to configure a database.yml.
  • Loading branch information...
commit 64b7cc9b7d24b2bdb829d925411f21701712c35f 1 parent 01a675c
@mperham mperham authored
View
1  .gitignore
@@ -1 +0,0 @@
-test/database.yml
View
42 Rakefile
@@ -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|
@@ -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)
@@ -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
@@ -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
View
12 TESTING.rdoc
@@ -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
@@ -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.
View
8 test/connection_test.rb
@@ -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
@@ -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
View
24 test/database.yml
@@ -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
View
0  test/database.yml.example → test/database.yml.mysql
File renamed without changes
View
8 test/database_test.rb
@@ -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
@@ -18,11 +18,11 @@ 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
@@ -30,7 +30,7 @@ def test_live_burrito
# 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
View
2  test/thread_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.