enum_test.rb 12.2 KB
Newer Older
1 2 3
require 'cases/helper'
require 'models/book'

4
class EnumTest < ActiveRecord::TestCase
5 6 7
  fixtures :books

  setup do
8
    @book = books(:awdr)
9 10 11
  end

  test "query state by predicate" do
G
George Claghorn 已提交
12
    assert @book.published?
13
    assert_not @book.written?
G
George Claghorn 已提交
14
    assert_not @book.proposed?
Y
Yury Korolev 已提交
15

G
George Claghorn 已提交
16
    assert @book.read?
17 18 19 20
    assert @book.in_english?
    assert @book.author_visibility_visible?
    assert @book.illustrator_visibility_visible?
    assert @book.with_medium_font_size?
21
  end
22

R
Robin Dupret 已提交
23
  test "query state with strings" do
G
George Claghorn 已提交
24 25
    assert_equal "published", @book.status
    assert_equal "read", @book.read_status
26 27 28
    assert_equal "english", @book.language
    assert_equal "visible", @book.author_visibility
    assert_equal "visible", @book.illustrator_visibility
29 30
  end

31
  test "find via scope" do
G
George Claghorn 已提交
32 33
    assert_equal @book, Book.published.first
    assert_equal @book, Book.read.first
34 35 36
    assert_equal @book, Book.in_english.first
    assert_equal @book, Book.author_visibility_visible.first
    assert_equal @book, Book.illustrator_visibility_visible.first
37 38
  end

G
Godfrey Chan 已提交
39
  test "find via where with values" do
G
George Claghorn 已提交
40
    published, written = Book.statuses[:published], Book.statuses[:written]
G
Godfrey Chan 已提交
41

G
George Claghorn 已提交
42
    assert_equal @book, Book.where(status: published).first
G
Godfrey Chan 已提交
43
    refute_equal @book, Book.where(status: written).first
G
George Claghorn 已提交
44
    assert_equal @book, Book.where(status: [published]).first
G
Godfrey Chan 已提交
45
    refute_equal @book, Book.where(status: [written]).first
G
George Claghorn 已提交
46
    refute_equal @book, Book.where("status <> ?", published).first
G
Godfrey Chan 已提交
47
    assert_equal @book, Book.where("status <> ?", written).first
48 49
  end

50
  test "find via where with symbols" do
G
George Claghorn 已提交
51
    assert_equal @book, Book.where(status: :published).first
G
Godfrey Chan 已提交
52
    refute_equal @book, Book.where(status: :written).first
G
George Claghorn 已提交
53
    assert_equal @book, Book.where(status: [:published]).first
G
Godfrey Chan 已提交
54
    refute_equal @book, Book.where(status: [:written]).first
G
George Claghorn 已提交
55
    refute_equal @book, Book.where.not(status: :published).first
56
    assert_equal @book, Book.where.not(status: :written).first
57 58 59
  end

  test "find via where with strings" do
G
George Claghorn 已提交
60
    assert_equal @book, Book.where(status: "published").first
61
    refute_equal @book, Book.where(status: "written").first
G
George Claghorn 已提交
62
    assert_equal @book, Book.where(status: ["published"]).first
63
    refute_equal @book, Book.where(status: ["written"]).first
G
George Claghorn 已提交
64
    refute_equal @book, Book.where.not(status: "published").first
65
    assert_equal @book, Book.where.not(status: "written").first
66 67
  end

G
Godfrey Chan 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
  test "build from scope" do
    assert Book.written.build.written?
    refute Book.written.build.proposed?
  end

  test "build from where" do
    assert Book.where(status: Book.statuses[:written]).build.written?
    refute Book.where(status: Book.statuses[:written]).build.proposed?
    assert Book.where(status: :written).build.written?
    refute Book.where(status: :written).build.proposed?
    assert Book.where(status: "written").build.written?
    refute Book.where(status: "written").build.proposed?
  end

82 83 84
  test "update by declaration" do
    @book.written!
    assert @book.written?
85 86 87 88
    @book.in_english!
    assert @book.in_english?
    @book.author_visibility_visible!
    assert @book.author_visibility_visible?
89
  end
90

91 92 93 94
  test "update by setter" do
    @book.update! status: :written
    assert @book.written?
  end
95 96 97 98 99

  test "enum methods are overwritable" do
    assert_equal "do publish work...", @book.published!
    assert @book.published?
  end
100 101 102 103 104 105

  test "direct assignment" do
    @book.status = :written
    assert @book.written?
  end

106 107 108 109 110
  test "assign string value" do
    @book.status = "written"
    assert @book.written?
  end

111 112
  test "enum changed attributes" do
    old_status = @book.status
113
    old_language = @book.language
G
George Claghorn 已提交
114
    @book.status = :proposed
115
    @book.language = :spanish
116
    assert_equal old_status, @book.changed_attributes[:status]
117
    assert_equal old_language, @book.changed_attributes[:language]
118 119 120 121
  end

  test "enum changes" do
    old_status = @book.status
122
    old_language = @book.language
G
George Claghorn 已提交
123
    @book.status = :proposed
124
    @book.language = :spanish
G
George Claghorn 已提交
125
    assert_equal [old_status, 'proposed'], @book.changes[:status]
126
    assert_equal [old_language, 'spanish'], @book.changes[:language]
127 128 129 130
  end

  test "enum attribute was" do
    old_status = @book.status
131
    old_language = @book.language
132
    @book.status = :published
133
    @book.language = :spanish
134
    assert_equal old_status, @book.attribute_was(:status)
135
    assert_equal old_language, @book.attribute_was(:language)
136 137 138
  end

  test "enum attribute changed" do
G
George Claghorn 已提交
139
    @book.status = :proposed
140
    @book.language = :french
141
    assert @book.attribute_changed?(:status)
142
    assert @book.attribute_changed?(:language)
143 144 145
  end

  test "enum attribute changed to" do
G
George Claghorn 已提交
146
    @book.status = :proposed
147
    @book.language = :french
G
George Claghorn 已提交
148
    assert @book.attribute_changed?(:status, to: 'proposed')
149
    assert @book.attribute_changed?(:language, to: 'french')
150 151 152 153
  end

  test "enum attribute changed from" do
    old_status = @book.status
154
    old_language = @book.language
G
George Claghorn 已提交
155
    @book.status = :proposed
156
    @book.language = :french
157
    assert @book.attribute_changed?(:status, from: old_status)
158
    assert @book.attribute_changed?(:language, from: old_language)
159 160 161 162
  end

  test "enum attribute changed from old status to new status" do
    old_status = @book.status
163
    old_language = @book.language
G
George Claghorn 已提交
164
    @book.status = :proposed
165
    @book.language = :french
G
George Claghorn 已提交
166
    assert @book.attribute_changed?(:status, from: old_status, to: 'proposed')
167
    assert @book.attribute_changed?(:language, from: old_language, to: 'french')
168 169
  end

170 171 172 173 174 175 176
  test "enum didn't change" do
    old_status = @book.status
    @book.status = old_status
    assert_not @book.attribute_changed?(:status)
  end

  test "persist changes that are dirty" do
G
George Claghorn 已提交
177
    @book.status = :proposed
178 179 180 181 182 183 184
    assert @book.attribute_changed?(:status)
    @book.status = :written
    assert @book.attribute_changed?(:status)
  end

  test "reverted changes that are not dirty" do
    old_status = @book.status
G
George Claghorn 已提交
185
    @book.status = :proposed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    assert @book.attribute_changed?(:status)
    @book.status = old_status
    assert_not @book.attribute_changed?(:status)
  end

  test "reverted changes are not dirty going from nil to value and back" do
    book = Book.create!(nullable_status: nil)

    book.nullable_status = :married
    assert book.attribute_changed?(:nullable_status)

    book.nullable_status = nil
    assert_not book.attribute_changed?(:nullable_status)
  end

201 202 203 204 205 206
  test "assign non existing value raises an error" do
    e = assert_raises(ArgumentError) do
      @book.status = :unknown
    end
    assert_equal "'unknown' is not a valid status", e.message
  end
207

208 209 210 211 212
  test "NULL values from database should be casted to nil" do
    Book.where(id: @book.id).update_all("status = NULL")
    assert_nil @book.reload.status
  end

213 214
  test "assign nil value" do
    @book.status = nil
215
    assert_nil @book.status
216 217 218 219
  end

  test "assign empty string value" do
    @book.status = ''
220
    assert_nil @book.status
221 222 223 224
  end

  test "assign long empty string value" do
    @book.status = '   '
225
    assert_nil @book.status
226 227
  end

228
  test "constant to access the mapping" do
229 230 231
    assert_equal 0, Book.statuses[:proposed]
    assert_equal 1, Book.statuses["written"]
    assert_equal 2, Book.statuses[:published]
232
  end
R
Robin Dupret 已提交
233

234 235 236
  test "building new objects with enum scopes" do
    assert Book.written.build.written?
    assert Book.read.build.read?
237 238
    assert Book.in_spanish.build.in_spanish?
    assert Book.illustrator_visibility_invisible.build.illustrator_visibility_invisible?
239
  end
R
Robin Dupret 已提交
240

241 242 243
  test "creating new objects with enum scopes" do
    assert Book.written.create.written?
    assert Book.read.create.read?
244 245
    assert Book.in_spanish.create.in_spanish?
    assert Book.illustrator_visibility_invisible.create.illustrator_visibility_invisible?
R
Robin Dupret 已提交
246
  end
247 248

  test "_before_type_cast returns the enum label (required for form fields)" do
249
    if @book.status_came_from_user?
G
George Claghorn 已提交
250
      assert_equal "published", @book.status_before_type_cast
251
    else
G
George Claghorn 已提交
252
      assert_equal "published", @book.status
253
    end
254
  end
255 256 257 258 259 260 261 262 263 264 265 266 267 268

  test "reserved enum names" do
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "books"
      enum status: [:proposed, :written, :published]
    end

    conflicts = [
      :column,     # generates class method .columns, which conflicts with an AR method
      :logger,     # generates #logger, which conflicts with an AR method
      :attributes, # generates #attributes=, which conflicts with an AR method
    ]

    conflicts.each_with_index do |name, i|
F
Franky W 已提交
269
      e = assert_raises(ArgumentError) do
270 271
        klass.class_eval { enum name => ["value_#{i}"] }
      end
F
Franky W 已提交
272
      assert_match(/You tried to define an enum named \"#{name}\" on the model/, e.message)
273 274 275 276 277 278 279 280 281 282 283 284 285 286
    end
  end

  test "reserved enum values" do
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "books"
      enum status: [:proposed, :written, :published]
    end

    conflicts = [
      :new,      # generates a scope that conflicts with an AR class method
      :valid,    # generates #valid?, which conflicts with an AR method
      :save,     # generates #save!, which conflicts with an AR method
      :proposed, # same value as an existing enum
287 288
      :public, :private, :protected, # some important methods on Module and Class
      :name, :parent, :superclass
289 290 291
    ]

    conflicts.each_with_index do |value, i|
F
Franky W 已提交
292
      e = assert_raises(ArgumentError, "enum value `#{value}` should not be allowed") do
293 294
        klass.class_eval { enum "status_#{i}" => [value] }
      end
F
Franky W 已提交
295
      assert_match(/You tried to define an enum named .* on the model/, e.message)
296 297 298 299 300
    end
  end

  test "overriding enum method should not raise" do
    assert_nothing_raised do
T
Tim Fenney 已提交
301
      Class.new(ActiveRecord::Base) do
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
        self.table_name = "books"

        def published!
          super
          "do publish work..."
        end

        enum status: [:proposed, :written, :published]

        def written!
          super
          "do written work..."
        end
      end
    end
  end
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

  test "validate uniqueness" do
    klass = Class.new(ActiveRecord::Base) do
      def self.name; 'Book'; end
      enum status: [:proposed, :written]
      validates_uniqueness_of :status
    end
    klass.delete_all
    klass.create!(status: "proposed")
    book = klass.new(status: "written")
    assert book.valid?
    book.status = "proposed"
    assert_not book.valid?
  end

  test "validate inclusion of value in array" do
    klass = Class.new(ActiveRecord::Base) do
      def self.name; 'Book'; end
      enum status: [:proposed, :written]
      validates_inclusion_of :status, in: ["written"]
    end
    klass.delete_all
    invalid_book = klass.new(status: "proposed")
    assert_not invalid_book.valid?
    valid_book = klass.new(status: "written")
    assert valid_book.valid?
  end
E
Evan Whalen 已提交
345 346

  test "enums are distinct per class" do
347 348 349 350 351 352 353 354
    klass1 = Class.new(ActiveRecord::Base) do
      self.table_name = "books"
      enum status: [:proposed, :written]
    end

    klass2 = Class.new(ActiveRecord::Base) do
      self.table_name = "books"
      enum status: [:drafted, :uploaded]
E
Evan Whalen 已提交
355
    end
356 357 358 359 360 361 362 363

    book1 = klass1.proposed.create!
    book1.status = :written
    assert_equal ['proposed', 'written'], book1.status_change

    book2 = klass2.drafted.create!
    book2.status = :uploaded
    assert_equal ['drafted', 'uploaded'], book2.status_change
E
Evan Whalen 已提交
364 365 366
  end

  test "enums are inheritable" do
367 368 369 370
    subklass1 = Class.new(Book)

    subklass2 = Class.new(Book) do
      enum status: [:drafted, :uploaded]
E
Evan Whalen 已提交
371
    end
372 373 374 375 376 377 378 379

    book1 = subklass1.proposed.create!
    book1.status = :written
    assert_equal ['proposed', 'written'], book1.status_change

    book2 = subklass2.drafted.create!
    book2.status = :uploaded
    assert_equal ['drafted', 'uploaded'], book2.status_change
E
Evan Whalen 已提交
380
  end
381 382 383 384 385 386 387 388 389 390 391 392 393 394

  test "declare multiple enums at a time" do
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "books"
      enum status: [:proposed, :written, :published],
           nullable_status: [:single, :married]
    end

    book1 = klass.proposed.create!
    assert book1.proposed?

    book2 = klass.single.create!
    assert book2.single?
  end
395 396 397 398 399 400 401 402 403 404 405 406 407

  test "query state by predicate with prefix" do
    assert @book.author_visibility_visible?
    assert_not @book.author_visibility_invisible?
    assert @book.illustrator_visibility_visible?
    assert_not @book.illustrator_visibility_invisible?
  end

  test "query state by predicate with custom prefix" do
    assert @book.in_english?
    assert_not @book.in_spanish?
    assert_not @book.in_french?
  end
408
end