Leon4gr45 commited on
Commit
d7ea091
·
verified ·
1 Parent(s): 573b432

Delete test/plausible_web/controllers/api/stats_controller/countries_test.exs with huggingface_hub

Browse files
test/plausible_web/controllers/api/stats_controller/countries_test.exs DELETED
@@ -1,511 +0,0 @@
1
- defmodule PlausibleWeb.Api.StatsController.CountriesTest do
2
- use PlausibleWeb.ConnCase
3
-
4
- defp query_countries(conn, site, opts) do
5
- always_on_filters = ["is_not", "visit:country", [<<0, 0>>, "ZZ"]]
6
-
7
- params = %{
8
- "dimensions" => Keyword.get(opts, :dimensions, ["visit:country", "visit:country_name"]),
9
- "date_range" => Keyword.get(opts, :date_range, "all"),
10
- "filters" => [
11
- always_on_filters
12
- | Keyword.get(opts, :filters, [])
13
- ],
14
- "metrics" => Keyword.get(opts, :metrics, ["visitors", "percentage"]),
15
- "include" => Keyword.get(opts, :include, nil),
16
- "pagination" => Keyword.get(opts, :pagination, nil),
17
- "order_by" => Keyword.get(opts, :order_by, nil)
18
- }
19
-
20
- conn
21
- |> post("/api/stats/#{site.domain}/query", params)
22
- |> json_response(200)
23
- end
24
-
25
- describe "querying for countries with POST /query" do
26
- setup [:create_user, :log_in, :create_site, :create_legacy_site_import]
27
-
28
- test "returns top countries by new visitors", %{conn: conn, site: site} do
29
- populate_stats(site, [
30
- build(:pageview, country_code: "EE"),
31
- build(:pageview, country_code: "EE"),
32
- build(:pageview, country_code: "GB"),
33
- build(:imported_locations, country: "EE"),
34
- build(:imported_locations, country: "GB"),
35
- build(:imported_visitors, visitors: 2)
36
- ])
37
-
38
- response1 = query_countries(conn, site, date_range: "day")
39
-
40
- assert response1["results"] == [
41
- %{"dimensions" => ["EE", "Estonia"], "metrics" => [2, 66.67]},
42
- %{"dimensions" => ["GB", "United Kingdom"], "metrics" => [1, 33.33]}
43
- ]
44
-
45
- response2 =
46
- query_countries(conn, site, date_range: "day", include: %{"imports" => true})
47
-
48
- assert response2["results"] == [
49
- %{"dimensions" => ["EE", "Estonia"], "metrics" => [3, 60.0]},
50
- %{"dimensions" => ["GB", "United Kingdom"], "metrics" => [2, 40.0]}
51
- ]
52
- end
53
-
54
- test "ignores unknown country code ZZ", %{conn: conn, site: site} do
55
- populate_stats(site, [
56
- build(:pageview, country_code: "ZZ"),
57
- build(:imported_locations, country: "ZZ")
58
- ])
59
-
60
- response =
61
- query_countries(conn, site, date_range: "day", include: %{"imports" => true})
62
-
63
- assert response["results"] == []
64
- end
65
-
66
- test "includes anonymous VPN country code A1", %{conn: conn, site: site} do
67
- populate_stats(site, [
68
- build(:pageview, country_code: "A1"),
69
- build(:pageview, country_code: "A1"),
70
- build(:pageview, country_code: "EE")
71
- ])
72
-
73
- response =
74
- query_countries(conn, site,
75
- date_range: "day",
76
- order_by: [["visitors", "desc"]]
77
- )
78
-
79
- assert response["results"] == [
80
- %{"dimensions" => ["A1", "Anonymous VPN Service"], "metrics" => [2, 66.67]},
81
- %{"dimensions" => ["EE", "Estonia"], "metrics" => [1, 33.33]}
82
- ]
83
- end
84
-
85
- test "searching for 'Anonymous' returns VPN visitors", %{
86
- conn: conn,
87
- site: site
88
- } do
89
- populate_stats(site, [
90
- build(:pageview, country_code: "A1"),
91
- build(:pageview, country_code: "A1"),
92
- build(:pageview, country_code: "EE"),
93
- build(:pageview, country_code: "GB")
94
- ])
95
-
96
- response =
97
- query_countries(conn, site,
98
- date_range: "day",
99
- filters: [["contains", "visit:country_name", ["Anonymous"]]]
100
- )
101
-
102
- assert response["results"] == [
103
- %{"dimensions" => ["A1", "Anonymous VPN Service"], "metrics" => [2, 100.0]}
104
- ]
105
- end
106
-
107
- test "calculates conversion_rate when filtering for goal", %{conn: conn, site: site} do
108
- populate_stats(site, [
109
- build(:pageview,
110
- user_id: 1,
111
- country_code: "EE"
112
- ),
113
- build(:event, user_id: 1, name: "Signup"),
114
- build(:pageview,
115
- user_id: 2,
116
- country_code: "EE"
117
- ),
118
- build(:pageview,
119
- user_id: 3,
120
- country_code: "GB"
121
- ),
122
- build(:event, user_id: 3, name: "Signup")
123
- ])
124
-
125
- insert(:goal, site: site, event_name: "Signup")
126
-
127
- response =
128
- query_countries(conn, site,
129
- date_range: "day",
130
- filters: [["is", "event:goal", ["Signup"]]],
131
- metrics: ["visitors", "total_visitors", "group_conversion_rate"],
132
- order_by: [["visit:country", "asc"]]
133
- )
134
-
135
- assert response["results"] == [
136
- %{"dimensions" => ["EE", "Estonia"], "metrics" => [1, 2, 50.0]},
137
- %{"dimensions" => ["GB", "United Kingdom"], "metrics" => [1, 1, 100.0]}
138
- ]
139
- end
140
-
141
- test "handles conversion_rate sort directive", %{conn: conn, site: site} do
142
- populate_stats(site, [
143
- build(:pageview,
144
- user_id: 1,
145
- country_code: "EE"
146
- ),
147
- build(:event, user_id: 1, name: "Signup"),
148
- build(:pageview,
149
- user_id: 2,
150
- country_code: "EE"
151
- ),
152
- build(:pageview,
153
- user_id: 3,
154
- country_code: "GB"
155
- ),
156
- build(:event, user_id: 3, name: "Signup")
157
- ])
158
-
159
- insert(:goal, site: site, event_name: "Signup")
160
-
161
- response =
162
- query_countries(conn, site,
163
- date_range: "day",
164
- filters: [["is", "event:goal", ["Signup"]]],
165
- metrics: ["visitors", "total_visitors", "group_conversion_rate"],
166
- order_by: [["group_conversion_rate", "desc"]]
167
- )
168
-
169
- assert response["results"] == [
170
- %{"dimensions" => ["GB", "United Kingdom"], "metrics" => [1, 1, 100.0]},
171
- %{"dimensions" => ["EE", "Estonia"], "metrics" => [1, 2, 50.0]}
172
- ]
173
- end
174
-
175
- test "returns top countries with :is filter on custom pageview props", %{
176
- conn: conn,
177
- site: site
178
- } do
179
- populate_stats(site, [
180
- build(:pageview,
181
- user_id: 123,
182
- country_code: "EE"
183
- ),
184
- build(:pageview,
185
- user_id: 123,
186
- country_code: "EE",
187
- "meta.key": ["author"],
188
- "meta.value": ["John Doe"]
189
- ),
190
- build(:pageview,
191
- country_code: "GB",
192
- "meta.key": ["author"],
193
- "meta.value": ["other"]
194
- ),
195
- build(:pageview,
196
- country_code: "US"
197
- )
198
- ])
199
-
200
- response =
201
- query_countries(conn, site,
202
- date_range: "day",
203
- filters: [["is", "event:props:author", ["John Doe"]]]
204
- )
205
-
206
- assert response["results"] == [
207
- %{"dimensions" => ["EE", "Estonia"], "metrics" => [1, 100.0]}
208
- ]
209
- end
210
-
211
- test "returns top countries with :is_not filter on custom pageview props", %{
212
- conn: conn,
213
- site: site
214
- } do
215
- populate_stats(site, [
216
- build(:pageview,
217
- user_id: 123,
218
- country_code: "EE",
219
- "meta.key": ["author"],
220
- "meta.value": ["John Doe"]
221
- ),
222
- build(:pageview,
223
- user_id: 123,
224
- country_code: "EE",
225
- "meta.key": ["author"],
226
- "meta.value": ["John Doe"]
227
- ),
228
- build(:pageview,
229
- country_code: "GB",
230
- "meta.key": ["author"],
231
- "meta.value": ["other"]
232
- ),
233
- build(:pageview,
234
- country_code: "GB"
235
- )
236
- ])
237
-
238
- response =
239
- query_countries(conn, site,
240
- date_range: "day",
241
- filters: [["is_not", "event:props:author", ["John Doe"]]]
242
- )
243
-
244
- assert response["results"] == [
245
- %{"dimensions" => ["GB", "United Kingdom"], "metrics" => [2, 100.0]}
246
- ]
247
- end
248
-
249
- test "returns top countries with :is (none) filter on custom pageview props", %{
250
- conn: conn,
251
- site: site
252
- } do
253
- populate_stats(site, [
254
- build(:pageview,
255
- country_code: "EE",
256
- "meta.key": ["author"],
257
- "meta.value": ["John Doe"]
258
- ),
259
- build(:pageview,
260
- country_code: "GB",
261
- "meta.key": ["logged_in"],
262
- "meta.value": ["true"]
263
- ),
264
- build(:pageview,
265
- country_code: "GB"
266
- )
267
- ])
268
-
269
- response =
270
- query_countries(conn, site,
271
- date_range: "day",
272
- filters: [["is", "event:props:author", ["(none)"]]]
273
- )
274
-
275
- assert response["results"] == [
276
- %{"dimensions" => ["GB", "United Kingdom"], "metrics" => [2, 100.0]}
277
- ]
278
- end
279
-
280
- test "returns top countries with :is_not (none) filter on custom pageview props", %{
281
- conn: conn,
282
- site: site
283
- } do
284
- populate_stats(site, [
285
- build(:pageview,
286
- country_code: "EE",
287
- "meta.key": ["author"],
288
- "meta.value": ["John Doe"]
289
- ),
290
- build(:pageview,
291
- country_code: "EE",
292
- "meta.key": ["author"],
293
- "meta.value": [""]
294
- ),
295
- build(:pageview,
296
- country_code: "GB",
297
- "meta.key": ["logged_in"],
298
- "meta.value": ["true"]
299
- ),
300
- build(:pageview,
301
- country_code: "GB"
302
- )
303
- ])
304
-
305
- response =
306
- query_countries(conn, site,
307
- date_range: "day",
308
- filters: [["is_not", "event:props:author", ["(none)"]]]
309
- )
310
-
311
- assert response["results"] == [
312
- %{"dimensions" => ["EE", "Estonia"], "metrics" => [2, 100.0]}
313
- ]
314
- end
315
-
316
- test "when list is filtered by country returns one country only", %{conn: conn, site: site} do
317
- populate_stats(site, [
318
- build(:pageview, country_code: "EE"),
319
- build(:pageview, country_code: "EE"),
320
- build(:pageview, country_code: "GB")
321
- ])
322
-
323
- response =
324
- query_countries(conn, site,
325
- date_range: "day",
326
- filters: [["is", "visit:country", ["GB"]]]
327
- )
328
-
329
- assert response["results"] == [
330
- %{"dimensions" => ["GB", "United Kingdom"], "metrics" => [1, 100.0]}
331
- ]
332
- end
333
-
334
- test "handles multiple segment filters", %{conn: conn, site: site, user: user} do
335
- %{id: segment_alfa} =
336
- insert(:segment,
337
- site_id: site.id,
338
- owner_id: user.id,
339
- name: "Ireland and Britain (excl London)",
340
- type: :site,
341
- segment_data: %{
342
- "filters" => [
343
- ["is", "visit:country", ["IE", "GB"]],
344
- ["is_not", "visit:city", [2_643_743]]
345
- ]
346
- }
347
- )
348
-
349
- %{id: segment_beta} =
350
- insert(:segment,
351
- site_id: site.id,
352
- owner_id: user.id,
353
- name: "Entered on root or blog",
354
- type: :personal,
355
- segment_data: %{
356
- "filters" => [
357
- ["is", "visit:entry_page", ["/", "/blog"]]
358
- ]
359
- }
360
- )
361
-
362
- populate_stats(site, [
363
- build(:pageview, country_code: "EE"),
364
- build(:pageview,
365
- country_code: "GB",
366
- # London
367
- city_geoname_id: 2_643_743
368
- ),
369
- build(:pageview,
370
- country_code: "GB",
371
- # London
372
- city_geoname_id: 2_643_743
373
- ),
374
- build(:pageview, country_code: "GB"),
375
- build(:pageview, country_code: "IE", pathname: "/other"),
376
- build(:pageview, country_code: "IE")
377
- ])
378
-
379
- response =
380
- query_countries(conn, site,
381
- date_range: "day",
382
- filters: [
383
- ["is", "segment", [segment_alfa]],
384
- ["is", "segment", [segment_beta]]
385
- ],
386
- order_by: [["visitors", "desc"], ["visit:country", "asc"]]
387
- )
388
-
389
- assert response["results"] == [
390
- %{"dimensions" => ["GB", "United Kingdom"], "metrics" => [1, 50.0]},
391
- %{"dimensions" => ["IE", "Ireland"], "metrics" => [1, 50.0]}
392
- ]
393
- end
394
-
395
- @tag :ee_only
396
- test "return revenue metrics for countries breakdown", %{conn: conn, site: site} do
397
- populate_stats(site, [
398
- build(:pageview,
399
- user_id: 1,
400
- country_code: "EE"
401
- ),
402
- build(:event,
403
- name: "Payment",
404
- user_id: 1,
405
- revenue_reporting_amount: Decimal.new("1000"),
406
- revenue_reporting_currency: "USD"
407
- ),
408
- build(:pageview,
409
- user_id: 2,
410
- country_code: "EE"
411
- ),
412
- build(:event,
413
- name: "Payment",
414
- user_id: 2,
415
- revenue_reporting_amount: Decimal.new("2000"),
416
- revenue_reporting_currency: "USD"
417
- ),
418
- build(:pageview,
419
- user_id: 3,
420
- country_code: "EE"
421
- ),
422
- build(:pageview,
423
- user_id: 4,
424
- country_code: "GB"
425
- ),
426
- build(:event,
427
- name: "Payment",
428
- user_id: 4,
429
- revenue_reporting_amount: Decimal.new("500"),
430
- revenue_reporting_currency: "USD"
431
- ),
432
- build(:pageview,
433
- user_id: 5,
434
- country_code: "GB"
435
- ),
436
- build(:pageview, user_id: 6),
437
- build(:event,
438
- name: "Payment",
439
- user_id: 6,
440
- revenue_reporting_amount: Decimal.new("600"),
441
- revenue_reporting_currency: "USD"
442
- ),
443
- build(:pageview, user_id: 7),
444
- build(:event,
445
- name: "Payment",
446
- user_id: 7,
447
- revenue_reporting_amount: nil
448
- )
449
- ])
450
-
451
- insert(:goal, %{site: site, event_name: "Payment", currency: :USD})
452
-
453
- response =
454
- query_countries(conn, site,
455
- date_range: "day",
456
- filters: [["is", "event:goal", ["Payment"]]],
457
- metrics: [
458
- "visitors",
459
- "total_visitors",
460
- "group_conversion_rate",
461
- "average_revenue",
462
- "total_revenue"
463
- ],
464
- order_by: [["visitors", "desc"], ["visit:country", "asc"]]
465
- )
466
-
467
- assert response["results"] == [
468
- %{
469
- "dimensions" => ["EE", "Estonia"],
470
- "metrics" => [
471
- 2,
472
- 3,
473
- 66.67,
474
- %{
475
- "currency" => "USD",
476
- "long" => "$1,500.00",
477
- "short" => "$1.5K",
478
- "value" => 1500.0
479
- },
480
- %{
481
- "currency" => "USD",
482
- "long" => "$3,000.00",
483
- "short" => "$3.0K",
484
- "value" => 3000.0
485
- }
486
- ]
487
- },
488
- %{
489
- "dimensions" => ["GB", "United Kingdom"],
490
- "metrics" => [
491
- 1,
492
- 2,
493
- 50.0,
494
- %{
495
- "currency" => "USD",
496
- "long" => "$500.00",
497
- "short" => "$500.0",
498
- "value" => 500.0
499
- },
500
- %{
501
- "currency" => "USD",
502
- "long" => "$500.00",
503
- "short" => "$500.0",
504
- "value" => 500.0
505
- }
506
- ]
507
- }
508
- ]
509
- end
510
- end
511
- end