-
Notifications
You must be signed in to change notification settings - Fork 4
Property based testing with StreamData #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SophieDeBenedetto
wants to merge
4
commits into
master
Choose a base branch
from
test-vote-calculation-with-stream-data
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c89a28a
Property-based testing with Stream Data for VoteCalculator
SophieDeBenedetto 31ac2d7
Add property tests and fix bug
michaelstalker 9b2705e
Update variable name to avoid reassignment
michaelstalker aa152a1
Add resources to README
michaelstalker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[ | ||
import_deps: [:ecto, :phoenix], | ||
import_deps: [:ecto, :phoenix, :stream_data], | ||
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"], | ||
subdirectories: ["priv/*/migrations"] | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
defmodule PointingPartyWeb.Presence do | ||
use Phoenix.Presence, otp_app: :pointing_party, | ||
pubsub_server: PointingParty.PubSub | ||
use Phoenix.Presence, | ||
otp_app: :pointing_party, | ||
pubsub_server: PointingParty.PubSub | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,96 @@ | ||
defmodule PointingParty.VoteCalculatorTest do | ||
use ExUnit.Case, async: true | ||
use ExUnitProperties | ||
|
||
@users_with_winner %{ | ||
"sean" => %{metas: [%{points: 1}]}, | ||
"michael" => %{metas: [%{points: 3}]}, | ||
"sophie" => %{metas: [%{points: 3}]} | ||
} | ||
alias PointingParty.{Card, VoteCalculator} | ||
|
||
@users_with_tie %{ | ||
"sean" => %{metas: [%{points: 1}]}, | ||
"michael" => %{metas: [%{points: 2}]}, | ||
"sophie" => %{metas: [%{points: 3}]} | ||
} | ||
# Properties | ||
# -------------- | ||
# When there is a tie, the result will be a list | ||
# The tie list will be sorted in increasing order | ||
# The tie list will contain unique elements | ||
# The tie list will have exactly two elements | ||
# The tie list contains only integers | ||
# The tie list integers must be valid voting options | ||
# The greatest element in the tie list will not be greater than the highest vote | ||
# | ||
# When there is a winner, the result will be an integer | ||
# When there is a winner, the result will be one of the valid voting options | ||
# When there is a winner, it will not be greater than the highest vote | ||
# | ||
# Notes | ||
# -------------- | ||
# Ties can happen when there are even or odd numbers of players | ||
# Winning votes can also have an even or odd number of players | ||
# Single-player games will always have a winner | ||
|
||
test "calculate_votes/1 calculates when there is a winner" do | ||
{"winner", 3} = PointingParty.VoteCalculator.calculate_votes(@users_with_winner) | ||
end | ||
|
||
test "calculate_votes/1 calculates when there is a tie" do | ||
{"tie", [1,2]} = PointingParty.VoteCalculator.calculate_votes(@users_with_tie) | ||
describe "calculate_votes/1" do | ||
setup do | ||
points_map = fixed_map(%{ | ||
points: member_of(Card.points_range()) | ||
}) | ||
metas_map = fixed_map(%{ | ||
metas: list_of(points_map, length: 1) | ||
}) | ||
user_generator = nonempty(map_of(string(:alphanumeric), metas_map)) | ||
|
||
[user_generator: user_generator] | ||
end | ||
|
||
property "calculated vote is a list or an integer", %{user_generator: user_generator} do | ||
check all users <- user_generator, | ||
{_event, winner} = VoteCalculator.calculate_votes(users), | ||
max_runs: 20 do | ||
assert is_list(winner) || is_integer(winner) | ||
end | ||
end | ||
|
||
property "the winning value is not more than the highest vote", %{user_generator: user_generator} do | ||
check all users <- user_generator, | ||
max_runs: 20 do | ||
max_vote = | ||
users | ||
|> Enum.map(fn {_username, %{metas: [%{points: points}]}} -> points end) | ||
|> Enum.max() | ||
|
||
case PointingParty.VoteCalculator.calculate_votes(users) do | ||
{"winner", winner} -> assert winner <= max_vote | ||
{"tie", [_lesser, greater]} -> assert greater <= max_vote | ||
end | ||
end | ||
end | ||
|
||
property "when there is a winner, calculated vote is a valid integer", %{user_generator: user_generator} do | ||
check all users <- user_generator, | ||
{event, winner} = PointingParty.VoteCalculator.calculate_votes(users), | ||
max_runs: 20 do | ||
if event == "winner" do | ||
assert winner in Card.points_range() | ||
end | ||
end | ||
end | ||
|
||
property "when there is a tie, calculated vote is a list with two sorted values", %{user_generator: user_generator} do | ||
check all users <- user_generator, | ||
{event, votes} = PointingParty.VoteCalculator.calculate_votes(users), | ||
max_runs: 20 do | ||
if event == "tie" do | ||
[lesser, greater] = votes | ||
|
||
assert lesser < greater | ||
end | ||
end | ||
end | ||
|
||
property "when there is a tie, calculated vote is a list of valid integers", %{user_generator: user_generator} do | ||
check all users <- user_generator, | ||
{event, votes} = PointingParty.VoteCalculator.calculate_votes(users), | ||
max_runs: 20 do | ||
if event == "tie" do | ||
assert Enum.all?(votes, fn vote -> vote in Card.points_range() end) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.