acvwilson-acvwilson-currency 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/currency.gemspec +5 -5
- data/lib/currency.rb +2 -2
- data/lib/currency/config.rb +26 -24
- data/lib/currency/core_extensions.rb +0 -15
- data/lib/currency/currency.rb +3 -6
- data/lib/currency/currency/factory.rb +14 -4
- data/lib/currency/exchange.rb +0 -2
- data/lib/currency/exchange/rate/source/base.rb +2 -2
- data/lib/currency/exchange/rate/source/federal_reserve.rb +3 -12
- data/lib/currency/exchange/rate/source/provider.rb +7 -10
- data/lib/currency/formatter.rb +9 -1
- data/lib/currency/money.rb +4 -7
- data/spec/ar_column_spec.rb +36 -49
- data/spec/ar_simple_spec.rb +9 -17
- data/spec/ar_spec_helper.rb +127 -0
- data/spec/config_spec.rb +44 -3
- data/spec/federal_reserve_spec.rb +55 -59
- data/spec/historical_writer_spec.rb +74 -140
- data/spec/macro_spec.rb +8 -8
- data/spec/money_spec.rb +1 -1
- data/spec/new_york_fed_spec.rb +11 -11
- data/spec/parser_spec.rb +14 -14
- data/spec/spec_helper.rb +6 -6
- data/spec/timed_cache_spec.rb +23 -23
- metadata +4 -5
- data/spec/ar_base_spec.rb +0 -140
- data/spec/ar_core_spec.rb +0 -64
@@ -1,56 +1,42 @@
|
|
1
1
|
# Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
|
2
2
|
# See LICENSE.txt for details.
|
3
|
+
require File.dirname(__FILE__) + '/ar_spec_helper'
|
3
4
|
|
4
|
-
require '
|
5
|
+
require File.dirname(__FILE__) + '/../lib/currency/exchange/rate/source/historical'
|
6
|
+
require File.dirname(__FILE__) + '/../lib/currency/exchange/rate/source/historical/writer'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/currency/exchange/rate/source/xe'
|
8
|
+
require File.dirname(__FILE__) + '/../lib/currency/exchange/rate/source/new_york_fed'
|
5
9
|
|
6
|
-
|
7
|
-
|
8
|
-
|
10
|
+
include Currency
|
11
|
+
RATE_CLASS = Exchange::Rate::Source::Historical::Rate
|
12
|
+
TABLE_NAME = RATE_CLASS.table_name
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
require 'currency/exchange/rate/source/historical/writer'
|
14
|
-
require 'currency/exchange/rate/source/xe'
|
15
|
-
require 'currency/exchange/rate/source/new_york_fed'
|
16
|
-
|
17
|
-
|
18
|
-
module Currency
|
19
|
-
|
20
|
-
class HistoricalWriterTest < ArTestBase
|
21
|
-
|
22
|
-
RATE_CLASS = Exchange::Rate::Source::Historical::Rate
|
23
|
-
TABLE_NAME = RATE_CLASS.table_name
|
24
|
-
|
25
|
-
class HistoricalRateMigration < AR_M
|
26
|
-
def self.up
|
27
|
-
RATE_CLASS.__create_table(self)
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.down
|
31
|
-
drop_table TABLE_NAME.intern
|
32
|
-
end
|
14
|
+
class HistoricalRateMigration < ActiveRecord::Migration
|
15
|
+
def self.up
|
16
|
+
RATE_CLASS.__create_table(self)
|
33
17
|
end
|
34
18
|
|
35
|
-
|
36
|
-
|
37
|
-
@currency_test_migration = HistoricalRateMigration
|
38
|
-
super
|
39
|
-
|
40
|
-
@src = Exchange::Rate::Source::Xe.new
|
41
|
-
@src2 = Exchange::Rate::Source::NewYorkFed.new
|
19
|
+
def self.down
|
20
|
+
drop_table TABLE_NAME.to_sym
|
42
21
|
end
|
22
|
+
end
|
43
23
|
|
44
|
-
|
45
|
-
before do
|
46
|
-
|
47
|
-
|
24
|
+
describe "HistoricalWriter" do
|
25
|
+
before(:all) do
|
26
|
+
ActiveRecord::Base.establish_connection(database_spec)
|
27
|
+
@currency_test_migration ||= HistoricalRateMigration
|
28
|
+
schema_down
|
29
|
+
schema_up
|
30
|
+
@xe_src = Exchange::Rate::Source::Xe.new
|
31
|
+
@fed_src = Exchange::Rate::Source::NewYorkFed.new
|
48
32
|
end
|
49
|
-
|
50
33
|
|
51
|
-
|
52
|
-
|
53
|
-
|
34
|
+
after(:all) do
|
35
|
+
schema_down
|
36
|
+
end
|
37
|
+
|
38
|
+
def setup_writer(source)
|
39
|
+
writer = Exchange::Rate::Source::Historical::Writer.new
|
54
40
|
writer.time_quantitizer = :current
|
55
41
|
writer.required_currencies = [ :USD, :GBP, :EUR, :CAD ]
|
56
42
|
writer.base_currencies = [ :USD ]
|
@@ -58,130 +44,78 @@ class HistoricalWriterTest < ArTestBase
|
|
58
44
|
writer.reciprocal_rates = true
|
59
45
|
writer.all_rates = true
|
60
46
|
writer.identity_rates = false
|
61
|
-
|
47
|
+
writer.source = source
|
62
48
|
writer
|
63
49
|
end
|
64
50
|
|
65
|
-
|
66
|
-
|
67
|
-
writer = test_writer
|
68
|
-
writer.source = @src
|
69
|
-
rates = writer.write_rates
|
70
|
-
rates.should.not == nil
|
71
|
-
rates.size.should > 0
|
72
|
-
12, rates.size.should.not == nil
|
73
|
-
assert_h_rates(rates, writer)
|
74
|
-
end
|
75
|
-
|
76
|
-
|
77
|
-
def writer_src2
|
78
|
-
writer = test_writer
|
79
|
-
writer.source = @src2
|
80
|
-
return unless writer.source.available?
|
51
|
+
it "can read, parse and write XE data" do
|
52
|
+
writer = setup_writer(@xe_src)
|
81
53
|
rates = writer.write_rates
|
82
|
-
rates.should.not == nil
|
83
|
-
rates.size.should > 0
|
84
54
|
rates.size.should == 12
|
85
55
|
assert_h_rates(rates, writer)
|
86
56
|
end
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
assert_raises(::RuntimeError) { writer.selected_rates }
|
96
|
-
end
|
97
|
-
|
98
|
-
|
99
|
-
it "historical rates" do
|
100
|
-
# Make sure there are historical Rates avail for today.
|
101
|
-
writer_src
|
102
|
-
writer_src2
|
103
|
-
|
104
|
-
# Force Historical Rate Source.
|
105
|
-
source = Exchange::Rate::Source::Historical.new
|
106
|
-
deriver = Exchange::Rate::Deriver.new(:source => source)
|
107
|
-
Exchange::Rate::Source.default = deriver
|
108
|
-
|
109
|
-
rates = source.get_raw_rates.should.not == nil
|
110
|
-
rates.empty?.should.not == true
|
111
|
-
# $stderr.puts "historical rates = #{rates.inspect}"
|
112
|
-
|
113
|
-
rates = source.get_rates.should.not == nil
|
114
|
-
rates.empty?.should.not == true
|
115
|
-
# $stderr.puts "historical rates = #{rates.inspect}"
|
116
|
-
|
117
|
-
m_usd = ::Currency.Money('1234.56', :USD, :now).should.not == nil
|
118
|
-
# $stderr.puts "m_usd = #{m_usd.to_s(:code => true)}"
|
119
|
-
m_eur = m_usd.convert(:EUR).should.not == nil
|
120
|
-
# $stderr.puts "m_eur = #{m_eur.to_s(:code => true)}"
|
121
|
-
|
57
|
+
|
58
|
+
it "can read, parse and write NewYorkFed data (will fail on weekends)" do
|
59
|
+
if @fed_src.available?
|
60
|
+
writer = setup_writer(@fed_src)
|
61
|
+
rates = writer.write_rates
|
62
|
+
rates.size.should == 12
|
63
|
+
assert_h_rates(rates, writer)
|
64
|
+
end
|
122
65
|
end
|
123
|
-
|
124
|
-
|
66
|
+
|
125
67
|
def assert_h_rates(rates, writer = nil)
|
126
|
-
hr0 = rates[0]
|
68
|
+
hr0 = rates[0]
|
69
|
+
hr0.should_not be_nil
|
127
70
|
rates.each do | hr |
|
128
71
|
found_hr = nil
|
129
72
|
begin
|
130
|
-
found_hr = hr.find_matching_this(:first)
|
73
|
+
found_hr = hr.find_matching_this(:first)
|
74
|
+
found_hr.should_not be_nil
|
131
75
|
rescue Object => err
|
132
76
|
raise "#{hr.inspect}: #{err}:\n#{err.backtrace.inspect}"
|
133
77
|
end
|
134
|
-
|
135
|
-
hr0.should.not == nil
|
136
|
-
|
137
|
-
hr.date.should == hr0.date
|
138
|
-
hr.date_0.should == hr0.date_0
|
139
|
-
hr.date_1.should == hr0.date_1
|
140
|
-
hr.source.should == hr0.source
|
141
|
-
|
142
78
|
assert_equal_rate(hr, found_hr)
|
143
79
|
assert_rate_defaults(hr, writer)
|
80
|
+
|
81
|
+
hr.instance_eval do
|
82
|
+
date.should == hr0.date
|
83
|
+
date_0.should == hr0.date_0
|
84
|
+
date_1.should == hr0.date_1
|
85
|
+
source.should == hr0.source
|
86
|
+
end
|
144
87
|
end
|
145
88
|
end
|
146
89
|
|
147
|
-
|
148
90
|
def assert_equal_rate(hr0, hr)
|
91
|
+
tollerance = 0.00001
|
149
92
|
hr.c1.to_s.should == hr0.c1.to_s
|
150
93
|
hr.c2.to_s.should == hr0.c2.to_s
|
151
94
|
hr.source.should == hr0.source
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
95
|
+
|
96
|
+
hr0.rate.should be_close(hr.rate, tollerance)
|
97
|
+
hr0.rate_avg.should be_close(hr.rate_avg, tollerance)
|
98
|
+
hr0.rate_samples.should == hr.rate_samples.should
|
99
|
+
|
100
|
+
hr0.rate_lo.should be_close(hr.rate_lo, tollerance)
|
101
|
+
hr0.rate_hi.should be_close(hr.rate_hi, tollerance)
|
102
|
+
hr0.rate_date_0.should be_close(hr.rate_date_0, tollerance)
|
103
|
+
hr0.rate_date_1.should be_close(hr.rate_date_1, tollerance)
|
104
|
+
|
105
|
+
hr0.date.should == hr.date
|
106
|
+
hr0.date_0.should == hr.date_0
|
107
|
+
hr0.date_1.should == hr.date_1
|
108
|
+
hr0.derived.should == hr.derived
|
163
109
|
end
|
164
110
|
|
165
|
-
|
166
111
|
def assert_rate_defaults(hr, writer)
|
167
|
-
hr.source
|
168
|
-
hr.rate_avg.should
|
169
|
-
|
170
|
-
hr.rate_lo.should
|
171
|
-
hr.rate_hi.should
|
172
|
-
hr.rate_date_0.should
|
173
|
-
hr.rate_date_1.should
|
112
|
+
hr.source.should == writer.source.name
|
113
|
+
hr.rate_avg.should == hr.rate
|
114
|
+
hr.rate_samples.should == 1
|
115
|
+
hr.rate_lo.should == hr.rate
|
116
|
+
hr.rate_hi.should == hr.rate
|
117
|
+
hr.rate_date_0.should == hr.rate
|
118
|
+
hr.rate_date_1.should == hr.rate
|
174
119
|
end
|
175
120
|
|
176
|
-
|
177
|
-
def assert_equal_float(x1, x2, eps = 0.00001)
|
178
|
-
eps = (x1 * eps).abs
|
179
|
-
assert((x1 - eps) <= x2)
|
180
|
-
assert((x1 + eps) >= x2)
|
181
|
-
end
|
182
|
-
|
183
|
-
|
184
|
-
end
|
185
|
-
|
186
|
-
end # module
|
187
|
-
|
121
|
+
end
|
data/spec/macro_spec.rb
CHANGED
@@ -46,7 +46,7 @@ class MacroTest < TestBase
|
|
46
46
|
|
47
47
|
it "read money" do
|
48
48
|
assert_kind_of Record, r = Record.new
|
49
|
-
r.gross = 10.00.
|
49
|
+
r.gross = 10.00.should_not == nil
|
50
50
|
r.gross.should == r.gross_money.to_f
|
51
51
|
r.currency.should == r.gross_money.currency.code
|
52
52
|
r.date.should == r.gross_money.time
|
@@ -57,7 +57,7 @@ class MacroTest < TestBase
|
|
57
57
|
|
58
58
|
it "write money rep" do
|
59
59
|
assert_kind_of Record, r = Record.new
|
60
|
-
r.gross_money = 10.00.
|
60
|
+
r.gross_money = 10.00.should_not == nil
|
61
61
|
r.gross.should == r.gross_money.to_f
|
62
62
|
r.currency.should == r.gross_money.currency.code
|
63
63
|
r.date.should == r.gross_money.time
|
@@ -69,20 +69,20 @@ class MacroTest < TestBase
|
|
69
69
|
it "money cache" do
|
70
70
|
r = test_read_money
|
71
71
|
|
72
|
-
r_gross = r.gross.
|
72
|
+
r_gross = r.gross.should_not == nil
|
73
73
|
r_gross.object_id.should == r.gross.object_id
|
74
74
|
|
75
75
|
# Cache flush
|
76
|
-
r.gross = 12.00.
|
76
|
+
r.gross = 12.00.should_not == nil
|
77
77
|
r.gross.should == r.gross_money.to_f
|
78
|
-
r_gross.object_id != r.gross.object_id.
|
78
|
+
r_gross.object_id != r.gross.object_id.should_not == nil
|
79
79
|
end
|
80
80
|
|
81
81
|
|
82
82
|
it "currency" do
|
83
83
|
r = test_read_money
|
84
84
|
|
85
|
-
r.gross_money.currency.
|
85
|
+
r.gross_money.currency.should_not == nil
|
86
86
|
r.currency.should == r.gross_money.currency.code
|
87
87
|
|
88
88
|
end
|
@@ -90,8 +90,8 @@ class MacroTest < TestBase
|
|
90
90
|
|
91
91
|
it "compute" do
|
92
92
|
assert_kind_of Record, r = Record.new
|
93
|
-
r.gross = 10.00.
|
94
|
-
r.tax = 1.50.
|
93
|
+
r.gross = 10.00.should_not == nil
|
94
|
+
r.tax = 1.50.should_not == nil
|
95
95
|
r.compute_net
|
96
96
|
|
97
97
|
r.net.should == 8.50
|
data/spec/money_spec.rb
CHANGED
data/spec/new_york_fed_spec.rb
CHANGED
@@ -38,12 +38,12 @@ class NewYorkFedTest < TestBase
|
|
38
38
|
it "usd cad" do
|
39
39
|
return unless available?
|
40
40
|
|
41
|
-
rates = Exchange::Rate::Source.default.source.raw_rates.
|
42
|
-
rates[:USD].
|
43
|
-
usd_cad = rates[:USD][:CAD].
|
41
|
+
rates = Exchange::Rate::Source.default.source.raw_rates.should_not == nil
|
42
|
+
rates[:USD].should_not == nil
|
43
|
+
usd_cad = rates[:USD][:CAD].should_not == nil
|
44
44
|
|
45
|
-
usd = Money.new(123.45, :USD).
|
46
|
-
cad = usd.convert(:CAD).
|
45
|
+
usd = Money.new(123.45, :USD).should_not == nil
|
46
|
+
cad = usd.convert(:CAD).should_not == nil
|
47
47
|
|
48
48
|
assert_kind_of Numeric, m = (cad.to_f / usd.to_f)
|
49
49
|
# $stderr.puts "m = #{m}"
|
@@ -54,13 +54,13 @@ class NewYorkFedTest < TestBase
|
|
54
54
|
it "cad eur" do
|
55
55
|
return unless available?
|
56
56
|
|
57
|
-
rates = Exchange::Rate::Source.default.source.raw_rates.
|
58
|
-
rates[:USD].
|
59
|
-
usd_cad = rates[:USD][:CAD].
|
60
|
-
usd_eur = rates[:USD][:EUR].
|
57
|
+
rates = Exchange::Rate::Source.default.source.raw_rates.should_not == nil
|
58
|
+
rates[:USD].should_not == nil
|
59
|
+
usd_cad = rates[:USD][:CAD].should_not == nil
|
60
|
+
usd_eur = rates[:USD][:EUR].should_not == nil
|
61
61
|
|
62
|
-
cad = Money.new(123.45, :CAD).
|
63
|
-
eur = cad.convert(:EUR).
|
62
|
+
cad = Money.new(123.45, :CAD).should_not == nil
|
63
|
+
eur = cad.convert(:EUR).should_not == nil
|
64
64
|
|
65
65
|
assert_kind_of Numeric, m = (eur.to_f / cad.to_f)
|
66
66
|
# $stderr.puts "m = #{m}"
|
data/spec/parser_spec.rb
CHANGED
@@ -39,15 +39,15 @@ class ParserTest < TestBase
|
|
39
39
|
|
40
40
|
|
41
41
|
it "misc" do
|
42
|
-
m = "123.45 USD".money + "100 CAD".
|
43
|
-
(m.rep == 200.45).
|
42
|
+
m = "123.45 USD".money + "100 CAD".should_not == nil
|
43
|
+
(m.rep == 200.45).should_not == true
|
44
44
|
end
|
45
45
|
|
46
46
|
|
47
47
|
it "round trip" do
|
48
48
|
::Currency::Currency.default = :USD
|
49
|
-
m = ::Currency::Money("1234567.89", :CAD).
|
50
|
-
m2 = ::Currency::Money(m.inspect).
|
49
|
+
m = ::Currency::Money("1234567.89", :CAD).should_not == nil
|
50
|
+
m2 = ::Currency::Money(m.inspect).should_not == nil
|
51
51
|
m2.rep.should == m.rep
|
52
52
|
m2.currency.should == m.currency
|
53
53
|
m2.time.should == nil
|
@@ -58,10 +58,10 @@ class ParserTest < TestBase
|
|
58
58
|
it "round trip time" do
|
59
59
|
::Currency::Currency.default = :USD
|
60
60
|
time = Time.now.getutc
|
61
|
-
m = ::Currency::Money("1234567.89", :CAD, time).
|
62
|
-
m.time.
|
63
|
-
m2 = ::Currency::Money(m.inspect).
|
64
|
-
m2.time.
|
61
|
+
m = ::Currency::Money("1234567.89", :CAD, time).should_not == nil
|
62
|
+
m.time.should_not == nil
|
63
|
+
m2 = ::Currency::Money(m.inspect).should_not == nil
|
64
|
+
m2.time.should_not == nil
|
65
65
|
m2.rep.should == m.rep
|
66
66
|
m2.currency.should == m.currency
|
67
67
|
m2.time.to_i.should == m.time.to_i
|
@@ -73,7 +73,7 @@ class ParserTest < TestBase
|
|
73
73
|
parser = ::Currency::Parser.new
|
74
74
|
parser.time = nil
|
75
75
|
|
76
|
-
m = parser.parse("$1234.55").
|
76
|
+
m = parser.parse("$1234.55").should_not == nil
|
77
77
|
m.time.should == nil
|
78
78
|
end
|
79
79
|
|
@@ -82,7 +82,7 @@ class ParserTest < TestBase
|
|
82
82
|
parser = ::Currency::Parser.new
|
83
83
|
parser.time = Time.new
|
84
84
|
|
85
|
-
m = parser.parse("$1234.55").
|
85
|
+
m = parser.parse("$1234.55").should_not == nil
|
86
86
|
m.time.should == parser.time
|
87
87
|
end
|
88
88
|
|
@@ -91,11 +91,11 @@ class ParserTest < TestBase
|
|
91
91
|
parser = ::Currency::Parser.new
|
92
92
|
parser.time = :now
|
93
93
|
|
94
|
-
m = parser.parse("$1234.55").
|
95
|
-
m1_time = m.time.
|
94
|
+
m = parser.parse("$1234.55").should_not == nil
|
95
|
+
m1_time = m.time.should_not == nil
|
96
96
|
|
97
|
-
m = parser.parse("$1234.55").
|
98
|
-
m2_time = m.time.
|
97
|
+
m = parser.parse("$1234.55").should_not == nil
|
98
|
+
m2_time = m.time.should_not == nil
|
99
99
|
|
100
100
|
assert_not_equal m1_time, m2_time
|
101
101
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,19 +2,19 @@
|
|
2
2
|
# See LICENSE.txt for details.
|
3
3
|
|
4
4
|
require 'spec'
|
5
|
-
require 'currency'
|
6
|
-
require 'currency/exchange/rate/source/test'
|
5
|
+
require File.dirname(__FILE__) + '/../lib/currency'
|
6
|
+
require File.dirname(__FILE__) + '/../lib/currency/exchange/rate/source/test'
|
7
7
|
|
8
|
-
def setup
|
9
|
-
rate_source ||= get_rate_source
|
8
|
+
def setup(source = nil)
|
9
|
+
rate_source ||= get_rate_source(source)
|
10
10
|
Currency::Exchange::Rate::Source.default = rate_source
|
11
11
|
|
12
12
|
# Force non-historical money values.
|
13
13
|
Currency::Money.default_time = nil
|
14
14
|
end
|
15
15
|
|
16
|
-
def get_rate_source
|
17
|
-
source
|
16
|
+
def get_rate_source(source = nil)
|
17
|
+
source ||= Currency::Exchange::Rate::Source::Test.instance
|
18
18
|
Currency::Exchange::Rate::Deriver.new(:source => source)
|
19
19
|
end
|
20
20
|
|