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/imported_test.exs

267 lines
7.7 KiB
Elixir

Implement UI for multiple imports (#3727) * Create a stub of site settings section for imports and exports * Use legacy site import indication to determine UA import handling * Add provisional logos for upcoming import sources * Stub basics of import page * Add very rudimentary support for multiple UA imports * Implement imports list as live view * Add support for opening LV modal from backend and closing from frontend * Introduce notion of themes to `button` and `button_link` components * Add confirmation modal on deleting import * Swap GA4 logo * Implement disabled state support for `button_link` component * Disable export and non-implemented import sources * Use native starts start date for upper boundary of import time range * Ensure integrations view uses legacy UA import flow * Remove unnecessary preload in SiteController * Remove unnecessary exception for legacy imports * Move API controller stats tests under PlausibleWeb * Test listing imports * Add test for explicit listener setup * Add tests for legacy flag state in UA importer * Add test for purging legacy import data * Add tests for `Sites.native_stats_start_date` * Test forgetting imports * Add `Stats.Clickhouse.imported_pageview_counts/1` and fix test flakiness * Show page view counts on imports list * Add tests for static imports and exports view * Adjust button look slightly * Use `case` instead of `cond` * Make feature flag customisable per site * Fix buttons and empty state styling * Add another import to seeds * Use JS confirm dialog instead of modal for deletion confirmations * Revert "Add support for opening LV modal from backend and closing from frontend" This reverts commit 260e6c753032b451542e24be9edc2118790b5a00. * Default `legacy` to false when inserting new import jobs * Drop `method` attribute from `button_link` and `unstyled_link` components
3 years ago
defmodule Plausible.ImportedTest do
use Plausible.DataCase
use Plausible
alias Plausible.Imported
describe "list_all_imports/1" do
test "returns empty when there are no imports" do
site = insert(:site)
assert Imported.list_all_imports(site) == []
end
test "returns imports in various states" do
site = insert(:site)
_rogue_import = insert(:site_import)
import1 = insert(:site_import, site: site, status: :pending)
import2 = insert(:site_import, site: site, status: :importing)
import3 = insert(:site_import, site: site, status: :completed)
import4 = insert(:site_import, site: site, status: :failed)
assert [%{id: id1}, %{id: id2}, %{id: id3}, %{id: id4}] = Imported.list_all_imports(site)
ids = [id1, id2, id3, id4]
assert import1.id in ids
assert import2.id in ids
assert import3.id in ids
assert import4.id in ids
end
test "returns one legacy import when present with respective site import entry" do
site = insert(:site)
{:ok, opts} = add_imported_data(%{site: site})
site = Map.new(opts).site
site_import = insert(:site_import, site: site, legacy: true)
site_import_id = site_import.id
assert [%{id: ^site_import_id}] = Imported.list_all_imports(site)
end
test "returns legacy import without respective site import entry" do
site = insert(:site)
{:ok, opts} = add_imported_data(%{site: site})
site = Map.new(opts).site
imported_start_date = site.imported_data.start_date
imported_end_date = site.imported_data.end_date
assert [
%{
id: 0,
source: :universal_analytics,
start_date: ^imported_start_date,
end_date: ^imported_end_date,
status: :completed
}
] = Imported.list_all_imports(site)
end
end
describe "get_imports_date_range/1" do
test "returns empty when there are no imports" do
site = insert(:site)
assert %{start_date: nil, end_date: nil} = Imported.get_imports_date_range(site)
end
test "returns empty when only incomplete or failed imports are present" do
site =
insert(:site)
|> Plausible.Site.start_import(
~D[2020-04-01],
~D[2022-06-22],
"Google Analytics",
"error"
)
|> Repo.update!()
_import1 = insert(:site_import, site: site, status: :pending)
_import2 = insert(:site_import, site: site, status: :importing)
_import3 = insert(:site_import, site: site, status: :failed)
_rogue_import = insert(:site_import, site: build(:site), status: :completed)
assert %{start_date: nil, end_date: nil} = Imported.get_imports_date_range(site)
end
test "returns start and end dates considering all imports" do
site =
insert(:site)
|> Plausible.Site.start_import(
~D[2020-04-01],
~D[2022-06-22],
"Google Analytics",
"ok"
)
|> Repo.update!()
_import1 =
insert(:site_import,
site: site,
start_date: ~D[2020-04-02],
end_date: ~D[2022-06-22],
status: :completed,
legacy: true
)
_import2 =
insert(:site_import,
site: site,
start_date: ~D[2022-06-22],
end_date: ~D[2024-01-08],
status: :completed
)
assert %{start_date: ~D[2020-04-01], end_date: ~D[2024-01-08]} =
Imported.get_imports_date_range(site)
end
end
describe "check_dates/3" do
test "crops dates from both ends when overlapping with existing import and native stats" do
site = insert(:site)
populate_stats(site, [
build(:pageview, timestamp: ~N[2023-10-25 15:58:00])
])
start_date = ~D[2016-04-03]
end_date = ~D[2021-05-12]
_existing_import =
insert(:site_import,
site: site,
start_date: start_date,
end_date: end_date,
status: :completed
)
assert {:ok, ~D[2021-05-12], ~D[2023-10-25]} =
Imported.check_dates(site, ~D[2021-04-11], ~D[2024-01-12])
end
test "picks longest continuous range when containing existing import" do
site = insert(:site)
populate_stats(site, [
build(:pageview, timestamp: ~N[2023-10-25 15:58:00])
])
start_date = ~D[2019-04-03]
end_date = ~D[2021-05-12]
_existing_import =
insert(:site_import,
site: site,
start_date: start_date,
end_date: end_date,
status: :completed
)
assert {:ok, ~D[2021-05-12], ~D[2023-10-25]} =
Imported.check_dates(site, ~D[2019-03-21], ~D[2024-01-12])
end
test "does not alter the dates when there are no imports and no native stats" do
site = insert(:site)
assert {:ok, ~D[2021-05-12], ~D[2024-01-12]} =
Imported.check_dates(site, ~D[2021-05-12], ~D[2024-01-12])
end
test "ignores input date range difference smaller than 2 days" do
site = insert(:site)
assert {:error, :no_time_window} =
Imported.check_dates(site, ~D[2024-01-12], ~D[2024-01-12])
assert {:error, :no_time_window} =
Imported.check_dates(site, ~D[2024-01-12], ~D[2024-01-13])
assert {:ok, ~D[2024-01-12], ~D[2024-01-14]} =
Imported.check_dates(site, ~D[2024-01-12], ~D[2024-01-14])
end
test "ignores imports with date range difference smaller than 2 days" do
site = insert(:site)
start_date = ~D[2024-01-12]
end_date = ~D[2024-01-13]
_existing_import =
insert(:site_import,
site: site,
start_date: start_date,
end_date: end_date,
status: :completed
)
assert {:ok, ~D[2021-04-22], ~D[2024-03-14]} =
Imported.check_dates(site, ~D[2021-04-22], ~D[2024-03-14])
end
test "returns no time window when input range starts after native stats start date" do
site = insert(:site)
populate_stats(site, [
build(:pageview, timestamp: ~N[2023-10-25 15:58:00])
])
assert {:error, :no_time_window} =
Imported.check_dates(site, ~D[2023-10-28], ~D[2024-01-13])
end
test "returns no time window when input range starts less than 2 days before native stats start date" do
site = insert(:site)
populate_stats(site, [
build(:pageview, timestamp: ~N[2023-10-25 15:58:00])
])
assert {:error, :no_time_window} =
Imported.check_dates(site, ~D[2023-10-24], ~D[2024-01-13])
end
test "crops time range at native stats start date when effective range is 2 days or longer" do
site = insert(:site)
populate_stats(site, [
build(:pageview, timestamp: ~N[2023-10-25 15:58:00])
])
assert {:ok, ~D[2023-10-23], ~D[2023-10-25]} =
Imported.check_dates(site, ~D[2023-10-23], ~D[2024-01-13])
end
test "returns no data error when start date missing" do
site = insert(:site)
assert {:error, :no_data} = Imported.check_dates(site, nil, nil)
end
test "returns no time window error when date range overlaps with existing import and stats completely" do
site = insert(:site)
populate_stats(site, [
build(:pageview, timestamp: ~N[2023-10-25 15:58:00])
])
start_date = ~D[2016-04-03]
end_date = ~D[2023-10-25]
_existing_import =
insert(:site_import,
site: site,
start_date: start_date,
end_date: end_date,
status: :completed
)
assert {:error, :no_time_window} =
Imported.check_dates(site, ~D[2021-04-11], ~D[2024-01-12])
end
end
Implement UI for multiple imports (#3727) * Create a stub of site settings section for imports and exports * Use legacy site import indication to determine UA import handling * Add provisional logos for upcoming import sources * Stub basics of import page * Add very rudimentary support for multiple UA imports * Implement imports list as live view * Add support for opening LV modal from backend and closing from frontend * Introduce notion of themes to `button` and `button_link` components * Add confirmation modal on deleting import * Swap GA4 logo * Implement disabled state support for `button_link` component * Disable export and non-implemented import sources * Use native starts start date for upper boundary of import time range * Ensure integrations view uses legacy UA import flow * Remove unnecessary preload in SiteController * Remove unnecessary exception for legacy imports * Move API controller stats tests under PlausibleWeb * Test listing imports * Add test for explicit listener setup * Add tests for legacy flag state in UA importer * Add test for purging legacy import data * Add tests for `Sites.native_stats_start_date` * Test forgetting imports * Add `Stats.Clickhouse.imported_pageview_counts/1` and fix test flakiness * Show page view counts on imports list * Add tests for static imports and exports view * Adjust button look slightly * Use `case` instead of `cond` * Make feature flag customisable per site * Fix buttons and empty state styling * Add another import to seeds * Use JS confirm dialog instead of modal for deletion confirmations * Revert "Add support for opening LV modal from backend and closing from frontend" This reverts commit 260e6c753032b451542e24be9edc2118790b5a00. * Default `legacy` to false when inserting new import jobs * Drop `method` attribute from `button_link` and `unstyled_link` components
3 years ago
end