Module: ActiveRecord::FindByExtension::Etest

Defined in:
lib/vex/active_record/find_by_extension.rb,
lib/vex/active_record/resolver.rb

Defined Under Namespace

Classes: Data

Instance Method Summary collapse

Instance Method Details

#setupObject


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vex/active_record/find_by_extension.rb', line 68

def setup
  Data.lite_table do
    string :name
    string :age
  end

  Data.create! :name => "name", :age => 2
  Data.create! :name => "name", :age => 3
  Data.create! :name => "name", :age => 4
  Data.create! :name => "name2", :age => 2
  Data.create! :name => "name2", :age => 3
  Data.create! :name => "name2", :age => 4

  assert_equal(6, Data.count)
end

#teardownObject


84
85
86
# File 'lib/vex/active_record/find_by_extension.rb', line 84

def teardown
  Data.destroy_all
end

#test_find_all_byObject


88
89
90
91
92
93
94
95
96
97
# File 'lib/vex/active_record/find_by_extension.rb', line 88

def test_find_all_by
  assert_equal(3, Data.find_all_by(:name => "name").length)
  assert_equal(3, Data.find_all_by(:name => [ "name" ]).length)
  assert_raise(ActiveRecord::StatementInvalid) {
    assert_equal(3, Data.find_all_by(:unknown => [ "name" ]).length)
  }

  assert_equal(2, Data.find_all_by(:name => [ "name" ], :age => [1, 2, 3]).length)
  assert_equal(2, Data.find_all_by({:name => [ "name" ]}, :conditions => { :age => [1, 2, 3]}).length)
end

#test_find_byObject


99
100
101
102
# File 'lib/vex/active_record/find_by_extension.rb', line 99

def test_find_by
  assert_equal("name", Data.find_by(:name => "name").name)
  assert_equal(nil, Data.find_by(:name => "namex"))
end

#test_find_by!Object


104
105
106
107
108
109
# File 'lib/vex/active_record/find_by_extension.rb', line 104

def test_find_by!
  assert_equal("name", Data.find_by!(:name => "name").name)
  assert_raise(ActiveRecord::RecordNotFound) do
    Data.find_by!(:name => "namex")
  end
end

#test_resolveObject


40
41
42
43
44
45
46
47
48
# File 'lib/vex/active_record/resolver.rb', line 40

def test_resolve
  data2 = Data.find_by :name => "name", :age => 2
  data3 = Data.find_by :name => "name", :age => 3

  assert_equal data2, Data.resolve(data2)
  assert_equal data2, Data.resolve(data2.id)
  assert_equal data2, Data.resolve(data2.id.to_s)
  assert_equal [ data2, data3 ].sort_by(&:id), Data.resolve([data2.id, data3.id]).sort_by(&:id)
end