Skip to content

Instantly share code, notes, and snippets.

View TylerPachal's full-sized avatar

Tyler Pachal TylerPachal

View GitHub Profile
let flag = true;
if (foo === bar) {
flag = false;
console.log(flag); // false
}
console.log(flag); // false
defmodule EctoValidationsTest do
@moduledoc """
I set up these tests to document how some of the built-in Ecto Changeset
validations work. It seems as though:
- If a field is marked as required, no subsequent validation will be done
when the field is not present in the params.
- Duplicating validate_required checks does not add duplicate errors, but
duplicating other types of validations does add duplicate errors.
- If a field passes its validate_required check, then all subsequent
validation checks will be run, even if some fail (no early exiting).
# This is the "top-level" function for my application
def handle_kafka_message(msg) do
# Try to do the processing
:ok = process(msg)
:ok = acknowledge(msg)
# Use catch/2 to handle errors, and send message to DLQ
catch kind, error ->
Datadog.emit("process_message_failure")
Logger.error("""
Trigger Handler Compatible?
raise rescue true
raise catch/1 false
raise catch/2 true
throw rescue false
throw catch/1 true
throw catch2 true
exit rescue false
exit catch/1 false
exit catch/2 true
try do
# code
catch
kind, value ->
# ...
end
@doc """
Asserts that the given changeset is invalid, and that when the
assertion_expression is applied to the error_message it results in a truthy
value.
"""
defmacro assert_invalid(changeset, field, assertion_expression) when is_atom(field) do
expr = Macro.to_string(assertion_expression)
quote do
c = unquote(changeset)
defmodule Demo do
use ExUnit.Case
# The macro comes from here
import TestHelper
# An example Ecto Schema for us to play around with
defmodule Person do
use Ecto.Schema
alias Ecto.Changeset
test "age is required" do
params = %{"name" => "tyler"}
changset = Person.changeset(%Person{}, params)
assert_invalid changeset, :age, error_message =~ "blank" && error_message =~ "please"
end
test "age is required" do
# Create changeset
params = %{"name" => "tyler"}
changset = Person.changeset(%Person{}, params)
# Changeset should be invalid
assert changeset.valid? == false
# Find the error message for the invalid field
error_message = Keyword.get(changeset.errors, :age)
@TylerPachal
TylerPachal / phoenix_user_files.ex
Last active October 10, 2019 15:18
Default Phoenix Model
### mix phx.routes
user_path GET /users PhoenixPlaygroundWeb.UserController :index
user_path GET /users/:id/edit PhoenixPlaygroundWeb.UserController :edit
user_path GET /users/new PhoenixPlaygroundWeb.UserController :new
user_path GET /users/:id PhoenixPlaygroundWeb.UserController :show
user_path POST /users PhoenixPlaygroundWeb.UserController :create
user_path PATCH /users/:id PhoenixPlaygroundWeb.UserController :update
PUT /users/:id PhoenixPlaygroundWeb.UserController :update
user_path DELETE /users/:id PhoenixPlaygroundWeb.UserController :delete