Commit d33d0d02 authored by Mikko Ahlroth's avatar Mikko Ahlroth

Merge branch 'tests-fix' into 'grid-refactor'

Fix test to run without errors

See merge request !49
parents bda5c677 d2aedbe5
defmodule CodeStats.LanguageTest do
use CodeStats.ModelCase
use CodeStats.DatabaseCase
alias CodeStats.Language
......
defmodule CodeStats.User.PulseTest do
use CodeStats.DatabaseCase
alias CodeStats.User.Pulse
@valid_attrs %{sent_at: "2010-04-17 14:00:00",
sent_at_local: "2010-04-17 14:00:00",
tz_offset: "0"}
@invalid_attrs %{}
test "changeset with valid attributes" do
assert {:ok, _} = Repo.insert(Pulse.changeset(%Pulse{}, @valid_attrs))
end
test "changeset with invalid attributes" do
assert {:error, _} = Repo.insert(Pulse.changeset(%Pulse{}, @invalid_attrs))
end
end
defmodule CodeStats.UserTest do
use CodeStats.ModelCase
use CodeStats.DatabaseCase
alias CodeStats.User
@valid_attrs %{email: "some content", name: "some content", password: "some content"}
@valid_attrs %{email: "some@content", username: "some content", password: "some content"}
@invalid_attrs %{}
test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
assert {:ok, _} = Repo.insert(User.changeset(%User{}, @valid_attrs))
end
test "changeset with invalid attributes" do
changeset = User.changeset(%User{}, @invalid_attrs)
refute changeset.valid?
assert {:error, _} = Repo.insert(User.changeset(%User{}, @invalid_attrs))
end
end
defmodule CodeStats.XPTest do
use CodeStats.ModelCase
use CodeStats.DatabaseCase
alias CodeStats.XP
......
defmodule CodeStats.PageControllerTest do
use CodeStats.ConnCase
defmodule CodeStatsWeb.PageControllerTest do
use CodeStatsWeb.ConnCase
test "GET /", %{conn: conn} do
conn = get conn, "/"
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
assert is_binary(html_response(conn, 200))
end
end
defmodule CodeStatsWeb.ErrorViewTest do
use CodeStatsWeb.ConnCase, async: true
# Bring render/3 and render_to_string/3 for testing custom views
import Phoenix.View
test "render 500.html", %{conn: conn} do
assert is_binary(render_to_string(CodeStatsWeb.ErrorView, "500.html", [conn: conn]))
end
test "render any other", %{conn: conn} do
assert is_binary(render_to_string(CodeStatsWeb.ErrorView, "505.html", [conn: conn]))
end
end
defmodule CodeStatsWeb.LayoutViewTest do
use CodeStatsWeb.ConnCase, async: true
end
defmodule CodeStatsWeb.PageViewTest do
use CodeStatsWeb.ConnCase, async: true
end
defmodule CodeStats.XPControllerTest do
use CodeStats.ConnCase
alias CodeStats.XP
@valid_attrs %{amount: 42}
@invalid_attrs %{}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
test "lists all entries on index", %{conn: conn} do
conn = get conn, xp_path(conn, :index)
assert json_response(conn, 200)["data"] == []
end
test "shows chosen resource", %{conn: conn} do
xp = Repo.insert! %XP{}
conn = get conn, xp_path(conn, :show, xp)
assert json_response(conn, 200)["data"] == %{"id" => xp.id,
"pulse_id" => xp.pulse_id,
"language_id" => xp.language_id,
"amount" => xp.amount}
end
test "does not show resource and instead throw error when id is nonexistent", %{conn: conn} do
assert_error_sent 404, fn ->
get conn, xp_path(conn, :show, -1)
end
end
test "creates and renders resource when data is valid", %{conn: conn} do
conn = post conn, xp_path(conn, :create), xp: @valid_attrs
assert json_response(conn, 201)["data"]["id"]
assert Repo.get_by(XP, @valid_attrs)
end
test "does not create resource and renders errors when data is invalid", %{conn: conn} do
conn = post conn, xp_path(conn, :create), xp: @invalid_attrs
assert json_response(conn, 422)["errors"] != %{}
end
test "updates and renders chosen resource when data is valid", %{conn: conn} do
xp = Repo.insert! %XP{}
conn = put conn, xp_path(conn, :update, xp), xp: @valid_attrs
assert json_response(conn, 200)["data"]["id"]
assert Repo.get_by(XP, @valid_attrs)
end
test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
xp = Repo.insert! %XP{}
conn = put conn, xp_path(conn, :update, xp), xp: @invalid_attrs
assert json_response(conn, 422)["errors"] != %{}
end
test "deletes chosen resource", %{conn: conn} do
xp = Repo.insert! %XP{}
conn = delete conn, xp_path(conn, :delete, xp)
assert response(conn, 204)
refute Repo.get(XP, xp.id)
end
end
defmodule CodeStats.PulseTest do
use CodeStats.ModelCase
alias CodeStats.Pulse
@valid_attrs %{sent_at: "2010-04-17 14:00:00"}
@invalid_attrs %{}
test "changeset with valid attributes" do
changeset = Pulse.changeset(%Pulse{}, @valid_attrs)
assert changeset.valid?
end
test "changeset with invalid attributes" do
changeset = Pulse.changeset(%Pulse{}, @invalid_attrs)
refute changeset.valid?
end
end
defmodule CodeStats.ChannelCase do
defmodule CodeStatsWeb.ChannelCase do
@moduledoc """
This module defines the test case to be used by
channel tests.
......@@ -14,6 +14,8 @@ defmodule CodeStats.ChannelCase do
"""
use ExUnit.CaseTemplate
alias Ecto.Adapters.SQL.Sandbox
alias CodeStats.Repo
using do
quote do
......@@ -27,13 +29,15 @@ defmodule CodeStats.ChannelCase do
# The default endpoint for testing
@endpoint CodeStats.Endpoint
@endpoint CodeStatsWeb.Endpoint
end
end
setup tags do
:ok = Sandbox.checkout(Repo)
unless tags[:async] do
Ecto.Adapters.SQL.restart_test_transaction(CodeStats.Repo, [])
Sandbox.mode(Repo, {:shared, self()})
end
:ok
......
defmodule CodeStats.ConnCase do
defmodule CodeStatsWeb.ConnCase do
@moduledoc """
This module defines the test case to be used by
tests that require setting up a connection.
......@@ -25,10 +25,10 @@ defmodule CodeStats.ConnCase do
import Ecto.Changeset
import Ecto.Query, only: [from: 1, from: 2]
import CodeStats.Router.Helpers
import CodeStatsWeb.Router.Helpers
# The default endpoint for testing
@endpoint CodeStats.Endpoint
@endpoint CodeStatsWeb.Endpoint
end
end
......
defmodule CodeStats.ModelCase do
defmodule CodeStats.DatabaseCase do
@moduledoc """
This module defines the test case to be used by
model tests.
......@@ -13,6 +13,9 @@ defmodule CodeStats.ModelCase do
"""
use ExUnit.CaseTemplate
alias Ecto.Adapters.SQL.Sandbox
alias CodeStats.Repo
alias CodeStatsWeb.ErrorHelpers
using do
quote do
......@@ -21,15 +24,15 @@ defmodule CodeStats.ModelCase do
import Ecto
import Ecto.Changeset
import Ecto.Query, only: [from: 1, from: 2]
import CodeStats.ModelCase
import CodeStats.DatabaseCase
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(CodeStats.Repo)
:ok = Sandbox.checkout(Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(CodeStats.Repo, {:shared, self()})
Sandbox.mode(Repo, {:shared, self()})
end
end
......@@ -57,7 +60,7 @@ defmodule CodeStats.ModelCase do
"""
def errors_on(struct, data) do
struct.__struct__.changeset(struct, data)
|> Ecto.Changeset.traverse_errors(&MyApp.ErrorHelpers.translate_error/1)
|> Ecto.Changeset.traverse_errors(&ErrorHelpers.translate_error/1)
|> Enum.flat_map(fn {key, errors} -> for msg <- errors, do: {key, msg} end)
end
end
defmodule CodeStats.ErrorViewTest do
use CodeStats.ConnCase, async: true
# Bring render/3 and render_to_string/3 for testing custom views
import Phoenix.View
test "renders 404.html" do
assert render_to_string(CodeStats.ErrorView, "404.html", []) ==
"Page not found"
end
test "render 500.html" do
assert render_to_string(CodeStats.ErrorView, "500.html", []) ==
"Server internal error"
end
test "render any other" do
assert render_to_string(CodeStats.ErrorView, "505.html", []) ==
"Server internal error"
end
end
defmodule CodeStats.LayoutViewTest do
use CodeStats.ConnCase, async: true
end
\ No newline at end of file
defmodule CodeStats.PageViewTest do
use CodeStats.ConnCase, async: true
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment