@@ -48,72 +48,17 @@ defmodule Plausible.Stats.QueryComparisonsTest do
4848 include: % QueryInclude { compare: :previous_period }
4949 } )
5050
51- assert % Stats.QueryResult { results: results } = Stats . query ( site , query )
51+ assert % Stats.QueryResult { results: results , comparison_results: comparison_results } =
52+ Stats . query ( site , query )
5253
5354 assert results == [
54- % {
55- dimensions: [ "2021-01-07" ] ,
56- metrics: [ 1 ] ,
57- comparison: % {
58- dimensions: [ "2020-12-31" ] ,
59- metrics: [ 0 ] ,
60- change: [ 100 ]
61- }
62- } ,
63- % {
64- dimensions: [ "2021-01-08" ] ,
65- metrics: [ 1 ] ,
66- comparison: % {
67- dimensions: [ "2021-01-01" ] ,
68- metrics: [ 2 ] ,
69- change: [ - 50 ]
70- }
71- } ,
72- % {
73- dimensions: [ "2021-01-09" ] ,
74- metrics: [ 0 ] ,
75- comparison: % {
76- dimensions: [ "2021-01-02" ] ,
77- metrics: [ 0 ] ,
78- change: [ 0 ]
79- }
80- } ,
81- % {
82- dimensions: [ "2021-01-10" ] ,
83- metrics: [ 0 ] ,
84- comparison: % {
85- dimensions: [ "2021-01-03" ] ,
86- metrics: [ 0 ] ,
87- change: [ 0 ]
88- }
89- } ,
90- % {
91- dimensions: [ "2021-01-11" ] ,
92- metrics: [ 0 ] ,
93- comparison: % {
94- dimensions: [ "2021-01-04" ] ,
95- metrics: [ 0 ] ,
96- change: [ 0 ]
97- }
98- } ,
99- % {
100- dimensions: [ "2021-01-12" ] ,
101- metrics: [ 0 ] ,
102- comparison: % {
103- dimensions: [ "2021-01-05" ] ,
104- metrics: [ 0 ] ,
105- change: [ 0 ]
106- }
107- } ,
108- % {
109- dimensions: [ "2021-01-13" ] ,
110- metrics: [ 0 ] ,
111- comparison: % {
112- dimensions: [ "2021-01-06" ] ,
113- metrics: [ 1 ] ,
114- change: [ - 100 ]
115- }
116- }
55+ % { dimensions: [ "2021-01-07" ] , metrics: [ 1 ] } ,
56+ % { dimensions: [ "2021-01-08" ] , metrics: [ 1 ] }
57+ ]
58+
59+ assert comparison_results == [
60+ % { dimensions: [ "2021-01-01" ] , metrics: [ 2 ] , change: [ - 50 ] } ,
61+ % { dimensions: [ "2021-01-06" ] , metrics: [ 1 ] , change: nil }
11762 ]
11863 end
11964
@@ -135,29 +80,21 @@ defmodule Plausible.Stats.QueryComparisonsTest do
13580
13681 query2 = Stats.Query . set_include ( query1 , :compare_match_day_of_week , true )
13782
138- assert % Stats.QueryResult { results: results1 } = Stats . query ( site , query1 )
139- assert % Stats.QueryResult { results: results2 } = Stats . query ( site , query2 )
83+ assert % Stats.QueryResult { meta: meta1 } = Stats . query ( site , query1 )
84+ assert % Stats.QueryResult { meta: meta2 } = Stats . query ( site , query2 )
14085
141- assert results1 == results2
86+ assert meta1 [ :time_labels ] == meta2 [ :time_labels ]
87+ assert meta1 [ :comparison_time_labels ] == meta2 [ :comparison_time_labels ]
14288
14389 expected_first_date = today |> Date . shift ( day: - 28 ) |> Date . to_iso8601 ( )
14490 expected_last_date = today |> Date . shift ( day: - 1 ) |> Date . to_iso8601 ( )
14591 expected_comparison_first_date = today |> Date . shift ( day: - 56 ) |> Date . to_iso8601 ( )
14692 expected_comparison_last_date = today |> Date . shift ( day: - 29 ) |> Date . to_iso8601 ( )
14793
148- assert % {
149- dimensions: [ actual_first_date ] ,
150- comparison: % {
151- dimensions: [ actual_comparison_first_date ]
152- }
153- } = List . first ( results1 )
154-
155- assert % {
156- dimensions: [ actual_last_date ] ,
157- comparison: % {
158- dimensions: [ actual_comparison_last_date ]
159- }
160- } = List . last ( results1 )
94+ actual_first_date = List . first ( meta1 [ :time_labels ] )
95+ actual_comparison_first_date = List . first ( meta1 [ :comparison_time_labels ] )
96+ actual_last_date = List . last ( meta1 [ :time_labels ] )
97+ actual_comparison_last_date = List . last ( meta1 [ :comparison_time_labels ] )
16198
16299 assert actual_first_date == expected_first_date
163100 assert actual_last_date == expected_last_date
@@ -189,40 +126,29 @@ defmodule Plausible.Stats.QueryComparisonsTest do
189126 now: ~U[ 2022-07-01 14:00:00Z]
190127 } )
191128
192- assert % Stats.QueryResult { results: results , meta: meta } = Stats . query ( site , query )
129+ assert % Stats.QueryResult {
130+ results: results ,
131+ comparison_results: comparison_results ,
132+ meta: meta
133+ } = Stats . query ( site , query )
193134
194135 time_labels = meta [ :time_labels ]
195136
196137 assert "2022-04-01" = List . first ( time_labels )
197138 assert "2022-04-05" = Enum . at ( time_labels , 4 )
198139 assert "2022-06-30" = List . last ( time_labels )
199140
200- assert % {
201- dimensions: [ "2022-04-01" ] ,
202- metrics: [ 1 ] ,
203- comparison: % {
204- dimensions: [ "2021-04-01" ] ,
205- metrics: [ 2 ]
206- }
207- } = Enum . find ( results , & ( & 1 [ :dimensions ] == [ "2022-04-01" ] ) )
208-
209- assert % {
210- dimensions: [ "2022-04-05" ] ,
211- metrics: [ 1 ] ,
212- comparison: % {
213- dimensions: [ "2021-04-05" ] ,
214- metrics: [ 2 ]
215- }
216- } = Enum . find ( results , & ( & 1 [ :dimensions ] == [ "2022-04-05" ] ) )
217-
218- assert % {
219- dimensions: [ "2022-06-30" ] ,
220- metrics: [ 1 ] ,
221- comparison: % {
222- dimensions: [ "2021-06-30" ] ,
223- metrics: [ 1 ]
224- }
225- } = Enum . find ( results , & ( & 1 [ :dimensions ] == [ "2022-06-30" ] ) )
141+ assert results == [
142+ % { dimensions: [ "2022-04-01" ] , metrics: [ 1 ] } ,
143+ % { dimensions: [ "2022-04-05" ] , metrics: [ 1 ] } ,
144+ % { dimensions: [ "2022-06-30" ] , metrics: [ 1 ] }
145+ ]
146+
147+ assert comparison_results == [
148+ % { dimensions: [ "2021-04-01" ] , metrics: [ 2 ] , change: [ - 50 ] } ,
149+ % { dimensions: [ "2021-04-05" ] , metrics: [ 2 ] , change: [ - 50 ] } ,
150+ % { dimensions: [ "2021-06-30" ] , metrics: [ 1 ] , change: [ 0 ] }
151+ ]
226152 end
227153
228154 test "dimensional comparison with low limit" , % { site: site } do
0 commit comments