Skip to content

Commit

Permalink
Start and end values test
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Monson committed May 11, 2023
1 parent b83a39f commit ad737ed
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ defmodule Fob.Repo.Migrations.CreateTieBreakerSchema do
def change do
create table(:tie_breaker_schema) do
add :date, :date
add :naive_datetime, :naive_datetime
add :naive_datetime_usec, :naive_datetime_usec
add :utc_datetime, :utc_datetime
add :utc_datetime_usec, :utc_datetime_usec
add :name, :string
end
end
Expand Down
85 changes: 85 additions & 0 deletions test/fob/between_bounds_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,89 @@ defmodule Fob.BetweenBoundsTest do

assert between_bounds(query, start, stop) |> c.repo.all() == records
end

test "between_bounds/3 can return a single record when start and stop are equal",
c do
schema = c.schema
query = from t in schema, order_by: [asc_nulls_last: :date, asc: :id]

[record] =
c.repo.all(query)
|> Enum.slice(_start_at = 3, _count = 1)

start = page_breaks(query, record)
stop = page_breaks(query, record)

assert between_bounds(query, start, stop) |> c.repo.all() == [record]
end

test "between_bounds/3 can return a single record when start and stop are equal and naive_datetime",
c do
schema = c.schema

query =
from t in schema, order_by: [asc_nulls_last: :naive_datetime, asc: :id]

[record] =
c.repo.all(query)
|> Enum.slice(_start_at = 3, _count = 1)

start = page_breaks(query, record)
stop = page_breaks(query, record)

assert between_bounds(query, start, stop) |> c.repo.all() == [record]
end

test "between_bounds/3 can return a single record when start and stop are equal and naive_datetime_usec",
c do
schema = c.schema

query =
from t in schema,
order_by: [asc_nulls_last: :naive_datetime_usec, asc: :id]

[record] =
c.repo.all(query)
|> Enum.slice(_start_at = 3, _count = 1)

start = page_breaks(query, record)
stop = page_breaks(query, record)

assert between_bounds(query, start, stop) |> c.repo.all() == [record]
end

test "between_bounds/3 can return a single record when start and stop are equal and utc_datetime",
c do
schema = c.schema

query =
from t in schema, order_by: [asc_nulls_last: :utc_datetime, asc: :id]

[record] =
c.repo.all(query)
|> Enum.slice(_start_at = 3, _count = 1)

start = page_breaks(query, record)
stop = page_breaks(query, record)

assert between_bounds(query, start, stop) |> c.repo.all() == [record]
end

test "between_bounds/3 can return a single record when start and stop are equal and utc_datetime_usec",
c do
schema = c.schema

query =
from t in schema, order_by: [asc_nulls_last: :utc_datetime_usec, asc: :id]

[record] =
c.repo.all(query)
|> Enum.slice(_start_at = 3, _count = 1)

start = page_breaks(query, record)
stop = page_breaks(query, record)

assert between_bounds(query, start, stop) |> c.repo.all() ==
[record]
end
end
1 change: 1 addition & 0 deletions test/fob/fragment_casting_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ defmodule Fob.FragmentCastingTest do
end)

records = Fob.next_page(c.query, page_breaks, 3) |> c.repo.all()

assert records |> ids() == Enum.slice(expected_ids, 9..11)
end

Expand Down
72 changes: 55 additions & 17 deletions test/support/tie_breaker_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,72 @@ defmodule TieBreakerSchema do

schema "tie_breaker_schema" do
field :date, :date
field :naive_datetime, :naive_datetime
field :naive_datetime_usec, :naive_datetime_usec
field :utc_datetime, :utc_datetime
field :utc_datetime_usec, :utc_datetime_usec
field :name
end

def seed do
t1 = random_time()
t2 = random_time()

[
{~D[2020-12-15], "a", 0},
{~D[2020-12-15], "a", 1},
{~D[2020-12-15], "a", 2},
{~D[2020-12-15], "a", 3},
{~D[2020-12-15], "b", 4},
{~D[2020-12-15], "b", 5},
{~D[2020-12-15], "b", 6},
{~D[2020-12-15], "b", 7},
{~D[2020-12-15], nil, 8},
{~D[2020-12-15], nil, 9},
{~D[2020-12-17], nil, 10},
{~D[2020-12-17], "b", 11},
{~D[2020-12-17], "b", 12},
{~D[2020-12-17], "c", 13},
{~D[2020-12-17], "c", 14},
{~D[2020-12-15] |> DateTime.new!(t1, "Etc/UTC"), "a", 0},
{~D[2020-12-15] |> DateTime.new!(t1, "Etc/UTC"), "a", 1},
{~D[2020-12-15] |> DateTime.new!(t2, "Etc/UTC"), "a", 2},
{~D[2020-12-15] |> DateTime.new!(t2, "Etc/UTC"), "a", 3},
{~D[2020-12-15] |> DateTime.new!(t1, "Etc/UTC"), "b", 4},
{~D[2020-12-15] |> DateTime.new!(t1, "Etc/UTC"), "b", 5},
{~D[2020-12-15] |> DateTime.new!(t2, "Etc/UTC"), "b", 6},
{~D[2020-12-15] |> DateTime.new!(t2), "b", 7},
{~D[2020-12-15] |> DateTime.new!(random_time(), "Etc/UTC"), nil, 8},
{~D[2020-12-15] |> DateTime.new!(random_time(), "Etc/UTC"), nil, 9},
{~D[2020-12-17] |> DateTime.new!(random_time(), "Etc/UTC"), nil, 10},
{~D[2020-12-17] |> DateTime.new!(random_time(), "Etc/UTC"), "b", 11},
{~D[2020-12-17] |> DateTime.new!(random_time(), "Etc/UTC"), "b", 12},
{~D[2020-12-17] |> DateTime.new!(random_time(), "Etc/UTC"), "c", 13},
{~D[2020-12-17] |> DateTime.new!(random_time(), "Etc/UTC"), "c", 14},
{nil, "c", 15},
{nil, "c", 16},
{nil, "c", 17},
{nil, "c", 18},
{nil, "c", 19}
]
|> Enum.map(fn {date, name, index} ->
%{id: index, name: name, date: date}
|> Enum.map(fn
{nil, name, index} ->
%{
id: index,
name: name,
date: nil,
naive_datetime: nil,
utc_datetime: nil,
utc_datetime_usec: nil
}

{datetime, name, index} ->
%{
id: index,
name: name,
date: DateTime.to_date(datetime),
naive_datetime:
DateTime.to_naive(DateTime.truncate(datetime, :second)),
naive_datetime_usec: DateTime.to_naive(datetime),
utc_datetime: DateTime.truncate(datetime, :second),
utc_datetime_usec: datetime
}
end)
end

def random_time do
r = &Enum.random(&1)

Time.new!(
_hour = r.(0..23),
_min = r.(0..59),
_sec = r.(0..59),
_micro = r.(0..1_000_000)
)
end
end

0 comments on commit ad737ed

Please sign in to comment.