-
Notifications
You must be signed in to change notification settings - Fork 382
/
test_finder_options.rb
133 lines (109 loc) · 3.97 KB
/
test_finder_options.rb
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require 'test_helper'
class FinderOptionsTest < Test::Unit::TestCase
include MongoMapper
should "raise error if provided something other than a hash" do
lambda { FinderOptions.new }.should raise_error(ArgumentError)
lambda { FinderOptions.new(1) }.should raise_error(ArgumentError)
end
should "have symbolize the keys of the hash provided" do
FinderOptions.new('offset' => 1).options.keys.map do |key|
key.should be_instance_of(Symbol)
end
end
context "Converting conditions to criteria" do
should "work with simple criteria" do
FinderOptions.new(:conditions => {:foo => 'bar'}).criteria.should == {
:foo => 'bar'
}
FinderOptions.new(:conditions => {:foo => 'bar', :baz => 'wick'}).criteria.should == {
:foo => 'bar',
:baz => 'wick'
}
end
should "use $in for arrays" do
FinderOptions.new(:conditions => {:foo => [1,2,3]}).criteria.should == {
:foo => {'$in' => [1,2,3]}
}
end
should "work arbitrarily deep" do
FinderOptions.new(:conditions => {:foo => {:bar => [1,2,3]}}).criteria.should == {
:foo => {:bar => {'$in' => [1,2,3]}}
}
end
end
context "ordering" do
should "single field with ascending direction" do
hash = OrderedHash.new
hash[:foo] = 1
FinderOptions.new(:order => 'foo asc').options[:sort].should == hash
FinderOptions.new(:order => 'foo ASC').options[:sort].should == hash
end
should "single field with descending direction" do
hash = OrderedHash.new
hash[:foo] = -1
FinderOptions.new(:order => 'foo desc').options[:sort].should == hash
FinderOptions.new(:order => 'foo DESC').options[:sort].should == hash
end
should "convert field without direction to ascending" do
hash = OrderedHash.new
hash[:foo] = 1
FinderOptions.new(:order => 'foo').options[:sort].should == hash
end
should "convert multiple fields with directions" do
hash = OrderedHash.new
hash[:foo] = -1
hash[:bar] = 1
hash[:baz] = -1
options = FinderOptions.new(:order => 'foo desc, bar asc, baz desc').options[:sort].should == hash
end
should "convert multiple fields with some missing directions" do
hash = OrderedHash.new
hash[:foo] = -1
hash[:bar] = 1
hash[:baz] = 1
options = FinderOptions.new(:order => 'foo desc, bar, baz').options[:sort].should == hash
end
end
context "offset" do
should "default to 0" do
FinderOptions.new({}).options[:offset].should == 0
end
should "use offset provided" do
FinderOptions.new(:offset => 2).options[:offset].should == 2
end
should "covert string to integer" do
FinderOptions.new(:offset => '2').options[:offset].should == 2
end
end
context "limit" do
should "default to 0" do
FinderOptions.new({}).options[:limit].should == 0
end
should "use offset provided" do
FinderOptions.new(:limit => 2).options[:limit].should == 2
end
should "covert string to integer" do
FinderOptions.new(:limit => '2').options[:limit].should == 2
end
end
context "fields" do
should "default to nil" do
FinderOptions.new({}).options[:fields].should be(nil)
end
should "be converted to nil if empty string" do
FinderOptions.new(:fields => '').options[:fields].should be(nil)
end
should "be converted to nil if []" do
FinderOptions.new(:fields => []).options[:fields].should be(nil)
end
should "should work with array" do
FinderOptions.new({:fields => %w(a b)}).options[:fields].should == %w(a b)
end
should "convert comma separated list to array" do
FinderOptions.new({:fields => 'a, b'}).options[:fields].should == %w(a b)
end
should "also work as select" do
FinderOptions.new(:select => %w(a b)).options[:fields].should == %w(a b)
end
end
end # FinderOptionsTest