File: sequence_setting_spec.rb

package info (click to toggle)
ruby-factory-bot 6.5.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,492 kB
  • sloc: ruby: 9,242; makefile: 6; sh: 4
file content (362 lines) | stat: -rw-r--r-- 12,333 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
describe "FactoryBot.set_sequence" do
  include FactoryBot::Syntax::Methods

  describe "on success" do
    describe "setting sequence to correct value" do
      it "works with Integer sequences" do
        FactoryBot.define do
          sequence(:email) { |n| "somebody#{n}@example.com" }
        end

        expect(generate_list(:email, 3).last).to eq "somebody3@example.com"
        FactoryBot.set_sequence(:email, 54321)
        expect(generate_list(:email, 3).last).to eq "somebody54323@example.com"
      end

      it "works with negative Integer sequences" do
        FactoryBot.define do
          sequence(:email, -50) { |n| "somebody#{n}@example.com" }
        end

        expect(generate_list(:email, 3).last).to eq "somebody-48@example.com"
        FactoryBot.set_sequence(:email, -25)
        expect(generate_list(:email, 3).last).to eq "somebody-23@example.com"
      end

      it "works with Enumerable sequences" do
        define_class("User") { attr_accessor :name }

        FactoryBot.define do
          factory :user do
            sequence(:name, %w[Jane Joe Josh Jayde John].to_enum)
          end
        end

        expect(generate(:user, :name)).to eq "Jane"
        FactoryBot.set_sequence(:user, :name, "Jayde")
        expect(generate(:user, :name)).to eq "Jayde"
        expect(generate(:user, :name)).to eq "John"
      end

      it "works with String sequences" do
        define_class("User") { attr_accessor :initial }

        FactoryBot.define do
          factory :user do
            factory :pilot do
              sequence(:initial, "a")
            end
          end
        end

        expect(generate_list(:pilot, :initial, 3)).to eq ["a", "b", "c"]
        FactoryBot.set_sequence(:pilot, :initial, "z")
        expect(generate_list(:pilot, :initial, 3)).to eq ["z", "aa", "ab"]
      end

      it "works with Date sequences" do
        define_class("User") { attr_accessor :dob }

        FactoryBot.define do
          factory :user do
            factory :pilot do
              factory :jet_pilot do
                sequence(:dob, Date.parse("2025-04-01"))
              end
            end
          end
        end

        expect(generate(:jet_pilot, :dob)).to eq Date.parse("2025-04-01")
        FactoryBot.set_sequence(:jet_pilot, :dob, Date.parse("2025-05-01"))
        expect(generate_list(:jet_pilot, :dob, 3).last).to eq Date.parse("2025-05-03")
      end

      it "works with lazy Integer sequences" do
        FactoryBot.define do
          sequence(:email, proc { 42 }) { |n| "somebody#{n}@example.com" }
        end

        expect(generate_list(:email, 3).last).to eq "somebody44@example.com"
        FactoryBot.set_sequence(:email, 54321)
        expect(generate_list(:email, 3).last).to eq "somebody54323@example.com"
      end

      it "does not collide with other factory or global sequences" do
        define_class("User") { attr_accessor :email }
        define_class("Admin") { attr_accessor :email }

        FactoryBot.define do
          sequence(:email) { |n| "global#{n}@example.com" }
          factory :user do
            sequence(:email) { |n| "user#{n}@example.com" }

            factory :admin do
              sequence(:email) { |n| "admin#{n}@example.com" }
            end
          end
        end

        generate_list :email, 2
        generate_list :user, :email, 2
        generate_list :admin, :email, 2

        expect(generate(:email)).to eq "global3@example.com"
        expect(generate(:user, :email)).to eq "user3@example.com"
        expect(generate(:admin, :email)).to eq "admin3@example.com"

        FactoryBot.set_sequence(:user, :email, 22222)
        FactoryBot.set_sequence(:admin, :email, 33333)

        expect(generate(:email)).to eq "global4@example.com"
        expect(generate(:user, :email)).to eq "user22222@example.com"
        expect(generate(:admin, :email)).to eq "admin33333@example.com"
      end
    end

    describe "sequence targeting by URI" do
      before do
        define_class("User")

        FactoryBot.define do
          sequence :counter

          trait :global_trait do
            sequence :counter
          end

          factory :parent, class: "User" do
            sequence :counter

            trait :parent_trait do
              sequence :counter
            end

            factory :child do
              sequence :counter

              trait :child_trait do
                sequence :counter
              end
            end
          end
        end
      end

      it "accepts symbolic URIs" do
        expect(generate(:counter)).to eq 1
        expect(generate(:global_trait, :counter)).to eq 1
        expect(generate(:parent, :counter)).to eq 1
        expect(generate(:parent, :parent_trait, :counter)).to eq 1
        expect(generate(:child, :counter)).to eq 1
        expect(generate(:child, :child_trait, :counter)).to eq 1

        FactoryBot.set_sequence :counter, 1000
        FactoryBot.set_sequence :global_trait, :counter, 3000
        FactoryBot.set_sequence :parent, :counter, 6000
        FactoryBot.set_sequence :parent, :parent_trait, :counter, 9000
        FactoryBot.set_sequence :child, :counter, 12_000
        FactoryBot.set_sequence :child, :child_trait, :counter, 15_000

        expect(generate(:counter)).to eq 1000
        expect(generate(:global_trait, :counter)).to eq 3000
        expect(generate(:parent, :counter)).to eq 6000
        expect(generate(:parent, :parent_trait, :counter)).to eq 9000
        expect(generate(:child, :counter)).to eq 12_000
        expect(generate(:child, :child_trait, :counter)).to eq 15_000
      end

      it "accepts string URIs" do
        expect(generate("counter")).to eq 1
        expect(generate("global_trait/counter")).to eq 1
        expect(generate("parent/counter")).to eq 1
        expect(generate("parent/parent_trait/counter")).to eq 1
        expect(generate("child/counter")).to eq 1
        expect(generate("child/child_trait/counter")).to eq 1

        FactoryBot.set_sequence "counter", 1000
        FactoryBot.set_sequence "global_trait/counter", 3000
        FactoryBot.set_sequence "parent/counter", 6000
        FactoryBot.set_sequence "parent/parent_trait/counter", 9000
        FactoryBot.set_sequence "child/counter", 12_000
        FactoryBot.set_sequence "child/child_trait/counter", 15_000

        expect(generate("counter")).to eq 1000
        expect(generate("global_trait/counter")).to eq 3000
        expect(generate("parent/counter")).to eq 6000
        expect(generate("parent/parent_trait/counter")).to eq 9000
        expect(generate("child/counter")).to eq 12_000
        expect(generate("child/child_trait/counter")).to eq 15_000
      end
    end

    describe "name format support" do
      it "accepts String or Symbol sequence names" do
        FactoryBot.define do
          sequence(:email) { |n| "user#{n}@example.com" }
        end

        FactoryBot.set_sequence(:email, 54321)
        expect(generate(:email)).to eq "user54321@example.com"

        FactoryBot.set_sequence("email", 777)
        expect(generate(:email)).to eq "user777@example.com"
      end

      it "accepts String or Symbol factory names" do
        define_class("User") { attr_accessor :email }

        FactoryBot.define do
          factory :user do
            sequence(:email) { |n| "user#{n}@example.com" }
          end
        end

        FactoryBot.set_sequence(:user, :email, 54321)
        expect(generate(:user, :email)).to eq "user54321@example.com"

        FactoryBot.set_sequence("user", "email", 777)
        expect(generate("user", "email")).to eq "user777@example.com"
      end
    end

    describe "alias support" do
      it "works with aliases for both sequence and factory" do
        define_class("User") { attr_accessor :email }

        FactoryBot.define do
          factory :user, aliases: [:author, :commenter] do
            sequence(:email, aliases: ["primary_email", :alt_email]) { |n| "user#{n}@example.com" }
          end
        end

        generate_list :user, :email, 2
        expect(generate(:user, :email)).to eq "user3@example.com"

        FactoryBot.set_sequence(:user, :email, 11111)
        expect(generate(:user, :email)).to eq "user11111@example.com"

        FactoryBot.set_sequence(:author, :primary_email, 22222)
        expect(generate(:user, :email)).to eq "user22222@example.com"

        FactoryBot.set_sequence(:commenter, :alt_email, 33333)
        expect(generate(:user, :email)).to eq "user33333@example.com"
      end
    end
  end

  describe "error handling" do
    describe "unknown sequence names" do
      it "raises an error for unknown global sequences" do
        expect { FactoryBot.set_sequence(:test, 54321) }
          .to raise_error KeyError, /Sequence not registered: 'test'/
      end

      it "raises an error for unknown factory sequences" do
        FactoryBot.define do
          factory :user do
            sequence(:email) { |n| "alt_user#{n}@example.com" }
          end
        end

        expect { FactoryBot.set_sequence(:user, :test, 54321) }
          .to raise_error KeyError, /Sequence not registered: 'user\/test'/
      end

      it "raises an error when factory sequence doesn't exist but global does" do
        FactoryBot.define do
          sequence(:email) { |n| "global#{n}@example.com" }

          factory :user do
            sequence(:alt_email) { |n| "alt_user#{n}@example.com" }
          end
        end

        expect { FactoryBot.set_sequence(:user, :email, 54321) }
          .to raise_error KeyError, /Sequence not registered: 'user\/email'/
      end

      it "raises an error for inherited sequences" do
        define_class("User") { attr_accessor :email }

        FactoryBot.define do
          sequence(:email) { |n| "global#{n}@example.com" }

          factory :user do
            sequence(:email) { |n| "user#{n}@example.com" }
            factory :admin
          end
        end

        admin = build(:admin)
        expect(admin.email).to eq "user1@example.com"

        expect { FactoryBot.set_sequence(:admin, :email, 54321) }
          .to raise_error KeyError, /Sequence not registered: 'admin\/email'/
      end
    end

    describe "invalid values" do
      it "raises an error when value is below minimum for Integer sequences" do
        FactoryBot.define do
          sequence(:counter, 1000)
        end

        expect { FactoryBot.set_sequence(:counter, 999) }
          .to raise_error ArgumentError, /Value cannot be less than: 1000/
      end

      it "raises an error for unmatched String values", :slow do
        FactoryBot.define do
          sequence(:char, "c")
        end

        expect { FactoryBot.set_sequence(:char, "a") }
          .to raise_error ArgumentError, /Unable to find 'a' in the sequence/
      end

      it "raises an error for unmatched Enumerable values" do
        names = %w[Jane Joe Josh Jayde John].to_enum

        allow_any_instance_of(FactoryBot::Sequence).to receive(:can_set_value_by_index?).and_return(false)

        FactoryBot.define do
          sequence(:name, names)
        end

        expect { FactoryBot.set_sequence(:name, "Jester") }
          .to raise_error ArgumentError, /Unable to find 'Jester' in the sequence/
      end

      it "times out when value cannot be found within timeout period", :slow do
        with_temporary_assignment(FactoryBot, :sequence_setting_timeout, 3) do
          FactoryBot.define do
            sequence(:test, "a")
          end

          start = Time.now
          expect { FactoryBot.set_sequence(:test, "zzzzzzzzzz") }
            .to raise_error ArgumentError, /Unable to find 'zzzzzzzzzz' in the sequence/

          duration = Time.now - start
          expect(duration >= 3.seconds).to be_truthy
          expect(duration < 4.seconds).to be_truthy
        end
      end

      it "leaves sequence unchanged when value is not found" do
        FactoryBot.define do
          sequence(:name, %w[Jane Joe Josh Jayde John].to_enum)
        end

        generate_list :name, 2
        expect(generate(:name)).to eq "Josh"

        expect { FactoryBot.set_sequence(:name, "Jester") }
          .to raise_error ArgumentError, /Unable to find 'Jester' in the sequence/

        expect(generate(:name)).to eq "Jayde"
      end
    end
  end
end