Add more steps from the book but with additoinal tests
This commit is contained in:
parent
ae41d2799d
commit
8a76966062
|
@ -51,6 +51,8 @@ defmodule Issues.CLI do
|
||||||
def process({user, project, count}) do
|
def process({user, project, count}) do
|
||||||
Issues.GithubIssues.fetch(user, project)
|
Issues.GithubIssues.fetch(user, project)
|
||||||
|> decode_response()
|
|> decode_response()
|
||||||
|
|> sort_into_descending_order
|
||||||
|
|> last(count)
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_response({:ok, body}), do: body
|
def decode_response({:ok, body}), do: body
|
||||||
|
@ -58,4 +60,15 @@ defmodule Issues.CLI do
|
||||||
IO.puts "Error fetching from Github: #{error["message"]}"
|
IO.puts "Error fetching from Github: #{error["message"]}"
|
||||||
System.halt(2)
|
System.halt(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sort_into_descending_order(list_of_issues) do
|
||||||
|
list_of_issues
|
||||||
|
|> Enum.sort(fn lhs, rhs -> Map.get(lhs, "created_at") >= Map.get(rhs, "created_at") end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def last(list, count) do
|
||||||
|
list
|
||||||
|
|> Enum.take(count)
|
||||||
|
|> Enum.reverse
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,12 @@ defmodule CliTest do
|
||||||
use ExUnit.Case
|
use ExUnit.Case
|
||||||
doctest Issues
|
doctest Issues
|
||||||
|
|
||||||
import Issues.CLI, only: [parse_args: 1]
|
import Issues.CLI,
|
||||||
|
only: [
|
||||||
|
parse_args: 1,
|
||||||
|
sort_into_descending_order: 1,
|
||||||
|
last: 2
|
||||||
|
]
|
||||||
|
|
||||||
test ":help returned by option parsing the -h and --help options" do
|
test ":help returned by option parsing the -h and --help options" do
|
||||||
assert parse_args(["-h", "anything"]) == :help
|
assert parse_args(["-h", "anything"]) == :help
|
||||||
|
@ -16,4 +21,22 @@ defmodule CliTest do
|
||||||
test "two values and default count returned if two values given" do
|
test "two values and default count returned if two values given" do
|
||||||
assert parse_args(["user", "project"]) == {"user", "project", 4}
|
assert parse_args(["user", "project"]) == {"user", "project", 4}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "sort descending orders the correct way" do
|
||||||
|
result = sort_into_descending_order(fake_created_at_list(["c", "a", "b"]))
|
||||||
|
issues = for issue <- result, do: Map.get(issue, "created_at")
|
||||||
|
assert issues == ~w{ c b a }
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fake_created_at_list(values) do
|
||||||
|
for value <- values, do: %{"created_at" => value, "other_data" => "xxx"}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "fetch the last two elements of a list" do
|
||||||
|
list = [1,2,3,4,5]
|
||||||
|
count = 2
|
||||||
|
expected = [2, 1]
|
||||||
|
result = last(list, count)
|
||||||
|
assert expected == result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue