forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar_to_model_hash_spec.rb
171 lines (142 loc) · 5.79 KB
/
ar_to_model_hash_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
RSpec.describe ToModelHash do
context "#to_model_hash" do
let(:test_disk_class) do
Class.new(ActiveRecord::Base) do
include ToModelHash
self.table_name = "test_disks"
end
end
let(:test_hardware_class) do
Class.new(ActiveRecord::Base) do
include ToModelHash
self.table_name = "test_hardwares"
end
end
let(:test_vm_class) do
Class.new(ActiveRecord::Base) do
include ToModelHash
self.table_name = "test_vms"
end
end
let(:test_os_class) do
Class.new(ActiveRecord::Base) do
include ToModelHash
self.table_name = "test_operating_systems"
end
end
let(:fixed_options) { test_vm_class.send(:to_model_hash_options_fixup, @test_to_model_hash_options) }
let(:mocked_preloader) { double }
let(:rails7) { !ActiveRecord::Associations::Preloader.instance_methods.include?(:preload) }
require 'active_support/testing/stream'
include ActiveSupport::Testing::Stream
before do
silence_stream($stdout) do
ActiveRecord::Schema.define do
create_table :test_vms, :force => true do |t|
t.string :name
end
create_table :test_hardwares, :force => true do |t|
t.integer :bitness
t.integer :test_vm_id
end
create_table :test_disks, :force => true do |t|
t.integer :num_disks
t.integer :something
t.integer :test_hardware_id
end
create_table :test_operating_systems, :force => true do |t|
t.string :name
end
end
end
test_disk_class.belongs_to :test_hardware, :anonymous_class => test_hardware_class, :inverse_of => :test_disks
test_hardware_class.has_many :test_disks, :anonymous_class => test_disk_class, :inverse_of => :test_hardware
test_hardware_class.belongs_to :test_vm, :anonymous_class => test_vm_class, :inverse_of => :test_hardware
test_vm_class.has_one :test_hardware, :anonymous_class => test_hardware_class, :inverse_of => :test_vm, :dependent => :destroy
test_vm_class.has_one :test_operating_system, :anonymous_class => test_os_class, :inverse_of => false, :dependent => :destroy
# we're testing the preload of associations, skip the recursive .to_model_hash
allow_any_instance_of(test_vm_class).to receive(:to_model_hash_recursive)
end
def assert_preloaded(associations)
if rails7
allow(ActiveRecord::Associations::Preloader).to receive(:new)
.with(associations: array_including(associations), records: array_including(instance_of(test_vm_class)), scope: nil)
.and_return(mocked_preloader)
expect(mocked_preloader).to receive(:call)
else
allow(ActiveRecord::Associations::Preloader).to receive(:new).and_return(mocked_preloader)
expect(mocked_preloader).to receive(:preload) do |_recs, assocs|
expect(assocs).to match_array(associations)
end
end
test_vm_class.new.to_model_hash(fixed_options)
end
it "included column" do
@test_to_model_hash_options = {
"include" => {"test_hardware" => {"columns" => ["bitness"]}}
}
assert_preloaded([:test_hardware])
end
it "nested included columns" do
@test_to_model_hash_options = {
"include" => {
"test_hardware" => {
"columns" => ["bitness"],
"include" => {"test_disks" => {"columns" => ["num_disks"]}}
}
}
}
assert_preloaded([{:test_hardware => [:test_disks]}])
end
it "columns included from different associations" do
@test_to_model_hash_options = {
"include" => {
"test_hardware" => {"columns" => ["bitness"]},
"test_operating_system" => {"columns" => ["name"]}
}
}
assert_preloaded([:test_hardware, :test_operating_system])
end
context "virtual columns" do
it "virtual column on main table" do
@test_to_model_hash_options = {
"cols" => ["bitness"]
}
test_vm_class.virtual_column :bitness, :type => :integer, :uses => :test_hardware
assert_preloaded([:bitness])
end
it "virtual column and included column" do
@test_to_model_hash_options = {
"cols" => ["bitness"],
"include" => {"test_hardware" => {"columns" => ["test_vm_id"]}}
}
test_vm_class.virtual_column :bitness, :type => :integer, :uses => :test_hardware
assert_preloaded([:bitness, :test_hardware])
end
it "virtual column matches included association column" do
@test_to_model_hash_options = {
"include" => {"test_hardware" => {"columns" => ["bitness"]}}
}
test_vm_class.virtual_column :bitness, :type => :integer, :uses => :test_hardware
assert_preloaded([:test_hardware])
end
it "included association virtual column " do
@test_to_model_hash_options = {
"include" => {"test_hardware" => {"columns" => ["num_disks"]}}
}
test_hardware_class.virtual_column :num_disks, :type => :integer, :uses => :test_disks
assert_preloaded([{:test_hardware => [:num_disks]}])
end
it "virtual and regular column included from different associations" do
@test_to_model_hash_options = {
"include" => {
"test_hardware" => {"columns" => ["num_disks"]},
"test_operating_system" => {"columns" => ["name"]}
}
}
test_hardware_class.virtual_column :num_disks, :type => :integer, :uses => :test_disks
assert_preloaded([:test_operating_system, {:test_hardware => [:num_disks]}])
end
end
end
end