Add extra tests for returning/filtering by '(not set)'

This commit is contained in:
Adam Rutkowski 2023-02-06 14:53:45 +01:00
parent 30d0df345a
commit cdd8545ffb
3 changed files with 87 additions and 0 deletions

View File

@ -129,6 +129,21 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
%{"name" => "Firefox", "visitors" => 1, "percentage" => 33}
]
end
test "returns (not set) when appropriate", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview,
user_id: 123,
browser: ""
)
])
conn = get(conn, "/api/stats/#{site.domain}/browsers?period=day")
assert json_response(conn, 200) == [
%{"name" => "(not set)", "visitors" => 1, "percentage" => 100}
]
end
end
describe "GET /api/stats/:domain/browser-versions" do
@ -155,5 +170,23 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do
%{"name" => "77.0", "visitors" => 1, "percentage" => 33}
]
end
test "returns results for (not set)", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, browser: "", browser_version: "")
])
filters = Jason.encode!(%{browser: "(not set)"})
conn =
get(
conn,
"/api/stats/#{site.domain}/browser-versions?period=day&filters=#{filters}"
)
assert json_response(conn, 200) == [
%{"name" => "(not set)", "visitors" => 1, "percentage" => 100}
]
end
end
end

View File

@ -19,6 +19,33 @@ defmodule PlausibleWeb.Api.StatsController.OperatingSystemsTest do
]
end
test "returns (not set) when appropriate", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview,
operating_system: ""
),
build(:pageview,
operating_system: "Linux"
)
])
conn = get(conn, "/api/stats/#{site.domain}/operating-systems?period=day")
assert json_response(conn, 200) == [
%{"name" => "(not set)", "visitors" => 1, "percentage" => 50},
%{"name" => "Linux", "visitors" => 1, "percentage" => 50}
]
filters = Jason.encode!(%{os: "(not set)"})
conn =
get(conn, "/api/stats/#{site.domain}/operating-systems?period=day&filters=#{filters}")
assert json_response(conn, 200) == [
%{"name" => "(not set)", "visitors" => 1, "percentage" => 100}
]
end
test "calculates conversion_rate when filtering for goal", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, user_id: 1, operating_system: "Mac"),

View File

@ -19,6 +19,33 @@ defmodule PlausibleWeb.Api.StatsController.ScreenSizesTest do
]
end
test "returns (not set) when appropriate", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview,
screen_size: ""
),
build(:pageview,
screen_size: "Desktop"
)
])
conn = get(conn, "/api/stats/#{site.domain}/screen-sizes?period=day")
assert json_response(conn, 200) == [
%{"name" => "(not set)", "visitors" => 1, "percentage" => 50},
%{"name" => "Desktop", "visitors" => 1, "percentage" => 50}
]
conn = get(conn, "/api/stats/#{site.domain}/screen-sizes?period=day")
filters = Jason.encode!(%{screen: "(not set)"})
conn = get(conn, "/api/stats/#{site.domain}/screen-sizes?period=day&filters=#{filters}")
assert json_response(conn, 200) == [
%{"name" => "(not set)", "visitors" => 1, "percentage" => 100}
]
end
test "returns screen sizes with :is filter on custom pageview props", %{
conn: conn,
site: site