From 83887c49ccc7fbc50ba1766bfa74c77994ef1900 Mon Sep 17 00:00:00 2001 From: Vignesh Joglekar Date: Thu, 27 May 2021 16:59:32 -0500 Subject: [PATCH] Adds tests for suggestions, formats goals suggestion query --- lib/plausible/stats/clickhouse.ex | 31 +++--- .../api/stats_controller/suggestions_test.exs | 97 +++++++++++++++++++ 2 files changed, 111 insertions(+), 17 deletions(-) create mode 100644 test/plausible_web/controllers/api/stats_controller/suggestions_test.exs diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex index 752d2e9a9..e98a4aa35 100644 --- a/lib/plausible/stats/clickhouse.ex +++ b/lib/plausible/stats/clickhouse.ex @@ -1618,6 +1618,17 @@ defmodule Plausible.Stats.Clickhouse do |> Enum.slice(0..9) end + def make_suggestions(site, _query, "goal", filter_search) do + Repo.all(from g in Plausible.Goal, where: g.domain == ^site.domain) + |> Enum.map(fn x -> if x.event_name, do: x.event_name, else: "Visit #{x.page_path}" end) + |> Enum.filter(fn goal -> + String.contains?( + String.downcase(goal), + String.downcase(filter_search) || filter_search == "" + ) + end) + end + def make_suggestions(site, query, filter_name, filter_search) do filter_query = "%#{filter_search}%" @@ -1718,24 +1729,10 @@ defmodule Plausible.Stats.Clickhouse do select: {e.screen_size}, where: fragment("? ilike ?", e.screen_size, ^filter_query) ) - - "goal" -> - nil end - if filter_name == "goal" do - Repo.all(from g in Plausible.Goal, where: g.domain == ^site.domain) - |> Enum.map(fn x -> if x.event_name, do: x.event_name, else: "Visit #{x.page_path}" end) - |> Enum.filter(fn goal -> - String.contains?( - String.downcase(goal), - String.downcase(filter_search) || filter_search == "" - ) - end) - else - ClickhouseRepo.all(q) - |> Enum.map(fn {suggestion} -> suggestion end) - |> Enum.filter(fn suggestion -> suggestion != "" end) - end + ClickhouseRepo.all(q) + |> Enum.map(fn {suggestion} -> suggestion end) + |> Enum.filter(fn suggestion -> suggestion != "" end) end end diff --git a/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs b/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs new file mode 100644 index 000000000..83bcb04f3 --- /dev/null +++ b/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs @@ -0,0 +1,97 @@ +defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do + use PlausibleWeb.ConnCase + import Plausible.TestUtils + + describe "GET /api/stats/:domain/suggestions/:filter_name" do + setup [:create_user, :log_in, :create_site] + + test "returns suggestions for pages without a query", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01") + + assert json_response(conn, 200) == ["/", "/register", "/contact", "/irrelevant"] + end + + test "returns suggestions for pages with a query", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01&q=re") + + assert json_response(conn, 200) == ["/register", "/irrelevant"] + end + + test "returns suggestions for pages without any suggestions", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01&q=/123") + + assert json_response(conn, 200) == [] + end + + test "returns suggestions for goals", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/goal?period=month&date=2019-01-01") + + assert json_response(conn, 200) == [] + end + + test "returns suggestions for sources", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/source?period=month&date=2019-01-01") + + assert json_response(conn, 200) == ["10words", "Bing"] + end + + test "returns suggestions for countries", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&q=GBR,UKR,URY,USA") + + assert json_response(conn, 200) == ["USA"] + end + + test "returns suggestions for countries without country in search", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&q=GBR,UKR,URY") + + assert json_response(conn, 200) == [] + end + + test "returns suggestions for screen sizes", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/screen?period=month&date=2019-01-01") + + assert json_response(conn, 200) == ["Desktop"] + end + + test "returns suggestions for browsers", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/browser?period=month&date=2019-01-01") + + assert json_response(conn, 200) == ["Chrome"] + end + + test "returns suggestions for browser versions", %{conn: conn, site: site} do + filters = Jason.encode!(%{browser: "Chrome"}) + conn = get(conn, "/api/stats/#{site.domain}/suggestions/browser_version?period=month&date=2019-01-01&filters=#{filters}") + + assert json_response(conn, 200) == ["78.0"] + end + + test "returns suggestions for OS", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/os?period=month&date=2019-01-01") + + assert json_response(conn, 200) == ["Mac"] + end + + test "returns suggestions for OS versions", %{conn: conn, site: site} do + filters = Jason.encode!(%{os: "Mac"}) + conn = get(conn, "/api/stats/#{site.domain}/suggestions/os_version?period=month&date=2019-01-01&filters=#{filters}") + + assert json_response(conn, 200) == ["10.15"] + end + + test "returns suggestions for OS versions with search", %{conn: conn, site: site} do + filters = Jason.encode!(%{os: "Mac"}) + conn = get(conn, "/api/stats/#{site.domain}/suggestions/os_version?period=month&date=2019-01-01&filters=#{filters}&q=11") + + assert json_response(conn, 200) == [] + end + + test "returns suggestions for referrers", %{conn: conn, site: site} do + conn = get(conn, "/api/stats/#{site.domain}/suggestions/referrer?period=month&date=2019-01-01") + + assert json_response(conn, 200) == ["10words.com/page1"] + end + + end + +end