Skip to content

Commit f6d72d2

Browse files
stream data tests
1 parent 1c72523 commit f6d72d2

File tree

3 files changed

+69
-24
lines changed

3 files changed

+69
-24
lines changed

lib/pointing_party/vote_calculator.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
defmodule PointingParty.VoteCalculator do
2+
3+
def calculate_votes(users) do
4+
case winning_vote(users) do
5+
top_two when is_list(top_two) -> {"tie", top_two}
6+
winner -> {"winner", winner}
7+
end
8+
end
9+
210
def winning_vote(users) do
311
votes = Enum.map(users, fn {_username, %{metas: [%{points: points}]}} -> points end)
412
calculated_votes = Enum.reduce(votes, %{}, fn vote, acc ->

lib/pointing_party_web/channels/room_channel.ex

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@ defmodule PointingPartyWeb.RoomChannel do
5757
defp finalize_voting(socket) do
5858
current_users = Presence.list(socket)
5959

60-
{event, points} =
61-
case VoteCalculator.winning_vote(current_users) do
62-
top_two when is_list(top_two) -> {"tie", top_two}
63-
winner -> {"winner", winner}
64-
end
65-
60+
{event, points} = VoteCalculator.calculate_votes(current_users)
6661
broadcast!(socket, event, %{points: points})
6762
end
6863

test/pointing_party/vote_calculator_test.exs

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,66 @@ defmodule PointingParty.VoteCalculatorTest do
22
use ExUnit.Case, async: true
33
use ExUnitProperties
44

5-
@users %{
6-
"sean" => %{metas: [%{points: 1}]},
7-
"michael" => %{metas: [%{points: 3}]},
8-
"sophie" => %{metas: [%{points: 3}]}
9-
}
10-
11-
describe "winning_vote/1" do
12-
test "calculates the card estimate from the votes" do
13-
# check all str <- string(:printable),
14-
# times <- integer(),
15-
# times >= 0 do
16-
# new_string = Repeater.duplicate(str, times)
17-
18-
check all users <- map_of(string(:printable), StreamData.fixed_map(%{
19-
metas: list_of(StreamData.fixed_map(%{points: integer()})),
20-
})) do
21-
winning = PointingParty.VoteCalculator.winning_vote(users)
22-
assert winning
5+
describe "calculate_votes/1" do
6+
setup do
7+
points_map = StreamData.fixed_map(%{points: integer(1..5)})
8+
metas_map = StreamData.fixed_map(%{
9+
metas: list_of(points_map, length: 1),
10+
})
11+
user = nonempty(map_of(string(:printable), metas_map))
12+
[user: user]
13+
end
14+
15+
test "winning value is a list or a integer", %{user: user} do
16+
check all users <- user do
17+
{_event, winner} = PointingParty.VoteCalculator.calculate_votes(users)
18+
assert is_list(winner) || is_integer(winner)
19+
end
20+
end
21+
22+
test "tie when winning value is a list, winner when winning value is an integer", %{user: user} do
23+
check all users <- user do
24+
{event, winner} = PointingParty.VoteCalculator.calculate_votes(users)
25+
cond do
26+
is_list(winner) ->
27+
assert event == "tie"
28+
is_integer(winner) ->
29+
assert event == "winner"
30+
end
31+
end
32+
end
33+
34+
test "the winning value is not more than the highest point value", %{user: user} do
35+
check all users <- user do
36+
{_event, winner} = PointingParty.VoteCalculator.calculate_votes(users)
37+
38+
max_vote = users
39+
|> Enum.map(fn {_username, %{metas: [%{points: points}]}} -> points end)
40+
|> Enum.max()
41+
42+
cond do
43+
is_list(winner) ->
44+
winner
45+
|> Enum.max()
46+
winner <= max_vote
47+
is_integer(winner) ->
48+
winner <= max_vote
49+
end
50+
end
51+
end
52+
53+
test "when the winner is a list of two sorted values", %{user: user} do
54+
check all users <- user do
55+
{_event, winner} = PointingParty.VoteCalculator.calculate_votes(users)
56+
if is_list(winner) do
57+
length = winner
58+
|> length
59+
assert length == 2
60+
61+
sorted = winner
62+
|> Enum.sort()
63+
assert sorted = winner
64+
end
2365
end
2466
end
2567
end

0 commit comments

Comments
 (0)