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.
@@ -1,7 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class DirtyDummy < ActiveRecord::Base
4
- set_table_name 'dummies'
3
+ DirtyDummy = anonymous_dummy do
5
4
  as_enum :gender, [:male, :female], :dirty => true
6
5
  end
7
6
 
@@ -10,7 +9,7 @@ class DirtyAttributesTest < ActiveSupport::TestCase
10
9
  reload_db
11
10
  end
12
11
 
13
- test "setting using changed? on enum" do
12
+ def test_setting_using_changed_on_enum
14
13
  jane = DirtyDummy.create!(:gender => :female)
15
14
  assert_equal 1, jane.gender_cd
16
15
  jane.gender = :male # operation? =)
@@ -19,7 +18,7 @@ class DirtyAttributesTest < ActiveSupport::TestCase
19
18
  assert_equal true, jane.gender_changed?
20
19
  end
21
20
 
22
- test "access old value via gender_was" do
21
+ def test_access_old_value_via_gender_was
23
22
  john = DirtyDummy.create!(:gender => :male)
24
23
  assert_equal 0, john.gender_cd
25
24
  john.gender = :female
@@ -28,7 +27,7 @@ class DirtyAttributesTest < ActiveSupport::TestCase
28
27
  assert_equal :male, john.gender_was
29
28
  end
30
29
 
31
- test "dirty methods are disabled by default (opt-in)" do
30
+ def test_dirty_methods_are_disabled_by_default
32
31
  no_dirty = Dummy.new
33
32
  assert !no_dirty.respond_to?(:gender_was), "should not respond_to :gender_was"
34
33
  assert !no_dirty.respond_to?(:gender_changed?), "should not respond_to :gender_changed?"
@@ -1,9 +1,9 @@
1
1
  require 'test_helper'
2
2
  require 'simple_enum/enum_hash'
3
3
 
4
- class EnumHashTest < ActiveSupport::TestCase
4
+ class EnumHashTest < MiniTest::Unit::TestCase
5
5
 
6
- test "create new EnumHash instance from array of symbols" do
6
+ def test_create_new_enumhash_instance_from_array_of_symbols
7
7
  genders = SimpleEnum::EnumHash.new [:male, :female]
8
8
 
9
9
  assert_same 0, genders[:male]
@@ -12,32 +12,30 @@ class EnumHashTest < ActiveSupport::TestCase
12
12
  assert_same :female, genders.female(true)
13
13
  end
14
14
 
15
- test "create new EnumHash instance from Hash" do
15
+ def test_create_new_enumhash_instance_from_hash
16
16
  status = SimpleEnum::EnumHash.new :inactive => 0, :active => 1, :archived => 99
17
17
 
18
18
  assert_same 0, status.inactive
19
19
  assert_same 1, status[:active]
20
20
  end
21
21
 
22
- test "create new EnumHash instance from ActiveRecord results" do
22
+ def test_create_new_enumhash_instance_from_query_results
23
23
  reload_db :genders => true
24
- genders = SimpleEnum::EnumHash.new Gender.find(:all)
25
-
26
- male = Gender.find(0)
27
-
28
- assert_same 0, genders[male]
29
- assert_same genders[male], genders[:male]
24
+ genders = SimpleEnum::EnumHash.new Gender.all
25
+
26
+ assert_same 0, genders[@male]
27
+ assert_same genders[@male], genders[:male]
30
28
  assert_same 1, genders.female
31
- assert_equal male, genders.send(:male, true)
29
+ assert_equal @male, genders.send(:male, true)
32
30
  end
33
31
 
34
- test "that EnumHash keys are ordered" do
32
+ def test_that_enumhash_keys_are_ordered
35
33
  ordered = SimpleEnum::EnumHash.new [:alpha, :beta, :gamma, :delta, :epsilon, :zeta, :eta]
36
34
  expected_keys = [:alpha, :beta, :gamma, :delta, :epsilon, :zeta, :eta]
37
35
  assert_equal expected_keys, ordered.keys
38
36
  end
39
37
 
40
- test "that when simple array is merge into a EnumHash, array values are the keys and indicies are values" do
38
+ def test_valid_key_value_association_when_simple_array_is_merged_into_enumhash
41
39
  a = [:a, :b, :c, :d]
42
40
  h = SimpleEnum::EnumHash.new(a)
43
41
 
@@ -48,7 +46,7 @@ class EnumHashTest < ActiveSupport::TestCase
48
46
  assert_equal [:a, :b, :c, :d], h.keys
49
47
  end
50
48
 
51
- test "that an already 'correct' looking array is converted to a hash" do
49
+ def test_that_an_already_correct_looking_array_is_converted_to_hash
52
50
  a = [[:a, 5], [:b, 10]]
53
51
  h = SimpleEnum::EnumHash.new(a)
54
52
 
@@ -57,16 +55,13 @@ class EnumHashTest < ActiveSupport::TestCase
57
55
  assert_equal [:a, :b], h.keys
58
56
  end
59
57
 
60
- test "that an array of ActiveRecords are converted to <obj> => obj.id" do
58
+ def test_that_an_array_of_query_results_are_converted_to_result_ids
61
59
  reload_db :genders => true # reload db
62
- a = Gender.find(:all, :order => :id)
63
-
64
- male = Gender.find(0)
65
- female = Gender.find(1)
60
+ a = Gender.all
66
61
 
67
62
  h = SimpleEnum::EnumHash.new(a)
68
63
 
69
- assert_same 0, h[male]
70
- assert_same 1, h[female]
64
+ assert_same 0, h[@male]
65
+ assert_same 1, h[@female]
71
66
  end
72
67
  end
data/test/finders_test.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  require 'test_helper'
2
2
 
3
- class FindersTest < ActiveSupport::TestCase
3
+ class FindersTest < MiniTest::Unit::TestCase
4
4
  def setup
5
5
  reload_db
6
6
  end
7
7
 
8
- test "find all where :gender = :female" do
9
- girls = Dummy.find :all, :conditions => { :gender_cd => Dummy.genders[:female] }, :order => 'name ASC'
8
+ def test_find_all_female_genders
9
+ girls = Dummy.where(:gender_cd => Dummy.genders[:female]).sort { |a,b| a.name <=> b.name }
10
10
 
11
11
  assert_equal 2, girls.length
12
12
 
@@ -15,8 +15,8 @@ class FindersTest < ActiveSupport::TestCase
15
15
  assert_equal true, girls.first.female?
16
16
  end
17
17
 
18
- test "find all where :word is 'gamma'" do
19
- gammas = Dummy.find :all, :conditions => { :word_cd => Dummy.words(:gamma) }
18
+ def test_find_all_gamma_words
19
+ gammas = Dummy.where(:word_cd => Dummy.words(:gamma)).all
20
20
 
21
21
  assert_equal 1, gammas.length
22
22
  assert_equal 'Chris', gammas.first.name
@@ -25,15 +25,19 @@ class FindersTest < ActiveSupport::TestCase
25
25
  assert_equal :gamma, gammas.first.word
26
26
  end
27
27
 
28
- test "find with string conditions for all :didum = :foo" do
29
- foos = Dummy.find :all, :conditions => ['other = ?', Dummy.didums(:foo)]
28
+ def test_find_all_with_attribute_didum_equal_to_foo
29
+ skip('Not available in Mongoid') if mongoid?
30
+
31
+ foos = Dummy.where('other = ?', Dummy.didums(:foo)).all
30
32
 
31
33
  assert_equal 1, foos.length
32
34
  assert_equal false, foos.first.foobar?
33
35
  end
34
36
 
35
- test "find using insecure inline string conditions" do
36
- men = Dummy.find :all, :conditions => "gender_cd = #{Dummy.genders(:male)}"
37
+ def test_find_using_insecure_inline_string_conditions
38
+ skip('Not available in Mongoid') if mongoid?
39
+
40
+ men = Dummy.where("gender_cd = #{Dummy.genders(:male)}").all
37
41
 
38
42
  assert_equal 1, men.length
39
43
  assert_equal true, men.first.male?
data/test/locales.yml CHANGED
@@ -3,15 +3,23 @@ en:
3
3
  activerecord:
4
4
  errors:
5
5
  models:
6
- simple_enum_test/validated_computer:
6
+ validated_computer:
7
7
  attributes:
8
8
  operating_system:
9
9
  invalid_enum: y u no os?
10
- enums:
11
- dummy:
12
- genders:
13
- female: Girl
14
- didums:
15
- foo:
16
- one: Foo
17
- other: Foos
10
+ mongoid:
11
+ errors:
12
+ models:
13
+ validated_computer:
14
+ attributes:
15
+ operating_system:
16
+ invalid_enum: y u no os?
17
+
18
+ enums:
19
+ dummy:
20
+ genders:
21
+ female: Girl
22
+ didums:
23
+ foo:
24
+ one: Foo
25
+ other: Foos
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class MongoidTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ reload_db
6
+ end
7
+
8
+ def test_creates_a_field_per_default
9
+ skip('Only available in mongoid') unless mongoid?
10
+ klass = anonymous_dummy do
11
+ as_enum :gender, [:male, :female]
12
+ end
13
+ refute_nil klass.new.fields['gender_cd']
14
+ end
15
+
16
+ def test_passing_custom_field_options
17
+ skip('Only available in mongoid') unless mongoid?
18
+ klass = anonymous_dummy do
19
+ field :verify, :type => Integer
20
+ as_enum :gender, [:male, :female], :field => { :type => Integer, :default => 1 }
21
+ end
22
+
23
+ gender_field = klass.new.fields['gender_cd']
24
+ refute_nil gender_field
25
+ assert_equal 1, gender_field.default
26
+ assert_equal klass.fields['verify'].class, gender_field.class
27
+ assert_equal :female, klass.new.gender
28
+ end
29
+
30
+ def test_skip_field_creation_if_field_false
31
+ skip('Only available in mongoid') unless mongoid?
32
+ klass = anonymous_dummy do
33
+ as_enum :gender, [:male, :female], :field => false
34
+ end
35
+
36
+ assert_nil klass.new.fields['gender_cd']
37
+ end
38
+ end
@@ -1,11 +1,11 @@
1
1
  require 'test_helper'
2
2
 
3
- class ObjectBackedTest < ActiveSupport::TestCase
3
+ class ObjectBackedTest < MiniTest::Unit::TestCase
4
4
  def setup
5
5
  reload_db :genders => true
6
6
  end
7
7
 
8
- test "how working with object backed columns work..." do
8
+ def test_how_working_with_object_backed_columns_work
9
9
  # simple object -> not db backed instance
10
10
  simple_obj = Class.new do
11
11
  attr_accessor :name
@@ -13,14 +13,13 @@ class ObjectBackedTest < ActiveSupport::TestCase
13
13
  @name = name
14
14
  end
15
15
  end
16
-
16
+
17
17
  # create new class by using simple_obj
18
- with_object = Class.new(ActiveRecord::Base) do
19
- set_table_name 'dummies'
18
+ with_object = anonymous_dummy do
20
19
  as_enum :gender, { simple_obj.new('Male') => 0, simple_obj.new('Female') => 1 }
21
20
  end
22
-
23
- d = with_object.find_by_name('Anna')
21
+
22
+ d = with_object.where(:name => 'Anna').first
24
23
 
25
24
  assert_same simple_obj, d.gender.class
26
25
  assert_equal 'Female', d.gender.name
@@ -29,37 +28,32 @@ class ObjectBackedTest < ActiveSupport::TestCase
29
28
  assert_same 0, with_object.male
30
29
  end
31
30
 
32
- test "db backed objects, using method described in 'Advanced Rails Recipes - Recipe 61: Look Up Constant Data Efficiently'" do
31
+ def test_db_backed_objects
32
+ # using method described in 'Advanced Rails Recipes - Recipe 61: Look Up Constant Data Efficiently'
33
33
  # "cache" as defined in ARR#61
34
- genders = Gender.find(:all)
34
+ genders = Gender.all
35
35
  # works without mapping... .map { |g| [g, g.id] }
36
36
 
37
37
  # use cached array of values
38
- with_db_obj = Class.new(ActiveRecord::Base) do
39
- set_table_name 'dummies'
38
+ with_db_obj = anonymous_dummy do
40
39
  as_enum :gender, genders
41
40
  end
42
41
 
43
- d = with_db_obj.find_by_name('Bella');
42
+ d = with_db_obj.where(:name => 'Bella').first
44
43
 
45
44
  assert_respond_to with_db_obj, :female
46
45
  assert_respond_to with_db_obj, :male
47
46
  assert_equal 0, with_db_obj.male
48
47
  end
49
48
 
50
- test "that accessing keys and values of each enumeration value works as expected" do
51
- genders = Gender.find(:all, :order => :id)
52
-
53
- male = genders.first
54
- female = genders.last
55
-
56
- with_db_obj = Class.new(ActiveRecord::Base) do
57
- set_table_name 'dummies'
58
- as_enum :gender, genders
49
+ def test_that_accesing_keys_and_values_of_each_enumeration_value_works_as_expected
50
+
51
+ with_db_obj = anonymous_dummy do
52
+ as_enum :gender, Gender.all.to_a
59
53
  end
60
54
 
61
- assert_same male.id, with_db_obj.male
62
- assert_same male, with_db_obj.male(true)
55
+ assert_same 0, with_db_obj.male
56
+ assert_equal @male.id, with_db_obj.male(true).id
63
57
 
64
58
  assert_same :male, Dummy.male(true)
65
59
  assert_same 0, Dummy.male
@@ -0,0 +1,99 @@
1
+ require 'active_record/version'
2
+
3
+ def orm_version
4
+ ActiveRecord::VERSION::STRING
5
+ end
6
+
7
+ def ar32?
8
+ ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MINOR >= 2
9
+ end
10
+
11
+ def setup_db
12
+ # create database connection (in memory db!)
13
+ ActiveRecord::Base.establish_connection({
14
+ :adapter => RUBY_PLATFORM =~ /java/ ? 'jdbcsqlite3' : 'sqlite3',
15
+ :database => ':memory:'})
16
+
17
+ # Fix visitor, for JRuby
18
+ if RUBY_PLATFORM =~ /java/ && ar32?
19
+ ActiveRecord::ConnectionAdapters::SQLiteAdapter.send(:define_method, :visitor) do
20
+ @visitor ||= Arel::Visitors::SQLite.new(self)
21
+ end
22
+ end
23
+ end
24
+
25
+ # Reload database
26
+ def reload_db(options = {})
27
+ ActiveRecord::Base.connection.create_table :dummies, :force => true do |t|
28
+ t.column :name, :string
29
+ t.column :gender_cd, :integer
30
+ t.column :word_cd, :string, :limit => 5
31
+ t.column :other, :integer
32
+ end
33
+
34
+ # Create ref-data table and fill with records
35
+ ActiveRecord::Base.connection.create_table :genders, :force => true do |t|
36
+ t.column :name, :string
37
+ end
38
+
39
+ # Validations
40
+ ActiveRecord::Base.connection.create_table :computers, :force => true do |t|
41
+ t.column :name, :string
42
+ t.column :operating_system_cd, :integer
43
+ t.column :manufacturer_cd, :integer
44
+ end
45
+
46
+ fill_db(options)
47
+ end
48
+
49
+ # Models
50
+ def anonymous_dummy(&block)
51
+ Class.new(ActiveRecord::Base) do
52
+ ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
53
+ instance_eval &block
54
+ end
55
+ end
56
+
57
+ def extend_computer(current_i18n_name = "Computer", &block)
58
+ Class.new(Computer) do
59
+ ar32? ? self.table_name = 'computers' : set_table_name('computers')
60
+ instance_eval &block
61
+ instance_eval <<-RUBY
62
+ def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
63
+ RUBY
64
+ end
65
+ end
66
+
67
+ def named_dummy(class_name, &block)
68
+ begin
69
+ return class_name.constantize
70
+ rescue NameError
71
+ klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
72
+ klass.module_eval do
73
+ ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
74
+ instance_eval &block
75
+ end
76
+
77
+ klass
78
+ end
79
+ end
80
+
81
+ class Dummy < ActiveRecord::Base
82
+ as_enum :gender, [:male, :female]
83
+ as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
84
+ as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
85
+ end
86
+
87
+ class Gender < ActiveRecord::Base
88
+ end
89
+
90
+ class Computer < ActiveRecord::Base
91
+ as_enum :manufacturer, [:dell, :compaq, :apple]
92
+ as_enum :operating_system, [:windows, :osx, :linux, :bsd]
93
+ end
94
+
95
+ # Used to test STI stuff
96
+ class SpecificDummy < Dummy
97
+ ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
98
+ end
99
+
@@ -0,0 +1,23 @@
1
+ class MockName < String
2
+ def name; to_s end
3
+
4
+ def self.mock!(name)
5
+ ActiveModel::Name.new(MockName.new(name))
6
+ end
7
+ end
8
+
9
+ def fill_db(options={})
10
+ options = { :fill => true, :genders => false }.merge(options)
11
+
12
+ if options[:fill]
13
+ # fill db with some rows
14
+ Dummy.create({ :name => 'Anna', :gender_cd => 1, :word_cd => 'alpha', :other => 0})
15
+ Dummy.create({ :name => 'Bella', :gender_cd => 1, :word_cd => 'beta', :other => 1})
16
+ Dummy.create({ :name => 'Chris', :gender_cd => 0, :word_cd => 'gamma', :other => 2})
17
+ end
18
+
19
+ if options[:genders]
20
+ @male = Gender.create!({ :name => 'male' })
21
+ @female = Gender.create!({ :name => 'female' })
22
+ end
23
+ end