this repo has no description
at main 2.9 kB view raw
1defmodule EslHnWeb.Api.ControllerTest do 2 use EslHnWeb.ConnCase, async: false 3 use ExUnitProperties 4 5 import EslHn.Test.Data 6 7 alias EslHn.Cache 8 9 setup do 10 on_exit(fn -> Cache.flush(EslHn) end) 11 end 12 13 describe "GET /" do 14 test "with empty cache returns empty list", %{conn: conn} do 15 resp = 16 conn 17 |> get(~p"/") 18 |> json_response(200) 19 20 assert resp == [] 21 end 22 23 property "with some stories in index these are fetched", %{conn: conn} do 24 check all(stories <- list_of(story(), max_length: 10)) do 25 ids = Enum.map(stories, & &1.id) 26 Cache.write(EslHn, :index, ids) 27 28 Cache.write_all(EslHn, Enum.map(stories, &{&1.id, &1})) 29 30 resp_ids = 31 conn 32 |> get(~p"/") 33 |> json_response(200) 34 |> Enum.map(& &1["id"]) 35 36 assert Enum.all?(ids, &(&1 in resp_ids)) 37 end 38 end 39 40 property "return at most 10 elements at once", %{conn: conn} do 41 check all(stories <- list_of(story())) do 42 ids = Enum.map(stories, & &1.id) 43 Cache.write(EslHn, :index, ids) 44 45 Cache.write_all(EslHn, Enum.map(stories, &{&1.id, &1})) 46 47 resp = 48 conn 49 |> get(~p"/") 50 |> json_response(200) 51 52 assert length(resp) <= 10 53 end 54 end 55 56 property "if more than 10 elements there are stories on second page", %{ 57 conn: conn 58 } do 59 check all(stories <- list_of(story(), min_length: 11)) do 60 ids = Enum.map(stories, & &1.id) 61 Cache.write(EslHn, :index, ids) 62 63 Cache.write_all(EslHn, Enum.map(stories, &{&1.id, &1})) 64 65 resp = 66 conn 67 |> get(~p"/?page=2") 68 |> json_response(200) 69 70 assert length(resp) in 1..10 71 end 72 end 73 74 property "if requested page is outside of the possible range, returns empty list", 75 %{ 76 conn: conn 77 } do 78 check all(stories <- list_of(story(), max_length: 50)) do 79 ids = Enum.map(stories, & &1.id) 80 Cache.write(EslHn, :index, ids) 81 82 Cache.write_all(EslHn, Enum.map(stories, &{&1.id, &1})) 83 84 resp = 85 conn 86 |> get(~p"/?page=2137") 87 |> json_response(200) 88 89 assert resp == [] 90 end 91 end 92 end 93 94 describe "GET /:id" do 95 test "for non-existent story returns 404", %{conn: conn} do 96 conn 97 |> get(~p"/2137") 98 |> json_response(404) 99 end 100 101 test "for non-integer story ID returns 404", %{conn: conn} do 102 conn 103 |> get(~p"/foo-bar") 104 |> json_response(404) 105 end 106 107 test "return data for existing story", %{conn: conn} do 108 Cache.write_all(EslHn, [ 109 {2137, %EslHn.Hn.Story{id: 2137, title: "Foo"}} 110 ]) 111 112 resp = 113 conn 114 |> get(~p"/2137") 115 |> json_response(200) 116 117 assert "Foo" == resp["title"] 118 end 119 end 120end