mongoid_money_field 3.2.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/money_spec.rb CHANGED
@@ -6,7 +6,7 @@ describe Mongoid::MoneyField do
6
6
 
7
7
  describe 'when money field is required' do
8
8
  it 'should be valid to save when field is filled in' do
9
- dummy = DummyMoneyRequired.new
9
+ dummy = DummyMoneyRequired.new
10
10
  dummy.price = '$10'
11
11
  dummy.should be_valid
12
12
  dummy.save.should eq true
@@ -19,19 +19,11 @@ describe Mongoid::MoneyField do
19
19
  dummy.errors.messages[:price][0].should eq "can't be blank"
20
20
  dummy.save.should eq false
21
21
  end
22
-
23
- it 'should be valid to save when field is filled in but currency is not' do
24
- dummy = DummyMoneyRequired.new
25
- dummy.price_cents = 123
26
- dummy.should be_valid
27
- dummy.save.should eq true
28
- dummy.price_currency.should eq Money.default_currency.iso_code
29
- end
30
22
  end
31
23
 
32
24
  describe 'when value is filled from code' do
33
25
  it 'should raise the error when value consists non digits' do
34
- dummy = DummyNotANumber.new
26
+ dummy = DummyNotANumber.new
35
27
  dummy.price = 'incorrect1'
36
28
  dummy.should_not be_valid
37
29
  dummy.errors.count.should eq 1
@@ -40,7 +32,7 @@ describe Mongoid::MoneyField do
40
32
  end
41
33
 
42
34
  it 'should raise the error when value consists more then one decimal separator' do
43
- dummy = DummyNotANumber.new
35
+ dummy = DummyNotANumber.new
44
36
  dummy.price = '121,212,22'
45
37
  dummy.should_not be_valid
46
38
  dummy.errors.count.should eq 1
@@ -57,7 +49,7 @@ describe Mongoid::MoneyField do
57
49
  end
58
50
 
59
51
  it 'should raise the error when value is not present' do
60
- dummy = DummyNotANumber.new( price: '' )
52
+ dummy = DummyNotANumber.new(price: '')
61
53
  dummy.should_not be_valid
62
54
  dummy.errors.count.should eq 1
63
55
  dummy.errors.messages[:price][0].should eq "is not a number"
@@ -67,8 +59,8 @@ describe Mongoid::MoneyField do
67
59
 
68
60
  describe 'when value is filled from SimpleForm' do
69
61
  it 'should raise the error when value consists non digits' do
70
- dummy = DummyNotANumber.new
71
- dummy.price_plain = 'incorrect1'
62
+ dummy = DummyNotANumber.new
63
+ dummy.price = 'incorrect1'
72
64
  dummy.should_not be_valid
73
65
  dummy.errors.count.should eq 1
74
66
  dummy.errors.messages[:price][0].should eq "is not a number"
@@ -76,8 +68,8 @@ describe Mongoid::MoneyField do
76
68
  end
77
69
 
78
70
  it 'should raise the error when value consists more then one decimal separator' do
79
- dummy = DummyNotANumber.new
80
- dummy.price_plain = '121,212,22'
71
+ dummy = DummyNotANumber.new
72
+ dummy.price = '121,212,22'
81
73
  dummy.should_not be_valid
82
74
  dummy.errors.count.should eq 1
83
75
  dummy.errors.messages[:price][0].should eq "is not a number"
@@ -87,7 +79,7 @@ describe Mongoid::MoneyField do
87
79
 
88
80
  describe 'when value should be a positive number' do
89
81
  it 'should raise the error when value lesser than 1' do
90
- dummy = DummyPositiveNumber.new( price: '-10' )
82
+ dummy = DummyPositiveNumber.new(price: '-10')
91
83
  dummy.should_not be_valid
92
84
  dummy.errors.count.should eq 1
93
85
  dummy.errors.messages[:price][0].should eq "must be greater than 1"
@@ -96,7 +88,15 @@ describe Mongoid::MoneyField do
96
88
  end
97
89
 
98
90
  it 'should raise the error when value lesser than 1' do
99
- dummy = DummyPositiveNumber.new( price: '0' )
91
+ dummy = DummyPositiveNumber.new(price: '-1000')
92
+ dummy.should_not be_valid
93
+ dummy.errors.count.should eq 1
94
+ dummy.errors.messages[:price][0].should eq "must be greater than 1"
95
+ dummy.save.should eq false
96
+ end
97
+
98
+ it 'should raise the error when value lesser than 1' do
99
+ dummy = DummyPositiveNumber.new(price: '0')
100
100
  dummy.should_not be_valid
101
101
  dummy.errors.count.should eq 1
102
102
  dummy.errors.messages[:price][0].should eq "must be greater than 1"
@@ -104,13 +104,19 @@ describe Mongoid::MoneyField do
104
104
  end
105
105
 
106
106
  it 'should be ok when value is greater than 1' do
107
- dummy = DummyPositiveNumber.new( price: '10' )
107
+ dummy = DummyPositiveNumber.new(price: '10')
108
+ dummy.should be_valid
109
+ dummy.save.should eq true
110
+ end
111
+
112
+ it 'should be ok when value is greater than 1' do
113
+ dummy = DummyPositiveNumber.new(price: '1000')
108
114
  dummy.should be_valid
109
115
  dummy.save.should eq true
110
116
  end
111
117
 
112
118
  it 'should be ok when value is not present' do
113
- dummy = DummyPositiveNumber.new( price: '' )
119
+ dummy = DummyPositiveNumber.new(price: '')
114
120
  dummy.should be_valid
115
121
  dummy.save.should eq true
116
122
  end
@@ -133,25 +139,25 @@ describe Mongoid::MoneyField do
133
139
 
134
140
  describe 'when persisting a document with a Money datatype' do
135
141
  it 'should be persisted normally when set as dollars' do
136
- dummy = DummyMoney.new
142
+ dummy = DummyMoney.new
137
143
  dummy.price = '$10'
138
144
  dummy.save.should eq true
139
145
  end
140
-
146
+
141
147
  it 'should be persisted normally when set as cents' do
142
- dummy = DummyMoney.new
148
+ dummy = DummyMoney.new
143
149
  dummy.price = '$9.99'
144
150
  dummy.save.should eq true
145
151
  end
146
-
152
+
147
153
  it 'should be persisted normally when set as Money' do
148
- dummy = DummyMoney.new
154
+ dummy = DummyMoney.new
149
155
  dummy.price = Money.parse(1.23)
150
156
  dummy.save.should eq true
151
157
  end
152
158
 
153
159
  it 'should be possible to set value to nil' do
154
- dummy = DummyMoney.new
160
+ dummy = DummyMoney.new
155
161
  dummy.price = Money.parse(1.23)
156
162
  dummy.save.should eq true
157
163
 
@@ -163,50 +169,50 @@ describe Mongoid::MoneyField do
163
169
  dummy.price.should be_nil
164
170
  end
165
171
  end
166
-
172
+
167
173
  describe 'when accessing a document from the datastore with a Money datatype' do
168
174
  before(:each) do
169
175
  DummyMoney.create(:description => "Test", :price => '9.99')
170
176
  end
171
-
177
+
172
178
  it 'should have a Money value that matches the money value that was initially persisted' do
173
179
  dummy = DummyMoney.first
174
180
  dummy.price.should eq Money.parse('9.99')
175
181
  end
176
182
  end
177
-
183
+
178
184
  describe 'when accessing a document from the datastore with a Money datatype set as money' do
179
185
  before(:each) do
180
- dm = DummyMoney.create(:description => "Test")
186
+ dm = DummyMoney.create(:description => "Test")
181
187
  dm.price = Money.parse('1.23')
182
188
  dm.save!
183
189
  end
184
-
190
+
185
191
  it 'should have a Money value that matches the money value that was initially persisted' do
186
192
  dummy = DummyMoney.first
187
193
  dummy.price.cents.should eq 123
188
194
  end
189
195
  end
190
-
196
+
191
197
  describe 'when accessing a document from the datastore with a Money datatype set as money with mass asignment' do
192
198
  before(:each) do
193
199
  DummyMoney.create(:description => "Test", :price => Money.parse('1.23'))
194
200
  end
195
-
201
+
196
202
  it 'should have a Money value that matches the money value that was initially persisted' do
197
203
  dummy = DummyMoney.first
198
204
  dummy.price.cents.should eq 123
199
205
  end
200
206
  end
201
-
207
+
202
208
  describe 'when accessing a document from the datastore with a Money datatype and empty value' do
203
209
  it 'should be nil' do
204
210
  dummy = DummyMoneyWithoutDefault.new
205
211
  dummy.save.should eq true
206
212
  DummyMoneyWithoutDefault.first.price.should be_nil
207
213
  end
208
-
209
- it 'should be 0 when used with default' do
214
+
215
+ it 'should be 0 when used with default' do
210
216
  dummy = DummyMoney.new
211
217
  dummy.save.should eq true
212
218
  DummyMoney.first.price.cents.should eq 0
@@ -226,7 +232,7 @@ describe Mongoid::MoneyField do
226
232
  dummy = DummyPrices.first
227
233
  dummy.price.currency.iso_code.should eq Money.default_currency.iso_code
228
234
  dummy.price.cents.should eq 100
229
-
235
+
230
236
  dummy.price2.should be_nil
231
237
 
232
238
  dummy.price1.cents.should eq 0
@@ -277,17 +283,25 @@ describe Mongoid::MoneyField do
277
283
  dummy.price.should eq Money.parse('1.23 USD')
278
284
  end
279
285
  end
280
-
286
+
281
287
  describe 'when accessing a document from the datastore with a Money datatype and blank value' do
282
288
  before(:each) do
283
289
  DummyMoney.create(description: "Test", price: '')
284
290
  end
285
-
291
+
286
292
  it 'should be nil' do
287
293
  dummy = DummyMoney.first
288
294
  dummy.price.should be_nil
289
295
  end
290
296
 
297
+ it 'stays nil' do
298
+ dummy = DummyMoney.first
299
+ dummy.price = ''
300
+ dummy.price.should be_nil
301
+ dummy.save.should be_true
302
+ DummyMoney.first.price.should be_nil
303
+ end
304
+
291
305
  it 'should be updated correctly' do
292
306
  dummy = DummyMoney.first
293
307
  dummy.price.should be_nil
@@ -298,7 +312,7 @@ describe Mongoid::MoneyField do
298
312
  dummy.price.cents.should eq 123
299
313
  end
300
314
  end
301
-
315
+
302
316
  describe 'when accessing a document from the datastore with embedded documents with money fields' do
303
317
  before(:each) do
304
318
  o = DummyOrder.new(first_name: 'test')
@@ -306,26 +320,26 @@ describe Mongoid::MoneyField do
306
320
  o.dummy_line_items << DummyLineItem.new({name: 'item 1', price: Money.new(1299)})
307
321
  li = DummyLineItem.new({name: 'item 2', price: Money.new(1499)})
308
322
  o.dummy_line_items.push li
309
-
323
+
310
324
  o.save
311
325
  end
312
-
326
+
313
327
  it 'should have correct value for first item' do
314
328
  o = DummyOrder.first
315
329
  o.dummy_line_items.first.price.should eq Money.parse('12.99')
316
330
  end
317
-
331
+
318
332
  it 'should have correct value for first item' do
319
333
  o = DummyOrder.first
320
334
  o.dummy_line_items.last.price.should eq Money.parse('14.99')
321
- end
335
+ end
322
336
  end
323
-
337
+
324
338
  describe 'when accessing a document from the datastore with multiple Money datatypes' do
325
339
  before(:each) do
326
340
  DummyPrices.create(description: "Test", price3: '1', price1: '1.23', price2: '2.33')
327
341
  end
328
-
342
+
329
343
  it 'should have correct Money value for field 1' do
330
344
  dummy = DummyPrices.first
331
345
  dummy.price1.should eq Money.parse('1.23')
@@ -0,0 +1,62 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ # from https://github.com/RubyMoney/money-rails/blob/master/spec/mongoid/three_spec.rb
6
+
7
+ describe Money do
8
+ let!(:priceable) { Priceable.create(:price => Money.new(100, 'EUR')) }
9
+ let!(:priceable_from_num) { Priceable.create(:price => 1) }
10
+ let!(:priceable_from_string) { Priceable.create(:price => '1 EUR' )}
11
+ let!(:priceable_from_hash) { Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"} )}
12
+ let!(:priceable_from_hash_with_indifferent_access) {
13
+ Priceable.create(:price => {:cents=>100, :currency_iso=>"EUR"}.with_indifferent_access)
14
+ }
15
+
16
+ context "mongoize" do
17
+ it "mongoizes correctly a Money object to a hash of cents and currency" do
18
+ priceable.price.cents.should == 100
19
+ priceable.price.currency.should == Money::Currency.find('EUR')
20
+ end
21
+
22
+ it "mongoizes correctly a Numeric object to a hash of cents and currency" do
23
+ priceable_from_num.price.cents.should == 100
24
+ priceable_from_num.price.currency.should == Money.default_currency
25
+ end
26
+
27
+ it "mongoizes correctly a String object to a hash of cents and currency" do
28
+ priceable_from_string.price.cents.should == 100
29
+ priceable_from_string.price.currency.should == Money::Currency.find('EUR')
30
+ end
31
+
32
+ it "mongoizes correctly a hash of cents and currency" do
33
+ priceable_from_hash.price.cents.should == 100
34
+ priceable_from_hash.price.currency.should == Money::Currency.find('EUR')
35
+ end
36
+
37
+ it "mongoizes correctly a HashWithIndifferentAccess of cents and currency" do
38
+ priceable_from_hash_with_indifferent_access.price.cents.should == 100
39
+ priceable_from_hash_with_indifferent_access.price.currency.should == Money::Currency.find('EUR')
40
+ end
41
+ end
42
+
43
+ context "demongoize" do
44
+ subject { Priceable.first.price }
45
+ it { should be_an_instance_of(Money) }
46
+ it { should == Money.new(100, 'EUR') }
47
+ it "returns nil if a nil value was stored" do
48
+ nil_priceable = Priceable.create(:price => nil)
49
+ nil_priceable.price.should be_nil
50
+ end
51
+ it 'returns nil if an unknown value was stored' do
52
+ zero_priceable = Priceable.create(:price => [])
53
+ zero_priceable.price.should be_nil
54
+ end
55
+ end
56
+
57
+ context "evolve" do
58
+ it "transforms correctly a Money object to a Mongo friendly value" do
59
+ Priceable.where(:price => Money.new(100, 'EUR')).first.should == priceable
60
+ end
61
+ end
62
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  # coding: utf-8
2
2
 
3
3
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
- $LOAD_PATH.unshift(File.dirname(__FILE__))
5
- require 'rspec'
6
- require 'simplecov'
7
- require 'mongoid'
8
4
 
9
- require 'database_cleaner'
5
+ require 'rubygems'
10
6
 
7
+ require 'simplecov'
11
8
  SimpleCov.start
12
9
 
10
+ require 'bundler/setup'
11
+ require 'mongoid'
12
+ require 'database_cleaner'
13
+ require 'mongoid-rspec'
14
+
13
15
  require 'mongoid_money_field'
14
16
 
15
17
  Money.default_currency = Money::Currency.new("RUB")
@@ -18,32 +20,29 @@ Money.default_currency = Money::Currency.new("RUB")
18
20
  # in ./support/ and its subdirectories.
19
21
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
20
22
 
23
+ ENV["MONGOID_ENV"] = "test"
24
+ Mongoid.load!("spec/support/mongoid.yml")
25
+
21
26
  def mongoid3?
22
27
  defined?(Mongoid::VERSION) && Gem::Version.new(Mongoid::VERSION) >= Gem::Version.new('3.0.0.rc')
23
28
  end
24
29
 
25
30
  Mongoid.configure do |config|
26
31
  if mongoid3?
27
- config.sessions[:default] = { :database => 'mongoid_money_field_test', :hosts => ['localhost:27017'] }
32
+ ENV["MONGOID_ENV"] = "test"
33
+ Mongoid.load!("spec/support/mongoid.yml")
28
34
  else
29
35
  config.master = Mongo::Connection.new.db('mongoid_money_field_test')
30
36
  end
31
37
  end
32
38
 
33
- DatabaseCleaner.orm = "mongoid"
34
-
35
39
  RSpec.configure do |config|
36
- config.before(:all) do
40
+ config.before :suite do
37
41
  DatabaseCleaner.strategy = :truncation
38
42
  end
39
-
40
- config.before(:each) do
41
- DatabaseCleaner.start
42
- end
43
-
44
- config.after(:each) do
43
+ config.after :each do
45
44
  DatabaseCleaner.clean
46
45
  end
47
-
46
+ config.include Mongoid::Matchers
48
47
  config.mock_with :rspec
49
48
  end
File without changes
File without changes
@@ -0,0 +1,30 @@
1
+ class Money3
2
+ include Mongoid::Document
3
+ store_in collection: 'compat3'
4
+
5
+ include Mongoid::MoneyField
6
+
7
+ field :description
8
+
9
+ field :price_currency, type: String
10
+ field :price_cents, type: Integer
11
+
12
+ field :price_no_default_currency, type: String
13
+ field :price_no_default_cents, type: Integer
14
+
15
+ field :price_with_fix_currency, type: String
16
+ field :price_with_fix_cents, type: Integer
17
+ end
18
+
19
+ class Money3Compat
20
+ include Mongoid::Document
21
+ store_in collection: 'compat3'
22
+
23
+ include Mongoid::MoneyField
24
+
25
+ field :description
26
+
27
+ money_field :price, default: 0
28
+ money_field :price_no_default
29
+ money_field :price_with_fix, fixed_currency: 'GBP'
30
+ end
@@ -0,0 +1,8 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid_money_field
5
+ hosts:
6
+ - localhost:27017
7
+ options:
8
+ consistency: :strong