@@ -40,6 +40,9 @@ describe('External API v2 Dashboards - old format', () => {
4040 name : 'Test External Dashboard' ,
4141 tiles : [ makeExternalChart ( { sourceId } ) , makeExternalChart ( { sourceId } ) ] ,
4242 tags : TEST_TAGS ,
43+ savedQuery : null ,
44+ savedQueryLanguage : null ,
45+ savedFilterValues : [ ] ,
4346 ...overrides ,
4447 } ) ;
4548
@@ -57,6 +60,9 @@ describe('External API v2 Dashboards - old format', () => {
5760 ] ,
5861 tags : TEST_TAGS ,
5962 filters : [ ] ,
63+ savedQuery : null ,
64+ savedQueryLanguage : null ,
65+ savedFilterValues : [ ] ,
6066 ...overrides ,
6167 } ) ;
6268
@@ -280,6 +286,9 @@ describe('External API v2 Dashboards - old format', () => {
280286 ] ,
281287 tags : [ 'format-test' ] ,
282288 filters : [ ] ,
289+ savedQuery : null ,
290+ savedQueryLanguage : null ,
291+ savedFilterValues : [ ] ,
283292 } ,
284293 } ) ;
285294
@@ -362,6 +371,80 @@ describe('External API v2 Dashboards - old format', () => {
362371 expect ( dashboards [ 0 ] . tiles ) . toHaveLength ( 2 ) ;
363372 } ) ;
364373
374+ it ( 'should create a dashboard with saved query defaults' , async ( ) => {
375+ const mockDashboard = createMockDashboard ( traceSource . _id . toString ( ) , {
376+ savedQuery : "service.name = 'api'" ,
377+ savedQueryLanguage : 'sql' ,
378+ savedFilterValues : [
379+ {
380+ type : 'sql' ,
381+ condition : "ServiceName IN ('hdx-oss-dev-api')" ,
382+ } ,
383+ ] ,
384+ } ) ;
385+
386+ const response = await authRequest ( 'post' , BASE_URL )
387+ . send ( mockDashboard )
388+ . expect ( 200 ) ;
389+
390+ expect ( response . body . data . savedQuery ) . toBe ( "service.name = 'api'" ) ;
391+ expect ( response . body . data . savedQueryLanguage ) . toBe ( 'sql' ) ;
392+ expect ( response . body . data . savedFilterValues ) . toEqual ( [
393+ {
394+ type : 'sql' ,
395+ condition : "ServiceName IN ('hdx-oss-dev-api')" ,
396+ } ,
397+ ] ) ;
398+
399+ const dashboardInDb = await Dashboard . findById (
400+ response . body . data . id ,
401+ ) . lean ( ) ;
402+ expect ( dashboardInDb ?. savedQuery ) . toBe ( "service.name = 'api'" ) ;
403+ expect ( dashboardInDb ?. savedQueryLanguage ) . toBe ( 'sql' ) ;
404+ expect ( dashboardInDb ?. savedFilterValues ) . toEqual ( [
405+ {
406+ type : 'sql' ,
407+ condition : "ServiceName IN ('hdx-oss-dev-api')" ,
408+ } ,
409+ ] ) ;
410+ } ) ;
411+
412+ it ( 'should default savedQueryLanguage to lucene when savedQuery is provided without a language' , async ( ) => {
413+ const mockDashboard = omit (
414+ createMockDashboard ( traceSource . _id . toString ( ) , {
415+ savedQuery : "service.name = 'api'" ,
416+ } ) ,
417+ 'savedQueryLanguage' ,
418+ ) ;
419+
420+ const response = await authRequest ( 'post' , BASE_URL )
421+ . send ( mockDashboard )
422+ . expect ( 200 ) ;
423+
424+ expect ( response . body . data . savedQuery ) . toBe ( "service.name = 'api'" ) ;
425+ expect ( response . body . data . savedQueryLanguage ) . toBe ( 'lucene' ) ;
426+
427+ const dashboardInDb = await Dashboard . findById (
428+ response . body . data . id ,
429+ ) . lean ( ) ;
430+ expect ( dashboardInDb ?. savedQueryLanguage ) . toBe ( 'lucene' ) ;
431+ } ) ;
432+
433+ it ( 'should return 400 when savedQueryLanguage is null and savedQuery is provided' , async ( ) => {
434+ const mockDashboard = createMockDashboard ( traceSource . _id . toString ( ) , {
435+ savedQuery : "service.name = 'api'" ,
436+ savedQueryLanguage : null ,
437+ } ) ;
438+
439+ const response = await authRequest ( 'post' , BASE_URL )
440+ . send ( mockDashboard )
441+ . expect ( 400 ) ;
442+
443+ expect ( response . body . message ) . toContain (
444+ 'savedQueryLanguage cannot be null when savedQuery is provided' ,
445+ ) ;
446+ } ) ;
447+
365448 it ( 'can create all chart types' , async ( ) => {
366449 const dashboardWithAllCharts = {
367450 name : 'Test Dashboard with All Chart Types' ,
@@ -881,6 +964,56 @@ describe('External API v2 Dashboards - old format', () => {
881964 expect ( updatedDashboardInDb ?. tiles ) . toHaveLength ( 2 ) ;
882965 } ) ;
883966
967+ it ( 'should update and clear saved query defaults' , async ( ) => {
968+ const dashboard = await createTestDashboard ( {
969+ savedQuery : 'service:api' ,
970+ savedQueryLanguage : 'lucene' ,
971+ savedFilterValues : [
972+ {
973+ type : 'lucene' ,
974+ condition : 'env:prod' ,
975+ } ,
976+ ] ,
977+ } ) ;
978+ const updatedDashboard = createMockDashboardWithIds (
979+ traceSource . _id . toString ( ) ,
980+ ) ;
981+
982+ const response = await authRequest ( 'put' , `${ BASE_URL } /${ dashboard . _id } ` )
983+ . send ( updatedDashboard )
984+ . expect ( 200 ) ;
985+
986+ expect ( response . body . data . savedQuery ) . toBeNull ( ) ;
987+ expect ( response . body . data . savedQueryLanguage ) . toBeNull ( ) ;
988+ expect ( response . body . data . savedFilterValues ) . toEqual ( [ ] ) ;
989+
990+ const updatedDashboardInDb = await Dashboard . findById (
991+ dashboard . _id ,
992+ ) . lean ( ) ;
993+ expect ( updatedDashboardInDb ?. savedQuery ) . toBeNull ( ) ;
994+ expect ( updatedDashboardInDb ?. savedQueryLanguage ) . toBeNull ( ) ;
995+ expect ( updatedDashboardInDb ?. savedFilterValues ) . toEqual ( [ ] ) ;
996+ } ) ;
997+
998+ it ( 'should return 400 when savedQueryLanguage is null and savedQuery is provided on update' , async ( ) => {
999+ const dashboard = await createTestDashboard ( ) ;
1000+ const updatedDashboard = createMockDashboardWithIds (
1001+ traceSource . _id . toString ( ) ,
1002+ {
1003+ savedQuery : "service.name = 'api'" ,
1004+ savedQueryLanguage : null ,
1005+ } ,
1006+ ) ;
1007+
1008+ const response = await authRequest ( 'put' , `${ BASE_URL } /${ dashboard . _id } ` )
1009+ . send ( updatedDashboard )
1010+ . expect ( 400 ) ;
1011+
1012+ expect ( response . body . message ) . toContain (
1013+ 'savedQueryLanguage cannot be null when savedQuery is provided' ,
1014+ ) ;
1015+ } ) ;
1016+
8841017 it ( 'should update dashboard filters when provided' , async ( ) => {
8851018 const dashboard = await createTestDashboard ( ) ;
8861019 const filterId1 = new ObjectId ( ) . toString ( ) ;
@@ -1814,6 +1947,9 @@ describe('External API v2 Dashboards - new format', () => {
18141947 ] ,
18151948 tags : [ 'format-test' ] ,
18161949 filters : [ ] ,
1950+ savedQuery : null ,
1951+ savedQueryLanguage : null ,
1952+ savedFilterValues : [ ] ,
18171953 } ,
18181954 } ) ;
18191955
@@ -1924,6 +2060,21 @@ describe('External API v2 Dashboards - new format', () => {
19242060 expect ( dashboards [ 0 ] . tiles ) . toHaveLength ( 2 ) ;
19252061 } ) ;
19262062
2063+ it ( 'should return 400 when savedQueryLanguage is null and savedQuery is provided' , async ( ) => {
2064+ const mockDashboard = createMockDashboard ( traceSource . _id . toString ( ) , {
2065+ savedQuery : "service.name = 'api'" ,
2066+ savedQueryLanguage : null ,
2067+ } ) ;
2068+
2069+ const response = await authRequest ( 'post' , BASE_URL )
2070+ . send ( mockDashboard )
2071+ . expect ( 400 ) ;
2072+
2073+ expect ( response . body . message ) . toContain (
2074+ 'savedQueryLanguage cannot be null when savedQuery is provided' ,
2075+ ) ;
2076+ } ) ;
2077+
19272078 it ( 'can create all chart types' , async ( ) => {
19282079 const dashboardWithAllCharts = {
19292080 name : 'Test Dashboard with All Chart Types' ,
@@ -2306,6 +2457,25 @@ describe('External API v2 Dashboards - new format', () => {
23062457 expect ( updatedDashboardInDb ?. tiles ) . toHaveLength ( 2 ) ;
23072458 } ) ;
23082459
2460+ it ( 'should return 400 when savedQueryLanguage is null and savedQuery is provided on update' , async ( ) => {
2461+ const dashboard = await createTestDashboard ( ) ;
2462+ const updatedDashboard = createMockDashboardWithIds (
2463+ traceSource . _id . toString ( ) ,
2464+ {
2465+ savedQuery : "service.name = 'api'" ,
2466+ savedQueryLanguage : null ,
2467+ } ,
2468+ ) ;
2469+
2470+ const response = await authRequest ( 'put' , `${ BASE_URL } /${ dashboard . _id } ` )
2471+ . send ( updatedDashboard )
2472+ . expect ( 400 ) ;
2473+
2474+ expect ( response . body . message ) . toContain (
2475+ 'savedQueryLanguage cannot be null when savedQuery is provided' ,
2476+ ) ;
2477+ } ) ;
2478+
23092479 it ( 'should update dashboard filters when provided' , async ( ) => {
23102480 const dashboard = await createTestDashboard ( ) ;
23112481 const filterId1 = new ObjectId ( ) . toString ( ) ;
0 commit comments