|
1 | 1 | defmodule PointingParty.VoteCalculatorTest do
|
2 | 2 | use ExUnit.Case, async: true
|
| 3 | + use ExUnitProperties |
3 | 4 |
|
4 |
| - @users_with_winner %{ |
5 |
| - "sean" => %{metas: [%{points: 1}]}, |
6 |
| - "michael" => %{metas: [%{points: 3}]}, |
7 |
| - "sophie" => %{metas: [%{points: 3}]} |
8 |
| - } |
| 5 | + describe "calculate_votes/1" do |
| 6 | + setup do |
| 7 | + points_map = fixed_map(%{points: integer(1..5)}) |
9 | 8 |
|
10 |
| - @users_with_tie %{ |
11 |
| - "sean" => %{metas: [%{points: 1}]}, |
12 |
| - "michael" => %{metas: [%{points: 2}]}, |
13 |
| - "sophie" => %{metas: [%{points: 3}]} |
14 |
| - } |
| 9 | + metas_map = |
| 10 | + fixed_map(%{ |
| 11 | + metas: list_of(points_map, length: 1) |
| 12 | + }) |
15 | 13 |
|
16 |
| - test "calculate_votes/1 calculates when there is a winner" do |
17 |
| - {"winner", 3} = PointingParty.VoteCalculator.calculate_votes(@users_with_winner) |
18 |
| - end |
| 14 | + users = nonempty(map_of(string(:alphanumeric), metas_map)) |
| 15 | + [users: users] |
| 16 | + end |
| 17 | + |
| 18 | + property "winning value is a list or a integer", %{users: users} do |
| 19 | + check all users <- users do |
| 20 | + {_event, winner} = PointingParty.VoteCalculator.calculate_votes(users) |
| 21 | + assert is_list(winner) || is_integer(winner) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + property "tie when winning value is a list, winner when winning value is an integer", %{users: users} do |
| 26 | + check all users <- users do |
| 27 | + {event, winner} = PointingParty.VoteCalculator.calculate_votes(users) |
| 28 | + |
| 29 | + cond do |
| 30 | + is_list(winner) -> |
| 31 | + assert event == "tie" |
| 32 | + |
| 33 | + is_integer(winner) -> |
| 34 | + assert event == "winner" |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + property "the winning value is not more than the highest point value", %{users: users} do |
| 40 | + check all users <- users do |
| 41 | + {_event, winner} = PointingParty.VoteCalculator.calculate_votes(users) |
| 42 | + |
| 43 | + max_vote = |
| 44 | + users |
| 45 | + |> Enum.map(fn {_username, %{metas: [%{points: points}]}} -> points end) |
| 46 | + |> Enum.max() |
| 47 | + |
| 48 | + cond do |
| 49 | + is_list(winner) -> |
| 50 | + assert Enum.max(winner) <= max_vote |
| 51 | + |
| 52 | + is_integer(winner) -> |
| 53 | + assert winner <= max_vote |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + property "when the winner is a list of two sorted values", %{users: users} do |
| 59 | + check all users <- users do |
| 60 | + {_event, winner} = PointingParty.VoteCalculator.calculate_votes(users) |
| 61 | + |
| 62 | + if is_list(winner) do |
| 63 | + assert length(winner) == 2 |
| 64 | + |
| 65 | + votes = Enum.map(users, fn {_username, %{metas: [%{points: points}]}} -> points end) |
| 66 | + |
| 67 | + calculated_votes = |
| 68 | + Enum.reduce(votes, %{}, fn vote, acc -> |
| 69 | + value = (acc[vote] || 0) + 1 |
| 70 | + Map.put(acc, vote, value) |
| 71 | + end) |
| 72 | + |
| 73 | + sorted = |
| 74 | + calculated_votes |
| 75 | + |> Enum.sort_by(fn ({_k, v}) -> v end) |
| 76 | + |> Enum.map(fn ({a, _b}) -> a end) |
| 77 | + |> Enum.take(2) |
19 | 78 |
|
20 |
| - test "calculate_votes/1 calculates when there is a tie" do |
21 |
| - {"tie", [1,2]} = PointingParty.VoteCalculator.calculate_votes(@users_with_tie) |
| 79 | + assert sorted == winner |
| 80 | + end |
| 81 | + end |
| 82 | + end |
22 | 83 | end
|
23 | 84 | end
|
0 commit comments