Use utc_datetime in db schema

This commit is contained in:
Tusooa Zhu 2022-03-18 09:36:13 -04:00
parent ebcda5265b
commit 0c78ab4a88
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
3 changed files with 75 additions and 4 deletions

View file

@ -16,11 +16,11 @@ defmodule Pleroma.Announcement do
schema "announcements" do
field(:data, :map)
field(:starts_at, :naive_datetime)
field(:ends_at, :naive_datetime)
field(:starts_at, :utc_datetime)
field(:ends_at, :utc_datetime)
field(:rendered, :map)
timestamps()
timestamps(type: :utc_datetime)
end
def change(struct, params \\ %{}) do
@ -155,6 +155,6 @@ def list_all_visible_when(time) do
end
def list_all_visible do
list_all_visible_when(NaiveDateTime.utc_now())
list_all_visible_when(DateTime.now("Etc/UTC") |> elem(1))
end
end

View file

@ -156,6 +156,25 @@ test "it updates a field", %{conn: conn} do
assert NaiveDateTime.compare(new.starts_at, starts_at) == :eq
end
test "it updates with time with utc timezone", %{conn: conn} do
%{id: id} = insert(:announcement)
now = DateTime.now("Etc/UTC") |> elem(1) |> DateTime.truncate(:second)
starts_at = DateTime.add(now, -10, :second)
_response =
conn
|> put_req_header("content-type", "application/json")
|> patch("/api/v1/pleroma/admin/announcements/#{id}", %{
starts_at: DateTime.to_iso8601(starts_at)
})
|> json_response_and_validate_schema(:ok)
new = Pleroma.Announcement.get_by_id(id)
assert DateTime.compare(new.starts_at, starts_at) == :eq
end
test "it updates a data field", %{conn: conn} do
%{id: id} = announcement = insert(:announcement, data: %{"all_day" => true})
@ -230,5 +249,33 @@ test "it creates an announcement", %{conn: conn} do
assert NaiveDateTime.compare(announcement.starts_at, starts_at) == :eq
assert NaiveDateTime.compare(announcement.ends_at, ends_at) == :eq
end
test "creating with time with utc timezones", %{conn: conn} do
content = "test post announcement api"
now = DateTime.now("Etc/UTC") |> elem(1) |> DateTime.truncate(:second)
starts_at = DateTime.add(now, -10, :second)
ends_at = DateTime.add(now, 10, :second)
response =
conn
|> put_req_header("content-type", "application/json")
|> post("/api/v1/pleroma/admin/announcements", %{
"content" => content,
"starts_at" => DateTime.to_iso8601(starts_at),
"ends_at" => DateTime.to_iso8601(ends_at),
"all_day" => true
})
|> json_response_and_validate_schema(:ok)
assert %{"content" => ^content, "all_day" => true} = response
announcement = Pleroma.Announcement.get_by_id(response["id"])
assert not is_nil(announcement)
assert DateTime.compare(announcement.starts_at, starts_at) == :eq
assert DateTime.compare(announcement.ends_at, ends_at) == :eq
end
end
end

View file

@ -23,6 +23,30 @@ test "it lists all announcements" do
refute Map.has_key?(Enum.at(response, 0), "read")
end
test "it returns time with utc timezone" do
start_time =
NaiveDateTime.utc_now()
|> NaiveDateTime.add(-999_999, :second)
|> NaiveDateTime.truncate(:second)
end_time =
NaiveDateTime.utc_now()
|> NaiveDateTime.add(999_999, :second)
|> NaiveDateTime.truncate(:second)
%{id: id} = insert(:announcement, %{starts_at: start_time, ends_at: end_time})
response =
build_conn()
|> get("/api/v1/announcements")
|> json_response_and_validate_schema(:ok)
assert [%{"id" => ^id}] = [announcement] = response
assert String.ends_with?(announcement["starts_at"], "Z")
assert String.ends_with?(announcement["ends_at"], "Z")
end
test "it does not list announcements starting after current time" do
time = NaiveDateTime.utc_now() |> NaiveDateTime.add(999_999, :second)
insert(:announcement, starts_at: time)