Skip to content

Instantly share code, notes, and snippets.

View cybrox's full-sized avatar
☠️
Tinkering

Sven Gehring cybrox

☠️
Tinkering
View GitHub Profile
@cybrox
cybrox / ex-mag1.ex
Created April 30, 2019 09:41
ex-mag1.ex
defmodule Recursive do
def function do
function()
end
end
@cybrox
cybrox / phx-stat2.ex
Created April 26, 2019 12:09
phx-stat2.ex
defmodule PhxstaticWeb.Router do
use PhxstaticWeb, :router
pipeline :static do
plug Plug.Static,
at: "/static",
from: {:phxstatic, "priv/test"}
end
scope "/", PhxstaticWeb do
@cybrox
cybrox / phx-stat1.ex
Last active April 26, 2019 12:05
phx-stat1.ex
defmodule PhxstaticWeb.Router do
use PhxstaticWeb, :router
pipeline :static do
plug Plug.Static,
at: "/static",
from: {:phxstatic, "priv/test"}
end
scope "/", PhxstaticWeb do
@cybrox
cybrox / phx-ete8.js
Last active March 20, 2019 16:07
phx-ete8.js
it ("shows email not confirmed notification in dashboard for unconfirmed user", () => {
cy.factorydb('user', {
username: "Sven Gehring",
password: "password",
email: "[email protected]"
})
// This is another custom command we use for getting our auth token
cy.login('[email protected]', 'password')
@cybrox
cybrox / phx-ete7.js
Created March 20, 2019 15:36
phx-ete7.js
Cypress.Commands.add("resetdb", () => {
cy.exec('docker-compose run myapp mix do ecto.drop, ecto.create, ecto.migrate')
})
Cypress.Commands.add("checkoutdb", () => {
cy.request('POST', '/api/end-to-end/db/checkout').as('checkoutDb')
})
Cypress.Commands.add("checkindb", () => {
cy.request('POST', '/api/end-to-end/db/checkin').as('checkinDb')
@cybrox
cybrox / phx-ete6.js
Created March 20, 2019 15:35
phx-ete6.js
before(() => {
// Before we run any tests, we reset our database.
// We also check back in any open database connection. If you save a
// test file, cypress will re-run the tests, not finishing the ones it
// is currently running, so we might end up with a checked-out connection
// lying around and blocking our database reset.
cy.checkindb()
cy.resetdb()
})
@cybrox
cybrox / phx-ete5.ex
Created March 20, 2019 15:35
phx-ete5.ex
post "/db/factory" do
# When piped through a generic Phoenix JSON API pipeline, using a route
# like this allows you to call your factory via your test API easily.
with {:ok, schema} <- Map.fetch(conn.body_params, "schema"),
{:ok, attrs} <- Map.fetch(conn.body_params, "attributes") do
db_schema = String.to_atom(schema)
db_attrs = Enum.map(attrs, fn {k, v} -> {String.to_atom(k), v} end)
db_entry = Factory.insert(db_schema, db_attrs)
send_resp(conn, 200, Poison.encode!(%{id: db_entry.id}))
@cybrox
cybrox / phx-ete4.ex
Created March 20, 2019 15:35
phx-ete4.ex
post "/db/checkout" do
# If the agent is registered and alive, a db connection is checked out already
# Otherwise, we spawn the agent and let it(!) check out the db connection
owner_process = Process.whereis(:db_owner_agent)
if owner_process && Process.alive?(owner_process) do
send_resp(conn, 200, "connection has already been checked out")
else
{:ok, _pid} = Agent.start_link(&checkout_shared_db_conn/0, name: :db_owner_agent)
send_resp(conn, 200, "checked out database connection")
end
@cybrox
cybrox / phx-ete4.ex
Created March 20, 2019 15:34
phx-ete4.ex
defp checkout_shared_db_conn do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo, ownership_timeout: :infinity)
:ok = Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
end
defp checkin_shared_db_conn(_) do
:ok = Ecto.Adapters.SQL.Sandbox.checkin(Repo)
end
@cybrox
cybrox / phx-ete3.ex
Created March 20, 2019 15:34
phx-ete3.ex
defmodule MyApp.Plug.TestEndToEnd do
use Plug.Router
plug :match
plug :dispatch
match _, do: send_resp(conn, 404, "not found")
end