You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/test/plausible/stats/interval_test.exs

127 lines
4.3 KiB
Elixir

defmodule Plausible.Stats.IntervalTest do
use Plausible.DataCase, async: true
import Plausible.Stats.Interval
test "default_for_period/1" do
assert default_for_period("realtime") == "minute"
assert default_for_period("day") == "hour"
assert default_for_period("7d") == "date"
assert default_for_period("12mo") == "month"
end
test "default_for_date_range/1" do
assert default_for_date_range(Date.range(~D[2022-01-01], ~D[2023-01-01])) == "month"
assert default_for_date_range(Date.range(~D[2022-01-01], ~D[2022-01-15])) == "date"
assert default_for_date_range(Date.range(~D[2022-01-01], ~D[2022-01-01])) == "hour"
end
describe "valid_by_period/1" do
test "for a newly created site" do
site = build(:site, stats_start_date: Date.utc_today())
assert valid_by_period(site: site) == %{
"realtime" => ["minute"],
"day" => ["minute", "hour"],
"7d" => ["hour", "date"],
"month" => ["date", "week"],
"30d" => ["date", "week"],
"6mo" => ["date", "week", "month"],
"12mo" => ["date", "week", "month"],
"year" => ["date", "week", "month"],
"custom" => ["date", "week", "month"],
"all" => ["date", "week", "month"]
}
end
test "for a site with stats starting over 12m ago" do
site = build(:site, stats_start_date: Timex.shift(Date.utc_today(), months: -13))
assert valid_by_period(site: site) == %{
"realtime" => ["minute"],
"day" => ["minute", "hour"],
"7d" => ["hour", "date"],
"month" => ["date", "week"],
"30d" => ["date", "week"],
"6mo" => ["date", "week", "month"],
"12mo" => ["date", "week", "month"],
"year" => ["date", "week", "month"],
"custom" => ["date", "week", "month"],
"all" => ["week", "month"]
}
end
test "for a query range exceeding 12m" do
ago_13m = Timex.shift(Date.utc_today(), months: -13)
site = build(:site, stats_start_date: ago_13m)
assert valid_by_period(site: site, from: ago_13m, to: Date.utc_today()) == %{
"realtime" => ["minute"],
"day" => ["minute", "hour"],
"7d" => ["hour", "date"],
"month" => ["date", "week"],
"30d" => ["date", "week"],
"6mo" => ["date", "week", "month"],
"12mo" => ["date", "week", "month"],
"year" => ["date", "week", "month"],
"custom" => ["week", "month"],
"all" => ["week", "month"]
}
end
end
describe "valid_for_period/3" do
test "common" do
Add multiple imports per site (#3724) * Clean up references to no longer active `google_analytics_imports` Oban queue * Stub CSV importer * Add SiteImport schema * Rename `Plausible.Imported` module file to match module name * Add `import_id` column to `Imported.*` CH schemas * Implement Importer behavior and manage imports state using new entities * Implement importer callbacks and maintain site.imported_data for UA * Keep imports in sync when forgetting all imports * Scope imported data queries to completed import IDs * Mark newly imported data with respective import ID * Clean up Importer implementation a bit * Test querying legacy and new imported data * Send Oban notifications on import worker failure too * Fix checking for forgettable imports and remove redundant function * Fix UA integration test * Change site import source to atom enum and add source label * Add typespecs and reduce repetition in `Plausible.Imported` * Improve documentation and typespecs * Add test for purging particular import * Switch email notification templates depending on import source * Document running import synchronously * Fix UA importer args parsing and ensure it's covered by tests * Clear `site.stats_start_date` on complete import to force recalculation * Test Oban notifications (h/t @ruslandoga) * Purge stats on import failure right away to reduce a chance of leaving debris behind * Fix typos Co-authored-by: hq1 <hq@mtod.org> * Fix another typo * Refactor fetching earliest import and earliest stats start date * Use `Date.after?` instead of `Timex.after?` * Cache import data in site virtual fields and limit queried imports to 5 * Ensure always current `stats_start_date` is used * Work around broken typespec in Timex * Make `SiteController.forget_imported` action idempotent * Discard irrecoverably failed import tasks * Use macros for site import statuses There's also a fix ensuring only complete imports are considered where relevant - couldn't isolate it as it was in a common hunk * Use `import_id` as worker job uniqueness criterion * Do not load imported stats data in plugins API context --------- Co-authored-by: hq1 <hq@mtod.org>
3 years ago
site = insert(:site)
assert valid_for_period?("month", "date", site: site)
refute valid_for_period?("30d", "month", site: site)
refute valid_for_period?("realtime", "week", site: site)
end
test "for a newly created site" do
site = build(:site, stats_start_date: Date.utc_today())
assert valid_for_period?("all", "date", site: site)
assert valid_for_period?("custom", "date",
site: site,
from: ~D[2023-06-01],
to: ~D[2023-07-01]
)
assert valid_for_period?("custom", "date",
site: site,
to: ~D[2023-06-01],
from: ~D[2023-07-01]
)
end
test "for a newly created site with >12m range" do
site = build(:site, stats_start_date: Date.utc_today())
assert valid_for_period?("all", "date", site: site)
refute valid_for_period?("custom", "date",
site: site,
from: ~D[2012-06-01],
to: ~D[2023-07-01]
)
refute valid_for_period?("custom", "date",
site: site,
to: ~D[2012-06-01],
from: ~D[2023-07-01]
)
end
test "for a site with stats starting over 12m ago" do
site = build(:site, stats_start_date: Timex.shift(Date.utc_today(), months: -13))
refute valid_for_period?("all", "date", site: site)
assert valid_for_period?("custom", "date",
site: site,
from: ~D[2023-06-01],
to: ~D[2023-07-01]
)
end
end
end