mongo-ruby-driver/test/conversions_test.rb

120 lines
3.0 KiB
Ruby
Raw Permalink Normal View History

2010-09-09 19:58:51 +00:00
require './test/test_helper'
2010-01-11 18:12:09 +00:00
require 'mongo/exceptions'
2009-10-06 23:20:21 +00:00
require 'mongo/util/conversions'
class ConversionsTest < Test::Unit::TestCase
include Mongo::Conversions
2009-12-29 16:31:59 +00:00
def test_array_as_sort_parameters_with_array_of_key_and_value
params = array_as_sort_parameters(["field1", "asc"])
assert_equal({"field1" => 1}, params)
2009-10-06 23:20:21 +00:00
end
def test_array_as_sort_parameters_with_array_of_string_and_values
params = array_as_sort_parameters([["field1", :asc], ["field2", :desc]])
assert_equal({ "field1" => 1, "field2" => -1 }, params)
end
def test_string_as_sort_parameters_with_string
params = string_as_sort_parameters("field")
assert_equal({ "field" => 1 }, params)
end
def test_string_as_sort_parameters_with_empty_string
params = string_as_sort_parameters("")
assert_equal({}, params)
end
def test_symbol_as_sort_parameters
params = string_as_sort_parameters(:field)
2009-10-06 23:20:21 +00:00
assert_equal({ "field" => 1 }, params)
end
def test_sort_value_when_value_is_one
assert_equal 1, sort_value(1)
end
def test_sort_value_when_value_is_one_as_a_string
assert_equal 1, sort_value("1")
end
def test_sort_value_when_value_is_negative_one
assert_equal(-1, sort_value(-1))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_negative_one_as_a_string
assert_equal(-1, sort_value("-1"))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_ascending
assert_equal 1, sort_value("ascending")
end
def test_sort_value_when_value_is_asc
assert_equal 1, sort_value("asc")
end
def test_sort_value_when_value_is_uppercase_ascending
assert_equal 1, sort_value("ASCENDING")
end
def test_sort_value_when_value_is_uppercase_asc
assert_equal 1, sort_value("ASC")
end
def test_sort_value_when_value_is_symbol_ascending
assert_equal 1, sort_value(:ascending)
end
def test_sort_value_when_value_is_symbol_asc
assert_equal 1, sort_value(:asc)
end
def test_sort_value_when_value_is_symbol_uppercase_ascending
assert_equal 1, sort_value(:ASCENDING)
end
def test_sort_value_when_value_is_symbol_uppercase_asc
assert_equal 1, sort_value(:ASC)
end
def test_sort_value_when_value_is_descending
assert_equal(-1, sort_value("descending"))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_desc
assert_equal(-1, sort_value("desc"))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_uppercase_descending
assert_equal(-1, sort_value("DESCENDING"))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_uppercase_desc
assert_equal(-1, sort_value("DESC"))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_symbol_descending
assert_equal(-1, sort_value(:descending))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_symbol_desc
assert_equal(-1, sort_value(:desc))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_uppercase_symbol_descending
assert_equal(-1, sort_value(:DESCENDING))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_uppercase_symbol_desc
assert_equal(-1, sort_value(:DESC))
2009-10-06 23:20:21 +00:00
end
def test_sort_value_when_value_is_invalid
assert_raise Mongo::InvalidSortValueError do
sort_value(2)
end
end
end