From 5ab6e0b0e27e109619ad897a32bbb7780dc87073 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Tue, 7 Nov 2017 18:40:23 +0100 Subject: [PATCH] Set modified_from to "code_corps" on tasks and comments when updating from code_corps --- lib/code_corps/model/comment.ex | 2 + lib/code_corps/model/task.ex | 2 + priv/repo/structure.sql | 2 +- test/lib/code_corps/comment/service_test.exs | 12 +++++ .../github/sync/issue/task/task_test.exs | 53 ++++++++++++------- test/lib/code_corps/model/comment_test.exs | 20 +++++++ test/lib/code_corps/model/task_test.exs | 18 +++++++ test/lib/code_corps/task/service_test.exs | 12 +++++ 8 files changed, 101 insertions(+), 20 deletions(-) diff --git a/lib/code_corps/model/comment.ex b/lib/code_corps/model/comment.ex index 4d6e376ec..dda5a3077 100644 --- a/lib/code_corps/model/comment.ex +++ b/lib/code_corps/model/comment.ex @@ -38,12 +38,14 @@ defmodule CodeCorps.Comment do |> validate_required([:task_id, :user_id]) |> assoc_constraint(:task) |> assoc_constraint(:user) + |> put_change(:modified_from, "code_corps") end def update_changeset(struct, params) do struct |> changeset(params) |> update_modified_at() + |> put_change(:modified_from, "code_corps") end defp set_created_and_modified_at(changeset) do diff --git a/lib/code_corps/model/task.ex b/lib/code_corps/model/task.ex index d70a83773..9baf96637 100644 --- a/lib/code_corps/model/task.ex +++ b/lib/code_corps/model/task.ex @@ -77,6 +77,7 @@ defmodule CodeCorps.Task do |> assoc_constraint(:project) |> assoc_constraint(:user) |> put_change(:status, "open") + |> put_change(:modified_from, "code_corps") end @spec update_changeset(struct, map) :: Ecto.Changeset.t @@ -88,6 +89,7 @@ defmodule CodeCorps.Task do |> set_closed_at() |> update_modified_at() |> maybe_assoc_with_repo(params) + |> put_change(:modified_from, "code_corps") end def apply_position(changeset) do diff --git a/priv/repo/structure.sql b/priv/repo/structure.sql index f3056e581..a1e27aab6 100644 --- a/priv/repo/structure.sql +++ b/priv/repo/structure.sql @@ -2,7 +2,7 @@ -- PostgreSQL database dump -- --- Dumped from database version 9.5.9 +-- Dumped from database version 10.0 -- Dumped by pg_dump version 10.0 SET statement_timeout = 0; diff --git a/test/lib/code_corps/comment/service_test.exs b/test/lib/code_corps/comment/service_test.exs index f8c0163b2..8a36a0c51 100644 --- a/test/lib/code_corps/comment/service_test.exs +++ b/test/lib/code_corps/comment/service_test.exs @@ -29,6 +29,11 @@ defmodule CodeCorps.Comment.ServiceTest do refute_received({:post, "https://api.github.com/" <> _rest, _body, _headers, _options}) end + test "sets modified_from to 'code_corps'" do + {:ok, comment} = valid_attrs() |> Comment.Service.create + assert comment.modified_from == "code_corps" + end + test "returns errored changeset if attributes are invalid" do {:error, changeset} = Comment.Service.create(@base_attrs) refute changeset.valid? @@ -92,6 +97,13 @@ defmodule CodeCorps.Comment.ServiceTest do refute_received({:patch, "https://api.github.com/" <> _rest, _body, _headers, _options}) end + test "sets modified_from to 'code_corps'" do + comment = insert(:comment, modified_from: "github") + {:ok, updated_comment} = comment |> Comment.Service.update(@update_attrs) + + assert updated_comment.modified_from == "code_corps" + end + @preloads [task: [github_repo: :github_app_installation]] test "propagates changes to github if comment is synced to github comment" do diff --git a/test/lib/code_corps/github/sync/issue/task/task_test.exs b/test/lib/code_corps/github/sync/issue/task/task_test.exs index 77e211ee8..cbaa20313 100644 --- a/test/lib/code_corps/github/sync/issue/task/task_test.exs +++ b/test/lib/code_corps/github/sync/issue/task/task_test.exs @@ -3,35 +3,45 @@ defmodule CodeCorps.GitHub.Sync.Issue.TaskTest do use CodeCorps.DbAccessCase - alias CodeCorps.{ - Project, - Repo, - Task - } + alias CodeCorps.{Repo, Task} alias CodeCorps.GitHub.Sync.Issue.Task, as: IssueTaskSyncer describe "sync all/3" do - test "creates missing, updates existing tasks for each project associated with the github repo" do + defp setup_test_data do + # Creates a user, 3 projects and a github issue all linked to a + # github repo. Returns that data as a map user = insert(:user) + projects = insert_list(3, :project) + github_repo = insert(:github_repo) + github_issue = insert( + :github_issue, + github_repo: github_repo, + github_updated_at: DateTime.utc_now |> Timex.shift(hours: 1) + ) - %{github_repo: github_repo} = github_issue = - insert(:github_issue, github_updated_at: DateTime.utc_now |> Timex.shift(hours: 1)) + projects |> Enum.each(&insert(:task_list, project: &1, inbox: true)) - [%{project: project_1}, _, _] = project_github_repos = - insert_list(3, :project_github_repo, github_repo: github_repo) + projects + |> Enum.each(&insert(:project_github_repo, project: &1, github_repo: github_repo)) - task_1 = insert(:task, project: project_1, github_issue: github_issue, github_repo: github_repo, user: user) + %{github_issue: github_issue, github_repo: github_repo, projects: projects, user: user} + end - project_ids = project_github_repos |> Enum.map(&Map.get(&1, :project_id)) + test "creates missing, updates existing tasks for each project associated with the github repo" do + %{ + github_issue: github_issue, + github_repo: github_repo, + projects: [project_1 | _] = projects, + user: user + } = setup_test_data() - project_ids |> Enum.each(fn project_id -> - project = Project |> Repo.get_by(id: project_id) - insert(:task_list, project: project, inbox: true) - end) + existing_task = + insert(:task, project: project_1, github_issue: github_issue, github_repo: github_repo, user: user) {:ok, tasks} = github_issue |> IssueTaskSyncer.sync_all(user) - assert Repo.aggregate(Task, :count, :id) == 3 + assert Repo.aggregate(Task, :count, :id) == projects |> Enum.count + assert tasks |> Enum.count == projects |> Enum.count tasks |> Enum.each(fn task -> assert task.user_id == user.id @@ -39,8 +49,13 @@ defmodule CodeCorps.GitHub.Sync.Issue.TaskTest do assert task.github_issue_id == github_issue.id end) - task_ids = tasks |> Enum.map(&Map.get(&1, :id)) - assert task_1.id in task_ids + assert existing_task.id in (tasks |> Enum.map(&Map.get(&1, :id))) + end + + test "sets task :modified_from to 'github'" do + %{github_issue: github_issue, user: user} = setup_test_data() + {:ok, tasks} = github_issue |> IssueTaskSyncer.sync_all(user) + assert tasks |> Enum.all?(fn task -> task.modified_from == "github" end) end test "fails on validation errors" do diff --git a/test/lib/code_corps/model/comment_test.exs b/test/lib/code_corps/model/comment_test.exs index 9d022c62c..3653716ad 100644 --- a/test/lib/code_corps/model/comment_test.exs +++ b/test/lib/code_corps/model/comment_test.exs @@ -1,7 +1,10 @@ defmodule CodeCorps.CommentTest do + @moduledoc false + use CodeCorps.ModelCase alias CodeCorps.Comment + alias Ecto.Changeset @valid_attrs %{markdown: "I love elixir!", state: "published"} @invalid_attrs %{} @@ -40,6 +43,14 @@ defmodule CodeCorps.CommentTest do {:ok, %Comment{created_at: created_at, modified_at: modified_at}} = Repo.insert(changeset) assert created_at == modified_at end + + test "sets modified_from to 'code_corps'" do + assert( + %Comment{} + |> Comment.create_changeset(%{}) + |> Changeset.get_field(:modified_from) == "code_corps" + ) + end end describe "update_changeset/2" do @@ -48,5 +59,14 @@ defmodule CodeCorps.CommentTest do changeset = Comment.update_changeset(comment, %{}) assert comment.modified_at < changeset.changes[:modified_at] end + + test "sets modified_from to 'code_corps'" do + assert( + :comment + |> insert(modified_from: "github") + |> Comment.update_changeset(%{}) + |> Changeset.get_field(:modified_from) == "code_corps" + ) + end end end diff --git a/test/lib/code_corps/model/task_test.exs b/test/lib/code_corps/model/task_test.exs index af31a6147..660bb2a36 100644 --- a/test/lib/code_corps/model/task_test.exs +++ b/test/lib/code_corps/model/task_test.exs @@ -2,6 +2,7 @@ defmodule CodeCorps.TaskTest do use CodeCorps.ModelCase alias CodeCorps.Task + alias Ecto.Changeset @valid_attrs %{ title: "Test task", @@ -65,6 +66,14 @@ defmodule CodeCorps.TaskTest do assert created_at == modified_at end + test "sets modified_from to 'code_corps'" do + assert( + %Task{} + |> Task.create_changeset(%{}) + |> Changeset.get_field(:modified_from) == "code_corps" + ) + end + test "sets the order when the task is not archived and position is set" do project = insert(:project) task_list = insert(:task_list) @@ -134,5 +143,14 @@ defmodule CodeCorps.TaskTest do {:ok, %Task{order: order}} = Repo.update(changeset) refute order end + + test "sets :modified_from to 'code_corps'" do + assert( + :task + |> insert(modified_from: "github") + |> Task.update_changeset(%{}) + |> Changeset.get_field(:modified_from) == "code_corps" + ) + end end end diff --git a/test/lib/code_corps/task/service_test.exs b/test/lib/code_corps/task/service_test.exs index 58b22702a..ab403fd2f 100644 --- a/test/lib/code_corps/task/service_test.exs +++ b/test/lib/code_corps/task/service_test.exs @@ -38,6 +38,11 @@ defmodule CodeCorps.Task.ServiceTest do refute_received({:post, "https://api.github.com/" <> _rest, _body, _headers, _options}) end + test "sets modified_from to 'code_corps'" do + {:ok, task} = valid_attrs() |> Task.Service.create + assert task.modified_from == "code_corps" + end + test "returns errored changeset if attributes are invalid" do {:error, changeset} = Task.Service.create(@base_attrs) refute changeset.valid? @@ -108,6 +113,13 @@ defmodule CodeCorps.Task.ServiceTest do refute_received({:patch, "https://api.github.com/" <> _rest, _body, _headers, _options}) end + test "sets modified_from to 'code_corps'" do + task = insert(:task, modified_from: "github") + {:ok, updated_task} = task |> Task.Service.update(@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})