Skip to content

Commit

Permalink
Merge pull request #1360 from code-corps/move-tasks-context
Browse files Browse the repository at this point in the history
Move Tasks context and fix bug with date comparison test
  • Loading branch information
joshsmith authored Jan 1, 2018
2 parents f0a959a + 9207a23 commit fb7d645
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.Task.Query do
defmodule CodeCorps.Tasks.Query do
@moduledoc ~S"""
Holds queries used to retrieve a list of, or a single `Task` record from the
database, using a provided map of parameters/filters.
Expand All @@ -20,7 +20,7 @@ defmodule CodeCorps.Task.Query do
"""
@spec list(map) :: list(Project.t)
def list(%{} = params) do
timing("Task.Query", "list") do
timing("Tasks.Query", "list") do
Task
|> Helpers.Query.id_filter(params)
|> apply_archived_status(params)
Expand Down
10 changes: 5 additions & 5 deletions lib/code_corps/task/service.ex → lib/code_corps/tasks/tasks.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.Task.Service do
defmodule CodeCorps.Tasks do
@moduledoc """
Handles special CRUD operations for `CodeCorps.Task`.
"""
Expand Down Expand Up @@ -28,8 +28,8 @@ defmodule CodeCorps.Task.Service do
@doc ~S"""
Performs all actions involved in creating a task on a project
"""
@spec create(map) :: result
def create(%{} = attributes) do
@spec create_task(map) :: result
def create_task(%{} = attributes) do
Multi.new
|> Multi.insert(:task, %Task{} |> Task.create_changeset(attributes))
|> Multi.run(:preload, fn %{task: %Task{} = task} ->
Expand All @@ -40,8 +40,8 @@ defmodule CodeCorps.Task.Service do
|> marshall_result()
end

@spec update(Task.t, map) :: result
def update(%Task{} = task, %{} = attributes) do
@spec update_task(Task.t, map) :: result
def update_task(%Task{} = task, %{} = attributes) do
Multi.new
|> Multi.update(:task, task |> Task.update_changeset(attributes))
|> Multi.run(:preload, fn %{task: %Task{} = task} ->
Expand Down
12 changes: 6 additions & 6 deletions lib/code_corps_web/controllers/task_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule CodeCorpsWeb.TaskController do
@moduledoc false
use CodeCorpsWeb, :controller

alias CodeCorps.{Analytics.SegmentTracker, Task, Policy, User}
alias CodeCorps.{Analytics.SegmentTracker, Policy, Task, Tasks, User}

import ScoutApm.Tracing

Expand All @@ -12,7 +12,7 @@ defmodule CodeCorpsWeb.TaskController do

@spec index(Conn.t, map) :: Conn.t
def index(%Conn{} = conn, %{} = params) do
tasks = Task.Query.list(params)
tasks = Tasks.Query.list(params)
tasks = preload(tasks)
timing("JaSerializer", "render") do
conn |> render("index.json-api", data: tasks)
Expand All @@ -21,7 +21,7 @@ defmodule CodeCorpsWeb.TaskController do

@spec show(Conn.t, map) :: Conn.t
def show(%Conn{} = conn, %{} = params) do
with %Task{} = task <- Task.Query.find(params),
with %Task{} = task <- Tasks.Query.find(params),
task <- preload(task)
do
conn |> render("show.json-api", data: task)
Expand All @@ -32,7 +32,7 @@ defmodule CodeCorpsWeb.TaskController do
def create(%Conn{} = conn, %{} = params) do
with %User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource,
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %Task{}, params),
{:ok, %Task{} = task} <- params |> Task.Service.create,
{:ok, %Task{} = task} <- params |> Tasks.create_task,
task <- preload(task)
do
current_user |> track_created(task)
Expand All @@ -44,10 +44,10 @@ defmodule CodeCorpsWeb.TaskController do

@spec update(Conn.t, map) :: Conn.t
def update(%Conn{} = conn, %{} = params) do
with %Task{} = task <- Task.Query.find(params),
with %Task{} = task <- Tasks.Query.find(params),
%User{} = current_user <- conn |> CodeCorps.Guardian.Plug.current_resource,
{:ok, :authorized} <- current_user |> Policy.authorize(:update, task),
{:ok, %Task{} = updated_task} <- task |> Task.Service.update(params),
{:ok, %Task{} = updated_task} <- task |> Tasks.update_task(params),
updated_task <- preload(updated_task)
do

Expand Down
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ defmodule CodeCorps.Mixfile do
CodeCorps.StripeInvoice,
CodeCorps.StripePlatformCard,
CodeCorps.StripePlatformCustomer,
CodeCorps.Task,
CodeCorps.Task.Query,
CodeCorps.TaskList,
CodeCorps.TaskSkill,
CodeCorps.Tasks,
CodeCorps.Tasks.Query,
CodeCorps.Transition.UserState,
CodeCorps.User,
CodeCorps.UserCategory,
Expand All @@ -162,7 +162,7 @@ defmodule CodeCorps.Mixfile do
CodeCorps.Services.MarkdownRendererService,
CodeCorps.Services.ProjectService,
CodeCorps.Services.UserService,
CodeCorps.Task.Service
CodeCorps.Tasks
],

"Policies": [
Expand Down
6 changes: 4 additions & 2 deletions test/lib/code_corps/messages/conversations_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule CodeCorps.Messages.ConversationsTest do
@moduledoc false

import DateTime, only: [compare: 2]

use CodeCorps.DbAccessCase

alias CodeCorps.{
Expand All @@ -9,10 +11,10 @@ defmodule CodeCorps.Messages.ConversationsTest do

describe "part_added_changeset/1" do
test "sets the updated_at to the current time" do
old_updated_at = Timex.now |> Timex.shift(days: -5)
old_updated_at = DateTime.utc_now() |> Timex.shift(days: -5)
conversation = %Conversation{updated_at: old_updated_at}
changeset = conversation |> Messages.Conversations.part_added_changeset()
assert changeset.changes[:updated_at] > old_updated_at
assert compare(old_updated_at, changeset.changes[:updated_at]) == :lt
end

test "sets status to open" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
defmodule CodeCorps.Task.QueryTest do
defmodule CodeCorps.Tasks.QueryTest do
@moduledoc false

use CodeCorps.DbAccessCase

alias CodeCorps.Task
alias CodeCorps.Tasks

describe "filter/2" do
defp get_sorted_ids(tasks) do
tasks |> Enum.map(&Map.get(&1, :id)) |> Enum.sort
end

defp list_sorted_ids(params) do
params |> Task.Query.list |> get_sorted_ids()
params |> Tasks.Query.list |> get_sorted_ids()
end

defp find_with_query(params) do
params |> Task.Query.find
params |> Tasks.Query.find
end

test "filters by project_id" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule CodeCorps.Task.ServiceTest do
defmodule CodeCorps.TasksTest do
@moduledoc false

use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers

alias CodeCorps.{GithubIssue, Repo, Task}
alias CodeCorps.{GithubIssue, Repo, Task, Tasks}

@base_attrs %{
"title" => "Test task",
Expand All @@ -26,9 +26,9 @@ defmodule CodeCorps.Task.ServiceTest do
|> Map.put("user_id", user.id)
end

describe "create/2" do
describe "create_task/2" do
test "creates task" do
{:ok, task} = valid_attrs() |> Task.Service.create
{:ok, task} = valid_attrs() |> Tasks.create_task

assert task.title == @base_attrs["title"]
assert task.markdown == @base_attrs["markdown"]
Expand All @@ -41,12 +41,12 @@ defmodule CodeCorps.Task.ServiceTest do
end

test "sets modified_from to 'code_corps'" do
{:ok, task} = valid_attrs() |> Task.Service.create
{:ok, task} = valid_attrs() |> Tasks.create_task
assert task.modified_from == "code_corps"
end

test "returns errored changeset if attributes are invalid" do
{:error, changeset} = Task.Service.create(@base_attrs)
{:error, changeset} = Tasks.create_task(@base_attrs)
refute changeset.valid?
refute Repo.one(Task)

Expand All @@ -65,7 +65,7 @@ defmodule CodeCorps.Task.ServiceTest do
{:ok, task} =
attrs
|> Map.put("github_repo_id", github_repo.id)
|> Task.Service.create
|> Tasks.create_task

assert task.title == @base_attrs["title"]
assert task.markdown == @base_attrs["markdown"]
Expand All @@ -90,20 +90,20 @@ defmodule CodeCorps.Task.ServiceTest do
assert {:error, :github} ==
attrs
|> Map.put("github_repo_id", github_repo.id)
|> Task.Service.create
|> Tasks.create_task
end

refute Repo.one(Task)
assert_received({:post, "https://api.github.com/repos/foo/bar/issues", _body, _headers, _options})
end
end

describe "update/2" do
describe "update_task/2" do
@update_attrs %{"title" => "foo", "markdown" => "bar", "status" => "closed"}

test "updates task" do
task = insert(:task)
{:ok, updated_task} = task |> Task.Service.update(@update_attrs)
{:ok, updated_task} = task |> Tasks.update_task(@update_attrs)

assert updated_task.id == task.id
assert updated_task.title == @update_attrs["title"]
Expand All @@ -117,14 +117,14 @@ defmodule CodeCorps.Task.ServiceTest do

test "sets modified_from to 'code_corps'" do
task = insert(:task, modified_from: "github")
{:ok, updated_task} = task |> Task.Service.update(@update_attrs)
{:ok, updated_task} = task |> Tasks.update_task(@update_attrs)

assert updated_task.modified_from == "code_corps"
end

test "returns {:error, changeset} if there are validation errors" do
task = insert(:task)
{:error, changeset} = task |> Task.Service.update(%{"title" => nil})
{:error, changeset} = task |> Tasks.update_task(%{"title" => nil})

refute changeset.valid?

Expand All @@ -140,7 +140,7 @@ defmodule CodeCorps.Task.ServiceTest do

attrs = @update_attrs |> Map.put("github_repo_id", github_repo.id)

{:ok, updated_task} = task |> Task.Service.update(attrs)
{:ok, updated_task} = task |> Tasks.update_task(attrs)

assert updated_task.github_issue_id
assert updated_task.github_repo_id == github_repo.id
Expand All @@ -156,7 +156,7 @@ defmodule CodeCorps.Task.ServiceTest do
github_issue = insert(:github_issue, number: 5)
task = insert(:task, github_repo: github_repo, github_issue: github_issue)

{:ok, updated_task} = task |> Task.Service.update(@update_attrs)
{:ok, updated_task} = task |> Tasks.update_task(@update_attrs)

assert updated_task.id == task.id
assert updated_task.title == @update_attrs["title"]
Expand All @@ -179,7 +179,7 @@ defmodule CodeCorps.Task.ServiceTest do
github_issue = insert(:github_issue, github_id: issue_github_id, number: number, github_pull_request: github_pull_request, github_repo: github_repo)
task = insert(:task, github_repo: github_repo, github_issue: github_issue)

{:ok, updated_task} = task |> Task.Service.update(@update_attrs)
{:ok, updated_task} = task |> Tasks.update_task(@update_attrs)

assert_received({:patch, "https://api.github.com/repos/octocat/Hello-World/issues/1347", _body, _headers, _options})

Expand All @@ -204,7 +204,7 @@ defmodule CodeCorps.Task.ServiceTest do
task = insert(:task, github_repo: github_repo, github_issue: github_issue)

with_mock_api(CodeCorps.GitHub.FailureAPI) do
assert {:error, :github} == task |> Task.Service.update(@update_attrs)
assert {:error, :github} == task |> Tasks.update_task(@update_attrs)
end

updated_task = Repo.one(Task)
Expand Down

0 comments on commit fb7d645

Please sign in to comment.