Skip to content
This repository has been archived by the owner on Nov 17, 2018. It is now read-only.

Commit

Permalink
* completed base unit test suite. tests should be refactored in the f…
Browse files Browse the repository at this point in the history
…uture in order to send real queries instead of simple SQL statements
  • Loading branch information
weppos committed Jun 5, 2008
1 parent 5272ca8 commit c4ab0db
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
6 changes: 5 additions & 1 deletion test/helper_database.rb
Expand Up @@ -13,9 +13,13 @@
#++


# reset database
# FIXME: move the following execution within the unit test setup method


# reset database and prepare for tests
database = File.dirname(__FILE__) + '/db/test.sqlite3'
File.delete(database) if File.file? database
FileUtils.mkpath(File.dirname(database)) unless File.directory? File.dirname(database)

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
Expand Down
62 changes: 59 additions & 3 deletions test/unit/test_multi_conditions.rb
Expand Up @@ -19,7 +19,6 @@
class MultiConditionsTest < Test::Unit::TestCase

def setup

end

# =========================================================================
Expand Down Expand Up @@ -50,9 +49,66 @@ def test_initialize_with_block
assert_kind_of(ActiveRecord::Base::MultiConditions, obj)
end
end


# =========================================================================
# Condition string
# =========================================================================
#
# TODO: Consider to move check from a simple string to a real SQL query.
# Using a real query statement is more efficient and ensures
# finder methods works as expected.
#
# TODO: add test for with_scope and named_scope(s).
#

def instance(condition = nil)
Task::MultiConditions.new(condition)
def test_single_condition_string
[{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]].each do |condition|
mc = Task::MultiConditions.new(condition)
assert_match(/"?foo"? = 'bar'$/, mc.to_conditions)
end
end

def test_single_condition_fixnum
[{:foo => 1}, "foo = 1", ["foo = ?", 1], ["foo = :foo", {:foo => 1}]].each do |condition|
mc = Task::MultiConditions.new(condition)
assert_match(/"?foo"? = 1$/, mc.to_conditions)
end
end

def test_multiple_conditions
first = [{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]]
second = [{:baz => 3}, "baz = 3", ["baz = ?", 3], ["baz = :baz", {:baz => 3}]]
first.size.times do |index|
mc = Task::MultiConditions.new
mc.append_condition(first[index])
mc.append_condition(second[index])
assert_match(/"?foo"? = 'bar'/, mc.to_conditions)
assert_match(/"?baz"? = 3/, mc.to_conditions)
end
end

def test_mixed_conditions
first = [{:foo => 'bar'}, "foo = 'bar'", ["foo = ?", 'bar'], ["foo = :foo", {:foo => 'bar'}]]
second = ["baz = 3", ["baz = ?", 3], ["baz = :baz", {:baz => 3}], {:baz => 3}]
first.size.times do |index|
mc = Task::MultiConditions.new
mc.append_condition(first[index])
mc.append_condition(second[index])
assert_match(/"?foo"? = 'bar'/, mc.to_conditions)
assert_match(/"?baz"? = 3/, mc.to_conditions)
end
end


protected

def instance(condition = nil)
Task::MultiConditions.new(condition)
end

def table_name
Task.table_name
end

end

0 comments on commit c4ab0db

Please sign in to comment.