2
2
3
3
RSpec . describe Superset ::Dashboard ::Export do
4
4
subject { described_class . new ( dashboard_id : dashboard_id , destination_path : destination_path ) }
5
+
5
6
let ( :dashboard_id ) { 18 }
6
7
let ( :destination_path ) { './tmp/superset_dashboard_backups/' }
8
+ let ( :datestamp ) { '20240322' }
9
+ let ( :zip_file_name ) { 'spec/fixtures/dashboard_18_export_20240322.zip' } # Example fixture zip file
7
10
8
11
describe '#perform' do
9
12
let ( :response ) { double ( 'response' ) }
10
- let ( :zip_file_name ) { 'spec/fixtures/dashboard_18_export_20240322.zip' } # example birth names dashboard export fixture
11
13
12
14
before do
15
+ FileUtils . rm_rf ( "#{ destination_path } #{ dashboard_id } " ) if File . directory? ( "#{ destination_path } #{ dashboard_id } " )
16
+
13
17
allow ( subject ) . to receive ( :response ) . and_return ( response )
14
18
allow ( subject ) . to receive ( :zip_file_name ) . and_return ( zip_file_name )
15
19
allow ( subject ) . to receive ( :save_exported_zip_file )
20
+
21
+ allow ( subject ) . to receive ( :datestamp ) . and_return ( datestamp )
22
+
23
+ @temp_dir = Dir . mktmpdir ( "superset_dashboard_exports" )
24
+ @extracted_files = [
25
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "metadata.yaml" ) ,
26
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "dashboards" , "Birth_Names_18.yaml" ) ,
27
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "charts" , "Boy_Name_Cloud_53920.yaml" ) ,
28
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "charts" , "Names_Sorted_by_Num_in_California_53929.yaml" ) ,
29
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "charts" , "Number_of_Girls_53930.yaml" ) ,
30
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "charts" , "Pivot_Table_53931.yaml" ) ,
31
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "charts" , "Top_10_Girl_Name_Share_53921.yaml" ) ,
32
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "databases" , "examples.yaml" ) ,
33
+ File . join ( @temp_dir , "dashboard_export_#{ datestamp } T123456" , "datasets" , "examples" , "video_game_sales.yaml" )
34
+ ]
35
+
36
+ FileUtils . mkdir_p ( File . dirname ( @extracted_files . first ) ) # Creates dashboard_export_20240322T123456
37
+ @extracted_files . each do |file_path |
38
+ FileUtils . mkdir_p ( File . dirname ( file_path ) )
39
+ File . write ( file_path , "dummy content for #{ File . basename ( file_path ) } " )
40
+ end
41
+
42
+ allow ( subject ) . to receive ( :unzip_file ) . and_return ( @extracted_files )
16
43
end
17
44
18
- it 'unzips into the destination path' do
45
+ after do
46
+ FileUtils . rm_rf ( @temp_dir ) if Dir . exist? ( @temp_dir )
47
+ FileUtils . rm_rf ( "#{ destination_path } #{ dashboard_id } " ) if File . directory? ( "#{ destination_path } #{ dashboard_id } " )
48
+ end
49
+
50
+ it 'unzips into the destination path with versioned filenames' do
19
51
subject . perform
20
- expect ( Dir . glob ( subject . destination_path + "/**/*" ) . sort ) . to match_array ( [
21
- "./tmp/superset_dashboard_backups/18" ,
52
+
53
+ expected_files = [
22
54
"./tmp/superset_dashboard_backups/18/charts" ,
23
55
"./tmp/superset_dashboard_backups/18/charts/Boy_Name_Cloud_53920.yaml" ,
24
56
"./tmp/superset_dashboard_backups/18/charts/Names_Sorted_by_Num_in_California_53929.yaml" ,
31
63
"./tmp/superset_dashboard_backups/18/databases/examples.yaml" ,
32
64
"./tmp/superset_dashboard_backups/18/datasets" ,
33
65
"./tmp/superset_dashboard_backups/18/datasets/examples" ,
34
- "./tmp/superset_dashboard_backups/18/datasets/examples/birth_names.yaml" ,
35
- "./tmp/superset_dashboard_backups/18/metadata.yaml"
36
- ] )
66
+ "./tmp/superset_dashboard_backups/18/datasets/examples/video_game_sales.yaml" ,
67
+ "./tmp/superset_dashboard_backups/18/metadata.yaml" ,
68
+ "./tmp/superset_dashboard_backups/18/dashboard_18_export_20240322.zip" # Include the zip file itself
69
+ ]
70
+
71
+ actual_files = Dir . glob ( "#{ destination_path } #{ dashboard_id } /**/*" ) . sort . map do |path |
72
+ Pathname . new ( path ) . relative_path_from ( Pathname . new ( '.' ) ) . to_s
73
+ end
74
+
75
+ actual_files = actual_files . map { |path | "./#{ path } " }
76
+
77
+ expect ( actual_files ) . to match_array ( expected_files )
37
78
end
38
79
end
39
- end
80
+ end
0 commit comments