@@ -98,7 +98,64 @@ def ids
98
98
assert_equal @transform . call ( model ) , { index : { _id : "1" , data : { } } }
99
99
end
100
100
end
101
- end
102
101
102
+ should "limit the relation to a specific scope" do
103
+ relation = mock ( )
104
+ relation . stubs ( :no_timeout ) . returns ( relation )
105
+ relation . expects ( :published ) . returns ( relation )
106
+ relation . expects ( :each_slice ) . returns ( [ ] )
107
+ DummyClassForMongoid . expects ( :all ) . returns ( relation )
108
+
109
+ DummyClassForMongoid . __send__ :extend , Elasticsearch ::Model ::Adapter ::Mongoid ::Importing
110
+ DummyClassForMongoid . __find_in_batches ( scope : :published ) do ; end
111
+ end
112
+
113
+ context "when limit the relation with proc" do
114
+ setup do
115
+ @query = Proc . new { where ( color : "red" ) }
116
+ end
117
+ should "query with a specific criteria" do
118
+ relation = mock ( )
119
+ relation . stubs ( :no_timeout ) . returns ( relation )
120
+ relation . expects ( :class_exec ) . returns ( relation )
121
+ relation . expects ( :each_slice ) . returns ( [ ] )
122
+ DummyClassForMongoid . expects ( :all ) . returns ( relation )
123
+
124
+ DummyClassForMongoid . __find_in_batches ( query : @query ) do ; end
125
+ end
126
+ end
127
+
128
+ context "when limit the relation with hash" do
129
+ setup do
130
+ @query = { color : "red" }
131
+ end
132
+ should "query with a specific criteria" do
133
+ relation = mock ( )
134
+ relation . stubs ( :no_timeout ) . returns ( relation )
135
+ relation . expects ( :class_exec ) . returns ( relation )
136
+ relation . expects ( :each_slice ) . returns ( [ ] )
137
+ DummyClassForMongoid . expects ( :all ) . returns ( relation )
138
+
139
+ DummyClassForMongoid . __find_in_batches ( query : @query ) do ; end
140
+ end
141
+ end
142
+
143
+ should "preprocess the batch if option provided" do
144
+ class << DummyClassForMongoid
145
+ # Updates/transforms the batch while fetching it from the database
146
+ # (eg. with information from an external system)
147
+ #
148
+ def update_batch ( batch )
149
+ batch . collect { |b | b . to_s + '!' }
150
+ end
151
+ end
152
+
153
+ DummyClassForMongoid . expects ( :__find_in_batches ) . returns ( [ :a , :b ] )
154
+
155
+ DummyClassForMongoid . __find_in_batches ( preprocess : :update_batch ) do |batch |
156
+ assert_same_elements [ "a!" , "b!" ] , batch
157
+ end
158
+ end
159
+ end
103
160
end
104
161
end
0 commit comments