enum_extensions 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,240 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class EnumExtensionsTest < ActiveSupport::TestCase
4
- test "truth" do
4
+
5
+ setup do
6
+ @book = books(:awdr)
7
+ end
8
+
9
+ test "module" do
5
10
  assert_kind_of Module, EnumExtensions
6
11
  end
12
+
13
+ test "query state with strings" do
14
+ assert_equal "proposed", @book.status
15
+ assert_equal "unread", @book.read_status
16
+ end
17
+
18
+ test "update by setter" do
19
+ @book.update! status: :written
20
+ assert_equal 'written', @book.status
21
+ end
22
+
23
+ test "direct assignment" do
24
+ @book.status = :written
25
+ assert_equal 'written', @book.status
26
+ end
27
+
28
+ test "assign string value" do
29
+ @book.status = "written"
30
+ assert_equal 'written', @book.status
31
+ end
32
+
33
+ test "enum changed attributes" do
34
+ old_status = @book.status
35
+ @book.status = :published
36
+ assert_equal old_status, @book.changed_attributes[:status]
37
+ end
38
+
39
+ test "enum changes" do
40
+ old_status = @book.status
41
+ @book.status = :published
42
+ assert_equal [old_status, 'published'], @book.changes[:status]
43
+ end
44
+
45
+ test "enum attribute was" do
46
+ old_status = @book.status
47
+ @book.status = :published
48
+ assert_equal old_status, @book.attribute_was(:status)
49
+ end
50
+
51
+ test "enum attribute changed" do
52
+ @book.status = :published
53
+ assert @book.attribute_changed?(:status)
54
+ end
55
+
56
+ test "enum attribute changed to" do
57
+ @book.status = :published
58
+ assert @book.attribute_changed?(:status, to: 'published')
59
+ end
60
+
61
+ test "enum attribute changed from" do
62
+ old_status = @book.status
63
+ @book.status = :published
64
+ assert @book.attribute_changed?(:status, from: old_status)
65
+ end
66
+
67
+ test "enum attribute changed from old status to new status" do
68
+ old_status = @book.status
69
+ @book.status = :published
70
+ assert @book.attribute_changed?(:status, from: old_status, to: 'published')
71
+ end
72
+
73
+ test "enum didn't change" do
74
+ old_status = @book.status
75
+ @book.status = old_status
76
+ assert_not @book.attribute_changed?(:status)
77
+ end
78
+
79
+ test "persist changes that are dirty" do
80
+ @book.status = :published
81
+ assert @book.attribute_changed?(:status)
82
+ @book.status = :written
83
+ assert @book.attribute_changed?(:status)
84
+ end
85
+
86
+ test "reverted changes that are not dirty" do
87
+ old_status = @book.status
88
+ @book.status = :published
89
+ assert @book.attribute_changed?(:status)
90
+ @book.status = old_status
91
+ assert_not @book.attribute_changed?(:status)
92
+ end
93
+
94
+ test "reverted changes are not dirty going from nil to value and back" do
95
+ book = Book.create!(nullable_status: nil)
96
+
97
+ book.nullable_status = :married
98
+ assert book.attribute_changed?(:nullable_status)
99
+
100
+ book.nullable_status = nil
101
+ assert_not book.attribute_changed?(:nullable_status)
102
+ end
103
+
104
+ test "assign non existing value raises an error" do
105
+ e = assert_raises(ArgumentError) do
106
+ @book.status = :unknown
107
+ end
108
+ assert_equal "'unknown' is not a valid status", e.message
109
+ end
110
+
111
+ test "assign nil value" do
112
+ @book.status = nil
113
+ assert @book.status.nil?
114
+ end
115
+
116
+ test "assign empty string value" do
117
+ @book.status = ''
118
+ assert @book.status.nil?
119
+ end
120
+
121
+ test "assign long empty string value" do
122
+ @book.status = ' '
123
+ assert @book.status.nil?
124
+ end
125
+
126
+ test "constant to access the mapping" do
127
+ assert_equal 0, Book.statuses[:proposed]
128
+ assert_equal 1, Book.statuses["written"]
129
+ assert_equal 2, Book.statuses[:published]
130
+ end
131
+
132
+ test "_before_type_cast returns the enum label (required for form fields)" do
133
+ assert_equal "proposed", @book.status_before_type_cast
134
+ end
135
+
136
+ test "reserved enum names" do
137
+ klass = Class.new(ActiveRecord::Base) do
138
+ self.table_name = "books"
139
+ enum status: [:proposed, :written, :published]
140
+ end
141
+
142
+ conflicts = [
143
+ :column, # generates class method .columns, which conflicts with an AR method
144
+ :logger, # generates #logger, which conflicts with an AR method
145
+ :attributes, # generates #attributes=, which conflicts with an AR method
146
+ ]
147
+
148
+ conflicts.each_with_index do |name, i|
149
+ assert_raises(ArgumentError, "enum name `#{name}` should not be allowed") do
150
+ klass.class_eval { enum name => ["value_#{i}"] }
151
+ end
152
+ end
153
+ end
154
+
155
+ test "reserved enum values" do
156
+ klass = Class.new(ActiveRecord::Base) do
157
+ self.table_name = "books"
158
+ enum status: [:proposed, :written, :published]
159
+ end
160
+
161
+ conflicts = [
162
+ :new, # generates a scope that conflicts with an AR class method
163
+ :valid, # generates #valid?, which conflicts with an AR method
164
+ :save, # generates #save!, which conflicts with an AR method
165
+ :proposed, # same value as an existing enum
166
+ :public, :private, :protected, # some important methods on Module and Class
167
+ :name, :parent, :superclass
168
+ ]
169
+
170
+ conflicts.each_with_index do |value, i|
171
+ assert_raises(ArgumentError, "enum value `#{value}` should not be allowed") do
172
+ klass.class_eval { enum "status_#{i}" => [value] }
173
+ end
174
+ end
175
+ end
176
+
177
+ test "validate uniqueness" do
178
+ klass = Class.new(ActiveRecord::Base) do
179
+ def self.name; 'Book'; end
180
+ enum status: [:proposed, :written]
181
+ validates_uniqueness_of :status
182
+ end
183
+ klass.delete_all
184
+ klass.create!(status: "proposed")
185
+ book = klass.new(status: "written")
186
+ assert book.valid?
187
+ book.status = "proposed"
188
+ assert_not book.valid?
189
+ end
190
+
191
+ test "validate inclusion of value in array" do
192
+ klass = Class.new(ActiveRecord::Base) do
193
+ def self.name; 'Book'; end
194
+ enum status: [:proposed, :written]
195
+ validates_inclusion_of :status, in: ["written"]
196
+ end
197
+ klass.delete_all
198
+ invalid_book = klass.new(status: "proposed")
199
+ assert_not invalid_book.valid?
200
+ valid_book = klass.new(status: "written")
201
+ assert valid_book.valid?
202
+ end
203
+
204
+ test "enums are distinct per class" do
205
+ klass1 = Class.new(ActiveRecord::Base) do
206
+ self.table_name = "books"
207
+ enum status: [:proposed, :written]
208
+ end
209
+
210
+ klass2 = Class.new(ActiveRecord::Base) do
211
+ self.table_name = "books"
212
+ enum status: [:drafted, :uploaded]
213
+ end
214
+
215
+ book1 = klass1.proposed.create!
216
+ book1.status = :written
217
+ assert_equal ['proposed', 'written'], book1.status_change
218
+
219
+ book2 = klass2.drafted.create!
220
+ book2.status = :uploaded
221
+ assert_equal ['drafted', 'uploaded'], book2.status_change
222
+ end
223
+
224
+ test "enums are inheritable" do
225
+ subklass1 = Class.new(Book)
226
+
227
+ subklass2 = Class.new(Book) do
228
+ enum status: [:drafted, :uploaded]
229
+ end
230
+
231
+ book1 = subklass1.create! status: :proposed
232
+ book1.status = :written
233
+ assert_equal ['proposed', 'written'], book1.status_change
234
+
235
+ book2 = subklass2.create! status: :drafted
236
+ book2.status = :uploaded
237
+ assert_equal ['drafted', 'uploaded'], book2.status_change
238
+ end
239
+
7
240
  end
@@ -0,0 +1,9 @@
1
+ awdr:
2
+ id: 1
3
+ name: "Agile Web Development with Rails"
4
+ format: "paperback"
5
+
6
+ rfr:
7
+ id: 2
8
+ name: "Ruby for Rails"
9
+ format: "ebook"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesús Dugarte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-03 00:00:00.000000000 Z
11
+ date: 2015-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -71,6 +71,7 @@ files:
71
71
  - test/dummy/app/assets/stylesheets/application.css
72
72
  - test/dummy/app/controllers/application_controller.rb
73
73
  - test/dummy/app/helpers/application_helper.rb
74
+ - test/dummy/app/models/book.rb
74
75
  - test/dummy/app/views/layouts/application.html.erb
75
76
  - test/dummy/bin/bundle
76
77
  - test/dummy/bin/rails
@@ -95,11 +96,18 @@ files:
95
96
  - test/dummy/config/locales/en.yml
96
97
  - test/dummy/config/routes.rb
97
98
  - test/dummy/config/secrets.yml
99
+ - test/dummy/db/development.sqlite3
100
+ - test/dummy/db/migrate/20151002191153_create_books.rb
101
+ - test/dummy/db/schema.rb
102
+ - test/dummy/db/test.sqlite3
103
+ - test/dummy/log/development.log
104
+ - test/dummy/log/test.log
98
105
  - test/dummy/public/404.html
99
106
  - test/dummy/public/422.html
100
107
  - test/dummy/public/500.html
101
108
  - test/dummy/public/favicon.ico
102
109
  - test/enum_extensions_test.rb
110
+ - test/fixtures/books.yml
103
111
  - test/test_helper.rb
104
112
  homepage: http://github.com/jdugarte/activerecord-enum-extensions/
105
113
  licenses:
@@ -121,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
129
  version: '0'
122
130
  requirements: []
123
131
  rubyforge_project:
124
- rubygems_version: 2.4.2
132
+ rubygems_version: 2.4.8
125
133
  signing_key:
126
134
  specification_version: 4
127
135
  summary: Scopeless enums, and some syntactic sugar for string enums
@@ -130,6 +138,7 @@ test_files:
130
138
  - test/dummy/app/assets/stylesheets/application.css
131
139
  - test/dummy/app/controllers/application_controller.rb
132
140
  - test/dummy/app/helpers/application_helper.rb
141
+ - test/dummy/app/models/book.rb
133
142
  - test/dummy/app/views/layouts/application.html.erb
134
143
  - test/dummy/bin/bundle
135
144
  - test/dummy/bin/rails
@@ -154,6 +163,12 @@ test_files:
154
163
  - test/dummy/config/routes.rb
155
164
  - test/dummy/config/secrets.yml
156
165
  - test/dummy/config.ru
166
+ - test/dummy/db/development.sqlite3
167
+ - test/dummy/db/migrate/20151002191153_create_books.rb
168
+ - test/dummy/db/schema.rb
169
+ - test/dummy/db/test.sqlite3
170
+ - test/dummy/log/development.log
171
+ - test/dummy/log/test.log
157
172
  - test/dummy/public/404.html
158
173
  - test/dummy/public/422.html
159
174
  - test/dummy/public/500.html
@@ -161,4 +176,5 @@ test_files:
161
176
  - test/dummy/Rakefile
162
177
  - test/dummy/README.rdoc
163
178
  - test/enum_extensions_test.rb
179
+ - test/fixtures/books.yml
164
180
  - test/test_helper.rb