Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use factory girl, lose shoulda

  • Loading branch information...
commit badcc79ed4498772aee3dc6d80cbbc46db863a10 1 parent 5d9edc0
@mjankowski mjankowski authored
Showing with 520 additions and 730 deletions.
  1. +2 −2 Gemfile
  2. +62 −28 Gemfile.lock
  3. +1 −2  gemfiles/rails-3.0.3-database-mysql.gemfile
  4. +5 −28 gemfiles/rails-3.0.3-database-mysql.gemfile.lock
  5. +1 −2  gemfiles/rails-3.0.3-database-mysql2.gemfile
  6. +5 −28 gemfiles/rails-3.0.3-database-mysql2.gemfile.lock
  7. +1 −2  gemfiles/rails-3.0.3-database-pg.gemfile
  8. +5 −28 gemfiles/rails-3.0.3-database-pg.gemfile.lock
  9. +1 −2  gemfiles/rails-3.0.3-database-sqlite3-ruby.gemfile
  10. +5 −28 gemfiles/rails-3.0.3-database-sqlite3-ruby.gemfile.lock
  11. +4 −4 lib/pacecar/datetime.rb
  12. +3 −3 lib/pacecar/duration.rb
  13. +2 −2 lib/pacecar/ranking.rb
  14. +4 −4 lib/pacecar/search.rb
  15. +24 −28 test/associations_test.rb
  16. +24 −35 test/boolean_test.rb
  17. +67 −108 test/datetime_test.rb
  18. +2 −0  test/dummy/app/models/mammal.rb
  19. +1 −1  test/dummy/app/models/user.rb
  20. +16 −28 test/duration_test.rb
  21. +17 −0 test/factories.rb
  22. +33 −53 test/helpers_test.rb
  23. +19 −26 test/limit_test.rb
  24. +36 −57 test/numeric_test.rb
  25. +16 −25 test/order_test.rb
  26. +12 −19 test/polymorph_test.rb
  27. +17 −23 test/presence_test.rb
  28. +15 −19 test/ranking_test.rb
  29. +55 −69 test/search_test.rb
  30. +53 −69 test/state_test.rb
  31. +2 −2 test/support/integration_case.rb
  32. +10 −5 test/test_helper.rb
View
4 Gemfile
@@ -2,7 +2,7 @@ source 'http://rubygems.org'
gem 'appraisal'
-gem 'shoulda'
gem 'mocha'
gem 'redgreen'
-gem 'capybara', '>= 0.4.0'
+
+gem 'factory_girl_rails'
View
90 Gemfile.lock
@@ -1,50 +1,84 @@
GEM
remote: http://rubygems.org/
specs:
+ abstract (1.0.0)
+ actionmailer (3.0.3)
+ actionpack (= 3.0.3)
+ mail (~> 2.2.9)
+ actionpack (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.13)
+ rack-test (~> 0.5.6)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.3)
+ activesupport (= 3.0.3)
+ builder (~> 2.1.2)
+ i18n (~> 0.4)
+ activerecord (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ activesupport (3.0.3)
appraisal (0.1)
bundler
rake
- capybara (0.4.0)
- celerity (>= 0.7.9)
- culerity (>= 0.2.4)
- mime-types (>= 1.16)
- nokogiri (>= 1.3.3)
- rack (>= 1.0.0)
- rack-test (>= 0.5.4)
- selenium-webdriver (>= 0.0.27)
- xpath (~> 0.1.2)
- celerity (0.8.4)
- childprocess (0.1.4)
- ffi (~> 0.6.3)
- culerity (0.2.12)
- ffi (0.6.3)
- rake (>= 0.8.7)
- json_pure (1.4.6)
+ arel (2.0.6)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ factory_girl (1.3.2)
+ factory_girl_rails (1.0)
+ factory_girl (~> 1.3)
+ rails (>= 3.0.0.beta4)
+ i18n (0.5.0)
+ mail (2.2.12)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
mime-types (1.16)
mocha (0.9.10)
rake
- nokogiri (1.4.4)
+ polyglot (0.3.1)
rack (1.2.1)
+ rack-mount (0.6.13)
+ rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
+ rails (3.0.3)
+ actionmailer (= 3.0.3)
+ actionpack (= 3.0.3)
+ activerecord (= 3.0.3)
+ activeresource (= 3.0.3)
+ activesupport (= 3.0.3)
+ bundler (~> 1.0)
+ railties (= 3.0.3)
+ railties (3.0.3)
+ actionpack (= 3.0.3)
+ activesupport (= 3.0.3)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
rake (0.8.7)
redgreen (1.2.2)
- rubyzip (0.9.4)
- selenium-webdriver (0.1.1)
- childprocess (= 0.1.4)
- ffi (~> 0.6.3)
- json_pure
- rubyzip
- shoulda (2.11.3)
- xpath (0.1.2)
- nokogiri (~> 1.3)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.23)
PLATFORMS
ruby
DEPENDENCIES
appraisal
- capybara (>= 0.4.0)
+ factory_girl_rails
mocha
redgreen
- shoulda
View
3  gemfiles/rails-3.0.3-database-mysql.gemfile
@@ -3,8 +3,7 @@
source "http://rubygems.org"
gem "rails", "3.0.3"
gem "mysql"
-gem "shoulda"
gem "mocha"
gem "redgreen"
gem "appraisal"
-gem "capybara", ">= 0.4.0"
+gem "factory_girl_rails"
View
33 gemfiles/rails-3.0.3-database-mysql.gemfile.lock
@@ -33,25 +33,13 @@ GEM
rake
arel (2.0.6)
builder (2.1.2)
- capybara (0.4.0)
- celerity (>= 0.7.9)
- culerity (>= 0.2.4)
- mime-types (>= 1.16)
- nokogiri (>= 1.3.3)
- rack (>= 1.0.0)
- rack-test (>= 0.5.4)
- selenium-webdriver (>= 0.0.27)
- xpath (~> 0.1.2)
- celerity (0.8.4)
- childprocess (0.1.4)
- ffi (~> 0.6.3)
- culerity (0.2.12)
erubis (2.6.6)
abstract (>= 1.0.0)
- ffi (0.6.3)
- rake (>= 0.8.7)
+ factory_girl (1.3.2)
+ factory_girl_rails (1.0)
+ factory_girl (~> 1.3)
+ rails (>= 3.0.0.beta4)
i18n (0.5.0)
- json_pure (1.4.6)
mail (2.2.12)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -61,7 +49,6 @@ GEM
mocha (0.9.10)
rake
mysql (2.8.1)
- nokogiri (1.4.4)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
@@ -83,28 +70,18 @@ GEM
thor (~> 0.14.4)
rake (0.8.7)
redgreen (1.2.2)
- rubyzip (0.9.4)
- selenium-webdriver (0.1.1)
- childprocess (= 0.1.4)
- ffi (~> 0.6.3)
- json_pure
- rubyzip
- shoulda (2.11.3)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
- xpath (0.1.2)
- nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
appraisal
- capybara (>= 0.4.0)
+ factory_girl_rails
mocha
mysql
rails (= 3.0.3)
redgreen
- shoulda
View
3  gemfiles/rails-3.0.3-database-mysql2.gemfile
@@ -2,9 +2,8 @@
source "http://rubygems.org"
gem "rails", "3.0.3"
-gem "shoulda"
gem "mocha"
gem "mysql2"
gem "redgreen"
gem "appraisal"
-gem "capybara", ">= 0.4.0"
+gem "factory_girl_rails"
View
33 gemfiles/rails-3.0.3-database-mysql2.gemfile.lock
@@ -33,25 +33,13 @@ GEM
rake
arel (2.0.6)
builder (2.1.2)
- capybara (0.4.0)
- celerity (>= 0.7.9)
- culerity (>= 0.2.4)
- mime-types (>= 1.16)
- nokogiri (>= 1.3.3)
- rack (>= 1.0.0)
- rack-test (>= 0.5.4)
- selenium-webdriver (>= 0.0.27)
- xpath (~> 0.1.2)
- celerity (0.8.4)
- childprocess (0.1.4)
- ffi (~> 0.6.3)
- culerity (0.2.12)
erubis (2.6.6)
abstract (>= 1.0.0)
- ffi (0.6.3)
- rake (>= 0.8.7)
+ factory_girl (1.3.2)
+ factory_girl_rails (1.0)
+ factory_girl (~> 1.3)
+ rails (>= 3.0.0.beta4)
i18n (0.5.0)
- json_pure (1.4.6)
mail (2.2.12)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -61,7 +49,6 @@ GEM
mocha (0.9.10)
rake
mysql2 (0.2.6)
- nokogiri (1.4.4)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
@@ -83,28 +70,18 @@ GEM
thor (~> 0.14.4)
rake (0.8.7)
redgreen (1.2.2)
- rubyzip (0.9.4)
- selenium-webdriver (0.1.1)
- childprocess (= 0.1.4)
- ffi (~> 0.6.3)
- json_pure
- rubyzip
- shoulda (2.11.3)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
- xpath (0.1.2)
- nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
appraisal
- capybara (>= 0.4.0)
+ factory_girl_rails
mocha
mysql2
rails (= 3.0.3)
redgreen
- shoulda
View
3  gemfiles/rails-3.0.3-database-pg.gemfile
@@ -3,8 +3,7 @@
source "http://rubygems.org"
gem "pg"
gem "rails", "3.0.3"
-gem "shoulda"
gem "mocha"
gem "redgreen"
gem "appraisal"
-gem "capybara", ">= 0.4.0"
+gem "factory_girl_rails"
View
33 gemfiles/rails-3.0.3-database-pg.gemfile.lock
@@ -33,25 +33,13 @@ GEM
rake
arel (2.0.6)
builder (2.1.2)
- capybara (0.4.0)
- celerity (>= 0.7.9)
- culerity (>= 0.2.4)
- mime-types (>= 1.16)
- nokogiri (>= 1.3.3)
- rack (>= 1.0.0)
- rack-test (>= 0.5.4)
- selenium-webdriver (>= 0.0.27)
- xpath (~> 0.1.2)
- celerity (0.8.4)
- childprocess (0.1.4)
- ffi (~> 0.6.3)
- culerity (0.2.12)
erubis (2.6.6)
abstract (>= 1.0.0)
- ffi (0.6.3)
- rake (>= 0.8.7)
+ factory_girl (1.3.2)
+ factory_girl_rails (1.0)
+ factory_girl (~> 1.3)
+ rails (>= 3.0.0.beta4)
i18n (0.5.0)
- json_pure (1.4.6)
mail (2.2.12)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -60,7 +48,6 @@ GEM
mime-types (1.16)
mocha (0.9.10)
rake
- nokogiri (1.4.4)
pg (0.10.0)
polyglot (0.3.1)
rack (1.2.1)
@@ -83,28 +70,18 @@ GEM
thor (~> 0.14.4)
rake (0.8.7)
redgreen (1.2.2)
- rubyzip (0.9.4)
- selenium-webdriver (0.1.1)
- childprocess (= 0.1.4)
- ffi (~> 0.6.3)
- json_pure
- rubyzip
- shoulda (2.11.3)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
- xpath (0.1.2)
- nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
appraisal
- capybara (>= 0.4.0)
+ factory_girl_rails
mocha
pg
rails (= 3.0.3)
redgreen
- shoulda
View
3  gemfiles/rails-3.0.3-database-sqlite3-ruby.gemfile
@@ -3,8 +3,7 @@
source "http://rubygems.org"
gem "rails", "3.0.3"
gem "sqlite3-ruby"
-gem "shoulda"
gem "mocha"
gem "redgreen"
gem "appraisal"
-gem "capybara", ">= 0.4.0"
+gem "factory_girl_rails"
View
33 gemfiles/rails-3.0.3-database-sqlite3-ruby.gemfile.lock
@@ -33,25 +33,13 @@ GEM
rake
arel (2.0.6)
builder (2.1.2)
- capybara (0.4.0)
- celerity (>= 0.7.9)
- culerity (>= 0.2.4)
- mime-types (>= 1.16)
- nokogiri (>= 1.3.3)
- rack (>= 1.0.0)
- rack-test (>= 0.5.4)
- selenium-webdriver (>= 0.0.27)
- xpath (~> 0.1.2)
- celerity (0.8.4)
- childprocess (0.1.4)
- ffi (~> 0.6.3)
- culerity (0.2.12)
erubis (2.6.6)
abstract (>= 1.0.0)
- ffi (0.6.3)
- rake (>= 0.8.7)
+ factory_girl (1.3.2)
+ factory_girl_rails (1.0)
+ factory_girl (~> 1.3)
+ rails (>= 3.0.0.beta4)
i18n (0.5.0)
- json_pure (1.4.6)
mail (2.2.12)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -60,7 +48,6 @@ GEM
mime-types (1.16)
mocha (0.9.10)
rake
- nokogiri (1.4.4)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
@@ -82,29 +69,19 @@ GEM
thor (~> 0.14.4)
rake (0.8.7)
redgreen (1.2.2)
- rubyzip (0.9.4)
- selenium-webdriver (0.1.1)
- childprocess (= 0.1.4)
- ffi (~> 0.6.3)
- json_pure
- rubyzip
- shoulda (2.11.3)
sqlite3-ruby (1.3.2)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
- xpath (0.1.2)
- nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
appraisal
- capybara (>= 0.4.0)
+ factory_girl_rails
mocha
rails (= 3.0.3)
redgreen
- shoulda
sqlite3-ruby
View
8 lib/pacecar/datetime.rb
@@ -43,19 +43,19 @@ def define_inside_outside_scopes(name)
{ :conditions => ["#{quoted_table_name}.#{connection.quote_column_name name} >= ? and #{quoted_table_name}.#{connection.quote_column_name name} <= ?", start, stop] }
}
scope "#{name}_outside".to_sym, lambda { |start, stop|
- { :conditions => ["#{quoted_table_name}.#{connection.quote_column_name name} <= ? and #{quoted_table_name}.#{connection.quote_column_name name} >= ?", start, stop] }
+ { :conditions => ["#{quoted_table_name}.#{connection.quote_column_name name} <= ? or #{quoted_table_name}.#{connection.quote_column_name name} >= ?", start, stop] }
}
end
def define_in_date_scopes(name)
scope "#{name}_in_year".to_sym, lambda { |year|
- { :conditions => ["year(#{quoted_table_name}.#{connection.quote_column_name name}) = ?", year] }
+ { :conditions => ["year(#{quoted_table_name}.#{connection.quote_column_name name}) = ?", year.to_i] }
}
scope "#{name}_in_month".to_sym, lambda { |month|
- { :conditions => ["month(#{quoted_table_name}.#{connection.quote_column_name name}) = ?", month] }
+ { :conditions => ["month(#{quoted_table_name}.#{connection.quote_column_name name}) = ?", month.to_i] }
}
scope "#{name}_in_day".to_sym, lambda { |day|
- { :conditions => ["day(#{quoted_table_name}.#{connection.quote_column_name name}) = ?", day] }
+ { :conditions => ["day(#{quoted_table_name}.#{connection.quote_column_name name}) = ?", day.to_i] }
}
end
View
6 lib/pacecar/duration.rb
@@ -13,13 +13,13 @@ def self.extended(base)
def define_duration_scopes
scope :with_duration_of, lambda { |duration, start, stop|
- { :conditions => ["datediff(#{quoted_table_name}.#{connection.quote_column_name start}, #{quoted_table_name}.#{connection.quote_column_name stop}) = ?", duration] }
+ { :conditions => ["abs(datediff(#{quoted_table_name}.#{connection.quote_column_name start}, #{quoted_table_name}.#{connection.quote_column_name stop})) = ?", duration] }
}
scope :with_duration_over, lambda { |duration, start, stop|
- { :conditions => ["datediff(#{quoted_table_name}.#{connection.quote_column_name start}, #{quoted_table_name}.#{connection.quote_column_name stop}) > ?", duration] }
+ { :conditions => ["abs(datediff(#{quoted_table_name}.#{connection.quote_column_name start}, #{quoted_table_name}.#{connection.quote_column_name stop})) > ?", duration] }
}
scope :with_duration_under, lambda { |duration, start, stop|
- { :conditions => ["datediff(#{quoted_table_name}.#{connection.quote_column_name start}, #{quoted_table_name}.#{connection.quote_column_name stop}) < ?", duration] }
+ { :conditions => ["abs(datediff(#{quoted_table_name}.#{connection.quote_column_name start}, #{quoted_table_name}.#{connection.quote_column_name stop})) < ?", duration] }
}
end
View
4 lib/pacecar/ranking.rb
@@ -16,8 +16,8 @@ def has_ranking(association)
def define_ranking_scope(association, name, direction)
scope "#{name}_#{association}",
:joins => "inner join #{association} on #{association}.#{reflections[association].primary_key_name} = #{quoted_table_name}.#{connection.quote_column_name primary_key}",
- :select => "#{quoted_table_name}.*, count(#{quoted_table_name}.#{connection.quote_column_name primary_key}) as #{association}_count",
- :group => "#{association}.#{reflections[association].primary_key_name}",
+ :select => "#{quoted_table_name}.*, count(#{reflections[association].quoted_table_name}.#{connection.quote_column_name reflections[association].primary_key_name}) as #{association}_count",
+ :group => "#{quoted_table_name}.#{connection.quote_column_name primary_key}",
:order => "#{association}_count #{direction}"
end
View
8 lib/pacecar/search.rb
@@ -20,13 +20,13 @@ def define_search_scopes
end
text_and_string_column_names.each do |name|
scope "#{name}_matches".to_sym, lambda { |query|
- { :conditions => ["#{quoted_table_name}.#{connection.quote_column_name(name)} LIKE :query", { :query => "%#{query}%" }] }
+ { :conditions => ["lower(#{quoted_table_name}.#{connection.quote_column_name(name)}) LIKE lower(:query)", { :query => "%#{query}%" }] }
}
scope "#{name}_starts_with".to_sym, lambda { |query|
- { :conditions => ["#{quoted_table_name}.#{connection.quote_column_name(name)} LIKE :query", { :query => "#{query}%" }] }
+ { :conditions => ["lower(#{quoted_table_name}.#{connection.quote_column_name(name)}) LIKE lower(:query)", { :query => "#{query}%" }] }
}
scope "#{name}_ends_with".to_sym, lambda { |query|
- { :conditions => ["#{quoted_table_name}.#{connection.quote_column_name(name)} LIKE :query", { :query => "%#{query}" }] }
+ { :conditions => ["lower(#{quoted_table_name}.#{connection.quote_column_name(name)}) LIKE lower(:query)", { :query => "%#{query}" }] }
}
end
end
@@ -37,7 +37,7 @@ def define_basic_search_scope
query = args.flatten.first
columns = opts[:on] || non_state_text_and_string_columns
joiner = opts[:require].eql?(:all) ? 'AND' : 'OR'
- match = columns.collect { |name| "#{quoted_table_name}.#{connection.quote_column_name(name)} LIKE :query" }.join(" #{joiner} ")
+ match = columns.collect { |name| "lower(#{quoted_table_name}.#{connection.quote_column_name(name)}) LIKE lower(:query)" }.join(" #{joiner} ")
{ :conditions => [match, { :query => "%#{query}%" } ] }
}
end
View
52 test/associations_test.rb
@@ -1,34 +1,30 @@
require 'test_helper'
-class AssociationsTest < Test::Unit::TestCase
+class AssociationsTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- context "with recent_records methods" do
- setup do
- @time = 2.days.ago.to_datetime
- end
- should "set the correct options for a recent methods for one association" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (((select count(*) from #{Comment.quoted_table_name} where #{Comment.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "user_id"} = #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name(@class.primary_key)} and #{Comment.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("created_at")} > '#{@time.to_s(:db)}') > 0))
- SQL
- assert_equal expected.strip, @class.recent_comments_since(@time).to_sql
- end
- should "set the correct options for a recent methods combining associations with or" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (((select count(*) from #{Article.quoted_table_name} where #{Article.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "user_id"} = #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name(@class.primary_key)} and #{Article.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("created_at")} > '#{@time.to_s(:db)}') > 0) or ((select count(*) from #{Comment.quoted_table_name} where #{Comment.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "user_id"} = #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name(@class.primary_key)} and #{Comment.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("created_at")} > '#{@time.to_s(:db)}') > 0))
- SQL
- assert_equal expected.strip, @class.recent_articles_or_comments_since(@time).to_sql
- end
- should "set the correct options for a recent methods combining associations with and" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (((select count(*) from #{Article.quoted_table_name} where #{Article.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "user_id"} = #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name(@class.primary_key)} and #{Article.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("created_at")} > '#{@time.to_s(:db)}') > 0) and ((select count(*) from #{Comment.quoted_table_name} where #{Comment.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "user_id"} = #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name(@class.primary_key)} and #{Comment.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("created_at")} > '#{@time.to_s(:db)}') > 0))
- SQL
- assert_equal expected.strip, @class.recent_articles_and_comments_since(@time).to_sql
- end
- end
+ setup do
+ @comment_user = Factory :user
+ @post_user = Factory :user
+ @both_user = Factory :user
+ Factory :comment, :user => @comment_user, :created_at => 10.days.ago
+ Factory :comment, :user => @comment_user, :created_at => 3.days.ago
+ Factory :post, :owner => @post_user, :created_at => 10.days.ago
+ Factory :post, :owner => @post_user, :created_at => 3.days.ago
+
+ Factory :comment, :user => @both_user, :created_at => 10.days.ago
+ Factory :comment, :user => @both_user, :created_at => 3.days.ago
+ Factory :post, :owner => @both_user, :created_at => 10.days.ago
+ Factory :post, :owner => @both_user, :created_at => 3.days.ago
+ end
+
+ test "set the correct options for a recent methods for one association" do
+ assert_equal [@comment_user, @both_user], User.recent_comments_since(5.days.ago)
+ end
+ test "set the correct options for a recent methods combining associations with or" do
+ assert_equal [@comment_user, @post_user, @both_user], User.recent_posts_or_comments_since(5.days.ago)
+ end
+ test "set the correct options for a recent methods combining associations with and" do
+ assert_equal [@both_user], User.recent_posts_and_comments_since(5.days.ago)
end
end
View
59 test/boolean_test.rb
@@ -1,41 +1,30 @@
require 'test_helper'
-class BooleanTest < Test::Unit::TestCase
+class BooleanTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- should "set the correct expected values for a boolean column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "admin"} = #{ActiveRecord::Base.connection.quoted_true})
- SQL
- assert_equal expected.strip, @class.admin.to_sql
- end
- should "set the correct expected values for a not_ boolean column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "admin"} = #{ActiveRecord::Base.connection.quoted_false})
- SQL
- assert_equal expected.strip, @class.not_admin.to_sql
- end
- context "With boolean column scopes that can count" do
- setup do
- @true_mock = mock
- @false_mock = mock
- @class.expects(:admin).returns @true_mock
- @class.expects(:not_admin).returns @false_mock
- end
- should "return correct value for balance class method when true greater than false" do
- @true_mock.expects(:count).returns 5
- @false_mock.expects(:count).returns 2
- assert_equal 3, @class.admin_balance
- end
- should "return correct value for balance class method when true less than false" do
- @true_mock.expects(:count).returns 2
- @false_mock.expects(:count).returns 5
- assert_equal -3, @class.admin_balance
- end
- end
+ setup do
+ @one = Factory :user, :admin => true
+ @two = Factory :user, :admin => true
+ @three = Factory :user, :admin => true
+ @four = Factory :user, :admin => false
+ @five = Factory :user, :admin => false
+ end
+
+ test "set the correct expected values for a boolean column method" do
+ assert_equal [@one, @two, @three], User.admin
+ end
+
+ test "set the correct expected values for a not_ boolean column method" do
+ assert_equal [@four, @five], User.not_admin
+ end
+
+ test "return correct value for balance class method when true greater than false" do
+ assert_equal 1, User.admin_balance
+ end
+
+ test "return correct value for balance class method when true less than false" do
+ 3.times { Factory :user, :admin => false }
+ assert_equal -2, User.admin_balance
end
end
View
175 test/datetime_test.rb
@@ -1,121 +1,80 @@
require 'test_helper'
-class DatetimeTest < Test::Unit::TestCase
+class DatetimeTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
+ setup do
+ date = DateTime.parse '2000-01-01'
+ @abe = Factory :user, :created_at => date, :rejected_at => date, :updated_at => date, :last_posted_on => date, :approved_at => date
+
+ date = DateTime.parse '2005-05-05'
+ @bob = Factory :user, :created_at => date, :rejected_at => date, :updated_at => date, :last_posted_on => date, :approved_at => date
+
+ date = DateTime.parse '2010-10-10'
+ @fox = Factory :user, :created_at => date, :rejected_at => date, :updated_at => date, :last_posted_on => date, :approved_at => date
+ end
+
+ [:created_at, :rejected_at, :updated_at, :last_posted_on, :approved_at].each do |column|
+ test "set the correct expected values for a #{column}_before method" do
+ date = DateTime.parse '2003-01-01'
+ assert_equal [@abe], User.send(:"#{column}_before", date)
+ end
+
+ test "set the correct expected values for a #{column}_after datetime column method" do
+ date = DateTime.parse '2007-01-01'
+ assert_equal [@fox], User.send(:"#{column}_after", date)
+ end
+
+ test "set the correct expected values for a #{column}_in_past method without a zone default" do
+ assert_nil Time.zone_default
+ now = DateTime.parse '2007-01-01'
+ User.stubs(:now).returns now
+ assert_equal [@abe, @bob], User.send(:"#{column}_in_past")
end
- context "for each date and datetime column" do
- [:created_at, :rejected_at, :updated_at, :last_posted_on, :approved_at].each do |column|
- context "with before and after methods for #{column}" do
- setup do
- @time = 5.days.ago.to_datetime
- end
- should "set the correct expected values for a #{column}_before method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} <= '#{@time.to_s(:db)}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_before", @time).to_sql
- end
- should "set the correct expected values for a after_ datetime column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} >= '#{@time.to_s(:db)}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_after", @time).to_sql
- end
- end
- context "with in_past and in_future methods" do
- setup do
- assert_nil Time.zone_default
- @now = Time.now.utc.to_datetime
- @class.stubs(:now).returns @now
- end
- should "set the correct expected values for a #{column}_in_past method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} <= '#{@now.to_s(:db)}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_in_past").to_sql
- end
- should "set the correct expected values for a #{column}_in_future datetime column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} >= '#{@now.to_s(:db)}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_in_future").to_sql
- end
- end
+ test "set the correct expected values for a #{column}_in_future datetime column method without a zone default" do
+ assert_nil Time.zone_default
+ now = DateTime.parse '2007-01-01'
+ User.stubs(:now).returns now
+ assert_equal [@fox], User.send(:"#{column}_in_future")
+ end
- context "with in_past and in_future methods given a zone_default" do
- setup do
- Time.zone_default = Time.__send__(:get_zone, "UTC")
- @now = Time.zone_default.now.to_datetime
- Time.zone_default.stubs(:now).returns @now
- end
- teardown do
- Time.zone_default = nil
- end
- should "set the correct expected values for a #{column}_in_past method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} <= '#{@now.to_s(:db)}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_in_past", @time).to_sql
- end
- should "set the correct expected values for a #{column}_in_future datetime column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} >= '#{@now.to_s(:db)}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_in_future", @time).to_sql
- end
- end
+ test "set the correct expected values for a #{column}_in_past method given a zone_default" do
+ Time.zone_default = Time.__send__(:get_zone, "UTC")
+ now = DateTime.parse '2007-01-01'
+ Time.zone_default.stubs(:now).returns now
+ assert_equal [@abe, @bob], User.send(:"#{column}_in_past")
+ Time.zone_default = nil
+ end
- context "with _inside and _outside methods" do
- setup do
- @start = 3.days.ago.to_datetime
- @stop = 2.days.ago.to_datetime
- end
- should "set the correct expected values for a #{column}_inside method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} >= '#{@start.to_s(:db)}' and #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} <= '#{@stop.to_s(:db)}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_inside", @start, @stop).to_sql
- end
- should "set the correct expected values for a #{column}_outside method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} <= '#{@start.to_s(:db)}' and #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column} >= '#{@stop.to_s(:db)}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_outside", @start, @stop).to_sql
- end
- end
+ test "set the correct expected values for a #{column}_in_future datetime column method given a zone_default" do
+ Time.zone_default = Time.__send__(:get_zone, "UTC")
+ now = DateTime.parse '2007-01-01'
+ Time.zone_default.stubs(:now).returns now
+ assert_equal [@fox], User.send(:"#{column}_in_future")
+ Time.zone_default = nil
+ end
- context "with year month and day methods" do
- setup do
- @year = '2000'
- @month = '01'
- @day = '01'
- end
- should "set the correct expected values for a #{column}_in_year method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (year(#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column}) = '#{@year}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_in_year", @year).to_sql
- end
- should "set the correct expected values for a #{column}_in_month method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (month(#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column}) = '#{@month}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_in_month", @month).to_sql
- end
- should "set the correct expected values for a #{column}_in_day method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (day(#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name column}) = '#{@day}')
- SQL
- assert_equal expected.strip, @class.send(:"#{column}_in_day", @day).to_sql
- end
- end
+ test "set the correct expected values for a #{column}_inside method" do
+ start = DateTime.parse '2003-01-01'
+ stop = DateTime.parse '2007-01-01'
+ assert_equal [@bob], User.send(:"#{column}_inside", start, stop)
+ end
+ test "set the correct expected values for a #{column}_outside method" do
+ start = DateTime.parse '2003-01-01'
+ stop = DateTime.parse '2007-01-01'
+ assert_equal [@abe, @fox], User.send(:"#{column}_outside", start, stop)
+ end
- end
+ test "set the correct expected values for a #{column}_in_year method" do
+ assert_equal [@abe], User.send(:"#{column}_in_year", '2000')
+ end
+ test "set the correct expected values for a #{column}_in_month method" do
+ assert_equal [@bob], User.send(:"#{column}_in_month", '05')
end
+ test "set the correct expected values for a #{column}_in_day method" do
+ assert_equal [@fox], User.send(:"#{column}_in_day", '10')
+ end
+
end
end
View
2  test/dummy/app/models/mammal.rb
@@ -2,4 +2,6 @@ class Mammal < ActiveRecord::Base
include Pacecar
+ has_many :posts, :as => :owner
+
end
View
2  test/dummy/app/models/user.rb
@@ -8,6 +8,6 @@ class User < ActiveRecord::Base
has_ranking :comments
has_recent_records :comments
- has_recent_records :articles, :comments
+ has_recent_records :posts, :comments
end
View
44 test/duration_test.rb
@@ -1,34 +1,22 @@
require 'test_helper'
-class DurationTest < Test::Unit::TestCase
+class DurationTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- context "with duration methods" do
- setup do
- @days = 14
- end
- should "set the correct expected values for a with_duration_of datetime column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (datediff(#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "created_at"}, #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "updated_at"}) = #{@days})
- SQL
- assert_equal expected.strip, @class.with_duration_of(@days, :created_at, :updated_at).to_sql
- end
- should "set the correct expected values for a with_duration_over datetime column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (datediff(#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "created_at"}, #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "updated_at"}) > #{@days})
- SQL
- assert_equal expected.strip, @class.with_duration_over(@days, :created_at, :updated_at).to_sql
- end
- should "set the correct expected values for a with_duration_under datetime column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (datediff(#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "created_at"}, #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "updated_at"}) < #{@days})
- SQL
- assert_equal expected.strip, @class.with_duration_under(@days, :created_at, :updated_at).to_sql
- end
- end
+ setup do
+ @same_user = Factory :user, :created_at => 15.days.ago.midnight, :updated_at => 15.days.ago.midnight
+ @updated_user = Factory :user, :created_at => 15.days.ago.midnight, :updated_at => 1.days.ago.midnight
+ end
+
+ test "set the correct expected values for a with_duration_of datetime column method" do
+ assert_equal [@updated_user], User.with_duration_of(14, :created_at, :updated_at)
+ end
+
+ test "set the correct expected values for a with_duration_over datetime column method" do
+ assert_equal [@updated_user], User.with_duration_over(10, :created_at, :updated_at)
+ end
+
+ test "set the correct expected values for a with_duration_under datetime column method" do
+ assert_equal [@same_user], User.with_duration_under(10, :created_at, :updated_at)
end
end
View
17 test/factories.rb
@@ -0,0 +1,17 @@
+Factory.sequence(:age) { |n| n.to_i }
+Factory.sequence(:rating) { |n| n.to_f }
+
+Factory.define :comment do |f|
+ f.association :user
+end
+
+Factory.define :mammal do |f|
+end
+
+Factory.define :post do |f|
+end
+
+Factory.define :user do |f|
+ f.age { Factory.next :age }
+ f.rating { Factory.next :rating }
+end
View
86 test/helpers_test.rb
@@ -1,66 +1,46 @@
require 'test_helper'
-class HelpersTest < Test::Unit::TestCase
+class HelpersTest < ActiveSupport::TestCase
- context "A class without a db table" do
- setup do
- @class = Article
- end
- should "return an empty array when asked about #safe_columns" do
- columns = @class.send :safe_columns
- assert_equal [], columns
- end
- should "survive an include of Pacecar" do
- assert_nothing_raised do
- @class.send :include, Pacecar
- end
+ test "A class without a db table should return an empty array when asked about #safe_columns" do
+ columns = Article.send :safe_columns
+ assert_equal [], columns
+ end
+ test "A class without a db table should survive an include of Pacecar" do
+ assert_nothing_raised do
+ Article.send :include, Pacecar
end
end
- context "A class with a db table" do
- setup do
- @class = Comment
- end
- should "return columns for #safe_column_names" do
- column_names = @class.send :safe_column_names
- assert_equal ['id', 'user_id', 'description', 'created_at', 'updated_at'], column_names
- end
+ test "A class with a db table should return columns for #safe_column_names" do
+ column_names = Comment.safe_column_names
+ assert_equal ['id', 'user_id', 'description', 'created_at', 'updated_at'], column_names
end
- context "A class with many column types" do
- setup do
- @class = User
- end
- should "return boolean columns for #boolean_column_names" do
- column_names = @class.send :boolean_column_names
- assert_equal ['admin'], column_names
- end
- should "return non boolean columns for #non_boolean_column_names" do
- column_names = @class.send :non_boolean_column_names
- assert_equal ['id', 'approved_at', 'rejected_at', 'last_posted_on', 'first_name', 'last_name', 'description', 'age', 'rating', 'created_at', 'updated_at'], column_names
- end
- should "return datetime columns for #datetime_column_names" do
- column_names = @class.send :datetime_column_names
- assert_equal ['approved_at', 'rejected_at', 'last_posted_on', 'created_at', 'updated_at'], column_names
- end
- should "return text and string columns for #text_and_string_column_names" do
- column_names = @class.send :text_and_string_column_names
- assert_equal ['first_name', 'last_name', 'description'], column_names
- end
- should "return numeric columns for #numeric_column_names" do
- column_names = @class.send :numeric_column_names
- assert_equal ['id', 'age', 'rating'], column_names
- end
+ test "A class with many column types should return boolean columns for #boolean_column_names" do
+ column_names = User.boolean_column_names
+ assert_equal ['admin'], column_names
+ end
+ test "A class with many column types should return non boolean columns for #non_boolean_column_names" do
+ column_names = User.non_boolean_column_names
+ assert_equal ['id', 'approved_at', 'rejected_at', 'last_posted_on', 'first_name', 'last_name', 'description', 'age', 'rating', 'created_at', 'updated_at'], column_names
+ end
+ test "A class with many column types should return datetime columns for #datetime_column_names" do
+ column_names = User.datetime_column_names
+ assert_equal ['approved_at', 'rejected_at', 'last_posted_on', 'created_at', 'updated_at'], column_names
+ end
+ test "A class with many column types should return text and string columns for #text_and_string_column_names" do
+ column_names = User.text_and_string_column_names
+ assert_equal ['first_name', 'last_name', 'description'], column_names
+ end
+ test "A class with many column types should return numeric columns for #numeric_column_names" do
+ column_names = User.numeric_column_names
+ assert_equal ['id', 'age', 'rating'], column_names
end
- context "A class with a state column" do
- setup do
- @class = Post
- end
- should "return all non state text and string columns for #non_state_text_and_string_column_names" do
- column_names = @class.send :non_state_text_and_string_columns
- assert_equal ['title', 'body'], column_names
- end
+ test "A class with a state column should return all non state text and string columns for #non_state_text_and_string_column_names" do
+ column_names = Post.non_state_text_and_string_columns
+ assert_equal ['title', 'body'], column_names
end
end
View
45 test/limit_test.rb
@@ -1,32 +1,25 @@
require 'test_helper'
-class LimitTest < Test::Unit::TestCase
+class LimitTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- context "with order methods" do
- should "set the correct expected values for a by_ column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} LIMIT 10
- SQL
- assert_equal expected.strip, @class.limited.to_sql
- end
- should "set the correct expected values for a by_ column method when sent args" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} LIMIT 20
- SQL
- assert_equal expected.strip, @class.limited(20).to_sql
- end
- should "set the correct expected values for a by_ column method when per_page defined" do
- @class.expects(:per_page).returns 30
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} LIMIT 30
- SQL
- assert_equal expected.strip, @class.limited.to_sql
- end
- end
+ setup do
+ 50.times { Factory :user }
+ end
+
+ test "set the correct expected values for a by_ column method" do
+ assert_equal 50, User.count
+ assert_equal 10, User.limited.all.size
+ end
+
+ test "set the correct expected values for a by_ column method when sent args" do
+ assert_equal 50, User.count
+ assert_equal 20, User.limited(20).all.size
+ end
+
+ test "set the correct expected values for a by_ column method when per_page defined" do
+ User.expects(:per_page).returns 30
+ assert_equal 50, User.count
+ assert_equal 30, User.limited.all.size
end
end
View
93 test/numeric_test.rb
@@ -1,64 +1,43 @@
require 'test_helper'
-class NumericTest < Test::Unit::TestCase
+class NumericTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- context "for an integer column" do
- should "set the correct expected values for a _greater_than column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "age"} > 21)
- SQL
- assert_equal expected.strip, @class.age_greater_than(21).to_sql
- end
- should "set the correct expected values for a _greater_than_or_equal_to column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "age"} >= 21)
- SQL
- assert_equal expected.strip, @class.age_greater_than_or_equal_to(21).to_sql
- end
- should "set the correct expected values for a _less_than column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "age"} < 21)
- SQL
- assert_equal expected.strip, @class.age_less_than(21).to_sql
- end
- should "set the correct expected values for a _less_than_or_equal_to column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "age"} <= 21)
- SQL
- assert_equal expected.strip, @class.age_less_than_or_equal_to(21).to_sql
- end
- end
+ setup do
+ @young = Factory :user, :age => 11, :rating => 10.0
+ @legal = Factory :user, :age => 21, :rating => 7.5
+ @older = Factory :user, :age => 31, :rating => 5.0
+ end
+
+ test "set the correct expected values for a _greater_than column method for an integer column" do
+ assert_equal [@older], User.age_greater_than(21)
+ end
+
+ test "set the correct expected values for a _greater_than_or_equal_to column method for an integer column" do
+ assert_equal [@legal, @older], User.age_greater_than_or_equal_to(21)
+ end
+
+ test "set the correct expected values for a _less_than column method for an integer column" do
+ assert_equal [@young], User.age_less_than(21)
+ end
+
+ test "set the correct expected values for a _less_than_or_equal_to column method for an integer column" do
+ assert_equal [@young, @legal], User.age_less_than_or_equal_to(21)
+ end
+
+ test "set the correct expected values for a _greater_than column method for a float column" do
+ assert_equal [@young], User.rating_greater_than(7.5)
+ end
+
+ test "set the correct expected values for a _greater_than_or_equal_to column method for a float column" do
+ assert_equal [@young, @legal], User.rating_greater_than_or_equal_to(7.5)
+ end
+
+ test "set the correct expected values for a _less_than column method for a float column" do
+ assert_equal [@older], User.rating_less_than(7.5)
+ end
- context "for a float column" do
- should "set the correct expected values for a _greater_than column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "rating"} > 21)
- SQL
- assert_equal expected.strip, @class.rating_greater_than(21).to_sql
- end
- should "set the correct expected values for a _greater_than_or_equal_to column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "rating"} >= 21)
- SQL
- assert_equal expected.strip, @class.rating_greater_than_or_equal_to(21).to_sql
- end
- should "set the correct expected values for a _less_than column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "rating"} < 21)
- SQL
- assert_equal expected.strip, @class.rating_less_than(21).to_sql
- end
- should "set the correct expected values for a _less_than_or_equal_to column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "rating"} <= 21)
- SQL
- assert_equal expected.strip, @class.rating_less_than_or_equal_to(21).to_sql
- end
- end
+ test "set the correct expected values for a _less_than_or_equal_to column method for a float column" do
+ assert_equal [@legal, @older], User.rating_less_than_or_equal_to(7.5)
end
end
View
41 test/order_test.rb
@@ -1,31 +1,22 @@
require 'test_helper'
-class OrderTest < Test::Unit::TestCase
+class OrderTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- context "with order methods" do
- should "set the correct expected values for a by_ column method with no args" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} ORDER BY #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "first_name"} asc
- SQL
- assert_equal expected.strip, @class.by_first_name.to_sql
- end
- should "set the correct expected values for a by_ column method with asc args" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} ORDER BY #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "first_name"} asc
- SQL
- assert_equal expected.strip, @class.by_first_name(:asc).to_sql
- end
- should "set the correct expected values for a by_ column method with desc args" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} ORDER BY #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "first_name"} desc
- SQL
- assert_equal expected.strip, @class.by_first_name(:desc).to_sql
- end
- end
+ setup do
+ @first = Factory :user, :first_name => 'Abe'
+ @last = Factory :user, :first_name => 'Zed'
+ end
+
+ test "set the correct expected values for a by_ column method with no args" do
+ assert_equal [@first, @last], User.by_first_name
+ end
+
+ test "set the correct expected values for a by_ column method with asc args" do
+ assert_equal [@first, @last], User.by_first_name(:asc)
+ end
+
+ test "set the correct expected values for a by_ column method with desc args" do
+ assert_equal [@last, @first], User.by_first_name(:desc)
end
end
View
31 test/polymorph_test.rb
@@ -1,25 +1,18 @@
require 'test_helper'
-class PolymorphTest < Test::Unit::TestCase
+class PolymorphTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = Post
- end
- context "with polymorph methods" do
- should "set the correct expected values on a _for column method with Class" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "owner_type"} = 'User')
- SQL
- assert_equal expected.strip, @class.for_owner_type(User).to_sql
- end
- should "set the correct expected values on a _for column method with String" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "owner_type"} = 'User')
- SQL
- assert_equal expected.strip, @class.for_owner_type('User').to_sql
- end
- end
+ setup do
+ @owned_by_user = Factory :post, :owner_type => 'User'
+ @owned_by_mammal = Factory :post, :owner_type => 'Mammal'
+ end
+
+ test "set the correct expected values on a _for column method with Class" do
+ assert_equal [@owned_by_user], Post.for_owner_type(User)
+ end
+
+ test "set the correct expected values on a _for column method with String" do
+ assert_equal [@owned_by_user], Post.for_owner_type('User')
end
end
View
40 test/presence_test.rb
@@ -1,29 +1,23 @@
require 'test_helper'
-class PresenceTest < Test::Unit::TestCase
+class PresenceTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- context "with presence methods" do
- should "set the correct expected values for a _present column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "first_name"} IS NOT NULL)
- SQL
- assert_equal expected.strip, @class.first_name_present.to_sql
- end
- should "set the correct expected values for a _missing column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "first_name"} IS NULL)
- SQL
- assert_equal expected.strip, @class.first_name_missing.to_sql
- end
- should "not setup methods for boolean columns" do
- assert_raise(NoMethodError) { @class.admin_missing }
- assert_raise(NoMethodError) { @class.admin_present }
- end
- end
+ setup do
+ @not_null = Factory :user, :first_name => 'Fake'
+ @null = Factory :user, :first_name => nil
+ end
+
+ test "set the correct expected values for a _present column method" do
+ assert_equal [@not_null], User.first_name_present
+ end
+
+ test "set the correct expected values for a _missing column method" do
+ assert_equal [@null], User.first_name_missing
+ end
+
+ test "not setup methods for boolean columns" do
+ assert_raise(NoMethodError) { User.admin_missing }
+ assert_raise(NoMethodError) { User.admin_present }
end
end
View
34 test/ranking_test.rb
@@ -1,25 +1,21 @@
require 'test_helper'
-class RankingTest < Test::Unit::TestCase
+class RankingTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- context "with association methods" do
- should "set the correct expected values on a maximum_ column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.*, count(#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name @class.primary_key}) as comments_count FROM #{@class.quoted_table_name} inner join comments on comments.user_id = #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name @class.primary_key} GROUP BY comments.user_id ORDER BY comments_count desc
- SQL
- assert_equal expected.strip, @class.maximum_comments.to_sql
- end
- should "set the correct expected values on a minimum_ column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.*, count(#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name @class.primary_key}) as comments_count FROM #{@class.quoted_table_name} inner join comments on comments.user_id = #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name @class.primary_key} GROUP BY comments.user_id ORDER BY comments_count asc
- SQL
- assert_equal expected.strip, @class.minimum_comments.to_sql
- end
- end
+ setup do
+ @many = Factory :user
+ 5.times { Factory :comment, :user => @many }
+ @few = Factory :user
+ 2.times { Factory :comment, :user => @few }
+ @none = Factory :user
+ end
+
+ test "set the correct expected values on a maximum_ column method" do
+ assert_equal [@many, @few], User.maximum_comments
+ end
+
+ test "set the correct expected values on a minimum_ column method" do
+ assert_equal [@few, @many], User.minimum_comments
end
end
View
124 test/search_test.rb
@@ -1,77 +1,63 @@
require 'test_helper'
-class SearchTest < Test::Unit::TestCase
+class SearchTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = User
- end
- should "set the correct expected values for a _equals column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} = 'test')
- SQL
- assert_equal expected.strip, @class.first_name_equals('test').to_sql
- end
- should "set the correct expected values for a _equals column method with an Array as value" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} IN ('test', 'this'))
- SQL
- assert_equal expected.strip, @class.first_name_equals(['test','this']).to_sql
- end
- should "set the correct expected values for a _equals column method with nil as value" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} IS NULL)
- SQL
- assert_equal expected.strip, @class.first_name_equals(nil).to_sql
- end
- should "set the correct expected values for a _matches column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} LIKE '%test%')
- SQL
- assert_equal expected.strip, @class.first_name_matches('test').to_sql
- end
- should "set the correct expected values for a _starts_with column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} LIKE 'test%')
- SQL
- assert_equal expected.strip, @class.first_name_starts_with('test').to_sql
- end
- should "set the correct expected values for a _ends_with column method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} LIKE '%test')
- SQL
- assert_equal expected.strip, @class.first_name_ends_with('test').to_sql
- end
- should "set the correct expected values for a search_for method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} LIKE '%test%' OR #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("last_name")} LIKE '%test%' OR #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("description")} LIKE '%test%')
- SQL
- assert_equal expected.strip, @class.search_for('test').to_sql
- end
- should "set the correct expected values for a search_for method with :on option" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} LIKE '%test%' OR #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("description")} LIKE '%test%')
- SQL
- assert_equal expected.strip, @class.search_for('test', :on => [:first_name, :description]).to_sql
- end
- should "set the correct expected values for a search_for method with an :require option" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("first_name")} LIKE '%test%' AND #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("last_name")} LIKE '%test%' AND #{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name("description")} LIKE '%test%')
- SQL
- assert_equal expected.strip, @class.search_for('test', :require => :all).to_sql
- end
+ setup do
+ @abe = Factory :user, :first_name => 'Abe', :last_name => 'Anderson', :description => 'Apple'
+ @bob = Factory :user, :first_name => 'Bob', :last_name => 'Babson', :description => 'Banana'
+ @cindy = Factory :user, :first_name => 'Cindy', :last_name => 'Clarkson', :description => 'Cookie'
+ @dave = Factory :user, :first_name => 'Dave'
+ @ed = Factory :user, :last_name => 'Dave'
+ @frank = Factory :user, :description => 'Dave'
+ @george = Factory :user, :first_name => 'Dave', :last_name => 'Dave', :description => 'Dave'
+
+ @null = Factory :user, :first_name => nil, :last_name => nil, :description => nil
+ end
+
+ test "set the correct expected values for a _equals column method" do
+ assert_equal [@abe], User.first_name_equals('Abe')
+ end
+
+ test "set the correct expected values for a _equals column method with an Array as value" do
+ assert_equal [@abe, @bob], User.first_name_equals(['Abe', 'Bob'])
+ end
+
+ test "set the correct expected values for a _equals column method with nil as value" do
+ assert_equal [@ed, @frank, @null], User.first_name_equals(nil)
+ end
+
+ test "set the correct expected values for a _matches column method" do
+ assert_equal [@cindy], User.first_name_matches('ind')
+ assert_equal [@cindy], User.first_name_matches('IND')
+ end
+
+ test "set the correct expected values for a _starts_with column method" do
+ assert_equal [@abe], User.first_name_starts_with('Ab')
+ assert_equal [@abe], User.first_name_starts_with('aB')
+ end
+
+ test "set the correct expected values for a _ends_with column method" do
+ assert_equal [@bob], User.first_name_ends_with('ob')
+ assert_equal [@bob], User.first_name_ends_with('Ob')
+ end
+
+ test "set the correct expected values for a search_for method" do
+ assert_equal [@dave, @ed, @frank, @george], User.search_for('Dave')
+ assert_equal [@dave, @ed, @frank, @george], User.search_for('dave')
+ end
+
+ test "set the correct expected values for a search_for method with :on option" do
+ assert_equal [@dave, @frank, @george], User.search_for('Dave', :on => [:first_name, :description])
+ assert_equal [@dave, @frank, @george], User.search_for('dave', :on => [:first_name, :description])
+ end
+
+ test "set the correct expected values for a search_for method with an :require option" do
+ assert_equal [@george], User.search_for('Dave', :require => :all)
+ assert_equal [@george], User.search_for('dave', :require => :all)
end
- context "A class which has included Pacecar but which has nothing to search" do
- setup do
- @class = Mammal
- end
- should "set the correct expected values for a search_for method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name}
- SQL
- assert_equal expected.strip, @class.search_for('test').to_sql
- end
+ test "set the correct expected values for a search_for method on a class with no columns" do
+ assert_equal [], Mammal.search_for('test')
end
end
View
122 test/state_test.rb
@@ -1,81 +1,65 @@
require 'test_helper'
-class StateTest < Test::Unit::TestCase
+class StateTest < ActiveSupport::TestCase
- context "A class which has included Pacecar" do
- setup do
- @class = Post
- end
- should "set the correct expected values for a column_state _state method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "publication_state"} = 'Draft')
- SQL
- assert_equal expected.strip, @class.publication_state_draft.to_sql
- end
- should "set the correct expected values for a column_not_state _state method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "publication_state"} <> 'Draft')
- SQL
- assert_equal expected.strip, @class.publication_state_not_draft.to_sql
- end
- should "set the correct expected values for a column_state _type method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "post_type"} = 'PostModern')
- SQL
- assert_equal expected.strip, @class.post_type_postmodern.to_sql
+ setup do
+ @draft_post = Factory :post, :publication_state => 'Draft'
+ @final_post = Factory :post, :publication_state => 'Final'
+ @free_post = Factory :post, :post_type => 'Free'
+ @open_post = Factory :post, :post_type => 'Open'
+
+ @post = Post.new
+ assert_not_nil Post::PUBLICATION_STATES
+ end
+
+ test "set the correct expected values for a column_state _state method" do
+ assert_equal [@draft_post], Post.publication_state_draft
+ end
+
+ test "set the correct expected values for a column_not_state _state method" do
+ assert_equal [@final_post], Post.publication_state_not_draft
+ end
+
+ test "set the correct expected values for a column_state _type method" do
+ assert_equal [@free_post], Post.post_type_free
+ end
+
+ test "set the correct expected values for a column_not_state _type method" do
+ assert_equal [@open_post], Post.post_type_not_free
+ end
+
+ test "set the correct expected values for a column_state method" do
+ assert_equal [@open_post], Post.post_type('Open')
+ end
+
+ test "set the correct expected values for a column_state_not method" do
+ assert_equal [@free_post], Post.post_type_not('Open')
+ end
+
+ Post::PUBLICATION_STATES.each do |state|
+ test "have a query method for a #{state} state value" do
+ assert @post.respond_to?("publication_state_#{state.downcase}?")
end
- should "set the correct expected values for a column_not_state _type method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "post_type"} <> 'PostModern')
- SQL
- assert_equal expected.strip, @class.post_type_not_postmodern.to_sql
+ test "have a non query method for a #{state} state value" do
+ assert @post.respond_to?("publication_state_not_#{state.downcase}?")
end
- should "set the correct expected values for a column_state method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "post_type"} = 'PostModern')
- SQL
- assert_equal expected.strip, @class.post_type('PostModern').to_sql
+
+ test "respond true to query method for a #{state} state value when in that state" do
+ @post.publication_state = state
+ assert @post.send("publication_state_#{state.downcase}?")
end
- should "set the correct expected values for a column_state_not method" do
- expected =<<-SQL
- SELECT #{@class.quoted_table_name}.* FROM #{@class.quoted_table_name} WHERE (#{@class.quoted_table_name}.#{ActiveRecord::Base.connection.quote_column_name "post_type"} <> 'PostModern')
- SQL
- assert_equal expected.strip, @class.post_type_not('PostModern').to_sql
+ test "respond false to not query method for a #{state} state value when in that state" do
+ @post.publication_state = state
+ assert ! @post.send("publication_state_not_#{state.downcase}?")
end
- end
- context "A Post" do
- setup do
- @post = Post.new
- assert_not_nil Post::PUBLICATION_STATES
+ test "respond false to query method for a #{state} state value when not in that state" do
+ @post.publication_state = 'Invalid'
+ assert ! @post.send("publication_state_#{state.downcase}?")
end
- Post::PUBLICATION_STATES.each do |state|
- context "with a #{state} state value" do
- should "have a query method" do
- assert @post.respond_to?("publication_state_#{state.downcase}?")
- end
- should "have a non query method" do
- assert @post.respond_to?("publication_state_not_#{state.downcase}?")
- end
- context "when in that state" do
- setup { @post.publication_state = state }
- should "respond true to query method" do
- assert @post.send("publication_state_#{state.downcase}?")
- end
- should "respond false to not query method" do
- assert ! @post.send("publication_state_not_#{state.downcase}?")
- end
- end
- context "when not in that state" do
- setup { @post.publication_state = 'Invalid' }
- should "respond false to query method" do
- assert ! @post.send("publication_state_#{state.downcase}?")
- end
- should "respond true to not query method" do
- assert @post.send("publication_state_not_#{state.downcase}?")
- end
- end
- end
+ test "respond true to not query method for a #{state} state value when not in that state" do
+ @post.publication_state = 'Invalid'
+ assert @post.send("publication_state_not_#{state.downcase}?")
end
end
View
4 test/support/integration_case.rb
@@ -1,5 +1,5 @@
-# Define a bare test case to use with Capybara
+# # Define a bare test case to use with Capybara
class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
- include Capybara
+ # include Capybara
include Rails.application.routes.url_helpers
end
View
15 test/test_helper.rb
@@ -11,14 +11,19 @@
Rails.backtrace_cleaner.remove_silencers!
# Configure capybara for integration testing
-require "capybara/rails"
-Capybara.default_driver = :rack_test
-Capybara.default_selector = :css
+# require "capybara/rails"
+# Capybara.default_driver = :rack_test
+# Capybara.default_selector = :css
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
-require 'shoulda'
-require 'shoulda/rails'
require 'mocha'
+require 'factory_girl'
+require 'test/factories'
begin require 'redgreen'; rescue LoadError; end
+
+class ActiveSupport::TestCase
+ self.use_transactional_fixtures = true
+ self.use_instantiated_fixtures = false
+end
Please sign in to comment.
Something went wrong with that request. Please try again.