From fdca817a7d3e26632859e34b1aba086a3ed1846b Mon Sep 17 00:00:00 2001 From: Karl-Aksel Puulmann Date: Fri, 18 Oct 2024 17:50:05 +0300 Subject: [PATCH] Clean up test code a bit --- test/plausible/ingestion/acquisition_test.exs | 63 ++++++++++--------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/test/plausible/ingestion/acquisition_test.exs b/test/plausible/ingestion/acquisition_test.exs index ec49dbac1..c764c0840 100644 --- a/test/plausible/ingestion/acquisition_test.exs +++ b/test/plausible/ingestion/acquisition_test.exs @@ -1,7 +1,7 @@ defmodule Plausible.Ingestion.EventTest do use Plausible.DataCase - setup_all _context do + setup_all do Plausible.DataMigration.AquisitionChannel.run(quiet: true) end @@ -69,35 +69,38 @@ defmodule Plausible.Ingestion.EventTest do for {test_data, index} <- Enum.with_index(@static_tests, 1) do @tag test_data: test_data test "static test #{index} - #{Jason.encode!(test_data)}", %{test_data: test_data} do - request = %{ - query_params: %{ - "utm_medium" => test_data[:utm_medium], - "utm_campaign" => test_data[:utm_campaign], - "utm_source" => test_data[:utm_source], - "gclid" => if(test_data[:click_id_source] == "Google", do: "123", else: nil), - "msclkid" => if(test_data[:click_id_source] == "Bing", do: "123", else: nil) - } - } - - channel = Plausible.Ingestion.Acquisition.get_channel(request, test_data[:referrer_source]) - assert channel == test_data.expected - end - - @tag test_data: test_data - test "clickhouse test #{index} - #{Jason.encode!(test_data)}", %{test_data: test_data} do - %{rows: [[channel]]} = - Plausible.IngestRepo.query!( - "SELECT acquisition_channel({$0:String}, {$1:String}, {$2:String}, {$3:String}, {$4:String})", - [ - test_data[:referrer_source] || "", - test_data[:utm_medium] || "", - test_data[:utm_campaign] || "", - test_data[:utm_source] || "", - test_data[:click_id_source] || "" - ] - ) - - assert channel == test_data.expected + assert reference_channel(test_data) == test_data.expected + assert clickhouse_channel(test_data) == test_data.expected end end + + def reference_channel(test_data) do + request = %{ + query_params: %{ + "utm_medium" => test_data[:utm_medium], + "utm_campaign" => test_data[:utm_campaign], + "utm_source" => test_data[:utm_source], + "gclid" => if(test_data[:click_id_source] == "Google", do: "123", else: nil), + "msclkid" => if(test_data[:click_id_source] == "Bing", do: "123", else: nil) + } + } + + Plausible.Ingestion.Acquisition.get_channel(request, test_data[:referrer_source]) + end + + def clickhouse_channel(test_data) do + %{rows: [[channel]]} = + Plausible.IngestRepo.query!( + "SELECT acquisition_channel({$0:String}, {$1:String}, {$2:String}, {$3:String}, {$4:String})", + [ + test_data[:referrer_source] || "", + test_data[:utm_medium] || "", + test_data[:utm_campaign] || "", + test_data[:utm_source] || "", + test_data[:click_id_source] || "" + ] + ) + + channel + end end