Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Created July 25, 2016 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcardarella/f1b528c707f3d2b4a73f8dc9400bd428 to your computer and use it in GitHub Desktop.
Save bcardarella/f1b528c707f3d2b4a73f8dc9400bd428 to your computer and use it in GitHub Desktop.
defmodule DockYard.Test.Acceptance.Api.V1.CaseStudyTest do
use DockYard.ConnCase, async: false
use EctoFixtures.Case, with: DockYard.Test.Fixtures
import JsonApiAssert
@fixtures [:case_study_1, :case_study_2, serialize: true]
test "GET /api/v1/case-studies", %{conn: conn, data: data} do
conn
|> get("/api/v1/case-studies")
|> json_response(200)
|> assert_data(data.case_study_1)
|> assert_data(data.case_study_2)
|> assert_relationship(data.solution_1, as: "solutions", for: data.case_study_1)
|> assert_relationship(data.solution_2, as: "solutions", for: data.case_study_1)
|> assert_relationship(data.solution_3, as: "solutions", for: data.case_study_2)
|> assert_relationship(data.solution_4, as: "solutions", for: data.case_study_2)
|> assert_relationship(data.testimonial_1, as: "testimonial", for: data.case_study_1)
|> assert_included(data.solution_1)
|> assert_included(data.solution_2)
|> assert_included(data.solution_3)
|> assert_included(data.solution_4)
|> assert_included(data.image_1)
|> assert_included(data.testimonial_1)
end
@fixtures [:case_study_1, :case_study_2, serialize: true]
test "GET /api/v1/case-studies/:id", %{conn: conn, data: data} do
conn
|> get("/api/v1/case-studies/test-1")
|> json_response(200)
|> assert_data(data.case_study_1)
|> refute_data(data.case_study_2)
|> assert_relationship(data.solution_1, as: "solutions", for: data.case_study_1)
|> assert_relationship(data.solution_2, as: "solutions", for: data.case_study_1)
|> assert_relationship(data.testimonial_1, as: "testimonial", for: data.case_study_1)
|> assert_included(data.solution_1)
|> assert_included(data.solution_2)
|> assert_included(data.testimonial_1)
end
end
defmodule DockYard.Test.Acceptance.Api.V1.CaseStudyTest do
use DockYard.ConnCase, async: false
alias DockYard.{CaseStudy,Solution,Image,Testimonial}
setup do
testimonial_1 = Repo.insert! %Testimonial{
slug: "test-1", body: "Testimonial 1", name: "Test 1 name",
title: "Test 1 title", company: "Test 1 company"
}
case_1 = Repo.insert! %CaseStudy{slug: "test", name: "Test", summary: "Test Summary", order: 2,
overview: "Test Overview", challenge: "Test Challenge", url: "http://test.com", testimonial_id: testimonial_1.id}
case_1_solution_1 = Repo.insert! %Solution{case_study_id: case_1.id, heading: "Solution 1", body: "Solution 1 body"}
case_1_solution_1_image = Repo.insert! %Image{
solution_id: case_1_solution_1.id, heading: "Image 1", caption: "Image 1 caption", path: "Image 1 path"
}
case_1_solution_2 = Repo.insert! %Solution{case_study_id: case_1.id, heading: "Solution 2", body: "Solution 2 body"}
case_2 = Repo.insert! %CaseStudy{slug: "test-2", name: "Test 2", summary: "Test 2 Summary", order: 1,
overview: "Test 2 Overview", challenge: "Test 2 Challenge", url: "http://test2.com"}
case_2_solution_1 = Repo.insert! %Solution{case_study_id: case_2.id, heading: "Solution 3", body: "Solution 3 body", order: 2}
case_2_solution_1_image = Repo.insert! %Image{
solution_id: case_2_solution_1.id, heading: "Image 2", caption: "Image 2 caption", path: "Image 2 path"
}
case_2_solution_2 = Repo.insert! %Solution{case_study_id: case_2.id, heading: "Solution 4", body: "Solution 4 body", order: 1}
{
:ok,
solutions: %{case_1_solution_1: case_1_solution_1, case_1_solution_2: case_1_solution_2,
case_2_solution_1: case_2_solution_1, case_2_solution_2: case_2_solution_2},
images: %{case_1_solution_1_image: case_1_solution_1_image, case_2_solution_1_image: case_2_solution_1_image}
}
end
test "GET /api/v1/case-studies", %{conn: conn, solutions: %{
case_1_solution_1: case_1_solution_1, case_1_solution_2: case_1_solution_2,
case_2_solution_1: case_2_solution_1, case_2_solution_2: case_2_solution_2
}} do
expected_payload = %{
data: [
%{
type: "case-study",
attributes: %{
name: "Test 2", summary: "Test 2 Summary", order: 1, overview: "Test 2 Overview",
challenge: "Test 2 Challenge", url: "http://test2.com"
},
id: "test-2",
relationships: %{
testimonial: %{
data: nil
},
solutions: %{
data: [
%{type: "solution", id: Integer.to_string(case_2_solution_1.id)},
%{type: "solution", id: Integer.to_string(case_2_solution_2.id)}
]
}
}
},
%{
type: "case-study",
attributes: %{
name: "Test", summary: "Test Summary", order: 2, overview: "Test Overview",
challenge: "Test Challenge", url: "http://test.com"
},
id: "test",
relationships: %{
testimonial: %{
data: %{
type: "testimonial",
id: "test-1"
}
},
solutions: %{
data: [
%{type: "solution", id: Integer.to_string(case_1_solution_1.id)},
%{type: "solution", id: Integer.to_string(case_1_solution_2.id)}
]
}
}
}
],
included: [
%{
type: "testimonial",
id: "test-1",
attributes: %{body: "Testimonial 1", company: "Test 1 company", name: "Test 1 name", title: "Test 1 title"}
},
%{
type: "image",
attributes: %{heading: "Image 1", caption: "Image 1 caption", path: "Image 1 path"}
},
%{
type: "solution",
id: Integer.to_string(case_1_solution_1.id),
attributes: %{body: "Solution 1 body", heading: "Solution 1"},
},
%{
type: "solution",
id: Integer.to_string(case_1_solution_2.id),
attributes: %{body: "Solution 2 body", heading: "Solution 2"},
},
%{
type: "image",
attributes: %{ heading: "Image 2", caption: "Image 2 caption", path: "Image 2 path" }
},
%{
type: "solution",
id: Integer.to_string(case_2_solution_1.id),
attributes: %{body: "Solution 3 body", heading: "Solution 3"},
},
%{
type: "solution",
id: Integer.to_string(case_2_solution_2.id),
attributes: %{body: "Solution 4 body", heading: "Solution 4"},
}
]
}
conn
|> get("/api/v1/case-studies")
|> json_response(200)
|> Voorhees.JSONApi.assert_schema(case_study_schema())
|> Voorhees.JSONApi.assert_payload(expected_payload)
end
test "GET /api/v1/case-studies/:id", %{conn: conn, solutions: %{case_1_solution_1: case_1_solution_1,
case_1_solution_2: case_1_solution_2}} do
expected_payload = %{
data: %{
type: "case-study",
attributes: %{
name: "Test", summary: "Test Summary", order: 2, overview: "Test Overview",
challenge: "Test Challenge", url: "http://test.com"
},
id: "test",
relationships: %{
testimonial: %{
data: %{
type: "testimonial",
id: "test-1"
}
},
solutions: %{
data: [
%{type: "solution", id: Integer.to_string(case_1_solution_1.id)},
%{type: "solution", id: Integer.to_string(case_1_solution_2.id)}
]
}
}
},
included: [
%{
type: "testimonial",
id: "test-1",
attributes: %{body: "Testimonial 1", company: "Test 1 company", name: "Test 1 name", title: "Test 1 title"}
},
%{
type: "image",
attributes: %{heading: "Image 1", caption: "Image 1 caption", path: "Image 1 path"}
},
%{
type: "solution",
id: Integer.to_string(case_1_solution_1.id),
attributes: %{body: "Solution 1 body", heading: "Solution 1"},
},
%{
type: "solution",
id: Integer.to_string(case_1_solution_2.id),
attributes: %{body: "Solution 2 body", heading: "Solution 2"},
}
]
}
conn
|> get("/api/v1/case-studies/test")
|> json_response(200)
|> Voorhees.JSONApi.assert_schema(case_study_schema)
|> Voorhees.JSONApi.assert_payload(expected_payload)
end
defp case_study_schema do
%{
"case-study": %{
attributes: [:challenge, :name, :order, :overview,
:summary, :url],
},
testimonial: %{
attributes: [:body, :company, :name, :title]
},
solution: %{
attributes: [:heading, :body, :order]
},
image: %{
attributes: [:heading, :caption, :path]
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment