Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* completed base unit test suite. tests should be refactored in the f…

…uture in order to send real queries instead of simple SQL statements
  • Loading branch information...
commit c4ab0dbb2f54df4d4f8632dbfc04b5b270b17694 1 parent 5272ca8
@weppos authored
Showing with 64 additions and 4 deletions.
  1. +5 −1 test/helper_database.rb
  2. +59 −3 test/unit/test_multi_conditions.rb
View
6 test/helper_database.rb
@@ -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',
View
62 test/unit/test_multi_conditions.rb
@@ -19,7 +19,6 @@
class MultiConditionsTest < Test::Unit::TestCase
def setup
-
end
# =========================================================================
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.