public
Description: Ruby on Rails plugin to simplify searching your model
Homepage: http://www.ridaalbarazi.com/blog/2008/11/05/simplysearchable-plugin/
Clone URL: git://github.com/rbarazi/simply_searchable.git
simply_searchable / test / simply_searchable_test.rb
100644 84 lines (69 sloc) 2.053 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'test/unit'
 
require 'rubygems'
require 'active_record'
 
$:.unshift File.dirname(__FILE__) + '/../lib'
require File.dirname(__FILE__) + '/../init'
 
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
 
# AR keeps printing annoying schema statements
$stdout = StringIO.new
 
def setup_db
  ActiveRecord::Base.logger
  ActiveRecord::Schema.define(:version => 1) do
    create_table :posts do |t|
      t.column :id, :integer
      t.column :title, :string
      t.column :body, :text
      t.column :category_id, :integer
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end
    create_table :categories do |t|
      t.column :id, :integer
      t.column :name, :string
      t.column :created_at, :datetime
      t.column :updated_at, :datetime
    end
  end
end
 
def teardown_db
  ActiveRecord::Base.connection.tables.each do |table|
    ActiveRecord::Base.connection.drop_table(table)
  end
end
 
setup_db
class Post < ActiveRecord::Base
  belongs_to :category
  simply_searchable
end
class Category < ActiveRecord::Base
  has_many :posts
end
teardown_db
 
class SimplySearchableTest < Test::Unit::TestCase
  
  def setup
    setup_db
    @category = Category.create(:name => 'The category')
    @post = Post.create(:title => 'Some title', :body => 'Some text here', :category_id => @category.id)
  end
 
  def teardown
    teardown_db
  end
 
  def test_creation_of_named_scope_methods_for_attributes
    assert Post.methods.include?('where_id')
    assert Post.methods.include?('where_title')
    assert Post.methods.include?('where_body')
  end
  
  def test_creation_of_named_scope_methods_for_associations
    assert Post.methods.include?('where_category')
  end
  
  def test_creation_of_list
    assert Post.methods.include?('list')
  end
  
  def test_attributes_conditions
    assert !Post.where_title('title').empty?
    assert !Post.where_body('text').empty?
  end
  
  def test_associations_conditions
    assert !Post.where_category(@category.id).empty?
  end
end