Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Rychlewski authored and Greg Rychlewski committed Jul 1, 2024
1 parent f5c8b29 commit c44df85
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/postgrex/extensions/timestamp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Postgrex.Extensions.Timestamp do
@min_year -4_713
@plus_infinity 9_223_372_036_854_775_807
@minus_infinity -9_223_372_036_854_775_808
@default_precision 6

def init(opts), do: Keyword.get(opts, :allow_infinite_timestamps, false)

Expand All @@ -28,7 +29,6 @@ defmodule Postgrex.Extensions.Timestamp do
def decode(infinity?) do
quote location: :keep do
<<8::int32(), microsecs::int64()>>, precision ->
precision = if precision == -1, do: 6, else: precision
unquote(__MODULE__).microsecond_to_elixir(microsecs, precision, unquote(infinity?))
end
end
Expand Down Expand Up @@ -77,6 +77,9 @@ defmodule Postgrex.Extensions.Timestamp do
end

defp split(secs, microsecs, precision) do
# use the default precision if the precision modifier from postgres is -1 (this means no precision specified)
# or if the precision is missing because we are in a super type which does not give us the sub-type's modifier
precision = if precision in [-1, nil], do: @default_precision, else: precision
NaiveDateTime.from_gregorian_seconds(secs + @gs_epoch, {microsecs, precision})
end

Expand Down
23 changes: 23 additions & 0 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,29 @@ defmodule QueryTest do
}
]
] = query("SELECT '[,]'::daterange", [])

assert [
[
%Postgrex.Range{
lower: %NaiveDateTime{
year: 2014,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0
},
upper: %NaiveDateTime{
year: 2014,
month: 12,
day: 31,
hour: 0,
minute: 0,
second: 0
}
}
]
] = query("SELECT '[2014-1-1,2014-12-31)'::tsrange", [])
end

@tag min_pg_version: "14.0"
Expand Down
25 changes: 25 additions & 0 deletions test/stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,29 @@ defmodule StreamTest do

assert [[42]] = query("SELECT 42", [])
end

test "stream with type modifier", context do
:ok =
query(
"""
INSERT INTO timestamps_stream (micro, milli, sec, sec_arr)
VALUES ('2000-01-01', '2000-01-01', '2000-01-01', '{2000-01-01, 2000-01-02}'),
('3000-01-01', '3000-01-01', '3000-01-01', '{3000-01-01, 3000-01-02}')
""",
[]
)

query = prepare("", "SELECT * FROM timestamps_stream")

transaction(fn conn ->
[[row1], [row2], []] =
stream(query, [], max_rows: 1) |> Enum.map(fn %Result{rows: rows} -> rows end)

assert [6, 3, 0, [0, 0]] = precision(row1)
assert [6, 3, 0, [0, 0]] = precision(row2)
end)
end

defp precision([_ | _] = dts), do: Enum.map(dts, &precision(&1))
defp precision(%NaiveDateTime{microsecond: {_, p}}), do: p
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ CREATE TYPE enum1 AS ENUM ('elixir', 'erlang');
CREATE TABLE uniques (a int UNIQUE);
CREATE TABLE timestamps (micro timestamp, milli timestamp(3), sec timestamp(0), sec_arr timestamp(0)[]);
CREATE TABLE timestamps_stream (micro timestamp, milli timestamp(3), sec timestamp(0), sec_arr timestamp(0)[]);
DROP TABLE IF EXISTS missing_oid;
DROP TYPE IF EXISTS missing_enum;
Expand Down

0 comments on commit c44df85

Please sign in to comment.