Skip to content

Commit ba44719

Browse files
committed
Refactor timestamp test
1 parent 94af38a commit ba44719

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

test/ecto/integration/timestamps_test.exs

+17-17
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ defmodule Ecto.Integration.TimestampsTest do
126126
test "insert and fetch nil values" do
127127
now = DateTime.utc_now()
128128

129-
{:ok, product} =
130-
%Product{}
131-
|> Product.changeset(%{name: "Nil Date Test", approved_at: now, ordered_at: now})
132-
|> TestRepo.insert()
129+
product = insert_product(%{name: "Nil Date Test", approved_at: now, ordered_at: now})
133130

134131
product = TestRepo.get(Product, product.id)
135132
assert product.name == "Nil Date Test"
@@ -146,34 +143,25 @@ defmodule Ecto.Integration.TimestampsTest do
146143
end
147144

148145
test "datetime comparisons" do
149-
account =
150-
%Account{}
151-
|> Account.changeset(%{name: "Test"})
152-
|> TestRepo.insert!()
146+
account = insert_account(%{name: "Test"})
153147

154-
%Product{}
155-
|> Product.changeset(%{
148+
insert_product(%{
156149
account_id: account.id,
157150
name: "Foo",
158151
approved_at: ~U[2023-01-01T01:00:00Z]
159152
})
160-
|> TestRepo.insert!()
161153

162-
%Product{}
163-
|> Product.changeset(%{
154+
insert_product(%{
164155
account_id: account.id,
165156
name: "Bar",
166157
approved_at: ~U[2023-01-01T02:00:00Z]
167158
})
168-
|> TestRepo.insert!()
169159

170-
%Product{}
171-
|> Product.changeset(%{
160+
insert_product(%{
172161
account_id: account.id,
173162
name: "Qux",
174163
approved_at: ~U[2023-01-01T03:00:00Z]
175164
})
176-
|> TestRepo.insert!()
177165

178166
since = ~U[2023-01-01T01:59:00Z]
179167

@@ -187,4 +175,16 @@ defmodule Ecto.Integration.TimestampsTest do
187175
|> order_by([p], desc: p.approved_at)
188176
|> TestRepo.all()
189177
end
178+
179+
defp insert_account(attrs) do
180+
%Account{}
181+
|> Account.changeset(attrs)
182+
|> TestRepo.insert!()
183+
end
184+
185+
defp insert_product(attrs) do
186+
%Product{}
187+
|> Product.changeset(attrs)
188+
|> TestRepo.insert!()
189+
end
190190
end

test/support/schemas/product.ex

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ defmodule EctoSQLite3.Schemas.Product do
2424

2525
def changeset(struct, attrs) do
2626
struct
27-
|> cast(attrs, [:name, :description, :tags, :account_id, :approved_at, :ordered_at])
27+
|> cast(attrs, [
28+
:name,
29+
:description,
30+
:tags,
31+
:account_id,
32+
:approved_at,
33+
:ordered_at,
34+
:inserted_at
35+
])
2836
|> validate_required([:name])
2937
|> maybe_generate_external_id()
3038
end

0 commit comments

Comments
 (0)