From 69e4eb0cb3bfe0e1c2cea96767992d94248d7b82 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 15 May 2023 09:16:03 +0200 Subject: [PATCH] Update funnel tests --- lib/plausible/funnels.ex | 12 +++++++++-- test/plausible/funnels_test.exs | 37 ++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/lib/plausible/funnels.ex b/lib/plausible/funnels.ex index 4e9b1e086..3f23c9cf2 100644 --- a/lib/plausible/funnels.ex +++ b/lib/plausible/funnels.ex @@ -1,5 +1,7 @@ defmodule Plausible.Funnels do @funnel_window_duration 86_400 + @min_funnel_size 2 + @max_funnel_size 5 alias Plausible.Funnel alias Plausible.Repo @@ -8,7 +10,9 @@ defmodule Plausible.Funnels do import Ecto.Query - def create(site, name, goals) when is_list(goals) do + def create(site, name, goals) + when is_list(goals) and length(goals) >= @min_funnel_size and + length(goals) <= @max_funnel_size do steps = goals |> Enum.with_index(1) @@ -26,6 +30,10 @@ defmodule Plausible.Funnels do |> Repo.insert() end + def create(_site, _name, _goals) do + {:error, :invalid_funnel_size} + end + def list(%Plausible.Site{id: site_id}) do Repo.all( from(f in Funnel, @@ -139,7 +147,7 @@ defmodule Plausible.Funnels do defp backfill_steps(funnel_result, funnel) do # Directly from ClickHouse we only get {step_idx(), visitor_count()} tuples. # but no totals including previous steps are aggregated. - # Hence we need to perform the appropriate backfill + # Hence we need to perform the appropriate backfill # and also calculate dropoff and conversion rate for each step. funnel_result = Enum.into(funnel_result, %{}) max_step = Enum.max_by(funnel.steps, & &1.step_order).step_order diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs index 054a6b4af..5a6c7b7df 100644 --- a/test/plausible/funnels_test.exs +++ b/test/plausible/funnels_test.exs @@ -10,11 +10,14 @@ defmodule Plausible.FunnelsTest do {:ok, g1} = Goals.create(site, %{"page_path" => "/go/to/blog/**"}) {:ok, g2} = Goals.create(site, %{"event_name" => "Signup"}) {:ok, g3} = Goals.create(site, %{"page_path" => "/checkout"}) + {:ok, g4} = Goals.create(site, %{"event_name" => "Leave feedback"}) + {:ok, g5} = Goals.create(site, %{"page_path" => "/recommend"}) + {:ok, g6} = Goals.create(site, %{"event_name" => "Extra event"}) - {:ok, %{site: site, goals: [g1, g2, g3]}} + {:ok, %{site: site, goals: [g1, g2, g3, g4, g5, g6]}} end - test "create and store a funnel given a set of goals", %{site: site, goals: [g1, g2, g3]} do + test "create and store a funnel given a set of goals", %{site: site, goals: [g1, g2, g3 | _]} do {:ok, funnel} = Funnels.create( site, @@ -35,7 +38,10 @@ defmodule Plausible.FunnelsTest do assert fg3.step_order == 3 end - test "retrieve a funnel by id and site, get steps in order", %{site: site, goals: [g1, g2, g3]} do + test "retrieve a funnel by id and site, get steps in order", %{ + site: site, + goals: [g1, g2, g3 | _] + } do {:ok, funnel} = Funnels.create( site, @@ -48,10 +54,22 @@ defmodule Plausible.FunnelsTest do assert [%{step_order: 1}, %{step_order: 2}, %{step_order: 3}] = funnel.steps end - test "a funnel can be made of max n(TBD) goals" do + test "a funnel cannot be made of < 2 goals", %{site: site, goals: [g1 | _]} do + assert {:error, :invalid_funnel_size} = + Funnels.create( + site, + "Lorem ipsum", + [g1] + ) end - test "a funnel can be deleted" do + test "a funnel can be made of 5 goals maximum", %{site: site, goals: too_many_goals} do + assert {:error, :invalid_funnel_size} = + Funnels.create( + site, + "Lorem ipsum", + too_many_goals + ) end test "a goal can only appear once in a funnel", %{goals: [g1 | _], site: site} do @@ -63,7 +81,7 @@ defmodule Plausible.FunnelsTest do ) end - test "funnels can be listed per site", %{site: site, goals: [g1, g2, g3]} do + test "funnels can be listed per site", %{site: site, goals: [g1, g2, g3 | _]} do Funnels.create(site, "Funnel 1", [g3, g1, g2]) Funnels.create(site, "Funnel 2", [g2, g1, g3]) @@ -71,12 +89,15 @@ defmodule Plausible.FunnelsTest do assert [%{name: "Funnel 1"}, %{name: "Funnel 2"}] = funnels_list end - test "funnels can be evaluated per site within a time range", %{site: site, goals: goals} do + test "funnels can be evaluated per site within a time range", %{ + site: site, + goals: [g1, g2, g3 | _] + } do {:ok, funnel} = Funnels.create( site, "From blog to signup and purchase", - goals + [g1, g2, g3] ) populate_stats(site, [