this repo has no description

Add tests for JSON output

hauleth.dev 125482fe 0b945a3f

verified
Changed files
+63
lib
esl_hn_web
api
test
esl_hn_web
+1
lib/esl_hn_web/api/json.ex
···
defp one(%EslHn.Hn.Story{} = story) do
%{
+
id: story.id,
title: story.title,
url: story.url,
score: story.score
+62
test/esl_hn_web/api/json_test.exs
···
+
defmodule EslHnWeb.API.JSONTest do
+
use ExUnit.Case, async: true
+
use ExUnitProperties
+
+
alias EslHn.Hn.Story
+
+
@subject EslHnWeb.API.JSON
+
+
doctest @subject
+
+
defp story do
+
gen all(
+
title <- string(:utf8, min_length: 1),
+
score <- positive_integer()
+
) do
+
%Story{
+
id: System.unique_integer([:positive]),
+
title: title,
+
score: score,
+
url: "https://example.com/#{URI.encode(title)}"
+
}
+
end
+
end
+
+
describe "index/1" do
+
test "for empty list renders empty list" do
+
assert [] == @subject.index(%{items: []})
+
end
+
+
property "length of encoded data is equal to items count" do
+
check all(stories <- list_of(story())) do
+
count = length(stories)
+
+
assert count == length(@subject.index(%{items: stories}))
+
end
+
end
+
+
property "all IDs are present in result" do
+
check all(stories <- list_of(story())) do
+
ids =
+
stories
+
|> Enum.map(& &1.id)
+
|> Enum.sort()
+
+
result =
+
@subject.index(%{items: stories})
+
|> Enum.map(& &1.id)
+
|> Enum.sort()
+
+
assert ids == result
+
end
+
end
+
end
+
+
describe "show/1" do
+
property "encoded title is the same as input title" do
+
check all story <- story() do
+
assert story.title == @subject.show(%{item: story}).title
+
end
+
end
+
end
+
end