lib/decidim/elections/test/factories.rb in decidim-elections-0.24.3 vs lib/decidim/elections/test/factories.rb in decidim-elections-0.25.0.rc1

- old
+ new

@@ -15,10 +15,12 @@ end factory :election, class: "Decidim::Elections::Election" do transient do organization { build(:organization) } + number_of_votes { Faker::Number.number(digits: 2) } + base_id { 10_000 } end upcoming title { generate_localized_title } description { Decidim::Faker::Localized.wrapped("<p>", "</p>") { generate_localized_title } } @@ -26,14 +28,15 @@ published_at { nil } blocked_at { nil } bb_status { nil } questionnaire component { create(:elections_component, organization: organization) } + salt { SecureRandom.hex(32) } trait :bb_test do bb_status { "key_ceremony" } - id { (10_000 + Decidim::Elections::Election.bb_statuses.keys.index(bb_status)) } + id { (base_id + Decidim::Elections::Election.bb_statuses.keys.index(bb_status)) } end trait :upcoming do start_time { 1.day.from_now } end @@ -114,16 +117,12 @@ key_ceremony_ended ongoing finished bb_status { "vote_ended" } - after(:build) do |election| - election.questions.each do |question| - question.answers.each do |answer| - answer.votes_count = Faker::Number.number(digits: 1) - end - end + after(:create) do |election, evaluator| + create_list(:vote, evaluator.number_of_votes, :accepted, election: election) end end trait :tally do vote_ended @@ -131,10 +130,16 @@ end trait :tally_ended do tally bb_status { "tally_ended" } + verifiable_results_file_hash { SecureRandom.hex(32) } + verifiable_results_file_url { Faker::Internet.url } + + after(:create) do |election| + create(:bb_closure, :with_results, election: election) + end end trait :results_published do tally_ended bb_status { "results_published" } @@ -217,14 +222,15 @@ question title { generate_localized_title } description { Decidim::Faker::Localized.wrapped("<p>", "</p>") { generate_localized_title } } weight { Faker::Number.number(digits: 1) } selected { false } - votes_count { 0 } trait :with_votes do - votes_count { Faker::Number.number(digits: 1) } + after(:build) do |answer| + create(:election_result, election: answer.question.election, question: answer.question, answer: answer) + end end trait :with_photos do transient do photos_number { 2 } @@ -240,25 +246,79 @@ end end end end + factory :bb_closure, class: "Decidim::Elections::BulletinBoardClosure" do + initialize_with do + Decidim::Elections::BulletinBoardClosure.find_or_create_by( + decidim_elections_election_id: election.id + ) + end + + election { create(:election, :complete) } + + trait :with_results do + after :create do |closure| + total_votes = closure.election.votes.count + closure.election.questions.each do |question| + max = total_votes + question.answers.each do |answer| + value = Faker::Number.between(from: 0, to: max) + closure.results << create(:election_result, election: closure.election, question: question, answer: answer, value: value) + max -= value + end + closure.results << create(:election_result, :blank_ballots, election: closure.election, question: question, value: max) + end + closure.results << create(:election_result, :total_ballots, election: closure.election, value: total_votes) + end + end + end + + factory :election_result, class: "Decidim::Elections::Result" do + transient do + election { create(:election, :tally_ended) } + end + + closurable { create :bb_closure, election: election } + question { create :question, election: election } + answer { create :election_answer, question: question } + value { Faker::Number.number(digits: 1) } + result_type { "valid_answers" } + + trait :null_ballots do + result_type { "null_ballots" } + answer { nil } + end + + trait :blank_ballots do + result_type { "blank_ballots" } + answer { nil } + end + + trait :total_ballots do + result_type { "total_ballots" } + answer { nil } + question { nil } + end + end + factory :action, class: "Decidim::Elections::Action" do election message_id { "a.message+id" } status { :pending } action { :start_key_ceremony } end factory :trustee, class: "Decidim::Elections::Trustee" do transient do election { nil } - organization { election&.component&.participatory_space&.organization || create(:organization) } end public_key { nil } user { build(:user, organization: organization) } + organization { create(:organization) } trait :considered do after(:build) do |trustee, evaluator| trustee.trustees_participatory_spaces << build(:trustees_participatory_space, trustee: trustee, election: evaluator.election, organization: evaluator.organization) end @@ -270,11 +330,11 @@ end end trait :with_public_key do considered - name { Faker::Name.name } + name { Faker::Name.unique.name } public_key { generate(:private_key).export.to_json } end end factory :trustees_participatory_space, class: "Decidim::Elections::TrusteesParticipatorySpace" do @@ -296,7 +356,12 @@ sequence(:voter_id) { |n| "voter_#{n}" } encrypted_vote_hash { "adf89asd0f89das7f" } status { "pending" } message_id { "decidim-test-authority.2.vote.cast+v.5826de088371d1b15b38f00c8203871caec07041ed0c8fb0c6fb875f0df763b6" } user { build(:user) } + email { "an_email@example.org" } + + trait :accepted do + status { "accepted" } + end end end