simple_model 1.2.26 → 1.2.27

Sign up to get free protection for your applications and to get access to all the features.
data/spec/base_spec.rb ADDED
@@ -0,0 +1,176 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe SimpleModel::Base do
4
+ # We need a clean class for each spec
5
+
6
+ around(:each) do |example|
7
+ class BaseTest < SimpleModel::Base; end
8
+
9
+ example.run
10
+
11
+ Object.send(:remove_const,:BaseTest) if defined?(:BaseTest)
12
+ end
13
+
14
+
15
+ context 'action methods' do
16
+ describe '#save' do
17
+ it "should perform the supplied methods" do
18
+ BaseTest.save :test
19
+ BaseTest.has_attribute :foo
20
+ BaseTest.send(:define_method, :test) do
21
+ self.foo = "test"
22
+ return true
23
+ end
24
+
25
+ base_test = BaseTest.new()
26
+
27
+ base_test.save
28
+ base_test.foo.should eql("test")
29
+ end
30
+
31
+ it "should be false if validation fails" do
32
+ BaseTest.save :test
33
+ BaseTest.has_attribute :foo
34
+ BaseTest.validates :foo, :presence => true
35
+ BaseTest.send(:define_method, :test) do
36
+ return true
37
+ end
38
+
39
+
40
+ base_test = BaseTest.new()
41
+ expect(base_test).to_not be_valid
42
+ expect(base_test.save).to eql(false)
43
+ end
44
+ end
45
+
46
+ describe '#save!' do
47
+ it "should perform the supplied methods" do
48
+ BaseTest.save :test
49
+ BaseTest.has_attribute :foo
50
+ BaseTest.send(:define_method, :test) do
51
+ self.foo = "test"
52
+ return true
53
+ end
54
+
55
+ base_test = BaseTest.new()
56
+
57
+ base_test.save!
58
+ base_test.foo.should eql("test")
59
+ end
60
+
61
+ it "should be false if validation fails" do
62
+ BaseTest.save :test
63
+ BaseTest.has_attribute :foo
64
+ BaseTest.validates :foo, :presence => true
65
+ BaseTest.send(:define_method, :test) do
66
+ return true
67
+ end
68
+
69
+
70
+ base_test = BaseTest.new()
71
+ expect(base_test).to_not be_valid
72
+ expect{base_test.save!}.to raise_error(SimpleModel::ValidationError)
73
+ end
74
+ end
75
+
76
+ describe '#destroy' do
77
+ it "should not preform validation by default" do
78
+ BaseTest.destroy :test
79
+ BaseTest.has_attribute :foo
80
+ BaseTest.validates :foo, :presence => true
81
+ BaseTest.send(:define_method, :test) do
82
+ return true
83
+ end
84
+
85
+ base_test = BaseTest.new()
86
+ expect(base_test.destroy).to eql(true)
87
+ end
88
+ end
89
+ end
90
+
91
+
92
+ context 'Callbacks' do
93
+ before(:each) do
94
+ class BaseTest < SimpleModel::Base
95
+ save :my_save_method
96
+ before_validation :set_foo
97
+ after_validation :set_bar
98
+ attr_accessor :foo,:bar
99
+ validates :foo, :presence => true
100
+
101
+
102
+ private
103
+
104
+ def my_save_method
105
+ true
106
+ end
107
+
108
+ def set_foo
109
+ self.foo = "foo"
110
+ end
111
+
112
+ def set_bar
113
+ self.bar = "bar"
114
+ end
115
+ end
116
+ end
117
+
118
+ let(:base_test) { BaseTest.new() }
119
+
120
+ it "should implement ActiveModel::Validations::Callbacks" do
121
+ base_test.valid?
122
+
123
+ expect(base_test.foo).to eql('foo')
124
+ expect(base_test.bar).to eql('bar')
125
+ end
126
+
127
+ it "should implement ActiveModel::Callbacks" do
128
+ base_test.save
129
+
130
+ expect(base_test.foo).to eql('foo')
131
+ expect(base_test.bar).to eql('bar')
132
+ end
133
+ end
134
+
135
+
136
+ context "regression tests" do
137
+ before(:each) do
138
+ class TestStuff < SimpleModel::Base
139
+ has_attribute :bar
140
+ validates_presence_of :bar
141
+ end
142
+
143
+ class NewTestStuff < TestStuff
144
+ has_boolean :foo
145
+ end
146
+
147
+ class OtherStuff < NewTestStuff
148
+ has_attribute :other
149
+ validates_numericality_of :other
150
+ end
151
+
152
+ end
153
+ it "should merge defined attributes when class are inherited" do
154
+ NewTestStuff.attribute_defined?(:bar).blank?.should eql(false)
155
+ NewTestStuff.attribute_defined?(:foo).blank?.should eql(false)
156
+ end
157
+ it "should merge defined attributes when class are inherited" do
158
+ TestStuff.new.respond_to?(:bar_will_change!).should eql(true)
159
+ t = OtherStuff.new
160
+ t.bar = [1,2,4]
161
+ NewTestStuff.new.respond_to?(:bar_will_change!).should eql(true)
162
+ NewTestStuff.new.respond_to?(:foo_will_change!).should eql(true)
163
+ end
164
+
165
+ it "should not throw exception method missing" do
166
+ o = OtherStuff.new
167
+ lambda { o.valid? }.should_not raise_error
168
+ end
169
+
170
+ after(:each) do
171
+ [:OtherStuff,:NewTestStuff].each do |con|
172
+ Object.send(:remove_const,con)
173
+ end
174
+ end
175
+ end
176
+ end
@@ -9,7 +9,7 @@ describe SimpleModel::ErrorHelpers do
9
9
  f = FooErrorClass.new
10
10
  f.errors.add(:foo, "something not great")
11
11
  f.errors.add(:bar, "doh")
12
- lambda {f.errors_for_flash}.should_not raise_error
12
+ expect {f.errors_for_flash}.to_not raise_error
13
13
  end
14
14
  end
15
15
  end
@@ -4,32 +4,32 @@ describe SimpleModel::ExtendCore, 'Float.rb' do
4
4
  include SimpleModel::ExtendCore
5
5
  end
6
6
 
7
- describe SimpleModel::ExtendCore, 'round' do
7
+ describe '#round_to' do
8
8
  it "should return float rounded to specified precision" do
9
- 0.5122.round_to.should eql(1.0)
10
- 0.3333.round_to(1).should eql(0.3)
11
- 0.33335.round_to(2).should eql(0.33)
12
- 0.33335.round_to(4).should eql(0.3334)
9
+ expect(0.5122.round_to).to eql(1.0)
10
+ expect(0.3333.round_to(1)).to eql(0.3)
11
+ expect(0.33335.round_to(2)).to eql(0.33)
12
+ expect(0.33335.round_to(4)).to eql(0.3334)
13
13
  end
14
14
  end
15
15
 
16
- describe SimpleModel::ExtendCore, 'to_currency_s' do
17
- it "should return a string" do
18
- 0.333.to_currency_s.class.should eql(String)
19
- end
16
+ describe '#to_currency_s' do
17
+ it { expect(0.333.to_currency_s).to be_a(String) }
18
+
20
19
  it "should prefix string with currency symbol" do
21
- 5.12.to_currency_s.include?("$").should eql(true)
20
+ expect(5.12.to_currency_s.include?("$")).to eql(true)
22
21
  end
23
- it "should padd with zeros for cents" do
24
- 5.0.to_currency_s.should eql("$5.00")
22
+
23
+ it "should pad with zeros for cents" do
24
+ expect(5.0.to_currency_s).to eql("$5.00")
25
25
  end
26
26
  it "should round string to nearest tenth" do
27
- 0.333.to_currency_s.should eql("$0.33")
27
+ expect(0.333.to_currency_s).to eql("$0.33")
28
28
  end
29
29
  it "should add commas to long numbers" do
30
- 500000000000.0.to_currency_s.should eql("$500,000,000,000.00")
31
- 50000000000.0.to_currency_s.should eql("$50,000,000,000.00")
32
- 5000000000.0.to_currency_s.should eql("$5,000,000,000.00")
30
+ expect(500000000000.0.to_currency_s).to eql("$500,000,000,000.00")
31
+ expect(50000000000.0.to_currency_s).to eql("$50,000,000,000.00")
32
+ expect(5000000000.0.to_currency_s).to eql("$5,000,000,000.00")
33
33
  end
34
34
  end
35
35
  end
@@ -38,60 +38,61 @@ describe SimpleModel::ExtendCore, 'String.rb' do
38
38
  before(:all) do
39
39
  include SimpleModel::ExtendCore
40
40
  end
41
- context '#safe_datetime_string' do
41
+ describe '#safe_datetime_string' do
42
42
  it "should set US formated datetime string to international" do
43
- "12/31/2010".safe_datetime_string.should eql("2010-12-31")
44
- "12/31/2010T23:31:59".safe_datetime_string.should eql("2010-12-31T23:31:59")
45
- "12/31/2010 23:31:59".safe_datetime_string.should eql("2010-12-31 23:31:59")
43
+ expect("12/31/2010".safe_datetime_string).to eql("2010-12-31")
44
+ expect("12/31/2010T23:31:59".safe_datetime_string).to eql("2010-12-31T23:31:59")
45
+ expect("12/31/2010 23:31:59".safe_datetime_string).to eql("2010-12-31 23:31:59")
46
46
  end
47
47
  end
48
-
49
- context '#to_b' do
48
+
49
+ describe '#to_b' do
50
50
  it "should return a Boolean" do
51
- "1".to_b.class.should eql(TrueClass)
52
- "".to_b.class.should eql(FalseClass)
51
+ expect("1".to_b.class).to eql(TrueClass)
52
+ expect("".to_b.class).to eql(FalseClass)
53
53
  end
54
54
  it "should return true if string is '1' or 't' or 'true'"do
55
55
  ['1','t','true'].each do |s|
56
- s.to_b.should eql(true)
56
+ expect(s.to_b).to eql(true)
57
57
  end
58
- end
58
+ end
59
59
  end
60
60
 
61
- context '#to_date' do
62
- it "should handle US formatted date strings" do
63
- lambda {"12/31/2010".to_date}.should_not raise_error
64
- "12/31/2010".to_date.class.should eql(Date)
65
- "12/31/2010".to_date.should eql(Date.parse("2010-12-31"))
66
- end
67
- it "should handle C# JSON datetime stamp" do
68
- lambda {"\/Date(1310669017000)\/".to_date}.should_not raise_error
69
- "\/Date(1310669017000)\/".to_date.should be_kind_of(Date)
70
- "\/Date(1310669017000)\/".to_date.should eql(Date.parse("2011-07-14"))
61
+ describe '#to_date' do
62
+ context "US formatted date strings" do
63
+ it {expect {"12/31/2010".to_date}.to_not raise_error }
64
+ it {expect("12/31/2010".to_date).to be_a(Date)}
65
+ it {expect("12/31/2010".to_date).to eql(Date.parse("2010-12-31"))}
66
+ end
67
+ context "C# JSON datetime stamp" do
68
+ it { expect {"\/Date(1310669017000)\/".to_date}.to_not raise_error }
69
+ it { expect("\/Date(1310669017000)\/".to_date).to be_a(Date) }
70
+ it { expect("\/Date(1310669017000)\/".to_date).to eql(Date.parse("2011-07-14")) }
71
71
  end
72
72
  end
73
-
74
- context '#to_time' do
75
- it "should handle US formatted date strings" do
76
- lambda {"12/31/2010 12:00:00".to_time}.should_not raise_error
77
- "12/31/2010 12:00:00".to_time.should be_kind_of(Time)
78
- "12/31/2010 12:00:00".to_time.should eql(Time.parse("2010-12-31 12:00:00"))
73
+
74
+ describe '#to_time' do
75
+ context "US formatted date strings" do
76
+ it { expect {"12/31/2010 12:00:00".to_time}.to_not raise_error }
77
+ it { expect("12/31/2010 12:00:00".to_time).to be_kind_of(Time) }
78
+ it { expect("12/31/2010 12:00:00".to_time).to eql(Time.parse("2010-12-31 12:00:00")) }
79
79
  end
80
- it "should handle C# JSON datetime stamp" do
81
- lambda {"\/Date(1310669017000)\/".to_time}.should_not raise_error
82
- "\/Date(1310669017000)\/".to_time.should be_kind_of(Time)
80
+
81
+ context "C# JSON datetime stamp" do
82
+ it { expect {"\/Date(1310669017000)\/".to_time}.to_not raise_error }
83
+ it { expect("\/Date(1310669017000)\/".to_time).to be_kind_of(Time) }
83
84
  end
84
85
  end
85
-
86
- context '#to_f' do
87
- it "return a Float from a string that may contain non-numeric values" do
88
- "$5,000.006".to_f.should eql(5000.006)
86
+
87
+ describe '#to_f' do
88
+ context "contains non-numeric values" do
89
+ it { expect("$5,000.006".to_f).to eql(5000.006) }
89
90
  end
90
91
  end
91
-
92
- context '#to_currency' do
93
- it "return a BigDecimal from a string that may contain non-numeric values" do
94
- "$5,000.006".to_currency.should eql(BigDecimal("5000.006"))
92
+
93
+ describe '#to_currency' do
94
+ context "contains non-numeric values" do
95
+ it { expect("$5,000.006".to_currency).to eql(BigDecimal("5000.006")) }
95
96
  end
96
97
  end
97
98
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.26
4
+ version: 1.2.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua T Mckinney
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-15 00:00:00.000000000 Z
11
+ date: 2014-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -125,9 +125,9 @@ files:
125
125
  - lib/simple_model/version.rb
126
126
  - simple_model.gemspec
127
127
  - spec/attributes_spec.rb
128
+ - spec/base_spec.rb
128
129
  - spec/error_helpers_spec.rb
129
130
  - spec/extend_core_spec.rb
130
- - spec/simple_model_spec.rb
131
131
  - spec/spec_helper.rb
132
132
  homepage: ''
133
133
  licenses: []
@@ -154,7 +154,7 @@ specification_version: 4
154
154
  summary: Simpifies building tableless models or models backed by webservices
155
155
  test_files:
156
156
  - spec/attributes_spec.rb
157
+ - spec/base_spec.rb
157
158
  - spec/error_helpers_spec.rb
158
159
  - spec/extend_core_spec.rb
159
- - spec/simple_model_spec.rb
160
160
  - spec/spec_helper.rb
@@ -1,247 +0,0 @@
1
- require 'spec_helper.rb'
2
-
3
- describe SimpleModel do
4
- context 'action methods' do
5
- describe "save" do
6
- it "should perform the supplied methods" do
7
- class TestStuff < SimpleModel::Base
8
- save :test
9
- attr_accessor :foo
10
-
11
- def test
12
- self.foo = "test"
13
- return true
14
- end
15
- end
16
-
17
- t = TestStuff.new
18
- t.save
19
- t.foo.should eql("test")
20
- end
21
-
22
- it "should be false if validation fails" do
23
- class TestStuff < SimpleModel::Base
24
- save :test
25
- has_decimal :price
26
- validates_inclusion_of :price, :in => 10..25
27
- validates :price, :presence => true
28
-
29
-
30
- def test
31
- true
32
- end
33
- end
34
-
35
- t = TestStuff.new
36
- t.save.should eql(false)
37
- end
38
- end
39
-
40
- describe "destroy" do
41
- it "should not preform validation by default" do
42
- class TestStuff < SimpleModel::Base
43
- destroy :test
44
- attr_accessor :foo
45
- validates :foo, :presence => true
46
- def test
47
- return true
48
- end
49
- end
50
-
51
- t = TestStuff.new
52
- t.destroy.should eql(true)
53
- end
54
- end
55
- context "action methods that end with '!'" do
56
- it 'should raise exception if validation fails' do
57
- class TestStuff < SimpleModel::Base
58
- save :my_save_method
59
- has_attributes :foo
60
-
61
- def my_save_method
62
- false
63
- end
64
- end
65
-
66
- t = TestStuff.new
67
- lambda {t.save!}.should raise_error(SimpleModel::ActionError)
68
- end
69
- it 'should raise exception if validation fails' do
70
- class TestStuff < SimpleModel::Base
71
- save :my_save_method
72
- has_attributes :foo
73
- validate :validates_bar
74
-
75
- def my_save_method
76
- self.errors.blank?
77
- end
78
-
79
- def validates_bar
80
- self.errors.add(:foo, "bar")
81
- end
82
- end
83
-
84
- t = TestStuff.new
85
- lambda {t.save!}.should raise_error(SimpleModel::ValidationError)
86
- end
87
-
88
- end
89
- end
90
-
91
-
92
- it 'Should add a boolean setter' do
93
- class TestStuff < SimpleModel::Base
94
- has_booleans :test_boolean
95
- end
96
- TestStuff.new.methods.include?(:test_boolean).should eql(true)
97
- #a.test.should eql(false)
98
- end
99
- it 'Should add a boolean setter' do
100
- class TestStuff < SimpleModel::Base
101
- has_booleans :test_boolean
102
- end
103
- t = TestStuff.new
104
- t.methods.include?(:test_boolean).should eql(true)
105
- t.test_boolean = true
106
- t.test_boolean.should eql(true)
107
- #a.test.should eql(false)
108
- end
109
- it 'Should add a error setter' do
110
-
111
- class TestStuff < SimpleModel::Base
112
- has_attributes :test_attr
113
- end
114
- a = TestStuff.new
115
-
116
- a.errors.add(:test_attr, "test")
117
- a.errors?.should eql(true)
118
- end
119
-
120
- it 'Should include validation callbacks' do
121
- class TestStuff < SimpleModel::Base
122
- end
123
- TestStuff.respond_to?(:before_validation).should eql(true)
124
- TestStuff.respond_to?(:after_save).should eql(true)
125
-
126
- end
127
-
128
- it 'Should perform validation callbacks' do
129
- class TestStuff < SimpleModel::Base
130
- before_validation :set_foo
131
- after_validation :set_bar
132
- attr_accessor :foo,:bar
133
- validates :foo, :presence => true
134
-
135
-
136
- private
137
- def set_foo
138
- self.foo = "foo"
139
- end
140
-
141
- def set_bar
142
- self.bar = "bar"
143
- end
144
- end
145
-
146
- t = TestStuff.new
147
- t.valid?
148
- t.foo.should eql('foo')
149
- t.bar.should eql('bar')
150
- end
151
-
152
- it "should run save and validation callbacks on save" do
153
- class TestStuff < SimpleModel::Base
154
- save :my_save_method
155
- before_save :set_foo
156
- after_validation :set_bar
157
- attr_accessor :foo,:bar
158
- validates :foo, :presence => true
159
-
160
-
161
- private
162
-
163
- def my_save_method
164
- true
165
- end
166
-
167
- def set_foo
168
- self.foo = "foo"
169
- end
170
-
171
- def set_bar
172
- self.bar = "bar"
173
- end
174
- end
175
-
176
- t = TestStuff.new
177
- t.save
178
- t.foo.should eql('foo')
179
- t.bar.should eql('bar')
180
- end
181
-
182
- it 'Should implement ActiveModel::Dirty' do
183
- class TestStuff < SimpleModel::Base
184
- save :my_save_method
185
- has_attributes :foo,:bar, :default => "def"
186
- has_boolean :boo,:bad, :default => true
187
- def my_save_method
188
- true
189
- end
190
- end
191
-
192
- t = TestStuff.new
193
- t.foo = "bar"
194
- t.foo_changed?.should eql(true)
195
- t.respond_to?(:foo_will_change!).should eql(true)
196
- t.respond_to?(:boo_will_change!).should eql(true)
197
- t.foo_change.should eql(["def","bar"])
198
- t.changed?.should eql(true)
199
- t.save
200
- t.changed?.should eql(false)
201
- end
202
-
203
- context "regression tests" do
204
- before(:each) do
205
- class TestStuff < SimpleModel::Base
206
- has_attribute :bar
207
- validates_presence_of :bar
208
- end
209
-
210
- class NewTestStuff < TestStuff
211
- has_boolean :foo
212
- end
213
-
214
- class OtherStuff < NewTestStuff
215
- has_attribute :other
216
- validates_numericality_of :other
217
- end
218
-
219
- end
220
- it "should merge defined attributes when class are inherited" do
221
- NewTestStuff.attribute_defined?(:bar).blank?.should eql(false)
222
- NewTestStuff.attribute_defined?(:foo).blank?.should eql(false)
223
- end
224
- it "should merge defined attributes when class are inherited" do
225
- TestStuff.new.respond_to?(:bar_will_change!).should eql(true)
226
- t = OtherStuff.new
227
- t.bar = [1,2,4]
228
- NewTestStuff.new.respond_to?(:bar_will_change!).should eql(true)
229
- NewTestStuff.new.respond_to?(:foo_will_change!).should eql(true)
230
- end
231
-
232
- it "should not throw exception method missing" do
233
- o = OtherStuff.new
234
- lambda { o.valid? }.should_not raise_error
235
- end
236
-
237
- after(:each) do
238
- [:OtherStuff,:NewTestStuff].each do |con|
239
- Object.send(:remove_const,con)
240
- end
241
- end
242
- end
243
-
244
- after(:each) do
245
- Object.send(:remove_const,:TestStuff)
246
- end
247
- end