Commit 79678f85 authored by Jens-Christian Fischer's avatar Jens-Christian Fischer
Browse files

Remove Ecto, add PingChannel (for tests)

parent e7b6b6a4
......@@ -6,8 +6,6 @@
use Mix.Config
# General application configuration
config :grains,
ecto_repos: [Grains.Repo]
# Configures the endpoint
config :grains, Grains.Endpoint,
......
......@@ -33,14 +33,6 @@ config :logger, :console, format: "[$level] $message\n"
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20
# Configure your database
config :grains, Grains.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "grains_dev",
hostname: "localhost",
pool_size: 10
# tell logger to load a LoggerFileBackend processes
config :logger,
......
......@@ -25,13 +25,6 @@ config :grains, Grains.Endpoint,
config :grains, Grains.Endpoint,
secret_key_base: System.get_env("SECRET_KEY_BASE")
config :grains, Grains.Repo,
adapter: Ecto.Adapters.Postgres,
username: System.get_env("DB_USERNAME"),
password: System.get_env("DB_PASSWORD"),
database: System.get_env("DB_DATABASE"),
hostname: System.get_env("DB_HOSTNAME"),
pool_size: 20
# tell logger to load a LoggerFileBackend processes
......
......@@ -8,8 +8,6 @@ defmodule Grains do
# Define workers and child supervisors to be supervised
children = [
# Start the Ecto repository
supervisor(Grains.Repo, []),
# Start the endpoint when the application starts
supervisor(Grains.Endpoint, []),
# Start your own worker by calling: Grains.Worker.start_link(arg1, arg2, arg3)
......
defmodule Grains.Repo do
use Ecto.Repo, otp_app: :grains
# use Ecto.Repo, otp_app: :grains
end
......@@ -9,7 +9,6 @@ defmodule Grains.Mixfile do
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
aliases: aliases(),
deps: deps()]
end
......@@ -19,7 +18,7 @@ defmodule Grains.Mixfile do
def application do
[mod: {Grains, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex, :logger_file_backend, :uuid]]
:logger_file_backend, :uuid]]
end
# Specifies which paths to compile per environment.
......@@ -32,8 +31,8 @@ defmodule Grains.Mixfile do
defp deps do
[{:phoenix, "~> 1.2.1"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.0"},
{:postgrex, ">= 0.0.0"},
# {:phoenix_ecto, "~> 3.0"},
# {:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
......@@ -43,15 +42,4 @@ defmodule Grains.Mixfile do
{:distillery, "~>1.2.2"}]
end
# Aliases are shortcuts or tasks specific to the current project.
# For example, to create, migrate and run the seeds file at once:
#
# $ mix ecto.setup
#
# See the documentation for `Mix` for more info on aliases.
defp aliases do
["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
"test": ["ecto.create --quiet", "ecto.migrate", "test"]]
end
end
......@@ -9,7 +9,8 @@
"phoenix": "file:deps/phoenix",
"phoenix_html": "file:deps/phoenix_html",
"jquery" : ">= 2.1",
"interface" : "git://github.com/charlieroberts/interface.js.git"
"interface" : "git://github.com/charlieroberts/interface.js.git",
"underscore": "~ 1.8.3"
},
"devDependencies": {
"babel-brunch": "~6.0.0",
......
......@@ -20,10 +20,6 @@ defmodule Grains.ChannelCase do
# Import conveniences for testing with channels
use Phoenix.ChannelTest
alias Grains.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
# The default endpoint for testing
......@@ -32,11 +28,6 @@ defmodule Grains.ChannelCase do
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Grains.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Grains.Repo, {:shared, self()})
end
:ok
end
......
......@@ -3,13 +3,7 @@ defmodule Grains.ConnCase do
This module defines the test case to be used by
tests that require setting up a connection.
Such tests rely on `Phoenix.ConnTest` and also
import other functionality to make it easier
to build and query models.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
Sucansaction which is reset at the beginning
of the test unless the test case is marked as async.
"""
......@@ -20,11 +14,6 @@ defmodule Grains.ConnCase do
# Import conveniences for testing with connections
use Phoenix.ConnTest
alias Grains.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import Grains.Router.Helpers
# The default endpoint for testing
......@@ -33,12 +22,6 @@ defmodule Grains.ConnCase do
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Grains.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Grains.Repo, {:shared, self()})
end
{:ok, conn: Phoenix.ConnTest.build_conn()}
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
end
defmodule Grains.ModelCase do
@moduledoc """
This module defines the test case to be used by
model tests.
You may define functions here to be used as helpers in
your model tests. See `errors_on/2`'s definition as reference.
Finally, if the test case interacts with the database,
it cannot be async. For this reason, every test runs
inside a transaction which is reset at the beginning
of the test unless the test case is marked as async.
"""
use ExUnit.CaseTemplate
using do
quote do
alias Grains.Repo
import Ecto
import Ecto.Changeset
import Ecto.Query
import Grains.ModelCase
end
end
setup tags do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Grains.Repo)
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Grains.Repo, {:shared, self()})
end
:ok
end
@doc """
Helper for returning list of errors in a struct when given certain data.
## Examples
Given a User schema that lists `:name` as a required field and validates
`:password` to be safe, it would return:
iex> errors_on(%User{}, %{password: "password"})
[password: "is unsafe", name: "is blank"]
You could then write your assertion like:
assert {:password, "is unsafe"} in errors_on(%User{}, %{password: "password"})
You can also create the changeset manually and retrieve the errors
field directly:
iex> changeset = User.changeset(%User{}, password: "password")
iex> {:password, "is unsafe"} in changeset.errors
true
"""
def errors_on(struct, data) do
struct.__struct__.changeset(struct, data)
|> Ecto.Changeset.traverse_errors(&Grains.ErrorHelpers.translate_error/1)
|> Enum.flat_map(fn {key, errors} -> for msg <- errors, do: {key, msg} end)
end
end
defmodule Grains.PingChannel do
use Phoenix.Channel
require Logger
def join(_topic, _payload, socket) do
Process.send_after(self(), :leave_or_crash, :rand.uniform(2000) + 2000)
{:ok, socket}
end
def handle_info(:leave_or_crash, socket) do
if :rand.uniform(5) == 1 do
Logger.warn("deliberately disconnecting the client")
Process.exit(socket.transport_pid, :kill)
{:noreply, socket}
else
Logger.warn("deliberately leaving the topic")
{:stop, :normal, socket}
end
end
def handle_info(_message, socket) do
{:noreply, socket}
end
def handle_in("ping", payload, socket) do
{:reply, {:ok, payload}, socket}
end
def handle_in(event, payload, socket) do
Logger.warn("unhandled event #{event} #{inspect payload}")
{:noreply, socket}
end
end
defmodule Grains.UserSocket do
use Phoenix.Socket
require Logger
## Channels
channel "slider:*", Grains.SliderChannel
channel "orientation:*", Grains.OrientationChannel
channel "presence:*", Grains.PresenceChannel
channel "ping", Grains.PingChannel
## Transports
transport :websocket, Phoenix.Transports.WebSocket
# transport :longpoll, Phoenix.Transports.LongPoll
......@@ -31,8 +35,13 @@ defmodule Grains.UserSocket do
IO.puts "verified : #{uuid}"
{:ok, assign(socket, :user_uuid, uuid) }
{:error, _reason} ->
IO.puts "error: #{_reason}"
:error
if token == "foobar" do
{:ok, assign(socket, :user_uuid, token)}
else
IO.puts "error: #{_reason}"
:error
end
end
end
......
56853
\ No newline at end of file
......@@ -13,6 +13,7 @@
// to also remove its path from "config.paths.watched".
import "phoenix_html";
import _ from "underscore"
import socket from "./socket"
import SliderPanel from "./slider";
import OrientationPanel from "./orientation";
......
import Interface from "interface.js";
import _ from "underscore";
let OrientationPanel = {
......@@ -6,6 +7,7 @@ let OrientationPanel = {
sliders: {},
slider_titles: ["pitch", "roll", "yaw", "heading"],
old_values: [0,0,0,0],
init(socket, domId) {
......@@ -62,13 +64,19 @@ let OrientationPanel = {
let scaled = values.map(function(i) {
return parseInt(i * 127);
});
let payload = {pitch: scaled[0],
roll: scaled[1],
yaw: scaled[2],
heading: scaled[3]
};
this.orientationChannel.push("orientation", payload)
.receive("error", e => console.log(e));
if (!_.isEqual(scaled, this.old_values)) {
console.log(scaled);
let payload = {pitch: scaled[0],
roll: scaled[1],
yaw: scaled[2],
heading: scaled[3]
};
this.orientationChannel.push("orientation", payload)
.receive("error", e => console.log(e));
this.old_values = scaled;
}
},
......
......@@ -18,11 +18,6 @@ defmodule Grains.Web do
def model do
quote do
use Ecto.Schema
import Ecto
import Ecto.Changeset
import Ecto.Query
end
end
......@@ -30,9 +25,6 @@ defmodule Grains.Web do
quote do
use Phoenix.Controller
alias Grains.Repo
import Ecto
import Ecto.Query
import Grains.Router.Helpers
import Grains.Gettext
......@@ -65,9 +57,6 @@ defmodule Grains.Web do
quote do
use Phoenix.Channel
alias Grains.Repo
import Ecto
import Ecto.Query
import Grains.Gettext
end
end
......
Supports Markdown
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