Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reduce File.dirname cruft: invoke individual tests with -Ilib:test pa…

…rameter.

Stolen assert_queries from Rails, let's put it to good use in finder_test.rb
  • Loading branch information...
commit 1d8a06568ac0259ed1371d13e866d5fab0623fdd 1 parent 64d6ef1
@mislav authored
View
2  Rakefile
@@ -9,6 +9,7 @@ desc 'Test the will_paginate plugin.'
Rake::TestTask.new(:test) do |t|
t.pattern = 'test/**/*_test.rb'
t.verbose = true
+ t.libs << 'test'
end
# I want to specify environment variables at call time
@@ -27,6 +28,7 @@ for configuration in %w( sqlite3 mysql postgres )
t.pattern = 'test/finder_test.rb'
t.verbose = true
t.env = { 'DB' => configuration }
+ t.libs << 'test'
end
end
View
2  test/array_pagination_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/helper'
+require 'helper'
require 'will_paginate/array'
class ArrayPaginationTest < Test::Unit::TestCase
View
2  test/boot.rb
@@ -19,5 +19,3 @@
gem 'activerecord'
end
end
-
-$:.unshift "#{plugin_root}/lib"
View
5 test/console
@@ -1,9 +1,8 @@
#!/usr/bin/env ruby
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
libs = []
-dirname = File.dirname(__FILE__)
libs << 'irb/completion'
-libs << File.join(dirname, 'lib', 'load_fixtures')
+libs << File.join('lib', 'load_fixtures')
-exec "#{irb}#{libs.map{ |l| " -r #{l}" }.join} --simple-prompt"
+exec "#{irb} -Ilib:test#{libs.map{ |l| " -r #{l}" }.join} --simple-prompt"
View
26 test/finder_test.rb
@@ -1,5 +1,5 @@
-require File.dirname(__FILE__) + '/helper'
-require File.dirname(__FILE__) + '/lib/activerecord_test_case'
+require 'helper'
+require 'lib/activerecord_test_case'
require 'will_paginate'
WillPaginate.enable_activerecord
@@ -12,18 +12,18 @@ def test_new_methods_presence
end
def test_simple_paginate
- entries = Topic.paginate :page => nil
- assert_equal 1, entries.current_page
- assert_nil entries.previous_page
- assert_nil entries.next_page
- assert_equal 1, entries.total_pages
- assert_equal 4, entries.size
+ assert_queries(1) do
+ entries = Topic.paginate :page => nil
+ assert_equal 1, entries.current_page
+ assert_equal 1, entries.total_pages
+ assert_equal 4, entries.size
+ end
- entries = Topic.paginate :page => 2
- assert_equal 2, entries.current_page
- assert_equal 1, entries.previous_page
- assert_equal 1, entries.total_pages
- assert entries.empty?
+ assert_queries(2) do
+ entries = Topic.paginate :page => 2
+ assert_equal 1, entries.total_pages
+ assert entries.empty?
+ end
end
def test_parameter_api
View
2  test/helper.rb
@@ -4,7 +4,7 @@
# gem install redgreen for colored test output
begin require 'redgreen'; rescue LoadError; end
-require File.join(File.dirname(__FILE__), 'boot') unless defined?(ActiveRecord)
+require 'boot' unless defined?(ActiveRecord)
class Test::Unit::TestCase
protected
View
15 test/lib/activerecord_test_case.rb
@@ -1,4 +1,4 @@
-require File.join(File.dirname(__FILE__), 'activerecord_test_connector')
+require 'lib/activerecord_test_connector'
class ActiveRecordTestCase < Test::Unit::TestCase
# Set our fixture path
@@ -18,6 +18,19 @@ def run(*args)
# Default so Test::Unit::TestCase doesn't complain
def test_truth
end
+
+ protected
+
+ def assert_queries(num = 1)
+ $query_count = 0
+ yield
+ ensure
+ assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
+ end
+
+ def assert_no_queries(&block)
+ assert_queries(0, &block)
+ end
end
ActiveRecordTestConnector.setup
View
25 test/lib/activerecord_test_connector.rb
@@ -6,6 +6,8 @@ class ActiveRecordTestConnector
cattr_accessor :able_to_connect
cattr_accessor :connected
+ FIXTURES_PATH = File.join(File.dirname(__FILE__), '..', 'fixtures')
+
# Set our defaults
self.connected = false
self.able_to_connect = true
@@ -14,8 +16,7 @@ def self.setup
unless self.connected || !self.able_to_connect
setup_connection
load_schema
- # require_fixture_models
- Dependencies.load_paths.unshift(File.dirname(__FILE__) + "/../fixtures")
+ Dependencies.load_paths.unshift FIXTURES_PATH
self.connected = true
end
rescue Exception => e # errors from ActiveRecord setup
@@ -38,7 +39,7 @@ def self.setup_connection
ActiveRecord::Base.establish_connection(configuration)
ActiveRecord::Base.configurations = { db => configuration }
- ActiveRecord::Base.connection
+ add_counting ActiveRecord::Base.connection
unless Object.const_defined?(:QUOTED_TYPE)
Object.send :const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')
@@ -48,13 +49,21 @@ def self.setup_connection
def self.load_schema
ActiveRecord::Base.silence do
ActiveRecord::Migration.verbose = false
- load File.dirname(__FILE__) + "/../fixtures/schema.rb"
+ load File.join(FIXTURES_PATH, 'schema.rb')
end
end
- def self.require_fixture_models
- models = Dir.glob(File.dirname(__FILE__) + "/../fixtures/*.rb")
- models = (models.grep(/user.rb/) + models).uniq
- models.each { |f| require f }
+ def self.add_counting(conn)
+ class << conn
+ IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/]
+
+ def execute_with_counting(sql, name = nil, &block)
+ $query_count ||= 0
+ $query_count += 1 unless IGNORED_SQL.any? { |r| sql =~ r }
+ execute_without_counting(sql, name, &block)
+ end
+
+ alias_method_chain :execute, :counting
+ end
end
end
View
8 test/lib/load_fixtures.rb
@@ -1,13 +1,11 @@
-dirname = File.dirname(__FILE__)
-require File.join(dirname, '..', 'boot')
-require File.join(dirname, 'activerecord_test_connector')
+require 'boot'
+require 'lib/activerecord_test_connector'
# setup the connection
ActiveRecordTestConnector.setup
# load all fixtures
-fixture_path = File.join(dirname, '..', 'fixtures')
-Fixtures.create_fixtures(fixture_path, ActiveRecord::Base.connection.tables)
+Fixtures.create_fixtures(ActiveRecordTestConnector::FIXTURES_PATH, ActiveRecord::Base.connection.tables)
require 'will_paginate'
WillPaginate.enable_activerecord
View
4 test/pagination_test.rb
@@ -1,6 +1,6 @@
-require File.dirname(__FILE__) + '/helper'
+require 'helper'
require 'action_controller'
-require File.dirname(__FILE__) + '/lib/html_inner_text'
+require 'lib/html_inner_text'
ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
Please sign in to comment.
Something went wrong with that request. Please try again.