From ab49f0af891c24dbb3648ba3a1630a84bf70faad Mon Sep 17 00:00:00 2001 From: nkls Date: Fri, 26 Jun 2020 11:36:15 +0200 Subject: [PATCH] Run formatter --- config/prod.exs | 4 +- config/releases.exs | 4 +- lib/liquid_voting/delegations.ex | 22 +++-- lib/liquid_voting/metrics.ex | 2 +- lib/liquid_voting/release.ex | 2 +- lib/liquid_voting/voting.ex | 55 ++++++----- lib/liquid_voting/voting_results.ex | 12 +-- lib/liquid_voting/voting_results/result.ex | 4 +- lib/liquid_voting/voting_weight.ex | 16 ++-- lib/liquid_voting_web/plugs/context.ex | 2 +- .../resolvers/delegations.ex | 62 ++++++++---- lib/liquid_voting_web/resolvers/voting.ex | 44 +++++---- .../resolvers/voting_results.ex | 2 +- lib/liquid_voting_web/router.ex | 4 +- lib/liquid_voting_web/schema/schema.ex | 96 ++++++++++--------- .../20190906163956_create_votes.exs | 5 +- ...composite_index_with_organization_uuid.exs | 9 +- ...0515_update_participant_index_on_email.exs | 5 +- ..._add_org_uuid_to_result_proposal_index.exs | 5 +- ...21155938_add_uniq_index_to_delegations.exs | 4 +- .../20200609161238_rename_results_fields.exs | 2 +- priv/repo/seeds.exs | 1 - test/liquid_voting/delegations_test.exs | 21 ++-- test/liquid_voting/participants_test.exs | 35 +++++-- test/liquid_voting/voting_results_test.exs | 36 +++++-- test/liquid_voting/voting_test.exs | 36 ++++--- test/liquid_voting/voting_weight_test.exs | 8 +- .../mutations/create_delegation_test.exs | 45 ++++++--- .../absinthe/mutations/create_vote_test.exs | 34 +++++-- .../mutations/delete_delegation_test.exs | 38 ++++++-- .../absinthe/mutations/delete_vote_test.exs | 18 +++- .../absinthe/queries/delegations_test.exs | 5 +- .../absinthe/queries/voting_result_test.exs | 5 +- test/support/factory.ex | 14 +-- 34 files changed, 428 insertions(+), 229 deletions(-) diff --git a/config/prod.exs b/config/prod.exs index 744d413e..6203efa4 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -2,7 +2,7 @@ use Mix.Config config :liquid_voting, LiquidVotingWeb.Endpoint, url: [ - host: System.get_env("APP_HOSTNAME") || "localhost" , + host: System.get_env("APP_HOSTNAME") || "localhost", port: String.to_integer(System.get_env("APP_PORT") || "4000") ], server: true @@ -10,5 +10,3 @@ config :liquid_voting, LiquidVotingWeb.Endpoint, config :logger, level: :info config :liquid_voting, LiquidVotingWeb.Endpoint, server: true - - diff --git a/config/releases.exs b/config/releases.exs index 6b24181a..e32d6af9 100644 --- a/config/releases.exs +++ b/config/releases.exs @@ -2,8 +2,8 @@ import Config db_user = System.fetch_env!("DB_USERNAME") db_password = System.fetch_env!("DB_PASSWORD") -db_name= System.fetch_env!("DB_NAME") -db_host= System.fetch_env!("DB_HOST") +db_name = System.fetch_env!("DB_NAME") +db_host = System.fetch_env!("DB_HOST") db_pool_size = System.get_env("DB_POOL_SIZE") || "10" port = System.get_env("APP_PORT") || "4000" diff --git a/lib/liquid_voting/delegations.ex b/lib/liquid_voting/delegations.ex index a56ebe79..799f17db 100644 --- a/lib/liquid_voting/delegations.ex +++ b/lib/liquid_voting/delegations.ex @@ -8,6 +8,7 @@ defmodule LiquidVoting.Delegations do alias LiquidVoting.Delegations.Delegation alias LiquidVoting.Voting + @doc """ Returns the list of delegations for an organization uuid @@ -20,8 +21,8 @@ defmodule LiquidVoting.Delegations do def list_delegations(organization_uuid) do Delegation |> where(organization_uuid: ^organization_uuid) - |> Repo.all - |> Repo.preload([:delegator,:delegate]) + |> Repo.all() + |> Repo.preload([:delegator, :delegate]) end @doc """ @@ -41,7 +42,7 @@ defmodule LiquidVoting.Delegations do def get_delegation!(id, organization_uuid) do Delegation |> Repo.get_by!(id: id, organization_uuid: organization_uuid) - |> Repo.preload([:delegator,:delegate]) + |> Repo.preload([:delegator, :delegate]) end @doc """ @@ -63,7 +64,12 @@ defmodule LiquidVoting.Delegations do delegate = Voting.get_participant_by_email!(delegate_email, organization_uuid) Delegation - |> Repo.get_by!([delegator_id: delegator.id, delegate_id: delegate.id, proposal_url: proposal_url, organization_uuid: organization_uuid]) + |> Repo.get_by!( + delegator_id: delegator.id, + delegate_id: delegate.id, + proposal_url: proposal_url, + organization_uuid: organization_uuid + ) end @doc """ @@ -85,7 +91,11 @@ defmodule LiquidVoting.Delegations do delegate = Voting.get_participant_by_email!(delegate_email, organization_uuid) Delegation - |> Repo.get_by!([delegator_id: delegator.id, delegate_id: delegate.id, organization_uuid: organization_uuid]) + |> Repo.get_by!( + delegator_id: delegator.id, + delegate_id: delegate.id, + organization_uuid: organization_uuid + ) end @doc """ @@ -109,7 +119,7 @@ defmodule LiquidVoting.Delegations do def create_delegation!(attrs \\ %{}) do %Delegation{} |> Delegation.changeset(attrs) - |> Repo.insert + |> Repo.insert() end @doc """ diff --git a/lib/liquid_voting/metrics.ex b/lib/liquid_voting/metrics.ex index bc679fae..c9e4d505 100644 --- a/lib/liquid_voting/metrics.ex +++ b/lib/liquid_voting/metrics.ex @@ -42,4 +42,4 @@ end defmodule LiquidVoting.Metrics.PrometheusExporter do use Prometheus.PlugExporter -end \ No newline at end of file +end diff --git a/lib/liquid_voting/release.ex b/lib/liquid_voting/release.ex index 9a627fe4..052d2983 100644 --- a/lib/liquid_voting/release.ex +++ b/lib/liquid_voting/release.ex @@ -15,4 +15,4 @@ defmodule LiquidVoting.Release do Application.load(@app) Application.fetch_env!(@app, :ecto_repos) end -end \ No newline at end of file +end diff --git a/lib/liquid_voting/voting.ex b/lib/liquid_voting/voting.ex index 52ae5602..8243609d 100644 --- a/lib/liquid_voting/voting.ex +++ b/lib/liquid_voting/voting.ex @@ -6,7 +6,7 @@ defmodule LiquidVoting.Voting do import Ecto.Query, warn: false alias LiquidVoting.Repo - alias LiquidVoting.Voting.{Vote,Participant} + alias LiquidVoting.Voting.{Vote, Participant} alias LiquidVoting.Delegations alias LiquidVoting.Delegations.Delegation @@ -24,22 +24,26 @@ defmodule LiquidVoting.Voting do """ def create_vote(attrs \\ %{}) do - Repo.transaction( - fn -> - case %Vote{} |> Vote.changeset(attrs) |> Repo.insert() do - {:ok, vote} -> - if delegation = Repo.get_by(Delegation, [delegator_id: attrs[:participant_id], organization_uuid: attrs[:organization_uuid]]) do - case Delegations.delete_delegation(delegation) do - {:ok, _delegation} -> vote - {:error, changeset} -> Repo.rollback(changeset) - end - else - vote + Repo.transaction(fn -> + case %Vote{} |> Vote.changeset(attrs) |> Repo.insert() do + {:ok, vote} -> + if delegation = + Repo.get_by(Delegation, + delegator_id: attrs[:participant_id], + organization_uuid: attrs[:organization_uuid] + ) do + case Delegations.delete_delegation(delegation) do + {:ok, _delegation} -> vote + {:error, changeset} -> Repo.rollback(changeset) end - {:error, changeset} -> Repo.rollback(changeset) - end + else + vote + end + + {:error, changeset} -> + Repo.rollback(changeset) end - ) + end) end @doc """ @@ -69,7 +73,7 @@ defmodule LiquidVoting.Voting do """ def list_votes(proposal_url, organization_uuid) do Vote - |> where([proposal_url: ^proposal_url, organization_uuid: ^organization_uuid]) + |> where(proposal_url: ^proposal_url, organization_uuid: ^organization_uuid) |> Repo.all() |> Repo.preload([:participant]) end @@ -90,7 +94,7 @@ defmodule LiquidVoting.Voting do """ def get_vote!(id, organization_uuid) do Vote - |> Repo.get_by!([id: id, organization_uuid: organization_uuid]) + |> Repo.get_by!(id: id, organization_uuid: organization_uuid) |> Repo.preload([:participant]) end @@ -109,9 +113,14 @@ defmodule LiquidVoting.Voting do """ def get_vote!(email, proposal_url, organization_uuid) do participant = get_participant_by_email!(email, organization_uuid) + Vote - |> Repo.get_by!([participant_id: participant.id, proposal_url: proposal_url, organization_uuid: organization_uuid]) - |> Repo.preload([:participant]) + |> Repo.get_by!( + participant_id: participant.id, + proposal_url: proposal_url, + organization_uuid: organization_uuid + ) + |> Repo.preload([:participant]) end # Just for seeding @@ -215,7 +224,7 @@ defmodule LiquidVoting.Voting do """ def get_participant!(id, organization_uuid) do Participant - |> Repo.get_by!([id: id, organization_uuid: organization_uuid]) + |> Repo.get_by!(id: id, organization_uuid: organization_uuid) end @doc """ @@ -234,7 +243,7 @@ defmodule LiquidVoting.Voting do """ def get_participant_by_email(email, organization_uuid) do Participant - |> Repo.get_by([email: email, organization_uuid: organization_uuid]) + |> Repo.get_by(email: email, organization_uuid: organization_uuid) end @doc """ @@ -253,7 +262,7 @@ defmodule LiquidVoting.Voting do """ def get_participant_by_email!(email, organization_uuid) do Participant - |> Repo.get_by!([email: email, organization_uuid: organization_uuid]) + |> Repo.get_by!(email: email, organization_uuid: organization_uuid) end @doc """ @@ -286,7 +295,7 @@ defmodule LiquidVoting.Voting do |> Repo.insert( on_conflict: {:replace_all_except, [:id]}, conflict_target: [:organization_uuid, :email] - ) + ) end @doc """ diff --git a/lib/liquid_voting/voting_results.ex b/lib/liquid_voting/voting_results.ex index cbf2f307..72af8a0c 100644 --- a/lib/liquid_voting/voting_results.ex +++ b/lib/liquid_voting/voting_results.ex @@ -30,7 +30,7 @@ defmodule LiquidVoting.VotingResults do } attrs = - Enum.reduce votes, attrs, fn (vote, attrs) -> + Enum.reduce(votes, attrs, fn vote, attrs -> {:ok, vote} = VotingWeight.update_vote_weight(vote) if vote.yes do @@ -38,14 +38,14 @@ defmodule LiquidVoting.VotingResults do else Map.update!(attrs, :against, &(&1 + vote.weight)) end - end + end) %Result{} |> Result.changeset(attrs) |> Repo.insert!( on_conflict: {:replace_all_except, [:id]}, conflict_target: [:organization_uuid, :proposal_url] - ) + ) end @doc """ @@ -98,7 +98,7 @@ defmodule LiquidVoting.VotingResults do """ def get_result!(id, organization_uuid) do Result - |> Repo.get_by!([id: id, organization_uuid: organization_uuid]) + |> Repo.get_by!(id: id, organization_uuid: organization_uuid) end @doc """ @@ -117,7 +117,7 @@ defmodule LiquidVoting.VotingResults do """ def get_result_by_proposal_url(proposal_url, organization_uuid) do Result - |> Repo.get_by([proposal_url: proposal_url, organization_uuid: organization_uuid]) + |> Repo.get_by(proposal_url: proposal_url, organization_uuid: organization_uuid) end @doc """ @@ -141,6 +141,6 @@ defmodule LiquidVoting.VotingResults do def create_result!(attrs \\ %{}) do %Result{} |> Result.changeset(attrs) - |> Repo.insert! + |> Repo.insert!() end end diff --git a/lib/liquid_voting/voting_results/result.ex b/lib/liquid_voting/voting_results/result.ex index 33ee18d6..f0043055 100644 --- a/lib/liquid_voting/voting_results/result.ex +++ b/lib/liquid_voting/voting_results/result.ex @@ -19,6 +19,8 @@ defmodule LiquidVoting.VotingResults.Result do result |> cast(attrs, all_fields) |> validate_required(required_fields) - |> unique_constraint(:organization_uuid_proposal_url, name: :uniq_index_organization_uuid_proposal_url) + |> unique_constraint(:organization_uuid_proposal_url, + name: :uniq_index_organization_uuid_proposal_url + ) end end diff --git a/lib/liquid_voting/voting_weight.ex b/lib/liquid_voting/voting_weight.ex index e3e34817..d9647f34 100644 --- a/lib/liquid_voting/voting_weight.ex +++ b/lib/liquid_voting/voting_weight.ex @@ -22,9 +22,9 @@ defmodule LiquidVoting.VotingWeight do """ def update_vote_weight(vote) do # :force it because sometimes votes come in with stale associations - vote = Repo.preload(vote, [participant: :delegations_received], force: true) + vote = Repo.preload(vote, [participant: :delegations_received], force: true) voter = vote.participant - + weight = 1 + delegation_weight(voter.delegations_received, vote.proposal_url) Voting.update_vote(vote, %{weight: weight}) @@ -38,7 +38,7 @@ defmodule LiquidVoting.VotingWeight do # Traverse up delegation tree and add up the accumulated weight. # If you're wondering, [_|_] matches a non-empty array. Equivalent to # matching [head|tail] but ignoring both head and tail variables - defp delegation_weight(delegations = [_|_], proposal_url, weight) do + defp delegation_weight(delegations = [_ | _], proposal_url, weight) do # TODO: Do this in SQL # # Not sure yet which tree/hierarchy handling pattern would fit here, @@ -51,22 +51,22 @@ defmodule LiquidVoting.VotingWeight do # # Add-up 1 unit of weight for each delegation, # then recurse on each delegators' own delegations - Enum.reduce delegations, weight, fn (delegation, weight) -> + Enum.reduce(delegations, weight, fn delegation, weight -> # Only proceed if delegation is global or is meant for the # proposal being voted on: if delegation.proposal_url == proposal_url || delegation.proposal_url == nil do - delegation = Repo.preload(delegation, [delegator: :delegations_received]) + delegation = Repo.preload(delegation, delegator: :delegations_received) delegator = delegation.delegator weight = weight + 1 delegation_weight(delegator.delegations_received, proposal_url, weight) - # If delegation is for a different proposal, just return the unchanged weight + # If delegation is for a different proposal, just return the unchanged weight else weight end - end + end) end # Base case for the above recursion: @@ -75,4 +75,4 @@ defmodule LiquidVoting.VotingWeight do defp delegation_weight(_ = [], _, weight) do weight end -end \ No newline at end of file +end diff --git a/lib/liquid_voting_web/plugs/context.ex b/lib/liquid_voting_web/plugs/context.ex index 79f61c75..7c9d5b86 100644 --- a/lib/liquid_voting_web/plugs/context.ex +++ b/lib/liquid_voting_web/plugs/context.ex @@ -21,4 +21,4 @@ defmodule LiquidVotingWeb.Plugs.Context do _ -> %{} end end -end \ No newline at end of file +end diff --git a/lib/liquid_voting_web/resolvers/delegations.ex b/lib/liquid_voting_web/resolvers/delegations.ex index 805aa7c6..cf7be124 100644 --- a/lib/liquid_voting_web/resolvers/delegations.ex +++ b/lib/liquid_voting_web/resolvers/delegations.ex @@ -1,5 +1,5 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do - alias LiquidVoting.{Delegations,Voting,VotingResults} + alias LiquidVoting.{Delegations, Voting, VotingResults} alias LiquidVotingWeb.Schema.ChangesetErrors def delegations(_, _, %{context: %{organization_uuid: organization_uuid}}) do @@ -13,24 +13,29 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do # Will add participants to the db if they don't exist yet, or fetch them if they do. # Their ids are used for delegator_id and delegate_id when inserting the delegation # with create_delegation_with_valid_arguments/1 - def create_delegation(_, %{delegator_email: delegator_email, delegate_email: delegate_email} = args, %{context: %{organization_uuid: organization_uuid}}) do + def create_delegation( + _, + %{delegator_email: delegator_email, delegate_email: delegate_email} = args, + %{context: %{organization_uuid: organization_uuid}} + ) do case Voting.upsert_participant(%{email: delegator_email, organization_uuid: organization_uuid}) do {:error, changeset} -> {:error, message: "Could not create delegation with given email", - details: ChangesetErrors.error_details(changeset) - } + details: ChangesetErrors.error_details(changeset)} {:ok, delegator} -> args = Map.put(args, :delegator_id, delegator.id) args = Map.put(args, :organization_uuid, organization_uuid) - case Voting.upsert_participant(%{email: delegate_email, organization_uuid: organization_uuid}) do + case Voting.upsert_participant(%{ + email: delegate_email, + organization_uuid: organization_uuid + }) do {:error, changeset} -> {:error, message: "Could not create delegation with given email", - details: ChangesetErrors.error_details(changeset) - } + details: ChangesetErrors.error_details(changeset)} {:ok, delegate} -> args = Map.put(args, :delegate_id, delegate.id) @@ -48,9 +53,7 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do case Delegations.create_delegation(args) do {:error, changeset} -> {:error, - message: "Could not create delegation", - details: ChangesetErrors.error_details(changeset) - } + message: "Could not create delegation", details: ChangesetErrors.error_details(changeset)} {:ok, delegation} -> {:ok, delegation} @@ -58,20 +61,41 @@ defmodule LiquidVotingWeb.Resolvers.Delegations do end ## Delete proposal-specific delegations - def delete_delegation(_, %{delegator_email: delegator_email, delegate_email: delegate_email, proposal_url: proposal_url}, %{context: %{organization_uuid: organization_uuid}}) do - deleted_delegation = Delegations.get_delegation!(delegator_email, delegate_email, proposal_url, organization_uuid) |> Delegations.delete_delegation! + def delete_delegation( + _, + %{ + delegator_email: delegator_email, + delegate_email: delegate_email, + proposal_url: proposal_url + }, + %{context: %{organization_uuid: organization_uuid}} + ) do + deleted_delegation = + Delegations.get_delegation!( + delegator_email, + delegate_email, + proposal_url, + organization_uuid + ) + |> Delegations.delete_delegation!() + VotingResults.publish_voting_result_change(proposal_url, organization_uuid) {:ok, deleted_delegation} - rescue - Ecto.NoResultsError -> {:error, message: "No delegation found to delete" } + rescue + Ecto.NoResultsError -> {:error, message: "No delegation found to delete"} end ## Delete global delegations - def delete_delegation(_, %{delegator_email: delegator_email, delegate_email: delegate_email}, %{context: %{organization_uuid: organization_uuid}}) do - deleted_delegation = Delegations.get_delegation!(delegator_email, delegate_email, organization_uuid) |> Delegations.delete_delegation! + def delete_delegation(_, %{delegator_email: delegator_email, delegate_email: delegate_email}, %{ + context: %{organization_uuid: organization_uuid} + }) do + deleted_delegation = + Delegations.get_delegation!(delegator_email, delegate_email, organization_uuid) + |> Delegations.delete_delegation!() + # VotingResults.publish_voting_result_change(proposal_url, organization_uuid) {:ok, deleted_delegation} - rescue - Ecto.NoResultsError -> {:error, message: "No delegation found to delete" } + rescue + Ecto.NoResultsError -> {:error, message: "No delegation found to delete"} end -end \ No newline at end of file +end diff --git a/lib/liquid_voting_web/resolvers/voting.ex b/lib/liquid_voting_web/resolvers/voting.ex index ed0ed5f5..c59575aa 100644 --- a/lib/liquid_voting_web/resolvers/voting.ex +++ b/lib/liquid_voting_web/resolvers/voting.ex @@ -1,5 +1,5 @@ defmodule LiquidVotingWeb.Resolvers.Voting do - alias LiquidVoting.{Voting,VotingResults} + alias LiquidVoting.{Voting, VotingResults} alias LiquidVotingWeb.Schema.ChangesetErrors def participants(_, _, %{context: %{organization_uuid: organization_uuid}}) do @@ -17,8 +17,7 @@ defmodule LiquidVotingWeb.Resolvers.Voting do {:error, changeset} -> {:error, message: "Could not create participant", - details: ChangesetErrors.error_details(changeset) - } + details: ChangesetErrors.error_details(changeset)} {:ok, participant} -> {:ok, participant} @@ -37,13 +36,14 @@ defmodule LiquidVotingWeb.Resolvers.Voting do {:ok, Voting.get_vote!(id, organization_uuid)} end - def create_vote(_, %{participant_email: email, proposal_url: _, yes: _} = args, %{context: %{organization_uuid: organization_uuid}}) do + def create_vote(_, %{participant_email: email, proposal_url: _, yes: _} = args, %{ + context: %{organization_uuid: organization_uuid} + }) do case Voting.upsert_participant(%{email: email, organization_uuid: organization_uuid}) do {:error, changeset} -> {:error, message: "Could not create vote with given email", - details: ChangesetErrors.error_details(changeset) - } + details: ChangesetErrors.error_details(changeset)} {:ok, participant} -> args = Map.put(args, :organization_uuid, organization_uuid) @@ -52,22 +52,24 @@ defmodule LiquidVotingWeb.Resolvers.Voting do end end - def create_vote(_, %{participant_id: _, proposal_url: _, yes: _} = args, %{context: %{organization_uuid: organization_uuid}}) do + def create_vote(_, %{participant_id: _, proposal_url: _, yes: _} = args, %{ + context: %{organization_uuid: organization_uuid} + }) do args = Map.put(args, :organization_uuid, organization_uuid) create_vote_with_valid_arguments(args) end def create_vote(_, %{proposal_url: _, yes: _}, _) do - {:error, message: "Could not create vote", details: "No participant identifier (id or email) submitted"} + {:error, + message: "Could not create vote", + details: "No participant identifier (id or email) submitted"} end defp create_vote_with_valid_arguments(args) do case Voting.create_vote(args) do {:error, changeset} -> {:error, - message: "Could not create vote", - details: ChangesetErrors.error_details(changeset) - } + message: "Could not create vote", details: ChangesetErrors.error_details(changeset)} {:ok, vote} -> VotingResults.publish_voting_result_change(vote.proposal_url, vote.organization_uuid) @@ -75,11 +77,19 @@ defmodule LiquidVotingWeb.Resolvers.Voting do end end - def delete_vote(_, %{participant_email: email, proposal_url: proposal_url}, %{context: %{organization_uuid: organization_uuid}}) do - deleted_vote = Voting.get_vote!(email, proposal_url, organization_uuid) |> Voting.delete_vote! - VotingResults.publish_voting_result_change(deleted_vote.proposal_url, deleted_vote.organization_uuid) + def delete_vote(_, %{participant_email: email, proposal_url: proposal_url}, %{ + context: %{organization_uuid: organization_uuid} + }) do + deleted_vote = + Voting.get_vote!(email, proposal_url, organization_uuid) |> Voting.delete_vote!() + + VotingResults.publish_voting_result_change( + deleted_vote.proposal_url, + deleted_vote.organization_uuid + ) + {:ok, deleted_vote} - rescue - Ecto.NoResultsError -> {:error, message: "No vote found to delete" } + rescue + Ecto.NoResultsError -> {:error, message: "No vote found to delete"} end -end \ No newline at end of file +end diff --git a/lib/liquid_voting_web/resolvers/voting_results.ex b/lib/liquid_voting_web/resolvers/voting_results.ex index 7da85155..160bf954 100644 --- a/lib/liquid_voting_web/resolvers/voting_results.ex +++ b/lib/liquid_voting_web/resolvers/voting_results.ex @@ -4,4 +4,4 @@ defmodule LiquidVotingWeb.Resolvers.VotingResults do def result(_, %{proposal_url: proposal_url}, %{context: %{organization_uuid: organization_uuid}}) do {:ok, VotingResults.get_result_by_proposal_url(proposal_url, organization_uuid)} end -end \ No newline at end of file +end diff --git a/lib/liquid_voting_web/router.ex b/lib/liquid_voting_web/router.ex index 3456ae70..576e72f8 100644 --- a/lib/liquid_voting_web/router.ex +++ b/lib/liquid_voting_web/router.ex @@ -14,8 +14,6 @@ defmodule LiquidVotingWeb.Router do socket: LiquidVotingWeb.UserSocket, interface: :simple - forward "/", Absinthe.Plug, - schema: LiquidVotingWeb.Schema.Schema - + forward "/", Absinthe.Plug, schema: LiquidVotingWeb.Schema.Schema end end diff --git a/lib/liquid_voting_web/schema/schema.ex b/lib/liquid_voting_web/schema/schema.ex index 141c7801..067690ea 100644 --- a/lib/liquid_voting_web/schema/schema.ex +++ b/lib/liquid_voting_web/schema/schema.ex @@ -2,7 +2,7 @@ defmodule LiquidVotingWeb.Schema.Schema do use Absinthe.Schema alias LiquidVoting.{Voting, VotingResults} - import_types Absinthe.Type.Custom + import_types(Absinthe.Type.Custom) import Absinthe.Resolution.Helpers, only: [dataloader: 1, dataloader: 3] alias LiquidVotingWeb.Resolvers @@ -10,97 +10,98 @@ defmodule LiquidVotingWeb.Schema.Schema do query do @desc "Get a voting result by its proposal url" field :voting_result, :result do - arg :proposal_url, non_null(:string) - resolve &Resolvers.VotingResults.result/3 + arg(:proposal_url, non_null(:string)) + resolve(&Resolvers.VotingResults.result/3) end @desc "Get a list of participants" field :participants, list_of(:participant) do - resolve &Resolvers.Voting.participants/3 + resolve(&Resolvers.Voting.participants/3) end @desc "Get a participant by its id" field :participant, :participant do - arg :id, non_null(:id) - resolve &Resolvers.Voting.participant/3 + arg(:id, non_null(:id)) + resolve(&Resolvers.Voting.participant/3) end @desc "Get a list of votes" field :votes, list_of(:vote) do - resolve &Resolvers.Voting.votes/3 + resolve(&Resolvers.Voting.votes/3) end @desc "Get a vote by its id" field :vote, :vote do - arg :id, non_null(:id) - resolve &Resolvers.Voting.vote/3 + arg(:id, non_null(:id)) + resolve(&Resolvers.Voting.vote/3) end @desc "Get a list of delegations" field :delegations, list_of(:delegation) do - resolve &Resolvers.Delegations.delegations/3 + resolve(&Resolvers.Delegations.delegations/3) end @desc "Get a delegation by its id" field :delegation, :delegation do - arg :id, non_null(:id) - resolve &Resolvers.Delegations.delegation/3 + arg(:id, non_null(:id)) + resolve(&Resolvers.Delegations.delegation/3) end end mutation do @desc "Create a participant" field :create_participant, :participant do - arg :name, non_null(:string) - arg :email, non_null(:string) - resolve &Resolvers.Voting.create_participant/3 + arg(:name, non_null(:string)) + arg(:email, non_null(:string)) + resolve(&Resolvers.Voting.create_participant/3) end @desc "Create a vote for a proposal" field :create_vote, :vote do - arg :proposal_url, non_null(:string) - arg :participant_id, :id - arg :participant_email, :string - arg :yes, non_null(:boolean) - resolve &Resolvers.Voting.create_vote/3 + arg(:proposal_url, non_null(:string)) + arg(:participant_id, :id) + arg(:participant_email, :string) + arg(:yes, non_null(:boolean)) + resolve(&Resolvers.Voting.create_vote/3) end @desc "Delete a vote for a proposal" field :delete_vote, :vote do - arg :proposal_url, non_null(:string) - arg :participant_id, :id - arg :participant_email, :string - resolve &Resolvers.Voting.delete_vote/3 + arg(:proposal_url, non_null(:string)) + arg(:participant_id, :id) + arg(:participant_email, :string) + resolve(&Resolvers.Voting.delete_vote/3) end @desc "Create a delegation" field :create_delegation, :delegation do - arg :delegator_id, :id - arg :delegate_id, :id - arg :delegator_email, :string - arg :delegate_email, :string - arg :proposal_url, :string - resolve &Resolvers.Delegations.create_delegation/3 + arg(:delegator_id, :id) + arg(:delegate_id, :id) + arg(:delegator_email, :string) + arg(:delegate_email, :string) + arg(:proposal_url, :string) + resolve(&Resolvers.Delegations.create_delegation/3) end @desc "Delete a delegation" field :delete_delegation, :delegation do - arg :delegator_id, :id - arg :delegate_id, :id - arg :delegator_email, :string - arg :delegate_email, :string - arg :proposal_url, :string - resolve &Resolvers.Delegations.delete_delegation/3 + arg(:delegator_id, :id) + arg(:delegate_id, :id) + arg(:delegator_email, :string) + arg(:delegate_email, :string) + arg(:proposal_url, :string) + resolve(&Resolvers.Delegations.delete_delegation/3) end end subscription do @desc "Subscribe to voting results changes for a proposal" field :voting_result_change, :result do - arg :proposal_url, non_null(:string) - config fn args, _res -> + arg(:proposal_url, non_null(:string)) + + config(fn args, _res -> {:ok, topic: args.proposal_url} - end + end) end end @@ -108,8 +109,12 @@ defmodule LiquidVotingWeb.Schema.Schema do field :id, non_null(:id) field :name, :string field :email, non_null(:string) + field :delegations_received, list_of(:delegation), - resolve: dataloader(Voting, :delegations_received, args: %{scope: :participant, foreign_key: :delegate_id}) + resolve: + dataloader(Voting, :delegations_received, + args: %{scope: :participant, foreign_key: :delegate_id} + ) end object :vote do @@ -118,11 +123,11 @@ defmodule LiquidVotingWeb.Schema.Schema do field :weight, non_null(:integer) field :proposal_url, non_null(:string) field :participant, non_null(:participant), resolve: dataloader(Voting) + field :voting_result, :result, resolve: fn vote, _, _ -> {:ok, VotingResults.get_result_by_proposal_url(vote.proposal_url, vote.organization_uuid)} end - end object :delegation do @@ -130,10 +135,15 @@ defmodule LiquidVotingWeb.Schema.Schema do field :delegator, non_null(:participant), resolve: dataloader(Voting) field :delegate, non_null(:participant), resolve: dataloader(Voting) field :proposal_url, :string + field :voting_result, :result, resolve: fn delegation, _, _ -> if delegation.proposal_url do - {:ok, VotingResults.get_result_by_proposal_url(delegation.proposal_url, delegation.organization_uuid)} + {:ok, + VotingResults.get_result_by_proposal_url( + delegation.proposal_url, + delegation.organization_uuid + )} else {:ok, nil} end @@ -151,7 +161,7 @@ defmodule LiquidVotingWeb.Schema.Schema do source = Dataloader.Ecto.new(LiquidVoting.Repo) loader = - Dataloader.new + Dataloader.new() |> Dataloader.add_source(Voting, source) |> Dataloader.add_source(Delegations, source) diff --git a/priv/repo/migrations/20190906163956_create_votes.exs b/priv/repo/migrations/20190906163956_create_votes.exs index d4636029..1b27fa3e 100644 --- a/priv/repo/migrations/20190906163956_create_votes.exs +++ b/priv/repo/migrations/20190906163956_create_votes.exs @@ -13,6 +13,9 @@ defmodule LiquidVoting.Repo.Migrations.CreateVotes do create index(:votes, [:participant_id]) create index(:votes, [:proposal_url]) - create unique_index(:votes, [:participant_id, :proposal_url], name: :unique_index_vote_id_participant_id_proposal_url) + + create unique_index(:votes, [:participant_id, :proposal_url], + name: :unique_index_vote_id_participant_id_proposal_url + ) end end diff --git a/priv/repo/migrations/20200516154111_update_vote_composite_index_with_organization_uuid.exs b/priv/repo/migrations/20200516154111_update_vote_composite_index_with_organization_uuid.exs index 61c54320..3148f324 100644 --- a/priv/repo/migrations/20200516154111_update_vote_composite_index_with_organization_uuid.exs +++ b/priv/repo/migrations/20200516154111_update_vote_composite_index_with_organization_uuid.exs @@ -2,7 +2,12 @@ defmodule LiquidVoting.Repo.Migrations.UpdateVoteCompositeIndexWithOrganizationU use Ecto.Migration def change do - drop unique_index(:votes, [:participant_id, :proposal_url], name: :unique_index_vote_id_participant_id_proposal_url) - create unique_index(:votes, [:organization_uuid, :participant_id, :proposal_url], name: :uniq_index_org_vote_participant_proposal) + drop unique_index(:votes, [:participant_id, :proposal_url], + name: :unique_index_vote_id_participant_id_proposal_url + ) + + create unique_index(:votes, [:organization_uuid, :participant_id, :proposal_url], + name: :uniq_index_org_vote_participant_proposal + ) end end diff --git a/priv/repo/migrations/20200519150515_update_participant_index_on_email.exs b/priv/repo/migrations/20200519150515_update_participant_index_on_email.exs index 52f6ed8c..6d84d6de 100644 --- a/priv/repo/migrations/20200519150515_update_participant_index_on_email.exs +++ b/priv/repo/migrations/20200519150515_update_participant_index_on_email.exs @@ -3,6 +3,9 @@ defmodule LiquidVoting.Repo.Migrations.UpdateParticipantIndexOnEmail do def change do drop unique_index(:participants, [:email]) - create unique_index(:participants, [:organization_uuid, :email], name: :uniq_index_organization_uuid_participant_email) + + create unique_index(:participants, [:organization_uuid, :email], + name: :uniq_index_organization_uuid_participant_email + ) end end diff --git a/priv/repo/migrations/20200520103205_add_org_uuid_to_result_proposal_index.exs b/priv/repo/migrations/20200520103205_add_org_uuid_to_result_proposal_index.exs index 3e97b105..e9f13a02 100644 --- a/priv/repo/migrations/20200520103205_add_org_uuid_to_result_proposal_index.exs +++ b/priv/repo/migrations/20200520103205_add_org_uuid_to_result_proposal_index.exs @@ -3,6 +3,9 @@ defmodule LiquidVoting.Repo.Migrations.AddOrgUuidToResultProposalIndex do def change do drop unique_index(:results, [:proposal_url]) - create unique_index(:results, [:organization_uuid, :proposal_url], name: :uniq_index_organization_uuid_proposal_url) + + create unique_index(:results, [:organization_uuid, :proposal_url], + name: :uniq_index_organization_uuid_proposal_url + ) end end diff --git a/priv/repo/migrations/20200521155938_add_uniq_index_to_delegations.exs b/priv/repo/migrations/20200521155938_add_uniq_index_to_delegations.exs index 792d1b61..092802f4 100644 --- a/priv/repo/migrations/20200521155938_add_uniq_index_to_delegations.exs +++ b/priv/repo/migrations/20200521155938_add_uniq_index_to_delegations.exs @@ -2,6 +2,8 @@ defmodule LiquidVoting.Repo.Migrations.AddUniqIndexToDelegations do use Ecto.Migration def change do - create unique_index(:delegations, [:organization_uuid, :delegator_id, :delegate_id], name: :uniq_index_org_delegator_delegate) + create unique_index(:delegations, [:organization_uuid, :delegator_id, :delegate_id], + name: :uniq_index_org_delegator_delegate + ) end end diff --git a/priv/repo/migrations/20200609161238_rename_results_fields.exs b/priv/repo/migrations/20200609161238_rename_results_fields.exs index 33e38dc6..171b2257 100644 --- a/priv/repo/migrations/20200609161238_rename_results_fields.exs +++ b/priv/repo/migrations/20200609161238_rename_results_fields.exs @@ -3,6 +3,6 @@ defmodule LiquidVoting.Repo.Migrations.RenameResultsFields do def change do rename table("results"), :yes, to: :in_favor - rename table("results"), :no, to: :against + rename table("results"), :no, to: :against end end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index d4726a64..eab44fe0 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -37,4 +37,3 @@ # VotingResults.calculate_result!(proposal_url) # |> IO.inspect - diff --git a/test/liquid_voting/delegations_test.exs b/test/liquid_voting/delegations_test.exs index c1920772..b6fba4ea 100644 --- a/test/liquid_voting/delegations_test.exs +++ b/test/liquid_voting/delegations_test.exs @@ -16,12 +16,12 @@ defmodule LiquidVoting.DelegationsTest do valid_attrs: %{ delegator_id: delegator.id, delegate_id: delegate.id, - organization_uuid: Ecto.UUID.generate + organization_uuid: Ecto.UUID.generate() }, update_attrs: %{ delegator_id: delegator.id, delegate_id: another_delegate.id, - organization_uuid: Ecto.UUID.generate + organization_uuid: Ecto.UUID.generate() }, invalid_attrs: %{delegator_id: delegator.id, delegate_id: nil, organization_uuid: nil} ] @@ -34,11 +34,14 @@ defmodule LiquidVoting.DelegationsTest do test "get_delegation!/2 returns the delegation with given id and organization_uuid" do delegation = insert(:delegation) - assert Delegations.get_delegation!(delegation.id, delegation.organization_uuid) == delegation + + assert Delegations.get_delegation!(delegation.id, delegation.organization_uuid) == + delegation end test "create_delegation/1 with valid data creates a delegation", context do - assert {:ok, %Delegation{} = delegation} = Delegations.create_delegation(context[:valid_attrs]) + assert {:ok, %Delegation{} = delegation} = + Delegations.create_delegation(context[:valid_attrs]) end test "create_delegation/1 with invalid data returns error changeset", context do @@ -50,6 +53,7 @@ defmodule LiquidVoting.DelegationsTest do proposal_url = """ https://www.bigassstring.com/search?ei=WdznXfzyIoeT1fAP79yWqAc&q=chrome+extension+popup+js+xhr+onload+document.body&oq=chrome+extension+popup+js+xhr+onload+document.body&gs_l=psy-ab.3...309222.313422..314027...0.0..1.201.1696.5j9j1....2..0....1..gws-wiz.2OvPoKSwZ_I&ved=0ahUKEwi8g5fQspzmAhWHSRUIHW-uBXUQ4dUDCAs&uact=5" """ + args = Map.merge(context[:valid_attrs], %{proposal_url: proposal_url}) assert {:ok, %Delegation{} = delegation} = Delegations.create_delegation(args) end @@ -61,13 +65,18 @@ defmodule LiquidVoting.DelegationsTest do test "update_delegation/2 with valid data updates the delegation", context do delegation = insert(:delegation) - assert {:ok, %Delegation{} = delegation} = Delegations.update_delegation(delegation, context[:update_attrs]) + + assert {:ok, %Delegation{} = delegation} = + Delegations.update_delegation(delegation, context[:update_attrs]) end test "delete_delegation/1 deletes the delegation" do delegation = insert(:delegation) assert {:ok, %Delegation{}} = Delegations.delete_delegation(delegation) - assert_raise Ecto.NoResultsError, fn -> Delegations.get_delegation!(delegation.id, delegation.organization_uuid) end + + assert_raise Ecto.NoResultsError, fn -> + Delegations.get_delegation!(delegation.id, delegation.organization_uuid) + end end test "change_delegation/1 returns a delegation changeset" do diff --git a/test/liquid_voting/participants_test.exs b/test/liquid_voting/participants_test.exs index 17ec2488..678c0f9f 100644 --- a/test/liquid_voting/participants_test.exs +++ b/test/liquid_voting/participants_test.exs @@ -6,8 +6,16 @@ defmodule LiquidVoting.ParticipantsTest do alias LiquidVoting.Voting.Participant describe "participants" do - @valid_attrs %{name: "some name", email: "some@email.com", organization_uuid: Ecto.UUID.generate} - @update_attrs %{name: "some updated name", email: "another@email.com", organization_uuid: Ecto.UUID.generate} + @valid_attrs %{ + name: "some name", + email: "some@email.com", + organization_uuid: Ecto.UUID.generate() + } + @update_attrs %{ + name: "some updated name", + email: "another@email.com", + organization_uuid: Ecto.UUID.generate() + } @invalid_attrs %{email: nil, organization_uuid: nil} test "list_participants/1 returns all participants for an organization_uuid" do @@ -22,16 +30,23 @@ defmodule LiquidVoting.ParticipantsTest do test "get_participant_by_email/2 returns the participant with given email and organization_uuid" do participant = insert(:participant) - assert Voting.get_participant_by_email(participant.email, participant.organization_uuid) == participant + + assert Voting.get_participant_by_email(participant.email, participant.organization_uuid) == + participant end test "get_participant_by_email/2 returns nil when a participant is not found" do - assert Voting.get_participant_by_email("non@participant.com", @valid_attrs[:organization_uuid]) == nil + assert Voting.get_participant_by_email( + "non@participant.com", + @valid_attrs[:organization_uuid] + ) == nil end test "get_participant_by_email!/2 returns the participant with given email and organization_uuid" do participant = insert(:participant) - assert Voting.get_participant_by_email!(participant.email, participant.organization_uuid) == participant + + assert Voting.get_participant_by_email!(participant.email, participant.organization_uuid) == + participant end test "get_participant_by_email!/2 raises Ecto.NoResultsError when participant is not found" do @@ -75,7 +90,10 @@ defmodule LiquidVoting.ParticipantsTest do test "update_participant/2 with valid data updates the participant" do participant = insert(:participant) - assert {:ok, %Participant{} = participant} = Voting.update_participant(participant, @update_attrs) + + assert {:ok, %Participant{} = participant} = + Voting.update_participant(participant, @update_attrs) + assert participant.name == "some updated name" end @@ -88,7 +106,10 @@ defmodule LiquidVoting.ParticipantsTest do test "delete_participant/1 deletes the participant" do participant = insert(:participant) assert {:ok, %Participant{}} = Voting.delete_participant(participant) - assert_raise Ecto.NoResultsError, fn -> Voting.get_participant!(participant.id, participant.organization_uuid) end + + assert_raise Ecto.NoResultsError, fn -> + Voting.get_participant!(participant.id, participant.organization_uuid) + end end test "change_participant/1 returns a participant changeset" do diff --git a/test/liquid_voting/voting_results_test.exs b/test/liquid_voting/voting_results_test.exs index 211f88a8..0517f240 100644 --- a/test/liquid_voting/voting_results_test.exs +++ b/test/liquid_voting/voting_results_test.exs @@ -8,6 +8,7 @@ defmodule LiquidVoting.VotingResultsTest do describe "calculate_result!/1" do setup do vote = insert(:vote, yes: true) + [ proposal_url: vote.proposal_url, organization_uuid: vote.organization_uuid @@ -15,30 +16,42 @@ defmodule LiquidVoting.VotingResultsTest do end test "returns a result with the number of in_favor and no votes", context do - assert %Result{in_favor: in_favor, against: against} = VotingResults.calculate_result!(context[:proposal_url], context[:organization_uuid]) + assert %Result{in_favor: in_favor, against: against} = + VotingResults.calculate_result!( + context[:proposal_url], + context[:organization_uuid] + ) + assert in_favor == 1 assert against == 0 end test "returns the same result struct for a given proposal_url", context do - %Result{id: id} = VotingResults.calculate_result!(context[:proposal_url], context[:organization_uuid]) - insert(:vote, proposal_url: context[:proposal_url], organization_uuid: context[:organization_uuid]) - %Result{id: new_id} = VotingResults.calculate_result!(context[:proposal_url], context[:organization_uuid]) + %Result{id: id} = + VotingResults.calculate_result!(context[:proposal_url], context[:organization_uuid]) + + insert(:vote, + proposal_url: context[:proposal_url], + organization_uuid: context[:organization_uuid] + ) + + %Result{id: new_id} = + VotingResults.calculate_result!(context[:proposal_url], context[:organization_uuid]) + assert id == new_id end - end describe "create, get and list results" do setup do - organization_uuid = Ecto.UUID.generate + organization_uuid = Ecto.UUID.generate() + [ valid_attrs: %{ in_favor: 42, against: 42, proposal_url: "https://proposals.com/1", organization_uuid: organization_uuid - }, update_attrs: %{ in_favor: 43, @@ -67,11 +80,16 @@ defmodule LiquidVoting.VotingResultsTest do test "get_result_by_proposal_url/2 returns the result with given proposal_url and organization_uuid" do result = insert(:voting_result) - assert VotingResults.get_result_by_proposal_url(result.proposal_url, result.organization_uuid) == result + + assert VotingResults.get_result_by_proposal_url( + result.proposal_url, + result.organization_uuid + ) == result end test "get_result_by_proposal_url/2 with invalid data returns nil" do - assert VotingResults.get_result_by_proposal_url("https://invalid.com", Ecto.UUID.generate) == nil + assert VotingResults.get_result_by_proposal_url("https://invalid.com", Ecto.UUID.generate()) == + nil end test "create_result/1 with valid data creates a result", context do diff --git a/test/liquid_voting/voting_test.exs b/test/liquid_voting/voting_test.exs index 5d854392..221bb9b4 100644 --- a/test/liquid_voting/voting_test.exs +++ b/test/liquid_voting/voting_test.exs @@ -15,13 +15,13 @@ defmodule LiquidVoting.VotingTest do yes: true, participant_id: participant.id, proposal_url: "http://proposals.com/1", - organization_uuid: Ecto.UUID.generate + organization_uuid: Ecto.UUID.generate() }, update_attrs: %{ yes: false, participant_id: participant.id, proposal_url: "http://proposals.com/2", - organization_uuid: Ecto.UUID.generate + organization_uuid: Ecto.UUID.generate() }, invalid_attrs: %{ yes: nil, @@ -41,6 +41,7 @@ defmodule LiquidVoting.VotingTest do proposal_url = """ https://www.bigassstring.com/search?ei=WdznXfzyIoeT1fAP79yWqAc&q=chrome+extension+popup+js+xhr+onload+document.body&oq=chrome+extension+popup+js+xhr+onload+document.body&gs_l=psy-ab.3...309222.313422..314027...0.0..1.201.1696.5j9j1....2..0....1..gws-wiz.2OvPoKSwZ_I&ved=0ahUKEwi8g5fQspzmAhWHSRUIHW-uBXUQ4dUDCAs&uact=5" """ + args = Map.merge(context[:valid_attrs], %{proposal_url: proposal_url}) assert {:ok, %Vote{} = vote} = Voting.create_vote(args) end @@ -48,12 +49,15 @@ defmodule LiquidVoting.VotingTest do test "create_vote/1 deletes previous delegation by participant if present" do participant = insert(:participant) delegation = insert(:delegation, delegator: participant) - assert {:ok, %Vote{}} = Voting.create_vote(%{ - yes: false, - participant_id: participant.id, - proposal_url: "http://proposals.com/any", - organization_uuid: delegation.organization_uuid - }) + + assert {:ok, %Vote{}} = + Voting.create_vote(%{ + yes: false, + participant_id: participant.id, + proposal_url: "http://proposals.com/any", + organization_uuid: delegation.organization_uuid + }) + assert LiquidVoting.Repo.get(Delegation, delegation.id) == nil end @@ -92,14 +96,20 @@ defmodule LiquidVoting.VotingTest do participant = Voting.get_participant!(vote.participant_id, vote.organization_uuid) assert Voting.get_vote!( - participant.email, - vote.proposal_url, - vote.organization_uuid - ) == vote + participant.email, + vote.proposal_url, + vote.organization_uuid + ) == vote end test "get_vote!/3 raises Ecto.NoResultsError if invalid attrs are passed in" do - assert_raise Ecto.NoResultsError, fn -> Voting.get_vote!("novote@gmail.com", "https://apropos.com/not", "a6158b19-6bf6-4457-9d13-ef8b141611b4") end + assert_raise Ecto.NoResultsError, fn -> + Voting.get_vote!( + "novote@gmail.com", + "https://apropos.com/not", + "a6158b19-6bf6-4457-9d13-ef8b141611b4" + ) + end end test "update_vote/2 with valid data updates the vote", context do diff --git a/test/liquid_voting/voting_weight_test.exs b/test/liquid_voting/voting_weight_test.exs index e3cb0bc6..a2fdaba4 100644 --- a/test/liquid_voting/voting_weight_test.exs +++ b/test/liquid_voting/voting_weight_test.exs @@ -39,7 +39,11 @@ defmodule LiquidVoting.VotingWeightTest do delegator_to_the_delegator_of_the_delegator = insert(:participant) insert(:delegation, delegate: delegator, delegator: delegator_to_the_delegator) - insert(:delegation, delegate: delegator, delegator: delegator_to_the_delegator_of_the_delegator) + + insert(:delegation, + delegate: delegator, + delegator: delegator_to_the_delegator_of_the_delegator + ) {:ok, vote} = VotingWeight.update_vote_weight(vote) @@ -71,4 +75,4 @@ defmodule LiquidVoting.VotingWeightTest do assert vote.weight == 2 end end -end \ No newline at end of file +end diff --git a/test/liquid_voting_web/absinthe/mutations/create_delegation_test.exs b/test/liquid_voting_web/absinthe/mutations/create_delegation_test.exs index 069aad47..9d7d0fd8 100644 --- a/test/liquid_voting_web/absinthe/mutations/create_delegation_test.exs +++ b/test/liquid_voting_web/absinthe/mutations/create_delegation_test.exs @@ -12,7 +12,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do test "with emails" do query = """ mutation { - createDelegation(delegatorEmail: "#{@new_delegator_email}", delegateEmail: "#{@new_delegate_email}") { + createDelegation(delegatorEmail: "#{@new_delegator_email}", delegateEmail: "#{ + @new_delegate_email + }") { delegator { email name @@ -25,7 +27,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do } """ - {:ok, %{data: %{"createDelegation" => delegation}}} = Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate}) + {:ok, %{data: %{"createDelegation" => delegation}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate()}) assert delegation["delegator"]["email"] == @new_delegator_email assert delegation["delegator"]["name"] == nil @@ -36,7 +39,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do test "including a proposal url" do query = """ mutation { - createDelegation(delegatorEmail: "#{@new_delegator_email}", delegateEmail: "#{@new_delegate_email}", proposalUrl: "#{@proposal_url}") { + createDelegation(delegatorEmail: "#{@new_delegator_email}", delegateEmail: "#{ + @new_delegate_email + }", proposalUrl: "#{@proposal_url}") { delegator { email } @@ -48,7 +53,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do } """ - {:ok, %{data: %{"createDelegation" => delegation}}} = Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate}) + {:ok, %{data: %{"createDelegation" => delegation}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate()}) assert delegation["proposalUrl"] == @proposal_url end @@ -58,7 +64,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do query = """ mutation { - createDelegation(delegatorEmail: "#{@new_delegator_email}", delegateEmail: "#{@new_delegate_email}", proposalUrl: "#{@proposal_url}") { + createDelegation(delegatorEmail: "#{@new_delegator_email}", delegateEmail: "#{ + @new_delegate_email + }", proposalUrl: "#{@proposal_url}") { delegator { email } @@ -74,7 +82,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do } """ - {:ok, %{data: %{"createDelegation" => delegation}}} = Absinthe.run(query, Schema, context: %{organization_uuid: result.organization_uuid}) + {:ok, %{data: %{"createDelegation" => delegation}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: result.organization_uuid}) assert delegation["votingResult"]["in_favor"] == 0 assert delegation["votingResult"]["against"] == 0 @@ -83,8 +92,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do describe "create delegation with existing delegator and delegate" do setup do - delegator = insert(:participant) - delegate = insert(:participant) + delegator = insert(:participant) + delegate = insert(:participant) + [ delegator: delegator, delegate: delegate @@ -94,7 +104,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do test "with ids", context do query = """ mutation { - createDelegation(delegatorId: "#{context[:delegator].id}", delegateId: "#{context[:delegate].id}") { + createDelegation(delegatorId: "#{context[:delegator].id}", delegateId: "#{ + context[:delegate].id + }") { delegator { email } @@ -105,7 +117,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do } """ - {:ok, %{data: %{"createDelegation" => delegation}}} = Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate}) + {:ok, %{data: %{"createDelegation" => delegation}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate()}) assert delegation["delegator"]["email"] == context[:delegator].email assert delegation["delegate"]["email"] == context[:delegate].email @@ -114,7 +127,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do test "with emails", context do query = """ mutation { - createDelegation(delegatorEmail: "#{context[:delegator].email}", delegateEmail: "#{context[:delegate].email}") { + createDelegation(delegatorEmail: "#{context[:delegator].email}", delegateEmail: "#{ + context[:delegate].email + }") { delegator { email } @@ -125,7 +140,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do } """ - {:ok, %{data: %{"createDelegation" => delegation}}} = Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate}) + {:ok, %{data: %{"createDelegation" => delegation}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate()}) assert delegation["delegator"]["email"] == context[:delegator].email assert delegation["delegate"]["email"] == context[:delegate].email @@ -145,10 +161,11 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateDelegationTest do } """ - {:ok, %{errors: [%{message: message, details: details}]}} = Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate}) + {:ok, %{errors: [%{message: message, details: details}]}} = + Absinthe.run(query, Schema, context: %{organization_uuid: Ecto.UUID.generate()}) assert message == "Could not create delegation" assert details == %{delegate_id: ["can't be blank"]} end end -end \ No newline at end of file +end diff --git a/test/liquid_voting_web/absinthe/mutations/create_vote_test.exs b/test/liquid_voting_web/absinthe/mutations/create_vote_test.exs index 7e7ca6a1..ea46f307 100644 --- a/test/liquid_voting_web/absinthe/mutations/create_vote_test.exs +++ b/test/liquid_voting_web/absinthe/mutations/create_vote_test.exs @@ -7,6 +7,7 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do describe "create vote" do setup do participant = insert(:participant) + [ participant_id: participant.id, participant_email: participant.email, @@ -20,7 +21,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do test "with a new participant's email", context do query = """ mutation { - createVote(participantEmail: "#{context[:new_participant_email]}", proposalUrl:"#{context[:proposal_url]}", yes: #{context[:yes]}) { + createVote(participantEmail: "#{context[:new_participant_email]}", proposalUrl:"#{ + context[:proposal_url] + }", yes: #{context[:yes]}) { participant { email } @@ -29,7 +32,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do } """ - {:ok, %{data: %{"createVote" => vote}}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{data: %{"createVote" => vote}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert vote["participant"]["email"] == context[:new_participant_email] assert vote["yes"] == context[:yes] @@ -38,7 +42,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do test "with an existing participant's email", context do query = """ mutation { - createVote(participantEmail: "#{context[:participant_email]}", proposalUrl:"#{context[:proposal_url]}", yes: #{context[:yes]}) { + createVote(participantEmail: "#{context[:participant_email]}", proposalUrl:"#{ + context[:proposal_url] + }", yes: #{context[:yes]}) { participant { email } @@ -47,7 +53,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do } """ - {:ok, %{data: %{"createVote" => vote}}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{data: %{"createVote" => vote}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert vote["participant"]["email"] == context[:participant_email] assert vote["yes"] == context[:yes] @@ -56,7 +63,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do test "including voting results in the response", context do query = """ mutation { - createVote(participantEmail: "#{context[:participant_email]}", proposalUrl:"#{context[:proposal_url]}", yes: #{context[:yes]}) { + createVote(participantEmail: "#{context[:participant_email]}", proposalUrl:"#{ + context[:proposal_url] + }", yes: #{context[:yes]}) { participant { email } @@ -70,7 +79,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do } """ - {:ok, %{data: %{"createVote" => vote}}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{data: %{"createVote" => vote}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert vote["votingResult"]["in_favor"] == 1 assert vote["votingResult"]["against"] == 0 @@ -79,7 +89,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do test "with participant's id", context do query = """ mutation { - createVote(participantId: "#{context[:participant_id]}", proposalUrl:"#{context[:proposal_url]}", yes: #{context[:yes]}) { + createVote(participantId: "#{context[:participant_id]}", proposalUrl:"#{ + context[:proposal_url] + }", yes: #{context[:yes]}) { participant { email } @@ -88,7 +100,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do } """ - {:ok, %{data: %{"createVote" => vote}}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{data: %{"createVote" => vote}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert vote["participant"]["email"] == context[:participant_email] assert vote["yes"] == context[:yes] @@ -106,10 +119,11 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.CreateVoteTest do } """ - {:ok, %{errors: [%{message: message, details: details}]}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{errors: [%{message: message, details: details}]}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert message == "Could not create vote" assert details == "No participant identifier (id or email) submitted" end end -end \ No newline at end of file +end diff --git a/test/liquid_voting_web/absinthe/mutations/delete_delegation_test.exs b/test/liquid_voting_web/absinthe/mutations/delete_delegation_test.exs index 266b5e77..94a37c00 100644 --- a/test/liquid_voting_web/absinthe/mutations/delete_delegation_test.exs +++ b/test/liquid_voting_web/absinthe/mutations/delete_delegation_test.exs @@ -7,8 +7,19 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteDelegationTest do describe "delete delegation specific to a proposal" do setup do delegation = insert(:delegation_for_proposal) - insert(:vote, participant: delegation.delegate, proposal_url: delegation.proposal_url, organization_uuid: delegation.organization_uuid) - insert(:voting_result, in_favor: 2, proposal_url: delegation.proposal_url, organization_uuid: delegation.organization_uuid) + + insert(:vote, + participant: delegation.delegate, + proposal_url: delegation.proposal_url, + organization_uuid: delegation.organization_uuid + ) + + insert(:voting_result, + in_favor: 2, + proposal_url: delegation.proposal_url, + organization_uuid: delegation.organization_uuid + ) + [ delegator_email: delegation.delegator.email, delegate_email: delegation.delegate.email, @@ -20,7 +31,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteDelegationTest do test "with participant emails", context do query = """ mutation { - deleteDelegation(delegatorEmail: "#{context[:delegator_email]}", delegateEmail: "#{context[:delegate_email]}", proposalUrl: "#{context[:proposal_url]}") { + deleteDelegation(delegatorEmail: "#{context[:delegator_email]}", delegateEmail: "#{ + context[:delegate_email] + }", proposalUrl: "#{context[:proposal_url]}") { proposalUrl votingResult { in_favor @@ -30,7 +43,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteDelegationTest do } """ - {:ok, %{data: %{"deleteDelegation" => delegation}}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{data: %{"deleteDelegation" => delegation}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert delegation["proposalUrl"] == context[:proposal_url] assert delegation["votingResult"]["in_favor"] == 1 @@ -50,7 +64,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteDelegationTest do } """ - {:ok, %{errors: [%{message: message}]}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{errors: [%{message: message}]}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert message == "No delegation found to delete" end @@ -59,6 +74,7 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteDelegationTest do describe "delete global delegation" do setup do delegation = insert(:delegation) + [ delegator_email: delegation.delegator.email, delegate_email: delegation.delegate.email, @@ -69,13 +85,16 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteDelegationTest do test "with participant emails", context do query = """ mutation { - deleteDelegation(delegatorEmail: "#{context[:delegator_email]}", delegateEmail: "#{context[:delegate_email]}") { + deleteDelegation(delegatorEmail: "#{context[:delegator_email]}", delegateEmail: "#{ + context[:delegate_email] + }") { proposalUrl } } """ - {:ok, %{data: %{"deleteDelegation" => delegation}}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{data: %{"deleteDelegation" => delegation}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert delegation["proposalUrl"] == context[:proposal_url] end @@ -93,9 +112,10 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteDelegationTest do } """ - {:ok, %{errors: [%{message: message}]}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{errors: [%{message: message}]}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert message == "No delegation found to delete" end end -end \ No newline at end of file +end diff --git a/test/liquid_voting_web/absinthe/mutations/delete_vote_test.exs b/test/liquid_voting_web/absinthe/mutations/delete_vote_test.exs index 7a65d6f2..5ce0b91e 100644 --- a/test/liquid_voting_web/absinthe/mutations/delete_vote_test.exs +++ b/test/liquid_voting_web/absinthe/mutations/delete_vote_test.exs @@ -8,6 +8,7 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteVoteTest do setup do vote = insert(:vote) result = insert(:voting_result, in_favor: 1, proposal_url: vote.proposal_url) + [ participant_email: vote.participant.email, proposal_url: vote.proposal_url, @@ -19,7 +20,9 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteVoteTest do test "with a participant's email and proposal_url", context do query = """ mutation { - deleteVote(participantEmail: "#{context[:participant_email]}", proposalUrl:"#{context[:proposal_url]}") { + deleteVote(participantEmail: "#{context[:participant_email]}", proposalUrl:"#{ + context[:proposal_url] + }") { participant { email } @@ -31,7 +34,8 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteVoteTest do } """ - {:ok, %{data: %{"deleteVote" => vote}}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + {:ok, %{data: %{"deleteVote" => vote}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert vote["participant"]["email"] == context[:participant_email] assert vote["votingResult"]["in_favor"] == 0 @@ -43,16 +47,20 @@ defmodule LiquidVotingWeb.Absinthe.Mutations.DeleteVoteTest do query = """ mutation { - deleteVote(participantEmail: "#{another_participant.email}", proposalUrl:"#{context[:proposal_url]}") { + deleteVote(participantEmail: "#{another_participant.email}", proposalUrl:"#{ + context[:proposal_url] + }") { participant { email } } } """ - {:ok, %{errors: [%{message: message}]}} = Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) + + {:ok, %{errors: [%{message: message}]}} = + Absinthe.run(query, Schema, context: %{organization_uuid: context[:organization_uuid]}) assert message == "No vote found to delete" end end -end \ No newline at end of file +end diff --git a/test/liquid_voting_web/absinthe/queries/delegations_test.exs b/test/liquid_voting_web/absinthe/queries/delegations_test.exs index 4351336a..996bf977 100644 --- a/test/liquid_voting_web/absinthe/queries/delegations_test.exs +++ b/test/liquid_voting_web/absinthe/queries/delegations_test.exs @@ -27,11 +27,12 @@ defmodule LiquidVotingWeb.Absinthe.Queries.DelegationsTest do } """ - {:ok, %{data: %{"delegations" => [payload | _]}}} = Absinthe.run(query, Schema, context: %{organization_uuid: organization_uuid}) + {:ok, %{data: %{"delegations" => [payload | _]}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: organization_uuid}) assert payload["delegate"]["email"] == delegation.delegate.email assert payload["delegator"]["email"] == delegation.delegator.email assert payload["votingResult"] == nil end end -end \ No newline at end of file +end diff --git a/test/liquid_voting_web/absinthe/queries/voting_result_test.exs b/test/liquid_voting_web/absinthe/queries/voting_result_test.exs index 288a4c5f..2540ae82 100644 --- a/test/liquid_voting_web/absinthe/queries/voting_result_test.exs +++ b/test/liquid_voting_web/absinthe/queries/voting_result_test.exs @@ -18,11 +18,12 @@ defmodule LiquidVotingWeb.Absinthe.Queries.VotingResultTest do } """ - {:ok, %{data: %{"votingResult" => result_payload}}} = Absinthe.run(query, Schema, context: %{organization_uuid: result.organization_uuid}) + {:ok, %{data: %{"votingResult" => result_payload}}} = + Absinthe.run(query, Schema, context: %{organization_uuid: result.organization_uuid}) assert result_payload["in_favor"] == result.in_favor assert result_payload["against"] == result.against assert result_payload["proposalUrl"] == result.proposal_url end end -end \ No newline at end of file +end diff --git a/test/support/factory.ex b/test/support/factory.ex index 82572eb6..62888df0 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -1,7 +1,7 @@ defmodule LiquidVoting.Factory do use ExMachina.Ecto, repo: LiquidVoting.Repo - alias LiquidVoting.Voting.{Vote,Participant} + alias LiquidVoting.Voting.{Vote, Participant} alias LiquidVoting.Delegations.Delegation alias LiquidVoting.VotingResults.Result @@ -9,12 +9,12 @@ defmodule LiquidVoting.Factory do %Participant{ name: sequence(:name, &"Jane Doe #{&1}"), email: sequence(:email, &"jane#{&1}@somedomain.com"), - organization_uuid: Ecto.UUID.generate + organization_uuid: Ecto.UUID.generate() } end def vote_factory do - organization_uuid = Ecto.UUID.generate + organization_uuid = Ecto.UUID.generate() %Vote{ yes: true, @@ -25,7 +25,7 @@ defmodule LiquidVoting.Factory do end def delegation_factory do - organization_uuid = Ecto.UUID.generate + organization_uuid = Ecto.UUID.generate() %Delegation{ delegator: build(:participant, organization_uuid: organization_uuid), @@ -35,7 +35,7 @@ defmodule LiquidVoting.Factory do end def delegation_for_proposal_factory do - organization_uuid = Ecto.UUID.generate + organization_uuid = Ecto.UUID.generate() %Delegation{ delegator: build(:participant, organization_uuid: organization_uuid), @@ -50,7 +50,7 @@ defmodule LiquidVoting.Factory do in_favor: 0, against: 0, proposal_url: sequence(:proposal_url, &"https://proposals.com/#{&1}"), - organization_uuid: Ecto.UUID.generate + organization_uuid: Ecto.UUID.generate() } end -end \ No newline at end of file +end