Skip to content

Commit

Permalink
Scaffold Survey Results tests
Browse files Browse the repository at this point in the history
See #2350
  • Loading branch information
matiasgarciaisaia committed Aug 13, 2024
1 parent a1aadf3 commit c9ee306
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/ask/survey_results.ex
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,14 @@ defmodule Ask.SurveyResults do
defp do_generate_file(file_type, _, _),
do: Logger.warn("No function for generating #{file_type} files")

defp write_to_file(file_type, survey, rows) do
def file_path(survey, file_type) do
filename = csv_filename(survey, file_prefix(file_type))
"#{@target_dir}/#{filename}"
end

defp write_to_file(file_type, survey, rows) do
File.mkdir_p!(@target_dir)
file = File.open!("#{@target_dir}/#{filename}", [:write, :utf8])
file = File.open!(file_path(survey, file_type), [:write, :utf8])
initial_datetime = Timex.now()

rows
Expand All @@ -380,8 +384,10 @@ defmodule Ask.SurveyResults do
defp file_prefix(:respondent_result), do: "respondents"
defp file_prefix(_), do: ""

defp should_generate_file(:interactions, survey) do
# FIXME: we probably don't need to check if we should generate the file
defp should_generate_file(:xxxx_interactions, survey) do
# TODO: when do we want to skip the re-generation of the file?
File.mkdir_p!(@target_dir) # ensure the directory exists
existing_files = File.ls!(@target_dir)

exists_file =
Expand Down
12 changes: 12 additions & 0 deletions test/ask/survey_results_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Ask.SurveyResultsTest do
use Ask.DataCase

alias Ask.SurveyResults

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) |> IO.inspect(label: "file_path")
assert "ID,Respondent ID,Mode,Channel,Disposition,Action Type,Action Data,Timestamp\r\n" == File.read!(path)
end
end

0 comments on commit c9ee306

Please sign in to comment.