|
10 | 10 | tags: tags ) }
|
11 | 11 |
|
12 | 12 | let(:source_dashboard_id) { 1 }
|
13 |
| - let(:source_dashboard) { double('source_dashboard', id: source_dashboard_id, url: "http://superset-host.com/superset/dashboard/#{source_dashboard_id}", json_metadata: json_metadata) } |
| 13 | + let(:source_dashboard) { double('source_dashboard', id: source_dashboard_id, url: "http://superset-host.com/superset/dashboard/#{source_dashboard_id}", json_metadata: json_metadata_initial_settings) } |
14 | 14 |
|
15 | 15 | let(:target_schema) { 'schema_two' }
|
16 | 16 | let(:target_database_id) { 6 }
|
|
23 | 23 | {id: source_dataset_1, datasource_name: "Dataset 1", schema: "schema_one", database: {id: 9, name: "db_9", backend: "postgresql"}, sql: 'SELECT * FROM table1'},
|
24 | 24 | {id: source_dataset_2, datasource_name: "Dataset 2", schema: "schema_one", database: {id: 9, name: "db_9", backend: "postgresql"}, sql: 'SELECT * FROM table1'}
|
25 | 25 | ]}
|
26 |
| - let(:json_metadata) { { 'native_filter_configuration' => [{ 'targets' => [{ 'datasetId' => 101 }]}]} } |
27 |
| - let(:new_json_metadata) { { 'native_filter_configuration' => [{ 'targets' => [{ 'datasetId' => 201 }]}]} } |
| 26 | + let(:source_chart_1) { 1001 } |
| 27 | + let(:source_chart_2) { 1002 } |
| 28 | + |
28 | 29 | let(:source_dashboard_filter_dataset_ids) { [source_dataset_1, source_dataset_2] }
|
29 | 30 | let(:source_dashboard_dataset_ids) {[source_dataset_1, source_dataset_2 ]}
|
30 | 31 | let(:dataset_duplication_tracker) { [{ source_dataset_id: 101, new_dataset_id: 201 }] }
|
31 | 32 |
|
32 | 33 | let(:new_dashboard_id) { 2 }
|
33 |
| - let(:new_dashboard) { double('new_dashboard', id: new_dashboard_id, url: "http://superset-host.com/superset/dashboard/#{new_dashboard_id}", json_metadata: json_metadata) } |
| 34 | + let(:new_dashboard) do |
| 35 | + OpenStruct.new( |
| 36 | + id: new_dashboard_id, |
| 37 | + url: "http://superset-host.com/superset/dashboard/#{new_dashboard_id}", |
| 38 | + result: { 'json_metadata' => json_metadata_initial_settings.to_json }, |
| 39 | + json_metadata: json_metadata_initial_settings ) # mock the new_dashboard_json_metadata method |
| 40 | + end |
| 41 | + # { double('new_dashboard', id: new_dashboard_id, url: "http://superset-host.com/superset/dashboard/#{new_dashboard_id}", json_metadata: json_metadata_initial_settings) } |
34 | 42 |
|
35 | 43 | let(:new_dataset_1) { 201 }
|
36 | 44 | let(:new_dataset_2) { 202 }
|
37 | 45 |
|
38 |
| - let(:new_chart_1) { 3001 } |
39 |
| - let(:new_chart_2) { 3002 } |
| 46 | + let(:new_chart_1) { 2001 } |
| 47 | + let(:new_chart_2) { 2002 } |
40 | 48 | let(:existing_target_datasets_list) {[]}
|
41 | 49 |
|
| 50 | + # initial json metadata settings will be copied to the new dashboard and requires updating |
| 51 | + let(:json_metadata_initial_settings) do |
| 52 | + { |
| 53 | + "chart_configuration"=> |
| 54 | + { "#{source_chart_1}"=>{"id"=>source_chart_1, "crossFilters"=>{"scope"=>"global", "chartsInScope"=>[source_chart_2]}}, |
| 55 | + "#{source_chart_2}"=>{"id"=>source_chart_2, "crossFilters"=>{"scope"=>"global", "chartsInScope"=>[source_chart_1]}}}, |
| 56 | + "global_chart_configuration"=>{"scope"=>{"rootPath"=>["ROOT_ID"], "excluded"=>[]}, "chartsInScope"=>[source_chart_1, source_chart_2]}, |
| 57 | + "native_filter_configuration"=> |
| 58 | + [ |
| 59 | + {"id"=>"NATIVE_FILTER-k-UxewZyI", |
| 60 | + "name"=>"JobTitleLimit5", |
| 61 | + "targets"=>[{"datasetId"=>source_dataset_1, "column"=>{"name"=>"job_title"}}], |
| 62 | + "chartsInScope"=>[source_chart_1, source_chart_2]}, |
| 63 | + {"id"=>"NATIVE_FILTER-eoi3FEQ1C", |
| 64 | + "name"=>"Count", |
| 65 | + "filterType"=>"filter_select", |
| 66 | + "targets"=>[{"datasetId"=>source_dataset_2, "column"=>{"name"=>"count"}}], |
| 67 | + "chartsInScope"=>[source_chart_1, source_chart_2]} |
| 68 | + ] |
| 69 | + } |
| 70 | + end |
| 71 | + |
| 72 | + # expected json metadata settings after the new dashboard is created and json is updated |
| 73 | + let(:json_metadata_updated_settings) do |
| 74 | + { |
| 75 | + "chart_configuration"=> |
| 76 | + { "#{new_chart_1}"=>{"id"=>new_chart_1, "crossFilters"=>{"scope"=>"global", "chartsInScope"=>[new_chart_2]}}, |
| 77 | + "#{new_chart_2}"=>{"id"=>new_chart_2, "crossFilters"=>{"scope"=>"global", "chartsInScope"=>[new_chart_1]}}}, |
| 78 | + "global_chart_configuration"=>{"scope"=>{"rootPath"=>["ROOT_ID"], "excluded"=>[]}, "chartsInScope"=>[new_chart_1, new_chart_2]}, |
| 79 | + "native_filter_configuration"=> |
| 80 | + [ |
| 81 | + {"id"=>"NATIVE_FILTER-k-UxewZyI", |
| 82 | + "name"=>"JobTitleLimit5", |
| 83 | + "targets"=>[{"datasetId"=>new_dataset_1, "column"=>{"name"=>"job_title"}}], |
| 84 | + "chartsInScope"=>[new_chart_1, new_chart_2]}, |
| 85 | + {"id"=>"NATIVE_FILTER-eoi3FEQ1C", |
| 86 | + "name"=>"Count", |
| 87 | + "filterType"=>"filter_select", |
| 88 | + "targets"=>[{"datasetId"=>new_dataset_2, "column"=>{"name"=>"count"}}], |
| 89 | + "chartsInScope"=>[new_chart_1, new_chart_2]} |
| 90 | + ] |
| 91 | + } |
| 92 | + end |
| 93 | + |
42 | 94 | before do
|
43 | 95 | allow(subject).to receive(:superset_host).and_return('http://superset-host.com')
|
44 | 96 | allow(subject).to receive(:target_database_available_schemas).and_return(target_database_available_schemas)
|
|
49 | 101 | allow(subject).to receive(:source_dashboard_filter_dataset_ids).and_return(source_dashboard_filter_dataset_ids)
|
50 | 102 | allow(subject).to receive(:source_dashboard_dataset_ids).and_return(source_dashboard_dataset_ids)
|
51 | 103 | allow(subject).to receive(:dataset_duplication_tracker).and_return(dataset_duplication_tracker)
|
52 |
| - allow(new_dashboard).to receive(:result).and_return({ 'json_metadata' => json_metadata.to_json }) |
53 | 104 | end
|
54 | 105 |
|
55 | 106 | describe '#perform' do
|
|
64 | 115 | expect(Superset::Dataset::UpdateSchema).to receive(:new).with(source_dataset_id: new_dataset_2, target_database_id: target_database_id, target_schema: target_schema).and_return(double(perform: new_dataset_2))
|
65 | 116 |
|
66 | 117 | # getting the list of charts for the source dashboard
|
67 |
| - allow(Superset::Dashboard::Charts::List).to receive(:new).with(source_dashboard_id).and_return(double(result: [{ 'slice_name' => "test", "id" => 3001}, { 'slice_name' => "test", "id" => 3002}], chart_ids: [new_chart_1, new_chart_2])) |
68 |
| - allow(Superset::Dashboard::Charts::List).to receive(:new).with(new_dashboard_id).and_return(double(result: [{ 'slice_name' => "test", "id" => 3001}, { 'slice_name' => "test", "id" => 3002}])) |
| 118 | + allow(Superset::Dashboard::Charts::List).to receive(:new).with(source_dashboard_id).and_return(double(result: [{ 'slice_name' => "chart 1", "id" => source_chart_1}, { 'slice_name' => "chart 2", "id" => source_chart_2}])) # , chart_ids: [source_chart_1, source_chart_2] |
| 119 | + allow(Superset::Dashboard::Charts::List).to receive(:new).with(new_dashboard_id).and_return(double(result: [{ 'slice_name' => "chart 1", "id" => new_chart_1}, { 'slice_name' => "chart 2", "id" => new_chart_2}])) |
69 | 120 |
|
70 | 121 | # getting the current dataset_id for the new charts .. still pointing to the old datasets
|
71 |
| - expect(Superset::Chart::Get).to receive(:new).with(3001).and_return(double(datasource_id: source_dataset_1)) |
72 |
| - expect(Superset::Chart::Get).to receive(:new).with(3002).and_return(double(datasource_id: source_dataset_2)) |
| 122 | + expect(Superset::Chart::Get).to receive(:new).with(new_chart_1).and_return(double(datasource_id: source_dataset_1)) |
| 123 | + expect(Superset::Chart::Get).to receive(:new).with(new_chart_2).and_return(double(datasource_id: source_dataset_2)) |
73 | 124 |
|
74 | 125 | # updating the new charts to point to the new datasets
|
75 | 126 | expect(Superset::Chart::UpdateDataset).to receive(:new).with(chart_id: new_chart_1, target_dataset_id: new_dataset_1, target_dashboard_id: new_dashboard_id).and_return(double(perform: true))
|
76 | 127 | expect(Superset::Chart::UpdateDataset).to receive(:new).with(chart_id: new_chart_2, target_dataset_id: new_dataset_2, target_dashboard_id: new_dashboard_id).and_return(double(perform: true))
|
77 | 128 |
|
78 |
| - # get json metadata |
79 |
| - #expect(Superset::Dashboard::Get).to receive(:new).with(new_dashboard_id).and_return(double(json_metadata: json_metadata)) |
80 |
| - #expect(Superset::Dashboard::Get).to receive(:new).with(source_dashboard_id).and_return(double(url: 'test.com', result: { 'json_metadata' => json_metadata.to_json })) |
81 |
| - |
82 |
| - # update dashboard json metadata chart datasets -- TODO add the chart config expected change here |
83 |
| - expect(Superset::Dashboard::Put).to receive(:new).once.with(target_dashboard_id: new_dashboard_id, params: { 'json_metadata' => json_metadata.to_json }).and_return(double(perform: true)) |
84 |
| - # update dashboard json metadata filter datasets |
85 |
| - expect(Superset::Dashboard::Put).to receive(:new).once.with(target_dashboard_id: new_dashboard_id, params: { 'json_metadata' => new_json_metadata.to_json }).and_return(double(perform: true)) |
| 129 | + # update dashboard json metadata chart datasets |
| 130 | + expect(Superset::Dashboard::Put).to receive(:new).once.with(target_dashboard_id: new_dashboard_id, params: { 'json_metadata' => json_metadata_updated_settings.to_json }).and_return(double(perform: true)) |
86 | 131 | end
|
87 | 132 |
|
88 |
| - context 'returns the new dashboard details' do |
89 |
| - specify { expect(subject.perform).to eq( { new_dashboard_id: 2, new_dashboard_url: "http://superset-host.com/superset/dashboard/2" }) } |
| 133 | + context 'completes duplicate process' do |
| 134 | + context 'and returns the new dashboard details' do |
| 135 | + specify do |
| 136 | + expect(subject.perform).to eq( { new_dashboard_id: 2, new_dashboard_url: "http://superset-host.com/superset/dashboard/2" }) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + context 'and updates the json_metadata as expected' do |
| 141 | + context 'with stardard json metadata ids' do |
| 142 | + specify do |
| 143 | + expect(subject.new_dashboard_json_metadata_configuration).to eq(json_metadata_initial_settings) |
| 144 | + subject.perform |
| 145 | + expect(subject.new_dashboard_json_metadata_configuration).to eq(json_metadata_updated_settings) |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + context 'with non stardard json metadata ids to confirm gsub' do |
| 150 | + let(:source_chart_1) { 11 } |
| 151 | + let(:source_chart_2) { 1111 } |
| 152 | + let(:json_metadata_initial_settings) do |
| 153 | + { |
| 154 | + "chart_configuration"=> |
| 155 | + { "#{source_chart_1}"=>{"id"=>source_chart_1, "crossFilters"=>{"scope"=>"global", "chartsInScope"=>[source_chart_2]}} , |
| 156 | + "#{source_chart_2}"=>{"id"=>source_chart_2, "crossFilters"=>{"scope"=>"global", "chartsInScope"=>[source_chart_1]}} } , |
| 157 | + "global_chart_configuration"=>{"scope"=>{"rootPath"=>["ROOT_ID"], "excluded"=>[]}, "chartsInScope"=>[source_chart_1, source_chart_2]}, |
| 158 | + "native_filter_configuration"=>[] |
| 159 | + } |
| 160 | + end |
| 161 | + |
| 162 | + let(:new_chart_1) { 222 } |
| 163 | + let(:new_chart_2) { 22222 } |
| 164 | + let!(:json_metadata_updated_settings) do |
| 165 | + { |
| 166 | + "chart_configuration"=> |
| 167 | + { "#{new_chart_1}"=>{"id"=>new_chart_1, "crossFilters"=>{"scope"=>"global", "chartsInScope"=>[new_chart_2]}} , |
| 168 | + "#{new_chart_2}"=>{"id"=>new_chart_2, "crossFilters"=>{"scope"=>"global", "chartsInScope"=>[new_chart_1]}} } , |
| 169 | + "global_chart_configuration"=>{"scope"=>{"rootPath"=>["ROOT_ID"], "excluded"=>[]}, "chartsInScope"=>[new_chart_1, new_chart_2]}, |
| 170 | + "native_filter_configuration"=>[] |
| 171 | + } |
| 172 | + end |
| 173 | + |
| 174 | + specify do |
| 175 | + expect(subject.new_dashboard_json_metadata_configuration).to eq(json_metadata_initial_settings) |
| 176 | + subject.perform |
| 177 | + expect(subject.new_dashboard_json_metadata_configuration).to eq(json_metadata_updated_settings) |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | + |
90 | 182 | end
|
91 | 183 |
|
92 | 184 | context 'and embedded domains' do
|
|
0 commit comments