simple_enum 1.4.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,101 @@
1
+ require 'mongoid/version'
2
+ require 'simple_enum/mongoid'
3
+
4
+ def orm_version
5
+ Mongoid::VERSION
6
+ end
7
+
8
+ def setup_db
9
+ # create database connection
10
+ Mongoid.configure do |config|
11
+ config.master = Mongo::Connection.new('localhost').db("simple-enum-test-suite")
12
+ config.use_utc = true
13
+ config.include_root_in_json = true
14
+ end
15
+ end
16
+
17
+
18
+ # Reload database
19
+ def reload_db(options = {})
20
+
21
+ # clear collections except system
22
+ Mongoid.master.collections.select do |collection|
23
+ collection.name !~ /system/
24
+ end.each(&:drop)
25
+
26
+ fill_db(options)
27
+ end
28
+
29
+ # models
30
+ def anonymous_dummy(&block)
31
+ Class.new do
32
+ include Mongoid::Document
33
+ include SimpleEnum::Mongoid
34
+ self.collection_name = 'dummies'
35
+ instance_eval &block
36
+ end
37
+ end
38
+
39
+ def extend_computer(current_i18n_name = "Computer", &block)
40
+ Class.new(Computer) do
41
+ self.collection_name = 'computers'
42
+ instance_eval &block
43
+ instance_eval <<-RUBY
44
+ def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
45
+ RUBY
46
+ end
47
+ end
48
+
49
+ def named_dummy(class_name, &block)
50
+ begin
51
+ return class_name.constantize
52
+ rescue NameError
53
+ klass = Object.const_set(class_name, Class.new)
54
+ klass.module_eval do
55
+ include Mongoid::Document
56
+ include SimpleEnum::Mongoid
57
+
58
+ self.collection_name = 'dummies'
59
+ instance_eval &block
60
+ end
61
+
62
+ klass
63
+ end
64
+
65
+ end
66
+
67
+ class Dummy
68
+ include Mongoid::Document
69
+ include SimpleEnum::Mongoid
70
+
71
+ as_enum :gender, [:male, :female]
72
+ as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
73
+ as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
74
+
75
+ before_save :check_typed
76
+
77
+ def check_typed
78
+ attributes['_type'] = nil unless (self.hereditary? || self.polymorphic?)
79
+ end
80
+
81
+ end
82
+
83
+ class Gender
84
+ include Mongoid::Document
85
+ include SimpleEnum::Mongoid
86
+
87
+ field :name, :type => String
88
+ end
89
+
90
+ class Computer
91
+ include Mongoid::Document
92
+ include SimpleEnum::Mongoid
93
+
94
+ field :name, :type => String
95
+
96
+ as_enum :manufacturer, [:dell, :compaq, :apple]
97
+ as_enum :operating_system, [:windows, :osx, :linux, :bsd]
98
+ end
99
+
100
+ # Used to test STI stuff
101
+ class SpecificDummy < Dummy; end
data/test/poro_test.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class POROTest < MiniTest::Unit::TestCase
4
+ class MyPORO
5
+ include SimpleEnum
6
+
7
+ attr_accessor :gender_cd
8
+ as_enum :gender, [:male, :female]
9
+ end
10
+
11
+ def test_reading_and_writing
12
+ poro = POROTest::MyPORO.new
13
+ poro.gender_cd = 1
14
+ assert_equal :female, poro.gender
15
+
16
+ poro.male!
17
+ assert_equal 0, poro.gender_cd
18
+ assert_equal :male, poro.gender
19
+ end
20
+ end
@@ -1,13 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
- class PrefixesTest < ActiveSupport::TestCase
3
+ class PrefixesTest < MiniTest::Unit::TestCase
4
4
  def setup
5
5
  reload_db
6
6
  end
7
7
 
8
- test "set :prefix => true and ensure that 'gender' is prefixed to <symbol>? and <symbol>! methods" do
9
- with_prefix = Class.new(ActiveRecord::Base) do
10
- set_table_name 'dummies'
8
+ def test_that_gender_is_prefixed_in_getters_and_setters
9
+ with_prefix = anonymous_dummy do
11
10
  as_enum :gender, [:male, :female], :prefix => true
12
11
  end
13
12
 
@@ -21,9 +20,8 @@ class PrefixesTest < ActiveSupport::TestCase
21
20
  assert !d.respond_to?(:male!)
22
21
  end
23
22
 
24
- test "set :prefix => 'didum' and ensure that 'didum' is prefix to <symbol>? and <symbol>! methods" do
25
- with_string_prefix = Class.new(ActiveRecord::Base) do
26
- set_table_name 'dummies'
23
+ def test_that_custom_prefix_is_applied_to_getters_and_setters
24
+ with_string_prefix = anonymous_dummy do
27
25
  as_enum :gender, [:male, :female], :prefix => 'didum'
28
26
  end
29
27
 
@@ -1,51 +1,76 @@
1
1
  require 'test_helper'
2
2
 
3
- class SimpleEnumTest < ActiveSupport::TestCase
3
+ class SimpleEnumTest < MiniTest::Unit::TestCase
4
4
  def setup
5
5
  reload_db
6
6
  end
7
7
 
8
- test "reading public enum_definitions" do
8
+ def test_reading_public_enum_definitions
9
9
  assert_equal "gender_cd", Dummy.enum_definitions[:gender][:column]
10
10
  end
11
11
 
12
- test "get the correct integer values when setting to symbol" do
12
+ def test_getting_the_correct_integer_values_when_setting_to_symbol
13
13
  d = Dummy.new
14
14
  d.gender = :male
15
15
  assert_equal(0, d.gender_cd)
16
16
  end
17
17
 
18
- test "get the correct symbol when setting the integer value" do
18
+ def test_getting_the_correct_symbold_when_setting_the_integer_value
19
19
  d = Dummy.new
20
20
  d.gender_cd = 1
21
21
  assert_equal(:female, d.gender)
22
22
  end
23
23
 
24
- test "verify that <symbol>? returns correct result" do
24
+ def test_that_checker_returns_correct_result
25
25
  d = Dummy.new
26
26
  d.gender = :male
27
27
  assert_equal(true, d.male?)
28
28
  assert_equal(false, d.female?)
29
29
  end
30
30
 
31
- test "get symbol when rows are fetched from db" do
31
+ def test_enum_comparisons
32
+ d = Dummy.new
33
+ assert_equal(false, d.gender?)
34
+ d.gender = :male
35
+ assert_equal(true, d.gender?)
36
+ assert_equal(true, d.gender?(:male))
37
+ assert_equal(false, d.gender?(:female))
38
+ assert_equal(false, d.gender?(:whot))
39
+ d.gender = :female
40
+ assert_equal(true, d.gender?(:female))
41
+ assert_equal(false, d.gender?(:male))
42
+ end
43
+
44
+ def test_enum_comparisons_with_strings
45
+ d = Dummy.new(:gender => :male)
46
+ assert_equal(true, d.gender?("male"))
47
+ end
48
+
49
+ def test_enum_comparisons_with_nil_always_returns_false
50
+ d = Dummy.new(:gender => :male)
51
+ assert_equal(false, d.gender?(nil))
52
+ end
53
+
54
+ def test_getting_symbol_when_data_is_fetched_from_datasource
55
+ dummies = Dummy.all
56
+
32
57
  # Anna
33
- assert_equal(:female, Dummy.find(1).gender)
34
- assert_equal(:alpha, Dummy.find(1).word)
35
- assert_equal(:foo, Dummy.find(1).didum)
58
+ assert_equal(:female, dummies[0].gender)
59
+ assert_equal(:alpha, dummies[0].word)
60
+ assert_equal(:foo, dummies[0].didum)
36
61
 
37
62
  # Bella
38
- assert_equal(true, Dummy.find(2).female?)
39
- assert_equal(true, Dummy.find(2).beta?)
40
- assert_equal(:bar, Dummy.find(2).didum)
63
+ assert_equal(true, dummies[1].female?)
64
+ assert_equal(true, dummies[1].beta?)
65
+ assert_equal(:bar, dummies[1].didum)
41
66
 
42
67
  # Chris
43
- assert_equal(false, Dummy.find(3).female?)
44
- assert_equal(:gamma, Dummy.find(3).word)
45
- assert_equal(:foobar, Dummy.find(3).didum)
68
+ assert_equal(false, dummies[2].female?)
69
+ assert_equal(:gamma, dummies[2].word)
70
+ assert_equal(:foobar, dummies[2].didum)
46
71
  end
47
72
 
48
- test "create and save new record then test symbols" do
73
+ def test_creating_and_saving_a_new_datasource_object_then_test_symbols
49
74
  d = Dummy.create({ :name => 'Dummy', :gender_cd => 0 }) # :gender => male
50
75
  assert_equal(true, d.male?)
51
76
 
@@ -55,16 +80,14 @@ class SimpleEnumTest < ActiveSupport::TestCase
55
80
  assert_equal(true, Dummy.find(d.id).female?)
56
81
  end
57
82
 
58
- test "validation :if" do
59
- class ValidateIfComputer < Computer
60
- set_table_name 'computers'
61
-
83
+ def test_validation_if
84
+ validate_if_comp = extend_computer do
62
85
  validates_as_enum :manufacturer, :if => lambda { |computer|
63
86
  computer.name == "Fred"
64
87
  }
65
88
  end
66
89
 
67
- computer = ValidateIfComputer.new(:manufacturer_cd => 48328432)
90
+ computer = validate_if_comp.new(:manufacturer_cd => 48328432)
68
91
 
69
92
  computer.name = nil
70
93
  assert_equal(true, computer.save)
@@ -73,16 +96,14 @@ class SimpleEnumTest < ActiveSupport::TestCase
73
96
  assert_equal(false, computer.save)
74
97
  end
75
98
 
76
- test "validation :unless" do
77
- class ValidateUnlessComputer < Computer
78
- set_table_name 'computers'
79
-
99
+ def test_validation_unless
100
+ validate_unless_comp = extend_computer do
80
101
  validates_as_enum :manufacturer, :unless => lambda { |computer|
81
102
  computer.name == "Unless"
82
103
  }
83
104
  end
84
105
 
85
- computer = ValidateUnlessComputer.new(:manufacturer_cd => 48328432)
106
+ computer = validate_unless_comp.new(:manufacturer_cd => 48328432)
86
107
 
87
108
  computer.name = nil
88
109
  assert_equal(false, computer.save)
@@ -92,14 +113,12 @@ class SimpleEnumTest < ActiveSupport::TestCase
92
113
  assert_equal(true, computer.save)
93
114
  end
94
115
 
95
- test "validation :on => :update" do
96
- class ValidateOnUpdateComputer < Computer
97
- set_table_name 'computers'
98
-
116
+ def test_validation_on_update
117
+ validate_update_comp = extend_computer do
99
118
  validates_as_enum :manufacturer, :on => :update
100
119
  end
101
120
 
102
- computer = ValidateOnUpdateComputer.new(:manufacturer_cd => nil)
121
+ computer = validate_update_comp.new(:manufacturer_cd => nil)
103
122
  assert_equal(true, computer.save)
104
123
 
105
124
  computer.name = 'Something else'
@@ -107,14 +126,12 @@ class SimpleEnumTest < ActiveSupport::TestCase
107
126
  assert_equal(1, computer.errors[:manufacturer].size)
108
127
  end
109
128
 
110
- test "validation :on => :create" do
111
- class ValidateOnCreateComputer < Computer
112
- set_table_name 'computers'
113
-
129
+ def test_validation_on_create
130
+ validate_create_comp = extend_computer do
114
131
  validates_as_enum :manufacturer, :on => :create
115
132
  end
116
133
 
117
- computer = ValidateOnCreateComputer.new(:manufacturer_cd => nil)
134
+ computer = validate_create_comp.new(:manufacturer_cd => nil)
118
135
  assert_equal(false, computer.save)
119
136
  assert_equal(1, computer.errors[:manufacturer].size)
120
137
 
@@ -125,14 +142,12 @@ class SimpleEnumTest < ActiveSupport::TestCase
125
142
  assert_equal(true, computer.save)
126
143
  end
127
144
 
128
- test "validation :allow_nil" do
129
- class ValidateAllowNilComputer < Computer
130
- set_table_name 'computers'
131
-
145
+ def test_validation_allow_nil
146
+ validate_nil_comp = extend_computer do
132
147
  validates_as_enum :manufacturer, :allow_nil => true
133
148
  end
134
149
 
135
- computer = ValidateAllowNilComputer.new(:manufacturer_cd => nil)
150
+ computer = validate_nil_comp.new(:manufacturer_cd => nil)
136
151
  assert_equal(true, computer.save)
137
152
 
138
153
  computer.manufacturer = :apple
@@ -143,37 +158,35 @@ class SimpleEnumTest < ActiveSupport::TestCase
143
158
  assert_equal(1, computer.errors[:manufacturer].size)
144
159
  end
145
160
 
146
- test "default error messages using translations" do
147
- class ValidatedComputer < Computer
148
- set_table_name 'computers'
161
+ def test_default_error_messages_using_translations
162
+ validated_comp = extend_computer("ValidatedComputer") do
149
163
  validates_as_enum :manufacturer
150
164
  validates_as_enum :operating_system
151
165
  end
152
166
 
153
- computer = ValidatedComputer.new
167
+ computer = validated_comp.new
154
168
  assert !computer.save, "save should return false"
155
169
  assert_equal "invalid option supplied.", computer.errors[:manufacturer].first
156
170
  assert_equal "y u no os?", computer.errors[:operating_system].first
157
171
  end
158
172
 
159
- test "allow setting custom error via :message" do
160
- class ValidateMessageComputer < Computer
161
- set_table_name 'computers'
173
+ def test_allow_setting_custom_error_via_message
174
+ validate_msg_comp = extend_computer do
162
175
  validates_as_enum :manufacturer, :message => "invalid manufacturer"
163
176
  end
164
177
 
165
- computer = ValidateMessageComputer.new
178
+ computer = validate_msg_comp.new
166
179
  assert !computer.valid?, "valid? should return false"
167
180
  assert_equal "invalid manufacturer", computer.errors[:manufacturer].first
168
181
  end
169
182
 
170
- test "raises ArgumentError if invalid symbol is passed" do
171
- assert_raise ArgumentError do
183
+ def test_that_argumenterror_is_raised_if_invalid_symbol_is_passed
184
+ assert_raises ArgumentError do
172
185
  Dummy.new :gender => :foo
173
186
  end
174
187
  end
175
188
 
176
- test "raises NO ArgumentError if :whiny => false is defined" do
189
+ def test_that_no_argumenterror_is_raised_if_whiny_is_false
177
190
  not_whiny = Class.new(Dummy) do
178
191
  as_enum :gender, [:male, :female], :whiny => false
179
192
  end
@@ -184,7 +197,7 @@ class SimpleEnumTest < ActiveSupport::TestCase
184
197
  assert_nil(d.gender)
185
198
  end
186
199
 
187
- test "ensure that setting to 'nil' works if :whiny => true and :whiny => false" do
200
+ def test_that_setting_to_nil_works_if_whiny_is_true_or_false
188
201
  d = Dummy.new :gender => :male
189
202
  assert_equal(:male, d.gender)
190
203
  d.gender = nil
@@ -204,13 +217,13 @@ class SimpleEnumTest < ActiveSupport::TestCase
204
217
  assert_nil(d.gender)
205
218
  end
206
219
 
207
- test "deprecation warning when using enum name == column name" do
220
+ def test_deprecation_warning_when_using_enum_name_eq_column_name
208
221
  original_behavior = ActiveSupport::Deprecation.behavior
209
222
  begin
210
223
  expected = 0
211
224
  ActiveSupport::Deprecation.silenced = false
212
225
  ActiveSupport::Deprecation.behavior = Proc.new { |msg, cb| expected += 1 if msg =~ /\[simple_enum\].+gender_cd/ }
213
- invalid_dummy = Class.new(ActiveRecord::Base) do
226
+ invalid_dummy = anonymous_dummy do
214
227
  as_enum :gender_cd, [:male, :female], :column => "gender_cd"
215
228
  end
216
229
 
data/test/test_helper.rb CHANGED
@@ -8,65 +8,38 @@ require 'bundler/setup'
8
8
 
9
9
  require 'test/unit'
10
10
  require 'active_support'
11
- require 'active_record'
12
11
  require 'active_support/version'
13
- require 'active_record/version'
12
+ require 'minitest/autorun'
14
13
 
15
- # create database connection (in memory db!)
16
- ActiveRecord::Base.establish_connection({
17
- :adapter => RUBY_PLATFORM =~ /java/ ? 'jdbcsqlite3' : 'sqlite3',
18
- :database => ':memory:'})
14
+ # setup fake rails env
15
+ ROOT = File.join(File.dirname(__FILE__), '..')
16
+ RAILS_ROOT = ROOT
17
+ RAILS_ENV = 'test'
19
18
 
20
- # load simple_enum
21
- require 'simple_enum'
22
-
23
- # load dummy class
24
- require File.join(File.dirname(__FILE__), 'models')
25
-
26
- # Test environment info
27
- puts "Testing against: activesupport-#{ActiveSupport::VERSION::STRING}, activerecord-#{ActiveRecord::VERSION::STRING}"
19
+ # load orms
20
+ ORM = ENV['ORM'] || 'active_record'
28
21
 
29
- # Add test locales
30
- I18n.load_path << File.join(File.dirname(__FILE__), 'locales.yml')
22
+ def mongoid?; return ORM == 'mongoid';end
23
+ def activerecord?; return ORM == 'active_record';end
31
24
 
32
- # Reload database
33
- def reload_db(options = {})
34
- options = { :fill => true, :genders => false }.merge(options)
35
- ActiveRecord::Base.connection.create_table :dummies, :force => true do |t|
36
- t.column :name, :string
37
- t.column :gender_cd, :integer
38
- t.column :word_cd, :string, :limit => 5
39
- t.column :other, :integer
40
- end
25
+ # load database implemntation
26
+ require ORM
41
27
 
42
- ActiveRecord::Base.connection.create_table :computers, :force => true do |t|
43
- t.column :name, :string
44
- t.column :operating_system_cd, :integer
45
- t.column :manufacturer_cd, :integer
46
- end
28
+ # load simple enum
29
+ require 'simple_enum'
47
30
 
48
- # Create ref-data table and fill with records
49
- ActiveRecord::Base.connection.create_table :genders, :force => true do |t|
50
- t.column :name, :string
51
- end
31
+ # load ORM specific stuff
32
+ require 'orm/common'
33
+ require "orm/#{ORM}"
52
34
 
53
- if options[:fill]
54
- # fill db with some rows
55
- Dummy.create({ :name => 'Anna', :gender_cd => 1, :word_cd => 'alpha', :other => 0})
56
- Dummy.create({ :name => 'Bella', :gender_cd => 1, :word_cd => 'beta', :other => 1})
57
- Dummy.create({ :name => 'Chris', :gender_cd => 0, :word_cd => 'gamma', :other => 2})
58
- end
35
+ # Add test locales
36
+ I18n.load_path << File.join(File.dirname(__FILE__), 'locales.yml')
59
37
 
60
- if options[:genders]
61
- male = Gender.new({ :name => 'male' })
62
- male.id = 0;
63
- male.save!
38
+ # setup db
39
+ setup_db
64
40
 
65
- female = Gender.new({ :name => 'female' })
66
- female.id = 1;
67
- female.save!
68
- end
69
- end
41
+ # Test environment info
42
+ puts "Testing against: activesupport-#{ActiveSupport::VERSION::STRING}, #{ORM.to_s}-#{orm_version}"
70
43
 
71
44
  # do some magic to initialze DB for IRB session
72
45
  if Object.const_defined?('IRB')