Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set modified_from to "code_corps" on tasks and comments when updating from codecorps #1163

Merged
merged 1 commit into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/code_corps/model/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/code_corps/model/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions test/lib/code_corps/comment/service_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A changeset test might seem enough, but I wanted to add a sort of "integration" test to. The service is where the modification gets commited to the db.


@preloads [task: [github_repo: :github_app_installation]]

test "propagates changes to github if comment is synced to github comment" do
Expand Down
53 changes: 34 additions & 19 deletions test/lib/code_corps/github/sync/issue/task/task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,59 @@ 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
assert task.markdown == github_issue.body
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the same reason as the service test above, this is a sort of an integration test, even though our sync changeset already has the needed assertions in its own tests.

I did not do the same for comments because of the complexity of the test data there. I'm hoping I'll get a chance to decouple the comment syncer from payloads first.

end

test "fails on validation errors" do
Expand Down
20 changes: 20 additions & 0 deletions test/lib/code_corps/model/comment_test.exs
Original file line number Diff line number Diff line change
@@ -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 %{}
Expand Down Expand Up @@ -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
Expand All @@ -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
18 changes: 18 additions & 0 deletions test/lib/code_corps/model/task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule CodeCorps.TaskTest do
use CodeCorps.ModelCase

alias CodeCorps.Task
alias Ecto.Changeset

@valid_attrs %{
title: "Test task",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions test/lib/code_corps/task/service_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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})
Expand Down