Skip to content

Commit

Permalink
Fix SurveyResults tests
Browse files Browse the repository at this point in the history
Small changes, nothing too relevant.

See #2350
  • Loading branch information
matiasgarciaisaia committed Nov 5, 2024
1 parent a5dda9b commit 59e67b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
3 changes: 2 additions & 1 deletion lib/ask/survey_results.ex
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ defmodule Ask.SurveyResults do
seconds_to_process_file = Timex.diff(Timex.now(), initial_datetime, :seconds)

Logger.info(
"Generation of #{file_type} file (survey_id: #{survey.id}) took #{seconds_to_process_file} seconds"
"Generation of #{file_prefix(file_type)} file (survey_id: #{survey.id}) took #{seconds_to_process_file} seconds"
)
end

Expand Down Expand Up @@ -430,6 +430,7 @@ defmodule Ask.SurveyResults do
defp file_prefix(:interactions), do: "respondents_interactions"
defp file_prefix(:incentives), do: "respondents_incentives"
defp file_prefix(:disposition_history), do: "disposition_history"
defp file_prefix(:respondents_results), do: "respondents"
defp file_prefix({:respondents_results, filter}) do
if RespondentsFilter.empty?(filter) do
"respondents"
Expand Down
51 changes: 27 additions & 24 deletions test/ask/survey_results_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Ask.SurveyResultsTest do
test "generates empty interactions file" do
survey = insert(:survey)
assert {:noreply, _, _} = SurveyResults.handle_cast({:interactions, survey.id, nil}, nil)
path = SurveyResults.file_path(survey, :interactions)
path = "./priv/static/" <> SurveyResults.file_path(survey, :interactions)
assert "ID,Respondent ID,Mode,Channel,Disposition,Action Type,Action Data,Timestamp\r\n" == File.read!(path)
end

Expand Down Expand Up @@ -136,7 +136,7 @@ defmodule Ask.SurveyResultsTest do
end
])

path = SurveyResults.file_path(survey, :interactions)
path = "./priv/static/" <> SurveyResults.file_path(survey, :interactions)
lines = File.read!(path) |> String.split("\r\n") |> Enum.reject(fn x -> String.length(x) == 0 end)
assert length(lines) == length(expected_list)
assert lines == expected_list
Expand Down Expand Up @@ -192,9 +192,9 @@ defmodule Ask.SurveyResultsTest do

insert(:response, respondent: respondent_2, field_name: "Smokes", value: "No")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{}}, nil)
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, %RespondentsFilter{}}, nil)

path = SurveyResults.file_path(survey, :respondent_result)
path = "./priv/static/" <> SurveyResults.file_path(survey, :respondents_results)
csv = File.read!(path)

[line1, line2, line3, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -311,9 +311,9 @@ defmodule Ask.SurveyResultsTest do
insert(:response, respondent: respondent, field_name: "Exercises", value: "No")
insert(:response, respondent: respondent, field_name: "Perfect Number", value: "100")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{}}, nil)
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, %RespondentsFilter{}}, nil)

path = SurveyResults.file_path(survey, :respondent_result)
path = "./priv/static/" <> SurveyResults.file_path(survey, :respondents_results)
csv = File.read!(path)

[line1, line2, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -376,8 +376,8 @@ defmodule Ask.SurveyResultsTest do

insert(:response, respondent: respondent_2, field_name: "Smokes", value: "No")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{}}, nil)
path = SurveyResults.file_path(survey, :respondent_result)
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, %RespondentsFilter{}}, nil)
path = "./priv/static/" <> SurveyResults.file_path(survey, :respondents_results)
csv = File.read!(path)

[line1, line2, line3, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -502,8 +502,8 @@ defmodule Ask.SurveyResultsTest do

insert(:response, respondent: respondent_2, field_name: "Smokes", value: "No")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{}}, nil)
path = SurveyResults.file_path(survey, :respondent_result)
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, %RespondentsFilter{}}, nil)
path = "./priv/static/" <> SurveyResults.file_path(survey, :respondents_results)
csv = File.read!(path)

[line1, line2, line3, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -600,8 +600,9 @@ defmodule Ask.SurveyResultsTest do

insert(:response, respondent: respondent_2, field_name: "Smokes", value: "No")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{disposition: :registered}}, nil)
path = SurveyResults.file_path(survey, :respondent_result)
filter = %RespondentsFilter{disposition: :registered}
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, filter}, nil)
path = "./priv/static/" <> SurveyResults.file_path(survey, {:respondents_results, filter})
csv = File.read!(path)

[line1, line2, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -673,8 +674,9 @@ defmodule Ask.SurveyResultsTest do

insert(:response, respondent: respondent_2, field_name: "Smokes", value: "No")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{since: Timex.shift(Timex.now(), hours: 2)}}, nil)
path = SurveyResults.file_path(survey, :respondent_result)
filter = %RespondentsFilter{since: Timex.shift(Timex.now(), hours: 2)}
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, filter}, nil)
path = "./priv/static/" <> SurveyResults.file_path(survey, {:respondents_results, filter})
csv = File.read!(path)

[line1, line2, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -744,8 +746,9 @@ defmodule Ask.SurveyResultsTest do

insert(:response, respondent: respondent_2, field_name: "Smokes", value: "No")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{state: :completed}}, nil)
path = SurveyResults.file_path(survey, :respondent_result)
filter = %RespondentsFilter{state: :completed}
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, filter}, nil)
path = "./priv/static/" <> SurveyResults.file_path(survey, {:respondents_results, filter})
csv = File.read!(path)

[line1, line2, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -845,8 +848,8 @@ defmodule Ask.SurveyResultsTest do
insert(:response, respondent: respondent_4, field_name: "Smokes", value: "Yes")
insert(:response, respondent: respondent_4, field_name: "Exercises", value: "No")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{}}, nil)
path = SurveyResults.file_path(survey, :respondent_result)
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, %RespondentsFilter{}}, nil)
path = "./priv/static/" <> SurveyResults.file_path(survey, :respondents_results)
csv = File.read!(path)

assert !String.contains?(group_1.name, [" ", ",", "*", ":", "?", "\\", "|", "/", "<", ">"])
Expand Down Expand Up @@ -1001,8 +1004,8 @@ defmodule Ask.SurveyResultsTest do

insert(:response, respondent: respondent_2, field_name: "Smokes", value: "No")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{}}, nil)
path = SurveyResults.file_path(survey, :respondent_result)
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, %RespondentsFilter{}}, nil)
path = "./priv/static/" <> SurveyResults.file_path(survey, :respondents_results)
csv = File.read!(path)

[line1, line2, line3, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -1103,8 +1106,8 @@ defmodule Ask.SurveyResultsTest do

insert(:response, respondent: respondent_1, field_name: "language", value: "es")

assert {:noreply, _, _} = SurveyResults.handle_cast({:respondent_result, survey.id, %RespondentsFilter{}}, nil)
path = SurveyResults.file_path(survey, :respondent_result)
assert {:noreply, _, _} = SurveyResults.handle_cast({:respondents_results, survey.id, %RespondentsFilter{}}, nil)
path = "./priv/static/" <> SurveyResults.file_path(survey, :respondents_results)
csv = File.read!(path)

[line1, line2, _] = csv |> String.split("\r\n")
Expand Down Expand Up @@ -1174,7 +1177,7 @@ defmodule Ask.SurveyResultsTest do
)

assert {:noreply, _, _} = SurveyResults.handle_cast({:disposition_history, survey.id, %RespondentsFilter{}}, nil)
path = SurveyResults.file_path(survey, :disposition_history)
path = "./priv/static/" <> SurveyResults.file_path(survey, :disposition_history)
csv = File.read!(path)

lines = csv |> String.split("\r\n") |> Enum.reject(fn x -> String.length(x) == 0 end)
Expand Down Expand Up @@ -1236,7 +1239,7 @@ defmodule Ask.SurveyResultsTest do
)

assert {:noreply, _, _} = SurveyResults.handle_cast({:incentives, survey.id, %RespondentsFilter{}}, nil)
path = SurveyResults.file_path(survey, :incentives)
path = "./priv/static/" <> SurveyResults.file_path(survey, :incentives)
csv = File.read!(path)

lines = csv |> String.split("\r\n") |> Enum.reject(fn x -> String.length(x) == 0 end)
Expand Down

0 comments on commit 59e67b7

Please sign in to comment.