This repository was archived by the owner on Jul 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathparam-input-test.js
More file actions
409 lines (381 loc) · 13.4 KB
/
param-input-test.js
File metadata and controls
409 lines (381 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { click, currentURL, fillIn, visit } from "@ember/test-helpers";
import { test } from "qunit";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import selectKit from "discourse/tests/helpers/select-kit-helper";
acceptance("Data Explorer Plugin | Param Input", function (needs) {
needs.user();
needs.settings({ data_explorer_enabled: true });
needs.pretender((server, helper) => {
server.get("/admin/plugins/explorer/groups.json", () => {
return helper.response([]);
});
server.get("/admin/plugins/explorer/schema.json", () => {
return helper.response({});
});
server.get("/admin/plugins/explorer/queries", () => {
return helper.response({
queries: [
{
id: -6,
name: "Top 100 Likers",
description:
"returns the top 100 likers for a given monthly period ordered by like_count. It accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",
username: "system",
group_ids: [],
last_run_at: "2021-02-11T08:29:59.337Z",
user_id: -1,
},
{
id: -7,
name: "Invalid Query",
description: "",
username: "bianca",
group_ids: [],
last_run_at: "2022-01-14T16:47:34.244Z",
user_id: 1,
},
{
id: 3,
name: "Params test",
description: "test for params.",
username: "system",
group_ids: [41],
last_run_at: "2021-02-11T08:29:59.337Z",
user_id: -1,
},
],
});
});
server.get("/admin/plugins/explorer/queries/-6", () => {
return helper.response({
query: {
id: -6,
sql: "-- [params]\n-- int :months_ago = 1\n\nWITH query_period AS (\n SELECT\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' as period_start,\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' as period_end\n )\n\n SELECT\n ua.user_id,\n count(1) AS like_count\n FROM user_actions ua\n INNER JOIN query_period qp\n ON ua.created_at >= qp.period_start\n AND ua.created_at <= qp.period_end\n WHERE ua.action_type = 1\n GROUP BY ua.user_id\n ORDER BY like_count DESC\n LIMIT 100\n",
name: "Top 100 Likers",
description:
"returns the top 100 likers for a given monthly period ordered by like_count. It accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",
param_info: [
{
identifier: "months_ago",
type: "int",
default: "1",
nullable: false,
},
],
created_at: "2021-02-02T12:21:11.449Z",
username: "system",
group_ids: [],
last_run_at: "2021-02-11T08:29:59.337Z",
hidden: false,
user_id: -1,
},
});
});
server.put("/admin/plugins/explorer/queries/-6", () => {
return helper.response({
success: true,
errors: [],
duration: 27.5,
result_count: 2,
columns: ["user_id", "like_count"],
default_limit: 1000,
relations: {
user: [
{
id: -2,
username: "discobot",
name: null,
avatar_template: "/user_avatar/localhost/discobot/{size}/2_2.png",
},
{
id: 2,
username: "andrey1",
name: null,
avatar_template:
"/letter_avatar_proxy/v4/letter/a/c0e974/{size}.png",
},
],
},
colrender: {
0: "user",
},
rows: [
[-2, 2],
[2, 2],
],
});
});
server.post("/admin/plugins/explorer/queries/-6/run", () => {
return helper.response({
success: true,
errors: [],
duration: 27.5,
result_count: 2,
params: { months_ago: "1" },
columns: ["user_id", "like_count"],
default_limit: 1000,
relations: {
user: [
{
id: -2,
username: "discobot",
name: null,
avatar_template: "/user_avatar/localhost/discobot/{size}/2_2.png",
},
{
id: 2,
username: "andrey1",
name: null,
avatar_template:
"/letter_avatar_proxy/v4/letter/a/c0e974/{size}.png",
},
],
},
colrender: {
0: "user",
},
rows: [
[-2, 2],
[2, 2],
],
});
});
server.get("/g/discourse/reports/-8", () => {
return helper.response({
query: {
id: -8,
sql: "-- [params]\n-- int :months_ago = 1\n\nWITH query_period AS (\n SELECT\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' as period_start,\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' as period_end\n )\n\n SELECT\n ua.user_id,\n count(1) AS like_count\n FROM user_actions ua\n INNER JOIN query_period qp\n ON ua.created_at >= qp.period_start\n AND ua.created_at <= qp.period_end\n WHERE ua.action_type = 1\n GROUP BY ua.user_id\n ORDER BY like_count DESC\n LIMIT 100\n",
name: "Top 100 Likers Report",
description:
"returns the top 100 likers for a given monthly period ordered by like_count. It accepts a ‘months_ago’ parameter, defaults to 1 to give results for the last calendar month.",
param_info: [
{
identifier: "months_ago",
type: "int",
default: "1",
nullable: false,
},
],
created_at: "2021-02-02T12:21:11.449Z",
username: "system",
group_ids: [41],
last_run_at: "2021-02-11T08:29:59.337Z",
hidden: false,
user_id: -1,
},
});
});
server.get("/admin/plugins/explorer/queries/-7", () => {
return helper.response({
query: {
id: -7,
sql: "-- [params]\n-- user_id :user\n\nSELECT :user_id\n\n",
name: "Invalid Query",
description: "",
param_info: [
{
identifier: "user",
type: "user_id",
default: null,
nullable: false,
},
],
created_at: "2022-01-14T16:40:05.458Z",
username: "bianca",
group_ids: [],
last_run_at: "2022-01-14T16:47:34.244Z",
hidden: false,
user_id: 1,
},
});
});
server.post("/admin/plugins/explorer/queries/-7/run", () => {
return helper.response({
success: true,
errors: [],
duration: 27.5,
params: { user_id: "null" },
columns: ["user_id"],
default_limit: 1000,
relations: {
user: [
{
id: 2,
username: "andrey1",
name: null,
avatar_template:
"/letter_avatar_proxy/v4/letter/a/c0e974/{size}.png",
},
],
},
colrender: {
0: "user",
},
rows: [],
});
});
server.post("/g/discourse/reports/-8/run", () => {
return helper.response({
success: true,
errors: [],
duration: 27.5,
result_count: 2,
params: { months_ago: "1" },
columns: ["user_id", "like_count"],
default_limit: 1000,
relations: {
user: [
{
id: -2,
username: "discobot",
name: null,
avatar_template: "/user_avatar/localhost/discobot/{size}/2_2.png",
},
{
id: 2,
username: "andrey1",
name: null,
avatar_template:
"/letter_avatar_proxy/v4/letter/a/c0e974/{size}.png",
},
],
},
colrender: {
0: "user",
},
rows: [
[-2, 2],
[2, 2],
],
});
});
server.get("/admin/plugins/explorer/queries/3", () => {
return helper.response({
query: {
id: 3,
sql: "SELECT 1",
name: "Params test",
description: "test for params.",
param_info: [],
created_at: "2021-02-02T12:21:11.449Z",
username: "system",
group_ids: [41],
last_run_at: "2021-02-11T08:29:59.337Z",
hidden: false,
user_id: -1,
},
});
});
server.put("/admin/plugins/explorer/queries/3", () => {
return helper.response({
query: {
id: 3,
sql: "-- [params]\n-- int :months_ago = 1\n\nSELECT 1",
name: "Params test",
description: "test for params.",
param_info: [
{
identifier: "months_ago",
type: "int",
default: "1",
nullable: false,
},
],
created_at: "2021-02-02T12:21:11.449Z",
username: "system",
group_ids: [41],
last_run_at: "2021-02-11T08:29:59.337Z",
hidden: false,
user_id: -1,
},
});
});
server.get("/admin/plugins/explorer/queries/4", () => {
return helper.response({
query: {
id: 4,
sql: "-- [params]\n-- null category_id :category\n\nSELECT 1",
name: "Params test - category_id chooser",
description: "Test for category_id param.",
param_info: [
{
identifier: "category",
type: "category_id",
default: null,
nullable: true,
},
],
created_at: "2025-06-03T09:05:59.337Z",
username: "system",
group_ids: [],
last_run_at: "2025-06-03T09:05:59.337Z",
hidden: false,
category_id: null,
},
});
});
server.post("/admin/plugins/explorer/queries/4/run", () => {
return helper.response({});
});
});
test("puts params for the query into the url", async function (assert) {
await visit("/admin/plugins/explorer/queries/-6");
const monthsAgoValue = "2";
await fillIn(".query-params input", monthsAgoValue);
await click("form.query-run button");
const searchParams = new URLSearchParams(currentURL().split("?")[1]);
const monthsAgoParam = JSON.parse(searchParams.get("params")).months_ago;
assert.strictEqual(monthsAgoParam, monthsAgoValue);
});
test("puts params for the query into the url for group reports", async function (assert) {
await visit("/g/discourse/reports/-8");
const monthsAgoValue = "2";
await fillIn(".query-params input", monthsAgoValue);
await click("form.query-run button");
const searchParams = new URLSearchParams(currentURL().split("?")[1]);
const monthsAgoParam = JSON.parse(searchParams.get("params")).months_ago;
assert.strictEqual(monthsAgoParam, monthsAgoValue);
});
test("loads the page if one of the parameter is null", async function (assert) {
await visit('/admin/plugins/explorer/queries/-7?params={"user":null}');
assert.dom(".query-params .user-chooser").exists();
assert.dom(".query-run .btn.btn-primary").exists();
});
test("loads the page if one of the parameter is null for group reports", async function (assert) {
await visit('/g/discourse/reports/-8?params={"months_ago":null}');
assert.dom(".query-params input").exists();
assert.dom(".query-run .btn.btn-primary").exists();
});
test("applies params when running a report", async function (assert) {
await visit("/g/discourse/reports/-8");
const monthsAgoValue = "2";
await fillIn(".query-params input", monthsAgoValue);
await click("form.query-run button");
assert.dom(".query-params input").hasValue(monthsAgoValue);
});
test("creates input boxes if has parameters when save", async function (assert) {
await visit("/admin/plugins/explorer/queries/3");
assert.dom(".query-params input").doesNotExist();
await click(".query-edit .btn-edit-query");
await fillIn(
".query-editor .ace_text-input",
"-- [params]\n-- int :months_ago = 1\n\nSELECT 1"
);
await click(".query-editor .ace_text-input"); // enables `Save Changes` button
await click(".query-edit .btn-save-query");
assert.dom(".query-params input").exists();
});
test("nullable category_id param: puts params for the query into the url", async function (assert) {
const selectedCategory = { id: "6", name: "support" };
const catChooser = selectKit(".category-chooser");
await visit("/admin/plugins/explorer/queries/4");
assert.strictEqual(catChooser.header().value(), null);
await catChooser.expand();
await catChooser.selectRowByIndex(0);
assert.strictEqual(catChooser.header().label(), selectedCategory.name);
await click("form.query-run button");
const searchParams = new URLSearchParams(currentURL().split("?")[1]);
const categoryIdParam = JSON.parse(searchParams.get("params")).category;
assert.strictEqual(categoryIdParam, selectedCategory.id);
});
});