Skip to content

Instantly share code, notes, and snippets.

@pkoch
Created September 4, 2017 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkoch/03fae0c1ef79d8db45f87ecee0270a43 to your computer and use it in GitHub Desktop.
Save pkoch/03fae0c1ef79d8db45f87ecee0270a43 to your computer and use it in GitHub Desktop.
.......................................................................................................................................
1) test voting_ended_at set twice (Api.CompetitionActionsTest)
test/actions/competition_actions_test.exs:10
** (ArgumentError) Postgrex expected an integer in -2147483648..2147483647, got <<43, 56, 71, 70, 233, 249, 65, 186, 183, 181, 147, 111, 154, 61, 218, 95>>. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
code: {:ok, _} = CompetitionActions.open_voting()
stacktrace:
(ecto) /home/pkoch/Desktop/lom/psc-api/deps/postgrex/lib/postgrex/type_module.ex:717: Ecto.Adapters.Postgres.TypeModule.encode_params/3
(postgrex) lib/postgrex/query.ex:45: DBConnection.Query.Postgrex.Query.encode/3
(db_connection) lib/db_connection.ex:1071: DBConnection.describe_run/5
(db_connection) lib/db_connection.ex:1142: anonymous fn/4 in DBConnection.run_meter/5
(db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
(db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4
(ecto) lib/ecto/adapters/postgres/connection.ex:93: Ecto.Adapters.Postgres.Connection.execute/4
(ecto) lib/ecto/adapters/sql.ex:243: Ecto.Adapters.SQL.sql_call/6
(ecto) lib/ecto/adapters/sql.ex:562: Ecto.Adapters.SQL.struct/7
(ecto) lib/ecto/repo/schema.ex:467: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:205: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
test/actions/competition_actions_test.exs:11: (test)
2) test voting_ended_at set (Api.CompetitionActionsTest)
test/actions/competition_actions_test.exs:6
** (ArgumentError) Postgrex expected an integer in -2147483648..2147483647, got <<18, 235, 11, 8, 58, 196, 71, 142, 190, 101, 224, 110, 134, 166, 81, 76>>. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
code: {:ok, _} = CompetitionActions.open_voting()
stacktrace:
(ecto) /home/pkoch/Desktop/lom/psc-api/deps/postgrex/lib/postgrex/type_module.ex:717: Ecto.Adapters.Postgres.TypeModule.encode_params/3
(postgrex) lib/postgrex/query.ex:45: DBConnection.Query.Postgrex.Query.encode/3
(db_connection) lib/db_connection.ex:1071: DBConnection.describe_run/5
(db_connection) lib/db_connection.ex:1142: anonymous fn/4 in DBConnection.run_meter/5
(db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
(db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4
(ecto) lib/ecto/adapters/postgres/connection.ex:93: Ecto.Adapters.Postgres.Connection.execute/4
(ecto) lib/ecto/adapters/sql.ex:243: Ecto.Adapters.SQL.sql_call/6
(ecto) lib/ecto/adapters/sql.ex:562: Ecto.Adapters.SQL.struct/7
(ecto) lib/ecto/repo/schema.ex:467: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:205: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
test/actions/competition_actions_test.exs:7: (test)
Finished in 1.5 seconds
137 tests, 2 failures
Randomized with seed 836992
Checking 56 source files ...
Please report incorrect results: https://github.com/rrrene/credo/issues
Analysis took 0.6 seconds (0.03s to load, 0.6s running checks)
216 mods/funs, found no issues.
defmodule Api.Competition do
@moduledoc """
TODO: Write.
"""
use Api.Web, :model
alias Ecto.{Changeset}
@valid_attrs ~w(
voting_started_at
voting_ended_at
)a
schema "competition" do
field :voting_started_at, :utc_datetime
field :voting_ended_at, :utc_datetime
end
defp _cant_change(%Changeset{changes: changes, data: data} = changeset, field) do
with {:ok, old} <- Map.get(data, field),
{:ok, new} <- Map.get(changes, field),
old != nil && old != new
do
Changeset.add_error(changeset, field, "can't be changed")
else
_ -> changeset
end
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @valid_attrs)
|> _cant_change(:voting_started_at)
|> _cant_change(:voting_ended_at)
end
end
defmodule Api.CompetitionActions do
use Api.Web, :action
alias Api.{Competition}
defp _change(params) do
(Repo.one(from(c in Competition)) || %Competition{})
|> Competition.changeset(params)
|> Repo.insert_or_update
end
def open_voting do
_change(%{
voting_started_at: DateTime.utc_now()
})
end
def close_voting do
_change(%{
voting_ended_at: DateTime.utc_now()
})
end
end
defmodule Api.CompetitionActionsTest do
use Api.ModelCase
alias Api.{CompetitionActions}
test "voting_ended_at set" do
{:ok, _} = CompetitionActions.open_voting()
end
test "voting_ended_at set twice" do
{:ok, _} = CompetitionActions.open_voting()
{:error, _} = CompetitionActions.open_voting()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment