-
Notifications
You must be signed in to change notification settings - Fork 616
/
recursive_import.rb
265 lines (233 loc) · 8.86 KB
/
recursive_import.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# frozen_string_literal: true
def should_support_recursive_import
describe "importing objects with associations" do
let(:new_topics) { Build(num_topics, :topic_with_book) }
let(:new_topics_with_invalid_chapter) do
chapter = new_topics.first.books.first.chapters.first
chapter.title = nil
new_topics
end
let(:num_topics) { 3 }
let(:num_books) { 6 }
let(:num_chapters) { 18 }
let(:num_endnotes) { 24 }
let(:new_question_with_rule) { FactoryBot.build :question, :with_rule }
it 'imports top level' do
assert_difference "Topic.count", +num_topics do
Topic.import new_topics, recursive: true
new_topics.each do |topic|
assert_not_nil topic.id
end
end
end
it 'imports first level associations' do
assert_difference "Book.count", +num_books do
Topic.import new_topics, recursive: true
new_topics.each do |topic|
topic.books.each do |book|
assert_equal topic.id, book.topic_id
end
end
end
end
it 'imports polymorphic associations' do
discounts = Array.new(1) { |i| Discount.new(amount: i) }
books = Array.new(1) { |i| Book.new(author_name: "Author ##{i}", title: "Book ##{i}") }
books.each do |book|
book.discounts << discounts
end
Book.import books, recursive: true
books.each do |book|
book.discounts.each do |discount|
assert_not_nil discount.discountable_id
assert_equal 'Book', discount.discountable_type
end
end
end
it 'imports polymorphic associations from subclass' do
discounts = Array.new(1) { |i| Discount.new(amount: i) }
dictionaries = Array.new(1) { |i| Dictionary.new(author_name: "Author ##{i}", title: "Book ##{i}") }
dictionaries.each do |dictionary|
dictionary.discounts << discounts
end
Dictionary.import dictionaries, recursive: true
assert_equal 1, Dictionary.last.discounts.count
dictionaries.each do |dictionary|
dictionary.discounts.each do |discount|
assert_not_nil discount.discountable_id
assert_equal 'Book', discount.discountable_type
end
end
end
[{ recursive: false }, {}].each do |import_options|
it "skips recursion for #{import_options}" do
assert_difference "Book.count", 0 do
Topic.import new_topics, import_options
end
end
end
it 'imports deeper nested associations' do
assert_difference "Chapter.count", +num_chapters do
assert_difference "EndNote.count", +num_endnotes do
Topic.import new_topics, recursive: true
new_topics.each do |topic|
topic.books.each do |book|
book.chapters.each do |chapter|
assert_equal book.id, chapter.book_id
end
book.end_notes.each do |endnote|
assert_equal book.id, endnote.book_id
end
end
end
end
end
end
# Models are only valid if all associations are valid
it "only imports models with valid associations" do
assert_difference "Topic.count", 2 do
assert_difference "Book.count", 4 do
assert_difference "Chapter.count", 12 do
assert_difference "EndNote.count", 16 do
Topic.import new_topics_with_invalid_chapter, recursive: true
end
end
end
end
end
it "skips validation of the associations if requested" do
assert_difference "Chapter.count", +num_chapters do
Topic.import new_topics_with_invalid_chapter, validate: false, recursive: true
end
end
it 'imports has_one associations' do
assert_difference 'Rule.count' do
Question.import [new_question_with_rule], recursive: true
end
end
it "imports an imported belongs_to association id" do
first_new_topic = new_topics[0]
second_new_topic = new_topics[1]
books = first_new_topic.books.to_a
Topic.import new_topics, validate: false
assert_difference "Book.count", books.size do
Book.import books, validate: false
end
books.each do |book|
assert_equal book.topic_id, first_new_topic.id
end
books.each { |book| book.topic_id = second_new_topic.id }
assert_no_difference "Book.count", books.size do
Book.import books, validate: false, on_duplicate_key_update: [:topic_id]
end
books.each do |book|
assert_equal book.topic_id, second_new_topic.id
end
books.each { |book| book.topic_id = nil }
assert_no_difference "Book.count", books.size do
Book.import books, validate: false, on_duplicate_key_update: [:topic_id]
end
books.each do |book|
assert_nil book.topic_id, nil
end
end
unless ENV["SKIP_COMPOSITE_PK"]
describe "with composite primary keys" do
it "should import models and set id" do
tags = []
tags << Tag.new(tag_id: 1, publisher_id: 1, tag: 'Mystery')
tags << Tag.new(tag_id: 2, publisher_id: 1, tag: 'Science')
assert_difference "Tag.count", +2 do
Tag.import tags
end
assert_equal 1, tags[0].tag_id
assert_equal 2, tags[1].tag_id
end
if ENV['AR_VERSION'].to_f >= 7.1
it "should import models with auto-incrementing ID successfully with recursive set to true" do
author = Author.create!(name: "Foo Barson")
books = []
2.times do |i|
books << CompositeBook.new(author_id: author.id, title: "Book #{i}", composite_chapters: [
CompositeChapter.new(title: "Book #{i} composite chapter 1"),
CompositeChapter.new(title: "Book #{i} composite chapter 2"),
])
end
assert_difference "CompositeBook.count", +2 do
assert_difference "CompositeChapter.count", +4 do
CompositeBook.import books, recursive: true
end
end
end
end
end
end
describe "all_or_none" do
[Book, Chapter, Topic, EndNote].each do |type|
it "creates #{type}" do
assert_difference "#{type}.count", 0 do
Topic.import new_topics_with_invalid_chapter, all_or_none: true, recursive: true
end
end
end
end
# If adapter supports on_duplicate_key_update and specific columns are specified, it is only applied to top level models so that SQL with invalid
# columns, keys, etc isn't generated for child associations when doing recursive import
if ActiveRecord::Base.connection.supports_on_duplicate_key_update?
describe "on_duplicate_key_update" do
let(:new_topics) { Build(1, :topic_with_book) }
it "imports objects with associations" do
assert_difference "Topic.count", +1 do
Topic.import new_topics, recursive: true, on_duplicate_key_update: [:updated_at], validate: false
new_topics.each do |topic|
assert_not_nil topic.id
end
end
end
context "when :all fields are updated" do
setup do
Topic.import new_topics, recursive: true
end
it "updates associated objects" do
new_author_name = 'Richard Bachman'
topic = new_topics.first
topic.books.each do |book|
book.author_name = new_author_name
end
assert_nothing_raised do
Topic.import new_topics, recursive: true, on_duplicate_key_update: :all
end
Topic.find(topic.id).books.each do |book|
assert_equal new_author_name, book.author_name
end
end
end
end
end
# If returning option is provided, it is only applied to top level models so that SQL with invalid
# columns, keys, etc isn't generated for child associations when doing recursive import
describe "returning" do
let(:new_topics) { Build(1, :topic_with_book) }
it "imports objects with associations" do
assert_difference "Topic.count", +1 do
Topic.import new_topics, recursive: true, returning: [:content], validate: false
new_topics.each do |topic|
assert_not_nil topic.id
end
end
end
end
# If no returning option is provided, it is ignored
describe "no returning" do
let(:new_topics) { Build(1, :topic_with_book) }
it "is ignored and imports objects with associations" do
assert_difference "Topic.count", +1 do
Topic.import new_topics, recursive: true, no_returning: true, validate: false
new_topics.each do |topic|
assert_not_nil topic.id
end
end
end
end
end
end