File tree 1 file changed +39
-0
lines changed 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -181,6 +181,40 @@ defmodule Ecto.Integration.TimestampsTest do
181
181
|> TestRepo . all ( )
182
182
end
183
183
184
+ test "using built in ecto functions" do
185
+ Application . put_env ( :ecto_sqlite3 , :datetime_type , :text_datetime )
186
+
187
+ account = insert_account ( % { name: "Test" } )
188
+
189
+ insert_product ( % {
190
+ account_id: account . id ,
191
+ name: "Foo" ,
192
+ inserted_at: days_ago ( 1 )
193
+ } )
194
+
195
+ insert_product ( % {
196
+ account_id: account . id ,
197
+ name: "Bar" ,
198
+ inserted_at: days_ago ( 2 )
199
+ } )
200
+
201
+ insert_product ( % {
202
+ account_id: account . id ,
203
+ name: "Qux" ,
204
+ inserted_at: days_ago ( 5 )
205
+ } )
206
+
207
+ assert [
208
+ % { name: "Foo" } ,
209
+ % { name: "Bar" }
210
+ ] =
211
+ Product
212
+ |> select ( [ p ] , p )
213
+ |> where ( [ p ] , p . inserted_at >= from_now ( - 3 , "day" ) )
214
+ |> order_by ( [ p ] , desc: p . inserted_at )
215
+ |> TestRepo . all ( )
216
+ end
217
+
184
218
defp insert_account ( attrs ) do
185
219
% Account { }
186
220
|> Account . changeset ( attrs )
@@ -192,4 +226,9 @@ defmodule Ecto.Integration.TimestampsTest do
192
226
|> Product . changeset ( attrs )
193
227
|> TestRepo . insert! ( )
194
228
end
229
+
230
+ defp days_ago ( days ) do
231
+ now = DateTime . utc_now ( )
232
+ DateTime . add ( now , - days * 24 * 60 * 60 , :second )
233
+ end
195
234
end
You can’t perform that action at this time.
0 commit comments