-
Notifications
You must be signed in to change notification settings - Fork 899
/
miq_report_result_spec.rb
226 lines (190 loc) · 8.56 KB
/
miq_report_result_spec.rb
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
describe MiqReportResult do
before do
EvmSpecHelper.local_miq_server
@user1 = FactoryGirl.create(:user_with_group)
end
it "#_async_generate_result" do
task = FactoryGirl.create(:miq_task)
EvmSpecHelper.local_miq_server
report = MiqReport.create(
:name => "VMs based on Disk Type",
:title => "VMs using thin provisioned disks",
:rpt_group => "Custom",
:rpt_type => "Custom",
:db => "VmInfra",
:cols => ["name"],
:col_order => ["name"],
:headers => ["Name"],
:order => "Ascending",
:template_type => "report"
)
report.generate_table(:userid => "admin")
task.miq_report_result = report.build_create_results({:userid => "admin"}, task.id)
task.miq_report_result._async_generate_result(task.id, :txt, :user => @user1)
task.reload
expect(task.state).to eq MiqTask::STATE_FINISHED
end
context "report result created by User 1 with current group 1" do
before do
@report_1 = FactoryGirl.create(:miq_report)
group_1 = FactoryGirl.create(:miq_group)
group_2 = FactoryGirl.create(:miq_group)
@user1.miq_groups << group_1
@report_result1 = FactoryGirl.create(:miq_report_result, :miq_report_id => @report_1.id, :miq_group => group_1)
@report_result2 = FactoryGirl.create(:miq_report_result, :miq_report_id => @report_1.id, :miq_group => group_1)
@report_result_nil_report_id = FactoryGirl.create(:miq_report_result)
@report_2 = FactoryGirl.create(:miq_report)
@report_result3 = FactoryGirl.create(:miq_report_result, :miq_report_id => @report_2.id, :miq_group => group_2)
User.current_user = @user1
end
describe ".with_report" do
it "returns report all results without nil report_id" do
report_result = MiqReportResult.with_report
expect(report_result).to match_array([@report_result1, @report_result2, @report_result3])
expect(report_result).not_to include(@report_result_nil_report_id)
end
it "returns only requested report results" do
report_result = MiqReportResult.with_report(@report_result1.miq_report_id)
expect(report_result).to match_array([@report_result1, @report_result2])
expect(report_result).not_to match_array([@report_result3, @report_result_nil_report_id])
end
end
describe ".with_current_user_groups" do
it "returns report results by generated by user 1, non-admin user logged" do
report_result = MiqReportResult.with_current_user_groups
expect(report_result).to match_array([@report_result1, @report_result2])
expect(report_result).not_to match_array([@report_result3, @report_result_nil_report_id])
end
it "returns report all results, admin user logged" do
admin_role = FactoryGirl.create(:miq_user_role, :role => "administrator", :read_only => false)
User.current_user.current_group.miq_user_role = admin_role
report_result = MiqReportResult.with_current_user_groups
expected_reports = [@report_result1, @report_result2, @report_result3, @report_result_nil_report_id]
expect(report_result).to match_array(expected_reports)
end
end
end
context "persisting generated report results" do
before do
5.times do |i|
vm = FactoryGirl.build(:vm_vmware)
vm.evm_owner_id = @user1.id if i > 2
vm.miq_group_id = @user1.current_group.id if vm.evm_owner_id || (i > 1)
vm.save
end
@report_theme = 'miq'
@show_title = true
@options = MiqReport.graph_options({ :title => "CPU (Mhz)", :type => "Line", :columns => ["col"] })
allow(Charting).to receive(:detect_available_plugin).and_return(C3Charting)
end
it "should save the original report metadata and the generated table as a binary blob" do
MiqReport.seed_report(name = "Vendor and Guest OS")
rpt = MiqReport.where(:name => name).last
rpt.generate_table(:userid => "test")
report_result = rpt.build_create_results(:userid => "test")
report_result.reload
expect(report_result).not_to be_nil
expect(report_result.report.kind_of?(MiqReport)).to be_truthy
expect(report_result.binary_blob).not_to be_nil
expect(report_result.report_results.kind_of?(MiqReport)).to be_truthy
expect(report_result.report_results.table).not_to be_nil
expect(report_result.report_results.table.data).not_to be_nil
end
it "should not include `extras[:grouping]` in the report column" do
MiqReport.seed_report(name = "Vendor and Guest OS")
rpt = MiqReport.where(:name => name).last
rpt.generate_table(:userid => "test")
report_result = rpt.build_create_results(:userid => "test")
report_result.report
report_result.report.extras[:grouping] = { "extra data" => "not saved" }
report_result.save
result_reload = MiqReportResult.last
expect(report_result.report.kind_of?(MiqReport)).to be_truthy
expect(result_reload.report.extras[:grouping]).to be_nil
expect(report_result.report.extras[:grouping]).to eq("extra data" => "not saved")
end
context "for miq_report_result is used different miq_group_id than user's current id" do
before do
MiqUserRole.seed
role = MiqUserRole.find_by(:name => "EvmRole-operator")
@miq_group = FactoryGirl.create(:miq_group, :miq_user_role => role, :description => "Group1")
MiqReport.seed_report(@name_of_report = "Vendor and Guest OS")
end
it "has passed miq_group_id and not user's miq_group_id(can be changed during scheduling and generating)" do
rpt = MiqReport.where(:name => @name_of_report).last
rpt.generate_table(:userid => "test")
report_result = rpt.build_create_results(:userid => "test", :miq_group_id => @miq_group.id) # passed group.id
report_result.reload
expect(@user1.current_group_id).not_to eq(@miq_group.id)
expect(report_result.miq_group_id).to eq(@miq_group.id)
end
end
end
describe "#status" do
let(:report_name) { "Vendor and Guest OS" }
let(:task) { FactoryGirl.create(:miq_task) }
let(:miq_report_result) do
MiqReport.seed_report(report_name)
report = MiqReport.where(:name => report_name).last
report.generate_table(:userid => "test")
task.miq_report_result = report.build_create_results({:userid => "test"}, task.id)
MiqReportResult.find_by(:miq_task_id => task.id)
end
it "returns 'Running' if associated task exists and report not ready" do
expect(miq_report_result.status).to eq "Running"
end
it "returns 'Complete' if report generated and associated task exists" do
task.update_status("Finished", "Ok", "Generate Report result")
expect(miq_report_result.status).to eq "Complete"
end
it "returns 'Complete' if task ssociated with report deleted" do
expect(miq_report_result.status).to eq "Running"
task.update_status("Finished", "Ok", "Generate Report result")
task.destroy
miq_report_result.reload
expect(miq_report_result.status).to eq "Complete"
end
end
describe "serializing and deserializing report results" do
it "can serialize and deserialize an MiqReport" do
report = FactoryGirl.build(:miq_report)
report_result = described_class.new
report_result.report_results = report
expect(report_result.report_results.to_hash).to eq(report.to_hash)
end
it "can serialize and deserialize a CSV" do
csv = CSV.generate { |c| c << %w(foo bar) << %w(baz qux) }
report_result = described_class.new
report_result.report_results = csv
expect(report_result.report_results).to eq(csv)
end
it "can serialize and deserialize a plain text report" do
txt = <<EOF
+--------------+
| Foo Report |
+--------------+
| Foo | Bar |
+--------------+
| baz | qux |
| quux | corge |
+--------------+
EOF
report_result = described_class.new
report_result.report_results = txt
expect(report_result.report_results).to eq(txt)
end
end
describe ".counts_by_userid" do
it "fetches counts" do
u1 = FactoryGirl.create(:user)
u2 = FactoryGirl.create(:user)
FactoryGirl.create(:miq_report_result, :userid => u1.userid)
FactoryGirl.create(:miq_report_result, :userid => u1.userid)
FactoryGirl.create(:miq_report_result, :userid => u2.userid)
expect(MiqReportResult.counts_by_userid).to match_array([
{:userid => u1.userid, :count => 2},
{:userid => u2.userid, :count => 1}
])
end
end
end