Nebo15 / ecto_paging

Cursor-based pagination for Ecto.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid order when query is ordered in desc order by default

AndrewDryga opened this issue · comments

Code:

    Request
    |> Repo.page(paging)

Test:

      request1 = RequestsFactory.insert(:request, id: "1")
      request2 = RequestsFactory.insert(:request, id: "2")
      request3 = RequestsFactory.insert(:request, id: "3")
      request4 = RequestsFactory.insert(:request, id: "4")
      request5 = RequestsFactory.insert(:request, id: "5")

      assert {[^request5], _paging} =
        Log.list_requests(%{}, %Paging{limit: 1})
      assert {[^request5, ^request4], _paging} =
        Log.list_requests(%{}, %Paging{limit: 2})
      assert {[^request3, ^request2], _paging} =
        Log.list_requests(%{}, %Paging{limit: 2, cursors: %Cursors{starting_after: request4.id}})
      assert {[^request3, ^request2], _paging} =
        Log.list_requests(%{}, %Paging{limit: 2, cursors: %Cursors{ending_before: request1.id}})

Result:

test list_requests/1 paginates results (Annon.Requests.LogTest)
     test/unit/requests/log_test.exs:60
     match (=) failed
     The following variables were pinned:
       request3 = %Annon.Requests.Request{__meta__: #Ecto.Schema.Metadata<:loaded, "logs">, api: %Annon.Requests.Request.API{id: "2", name: "An API #2", request: %Annon.Requests.Request.API.Request{host: "www.example2.com", path: "/my_api/", port: 80, scheme: "http"}}, id: "3", idempotency_key: "c8522980-8f2f-479c-a935-0ad2ec605661", inserted_at: ~N[2017-05-01 21:26:57.477140], ip_address: "129.168.1.10", latencies: %Annon.Requests.Request.Latencies{client_request: 102, gateway: 2, upstream: 100}, request: %Annon.Requests.Request.HTTPRequest{body: %{}, headers: [%{"content-type" => "application/json"}], method: "GET", query: %{"key" => "value"}, uri: "/my_api/"}, response: %Annon.Requests.Request.HTTPResponse{body: "", headers: [%{"content-type" => "application/json"}], status_code: 200}, status_code: 200, updated_at: ~N[2017-05-01 21:26:57.477144]}
       request2 = %Annon.Requests.Request{__meta__: #Ecto.Schema.Metadata<:loaded, "logs">, api: %Annon.Requests.Request.API{id: "1", name: "An API #1", request: %Annon.Requests.Request.API.Request{host: "www.example1.com", path: "/my_api/", port: 80, scheme: "http"}}, id: "2", idempotency_key: "f51ac9cb-de31-40e4-989b-4cd827823fb6", inserted_at: ~N[2017-05-01 21:26:57.475188], ip_address: "129.168.1.10", latencies: %Annon.Requests.Request.Latencies{client_request: 102, gateway: 2, upstream: 100}, request: %Annon.Requests.Request.HTTPRequest{body: %{}, headers: [%{"content-type" => "application/json"}], method: "GET", query: %{"key" => "value"}, uri: "/my_api/"}, response: %Annon.Requests.Request.HTTPResponse{body: "", headers: [%{"content-type" => "application/json"}], status_code: 200}, status_code: 200, updated_at: ~N[2017-05-01 21:26:57.475193]}
     code:  {[^request3, ^request2], _paging} = Log.list_requests(%{}, %Paging{limit: 2, cursors: %Cursors{starting_after: request4.id()}})
     right: {[%Annon.Requests.Request{__meta__: #Ecto.Schema.Metadata<:loaded, "logs">,
               api: %Annon.Requests.Request.API{id: "4", name: "An API #4",
                request: %Annon.Requests.Request.API.Request{host: "www.example4.com",
                 path: "/my_api/", port: 80, scheme: "http"}}, id: "5",
               idempotency_key: "20d8ac04-6642-4ce3-ab7b-866c08d138fe",
               inserted_at: ~N[2017-05-01 21:26:57.479877],
               ip_address: "129.168.1.10",
               latencies: %Annon.Requests.Request.Latencies{client_request: 102,
                gateway: 2, upstream: 100},
               request: %Annon.Requests.Request.HTTPRequest{body: %{},
                headers: [%{"content-type" => "application/json"}],
                method: "GET", query: %{"key" => "value"}, uri: "/my_api/"},
               response: %Annon.Requests.Request.HTTPResponse{body: "",
                headers: [%{"content-type" => "application/json"}],
                status_code: 200}, status_code: 200,
               updated_at: ~N[2017-05-01 21:26:57.479881]}],
             %Ecto.Paging{cursors: %Ecto.Paging.Cursors{ending_before: nil,
               starting_after: "5"}, has_more: false, limit: 2, size: nil}}
     stacktrace:
       test/unit/requests/log_test.exs:71: (test)

Query:

 00:26:57.502 [debug] QUERY OK source="logs" db=1.4ms
     SELECT l0."id", l0."api", l0."request", l0."response", l0."latencies", l0."idempotency_key", l0."ip_address", l0."status_code", l0."inserted_at", l0."updated_at" FROM "logs" AS l0 WHERE (l0."inserted_at" > $1) ORDER BY l0."inserted_at" DESC, l0."inserted_at" LIMIT $2 [{{2017, 5, 1}, {21, 26, 57, 478557}}, 2]