Skip to content

Commit

Permalink
Merge pull request consuldemocracy#3731 from consul/array_specs
Browse files Browse the repository at this point in the history
Test exact array contents where possible
  • Loading branch information
javierm committed Sep 29, 2019
2 parents 9858c18 + 167d3d7 commit aee260f
Show file tree
Hide file tree
Showing 25 changed files with 214 additions and 324 deletions.
8 changes: 4 additions & 4 deletions spec/features/budgets/investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,10 @@ def investments_order
visit new_budget_investment_path(budget)

select_options = find("#budget_investment_heading_id").all("option").collect(&:text)
expect(select_options.first).to eq("")
expect(select_options.second).to eq("Toda la ciudad")
expect(select_options.third).to eq("Health: More health professionals")
expect(select_options.fourth).to eq("Health: More hospitals")
expect(select_options).to eq ["",
"Toda la ciudad",
"Health: More health professionals",
"Health: More hospitals"]
end
end

Expand Down
5 changes: 1 addition & 4 deletions spec/helpers/geozones_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@

select_options = geozone_select_options

expect(select_options.size).to eq 3
expect(select_options.first).to eq [g1.name, g1.id]
expect(select_options[1]).to eq [g2.name, g2.id]
expect(select_options.last).to eq [g3.name, g3.id]
expect(select_options).to eq [[g1.name, g1.id], [g2.name, g2.id], [g3.name, g3.id]]
end
end

Expand Down
12 changes: 6 additions & 6 deletions spec/lib/acts_as_taggable_on_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
proposal.tag_list.add(tag)
proposal.save

expect(ActsAsTaggableOn::Tag.public_for_api).to include(tag)
expect(ActsAsTaggableOn::Tag.public_for_api).to eq [tag]
end

it "returns tags whose kind is 'category' and have at least one tagging whose taggable is not hidden" do
Expand All @@ -82,7 +82,7 @@
proposal.tag_list.add(tag)
proposal.save

expect(ActsAsTaggableOn::Tag.public_for_api).to include(tag)
expect(ActsAsTaggableOn::Tag.public_for_api).to eq [tag]
end

it "blocks other kinds of tags" do
Expand All @@ -91,13 +91,13 @@
proposal.tag_list.add(tag)
proposal.save

expect(ActsAsTaggableOn::Tag.public_for_api).not_to include(tag)
expect(ActsAsTaggableOn::Tag.public_for_api).to be_empty
end

it "blocks tags that don't have at least one tagged element" do
tag = create(:tag)
create(:tag)

expect(ActsAsTaggableOn::Tag.public_for_api).not_to include(tag)
expect(ActsAsTaggableOn::Tag.public_for_api).to be_empty
end

it "only permits tags on proposals or debates" do
Expand Down Expand Up @@ -126,7 +126,7 @@
proposal.tag_list.add(tag)
proposal.save

expect(ActsAsTaggableOn::Tag.public_for_api).to include(tag)
expect(ActsAsTaggableOn::Tag.public_for_api).to eq [tag]

proposal.delete

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/email_digests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

email_digest = EmailDigest.new(user1)

expect(email_digest.notifications).to include(notification1)
expect(email_digest.notifications).to eq [notification1]
expect(email_digest.notifications).not_to include(notification2)
end

Expand All @@ -29,7 +29,7 @@

email_digest = EmailDigest.new(user)

expect(email_digest.notifications).to include(notification1)
expect(email_digest.notifications).to eq [notification1]
expect(email_digest.notifications).not_to include(notification2)
end

Expand Down
40 changes: 18 additions & 22 deletions spec/lib/user_segments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
active_user = create(:user)
erased_user = create(:user, erased_at: Time.current)

expect(UserSegments.all_users).to include active_user
expect(UserSegments.all_users).to eq [active_user]
expect(UserSegments.all_users).not_to include erased_user
end
end
Expand All @@ -21,7 +21,7 @@
active_admin = create(:administrator).user
erased_user = create(:user, erased_at: Time.current)

expect(UserSegments.administrators).to include active_admin
expect(UserSegments.administrators).to eq [active_admin]
expect(UserSegments.administrators).not_to include active_user
expect(UserSegments.administrators).not_to include erased_user
end
Expand All @@ -34,9 +34,8 @@
create(:proposal, :retired, author: user3)

all_proposal_authors = UserSegments.all_proposal_authors
expect(all_proposal_authors).to include user1
expect(all_proposal_authors).to include user2
expect(all_proposal_authors).to include user3

expect(all_proposal_authors).to match_array [user1, user2, user3]
end

it "does not return duplicated users" do
Expand All @@ -45,7 +44,8 @@
create(:proposal, :retired, author: user1)

all_proposal_authors = UserSegments.all_proposal_authors
expect(all_proposal_authors).to contain_exactly(user1)

expect(all_proposal_authors).to eq [user1]
end
end

Expand All @@ -54,8 +54,8 @@
create(:proposal, author: user1)

proposal_authors = UserSegments.proposal_authors
expect(proposal_authors).to include user1
expect(proposal_authors).not_to include user2

expect(proposal_authors).to eq [user1]
end

it "does not return duplicated users" do
Expand All @@ -74,8 +74,8 @@
investment.update(budget: budget)

investment_authors = UserSegments.investment_authors
expect(investment_authors).to include user1
expect(investment_authors).not_to include user2

expect(investment_authors).to eq [user1]
end

it "does not return duplicated users" do
Expand Down Expand Up @@ -112,11 +112,7 @@
unfeasible_investment_finished.update(budget: budget)

investment_authors = UserSegments.feasible_and_undecided_investment_authors
expect(investment_authors).to include user1
expect(investment_authors).to include user2
expect(investment_authors).to include user3
expect(investment_authors).to include user4
expect(investment_authors).to include user5
expect(investment_authors).to match_array [user1, user2, user3, user4, user5]
expect(investment_authors).not_to include user6
end

Expand All @@ -141,8 +137,8 @@
unselected_investment.update(budget: budget)

investment_authors = UserSegments.selected_investment_authors
expect(investment_authors).to include user1
expect(investment_authors).not_to include user2

expect(investment_authors).to eq [user1]
end

it "does not return duplicated users" do
Expand All @@ -166,8 +162,8 @@
selected_investment.update(budget: budget)

investment_authors = UserSegments.winner_investment_authors
expect(investment_authors).to include user1
expect(investment_authors).not_to include user2

expect(investment_authors).to eq [user1]
end

it "does not return duplicated users" do
Expand All @@ -190,7 +186,8 @@
investment1.update(budget: budget)

current_budget_investments = UserSegments.current_budget_investments
expect(current_budget_investments).to include investment1

expect(current_budget_investments).to eq [investment1]
expect(current_budget_investments).not_to include investment2
end
end
Expand Down Expand Up @@ -218,8 +215,7 @@
create(:user, email: "last@email.com")

emails = UserSegments.user_segment_emails(:all_users)
expect(emails.first).to eq "first@email.com"
expect(emails.last).to eq "last@email.com"
expect(emails).to eq ["first@email.com", "last@email.com"]
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/models/budget/ballot/line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@

ballot_lines_by_investment = Budget::Ballot::Line.by_investment(investment1.id)

expect(ballot_lines_by_investment).to include ballot_line1
expect(ballot_lines_by_investment).to include ballot_line2
expect(ballot_lines_by_investment).to match_array [ballot_line1, ballot_line2]
expect(ballot_lines_by_investment).not_to include ballot_line3
end

Expand Down
12 changes: 4 additions & 8 deletions spec/models/budget/investment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@

investments_by_budget = Budget::Investment.by_budget(budget1)

expect(investments_by_budget).to include investment1
expect(investments_by_budget).to include investment2
expect(investments_by_budget).to match_array [investment1, investment2]
expect(investments_by_budget).not_to include investment3
end
end
Expand Down Expand Up @@ -656,8 +655,7 @@

results = Budget::Investment.apply_filters_and_search(budget, search: "health")

expect(results).to include investment1
expect(results).to include investment2
expect(results).to match_array [investment1, investment2]
expect(results).not_to include investment3
end
end
Expand Down Expand Up @@ -883,9 +881,7 @@
most_voted = create(:budget_investment, cached_votes_up: 10)
some_votes = create(:budget_investment, cached_votes_up: 5)

expect(Budget::Investment.sort_by_confidence_score.first).to eq most_voted
expect(Budget::Investment.sort_by_confidence_score.second).to eq some_votes
expect(Budget::Investment.sort_by_confidence_score.third).to eq least_voted
expect(Budget::Investment.sort_by_confidence_score).to eq [most_voted, some_votes, least_voted]
end

it "orders by confidence_score and then by id" do
Expand Down Expand Up @@ -937,7 +933,7 @@
inv2 = create(:budget_investment)
create(:vote, votable: inv1)

expect(Budget::Investment.with_supports).to include(inv1)
expect(Budget::Investment.with_supports).to eq [inv1]
expect(Budget::Investment.with_supports).not_to include(inv2)
end
end
Expand Down
36 changes: 16 additions & 20 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,60 +139,56 @@
it "returns comments" do
comment = create(:comment)

expect(Comment.public_for_api).to include(comment)
expect(Comment.public_for_api).to eq [comment]
end

it "does not return hidden comments" do
hidden_comment = create(:comment, :hidden)
create(:comment, :hidden)

expect(Comment.public_for_api).not_to include(hidden_comment)
expect(Comment.public_for_api).to be_empty
end

it "returns comments on debates" do
debate = create(:debate)
comment = create(:comment, commentable: debate)
comment = create(:comment, commentable: create(:debate))

expect(Comment.public_for_api).to include(comment)
expect(Comment.public_for_api).to eq [comment]
end

it "does not return comments on hidden debates" do
hidden_debate = create(:debate, :hidden)
comment = create(:comment, commentable: hidden_debate)
create(:comment, commentable: create(:debate, :hidden))

expect(Comment.public_for_api).not_to include(comment)
expect(Comment.public_for_api).to be_empty
end

it "returns comments on proposals" do
proposal = create(:proposal)
comment = create(:comment, commentable: proposal)

expect(Comment.public_for_api).to include(comment)
expect(Comment.public_for_api).to eq [comment]
end

it "does not return comments on hidden proposals" do
hidden_proposal = create(:proposal, :hidden)
comment = create(:comment, commentable: hidden_proposal)
create(:comment, commentable: create(:proposal, :hidden))

expect(Comment.public_for_api).not_to include(comment)
expect(Comment.public_for_api).to be_empty
end

it "does not return comments on elements which are not debates or proposals" do
budget_investment = create(:budget_investment)
comment = create(:comment, commentable: budget_investment)
create(:comment, commentable: create(:budget_investment))

expect(Comment.public_for_api).not_to include(comment)
expect(Comment.public_for_api).to be_empty
end

it "does not return comments with no commentable" do
comment = build(:comment, commentable: nil).save!(validate: false)
build(:comment, commentable: nil).save!(validate: false)

expect(Comment.public_for_api).not_to include(comment)
expect(Comment.public_for_api).to be_empty
end

it "does not return internal valuation comments" do
valuation_comment = create(:comment, :valuation)
create(:comment, :valuation)

expect(Comment.public_for_api).not_to include(valuation_comment)
expect(Comment.public_for_api).to be_empty
end
end
end
Loading

0 comments on commit aee260f

Please sign in to comment.