Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Nov 15, 2024
1 parent cdba467 commit 086f315
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ask/survey.ex
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ defmodule Ask.Survey do
%{survey | down_channels: down_channels}
end

def with_active_channels(provider, base_url) do
def with_active_channel(provider, base_url) do
query =
from s in Survey,
where: s.state == :running,
Expand Down
4 changes: 2 additions & 2 deletions lib/ask_web/controllers/survey_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ defmodule AskWeb.SurveyController do
end
end

def active(conn, %{"provider" => provider, "base_url" => base_url}) do
surveys = Survey.with_active_channels(provider, base_url)
def active_channel(conn, %{"provider" => provider, "base_url" => base_url}) do
surveys = Survey.with_active_channel(provider, base_url)

render(conn, "index.json", surveys: surveys)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ask_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ defmodule AskWeb.Router do
get "/settings", UserController, :settings, as: :settings
post "/update_settings", UserController, :update_settings, as: :update_settings

get "/surveys/active_channel/:provider", SurveyController, :active, as: :active
get "/surveys/active_channel/:provider", SurveyController, :active_channel, as: :surveys_active_channel
end
end

Expand Down
25 changes: 25 additions & 0 deletions test/ask/survey_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@ defmodule Ask.SurveyTest do
assert running_channels == [Enum.at(channels, 1).id]
end

test "enumerates surveys with active channel" do
surveys = [
insert(:survey, state: :ready),
insert(:survey, state: :running),
insert(:survey, state: :running),
insert(:survey, state: :running),
]

channels = [
insert(:channel, provider: "sms", base_url: "test"),
insert(:channel, provider: "sms", base_url: "test"),
insert(:channel, provider: "ivr", base_url: "prod"),
insert(:channel, provider: "sms", base_url: "test"),
]

setup_surveys_with_channels(surveys, channels)

active_surveys =
Survey.with_active_channel("sms", "test")
|> Enum.map(fn c -> c.id end)
|> Enum.sort()

assert active_surveys == [Enum.at(surveys, 1).id, Enum.at(surveys, 3).id]
end

test "enumerates channels of a survey" do
survey = insert(:survey)
channel_1 = insert(:channel)
Expand Down
60 changes: 60 additions & 0 deletions test/ask_web/controllers/survey_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3282,6 +3282,66 @@ defmodule AskWeb.SurveyControllerTest do
end
end

test "surveys with active channel", %{conn: conn, user: user} do
project = create_project_for_user(user)
surveys = [
insert(:survey, project: project, state: :not_ready),
insert(:survey, project: project, state: :running),
]
channels = [
insert(:channel, provider: "sms", base_url: "test"),
insert(:channel, provider: "sms", base_url: "test"),
]
setup_surveys_with_channels(surveys, channels)
survey = Survey |> Repo.get(Enum.at(surveys, 1).id)

result = get(conn, surveys_active_channel_path(conn, :active_channel, "sms", base_url: "test"))

assert json_response(result, 200)["data"] == [
%{
"cutoff" => survey.cutoff,
"id" => survey.id,
"mode" => survey.mode,
"name" => survey.name,
"description" => nil,
"project_id" => project.id,
"state" => "running",
"locked" => false,
"exit_code" => nil,
"exit_message" => nil,
"schedule" => %{
"blocked_days" => [],
"day_of_week" => %{
"fri" => true,
"mon" => true,
"sat" => true,
"sun" => true,
"thu" => true,
"tue" => true,
"wed" => true
},
"end_time" => "23:59:59",
"start_time" => "00:00:00",
"start_date" => nil,
"end_date" => nil,
"timezone" => "Etc/UTC"
},
"next_schedule_time" => nil,
"started_at" => nil,
"ended_at" => nil,
"updated_at" => to_iso8601(survey.updated_at),
"down_channels" => [],
"folder_id" => nil,
"first_window_started_at" => nil,
"panel_survey_id" => nil,
"last_window_ends_at" => nil,
"is_deletable" => false,
"is_movable" => true,
"generates_panel_survey" => false
},
]
end

def prepare_for_state_update(user) do
project = create_project_for_user(user)
questionnaire = insert(:questionnaire, name: "test", project: project)
Expand Down

0 comments on commit 086f315

Please sign in to comment.