Skip to content

Commit

Permalink
creates tests for get_patients action of team member controller #1 #6
Browse files Browse the repository at this point in the history
  • Loading branch information
RobStallion committed Jul 29, 2017
1 parent 4adcfe0 commit 2982a77
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
10 changes: 6 additions & 4 deletions lib/epjs_app/clinician_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ defmodule EpjsApp.ClinicianConnection do
%{decrypted_time_str: decrypted_time_str, decrypted_user_guid: decrypted_user_guid, clinician: Repo.all(query)}
end

def get_patients(%{"clinician" => clinician}) do
def get_patients(%{"email" => email}) do
ids_query = from e in EPJSTeamMember,
where: e."Email" == ^clinician.email,
select: e."Patient_ID"
patient_ids = Repo.all(ids_query)
where: e."Email" == ^email,
select: e."Patient_ID"
patient_ids =
Repo.all(ids_query)

patients = patient_ids
|> Enum.map(fn id ->
Repo.all(from e in EPJSUser,
where: e."Patient_ID" == ^id)
end)
|> List.flatten

%{patient_ids: patient_ids, patients: patients}
end
Expand Down
63 changes: 53 additions & 10 deletions test/controllers/team_member_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
defmodule EpjsApp.TeamMemberControllerTest do
use EpjsApp.ConnCase
alias EpjsApp.{EPJSTeamMember, Repo}
alias EpjsApp.{EPJSUser, EPJSTeamMember, Repo}

describe "team member controller returns json response with valid user data" do
describe "routes with no clinician return empty lists" do
test "GET /epjsteammember/clinician-connection/find-clinician?user_data=", %{conn: conn} do
user_data = "[email protected]&UserId=badS tring&tokenexpiry=3000-06-23T11:15:53"
conn = get conn, "/epjsteammember/clinician-connection/find-clinician?user_data=" <> user_data
assert conn.resp_body == "{\"decrypted_user_guid\":\"randomstringtotestwith\",\"decrypted_time_str\":\"3000-06-23T11:15:53\",\"clinician\":[]}"
assert conn.resp_body
|> Poison.decode!
|> Map.get("clinician") == []
end

test "GET /epjsteammember/clinician-connection/get-patients?clinician= with clinician not in db returns no data", %{conn: conn} do
clinician = %{
id: 321,
first_name: "rob",
last_name: "stallion",
email: "[email protected]",
security_question: "Question?",
security_answer: "Answer",
role: "clinician",
user_guid: "randomstringtotestwith"
} |> Poison.encode!

conn = get conn, "/epjsteammember/clinician-connection/get-patients?clinician=" <> clinician
assert conn.resp_body
|> Poison.decode!
|> Map.get("patient_ids") == []
end
end

describe "routes with clinician return lists" do
setup %{} do
Repo.insert!(%EPJSTeamMember{
Patient_ID: 123,
Expand All @@ -14,28 +43,42 @@ defmodule EpjsApp.TeamMemberControllerTest do
User_Guid: "randomstringtotestwith"
})

Repo.insert!(%EPJSUser{
Patient_ID: 123,
Forename: "kat",
Surname: "bow"
})

:ok
end

test "GET /epjsteammember/clinician-connection/find-clinician?user_data=", %{conn: conn} do
user_data = "[email protected]&UserId=randomstringtotestwith&tokenexpiry=3000-06-23T11:15:53"
conn = get conn, "http://localhost:4001/epjsteammember/clinician-connection/find-clinician?user_data=" <> user_data
conn = get conn, "/epjsteammember/clinician-connection/find-clinician?user_data=" <> user_data
clinician = conn.resp_body
|> Poison.decode!
|> Map.get("clinician")
|> Enum.at(0)
assert clinician["Staff_ID"] == 321
end
end

describe "team member controller returns empty clinician list with invalid user_data" do
test "GET /epjsteammember/clinician-connection/find-clinician?user_data=", %{conn: conn} do
user_data = "[email protected]&UserId=randomstringtotestwith&tokenexpiry=3000-06-23T11:15:53"
conn = get conn, "http://localhost:4001/epjsteammember/clinician-connection/find-clinician?user_data=" <> user_data
assert conn.resp_body == "{\"decrypted_user_guid\":\"randomstringtotestwith\",\"decrypted_time_str\":\"3000-06-23T11:15:53\",\"clinician\":[]}"
test "GET /epjsteammember/clinician-connection/get-patients?clinician= returns expected data", %{conn: conn} do
clinician = %{
id: 321,
first_name: "rob",
last_name: "stallion",
email: "[email protected]",
security_question: "Question?",
security_answer: "Answer",
role: "clinician",
user_guid: "randomstringtotestwith"
} |> Poison.encode!

conn = get conn, "/epjsteammember/clinician-connection/get-patients?clinician=" <> clinician
assert conn.resp_body
|> Poison.decode!
|> Map.get("clinician") == []
|> Map.get("patient_ids") == [123]
end
end
end
"/epjsteammember/clinician-connection/get-patients"
2 changes: 1 addition & 1 deletion web/controllers/team_member_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule EpjsApp.TeamMemberController do

def get_patients(conn, %{"clinician" => clinician}) do
patient_details = clinician
|> Poison.decode
|> Poison.decode!
|> ClinicianConnection.get_patients

json conn, patient_details
Expand Down

0 comments on commit 2982a77

Please sign in to comment.