This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let flag = true; | |
if (foo === bar) { | |
flag = false; | |
console.log(flag); // false | |
} | |
console.log(flag); // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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(""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try do | |
# code | |
catch | |
kind, value -> | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test "age is required" do | |
params = %{"name" => "tyler"} | |
changset = Person.changeset(%Person{}, params) | |
assert_invalid changeset, :age, error_message =~ "blank" && error_message =~ "please" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 |