Skip to content

Commit

Permalink
Refactor timestamp test
Browse files Browse the repository at this point in the history
  • Loading branch information
warmwaffles committed May 15, 2024
1 parent 94af38a commit ba44719
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
34 changes: 17 additions & 17 deletions test/ecto/integration/timestamps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ defmodule Ecto.Integration.TimestampsTest do
test "insert and fetch nil values" do
now = DateTime.utc_now()

{:ok, product} =
%Product{}
|> Product.changeset(%{name: "Nil Date Test", approved_at: now, ordered_at: now})
|> TestRepo.insert()
product = insert_product(%{name: "Nil Date Test", approved_at: now, ordered_at: now})

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

test "datetime comparisons" do
account =
%Account{}
|> Account.changeset(%{name: "Test"})
|> TestRepo.insert!()
account = insert_account(%{name: "Test"})

%Product{}
|> Product.changeset(%{
insert_product(%{
account_id: account.id,
name: "Foo",
approved_at: ~U[2023-01-01T01:00:00Z]
})
|> TestRepo.insert!()

%Product{}
|> Product.changeset(%{
insert_product(%{
account_id: account.id,
name: "Bar",
approved_at: ~U[2023-01-01T02:00:00Z]
})
|> TestRepo.insert!()

%Product{}
|> Product.changeset(%{
insert_product(%{
account_id: account.id,
name: "Qux",
approved_at: ~U[2023-01-01T03:00:00Z]
})
|> TestRepo.insert!()

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

Expand All @@ -187,4 +175,16 @@ defmodule Ecto.Integration.TimestampsTest do
|> order_by([p], desc: p.approved_at)
|> TestRepo.all()
end

defp insert_account(attrs) do
%Account{}
|> Account.changeset(attrs)
|> TestRepo.insert!()
end

defp insert_product(attrs) do
%Product{}
|> Product.changeset(attrs)
|> TestRepo.insert!()
end
end
10 changes: 9 additions & 1 deletion test/support/schemas/product.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ defmodule EctoSQLite3.Schemas.Product do

def changeset(struct, attrs) do
struct
|> cast(attrs, [:name, :description, :tags, :account_id, :approved_at, :ordered_at])
|> cast(attrs, [
:name,
:description,
:tags,
:account_id,
:approved_at,
:ordered_at,
:inserted_at
])
|> validate_required([:name])
|> maybe_generate_external_id()
end
Expand Down

0 comments on commit ba44719

Please sign in to comment.