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 @@ -176,6 +176,40 @@ defmodule Ecto.Integration.TimestampsTest do
176
176
|> TestRepo . all ( )
177
177
end
178
178
179
+ test "using built in ecto functions" do
180
+ Application . put_env ( :ecto_sqlite3 , :datetime_type , :text_datetime )
181
+
182
+ account = insert_account ( % { name: "Test" } )
183
+
184
+ insert_product ( % {
185
+ account_id: account . id ,
186
+ name: "Foo" ,
187
+ inserted_at: days_ago ( 1 )
188
+ } )
189
+
190
+ insert_product ( % {
191
+ account_id: account . id ,
192
+ name: "Bar" ,
193
+ inserted_at: days_ago ( 2 )
194
+ } )
195
+
196
+ insert_product ( % {
197
+ account_id: account . id ,
198
+ name: "Qux" ,
199
+ inserted_at: days_ago ( 5 )
200
+ } )
201
+
202
+ assert [
203
+ % { name: "Foo" } ,
204
+ % { name: "Bar" }
205
+ ] =
206
+ Product
207
+ |> select ( [ p ] , p )
208
+ |> where ( [ p ] , p . inserted_at >= from_now ( - 3 , "day" ) )
209
+ |> order_by ( [ p ] , desc: p . inserted_at )
210
+ |> TestRepo . all ( )
211
+ end
212
+
179
213
defp insert_account ( attrs ) do
180
214
% Account { }
181
215
|> Account . changeset ( attrs )
@@ -187,4 +221,9 @@ defmodule Ecto.Integration.TimestampsTest do
187
221
|> Product . changeset ( attrs )
188
222
|> TestRepo . insert! ( )
189
223
end
224
+
225
+ defp days_ago ( days ) do
226
+ now = DateTime . utc_now ( )
227
+ DateTime . add ( now , - days * 24 * 60 * 60 , :second )
228
+ end
190
229
end
You can’t perform that action at this time.
0 commit comments