-
Notifications
You must be signed in to change notification settings - Fork 1
/
bank_parser_spec.rb
176 lines (146 loc) · 4.34 KB
/
bank_parser_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe TotalRecall::Config do
include FakeFS::SpecHelpers
def stubbed_file(path, content)
# SOURCE http://edgeapi.rubyonrails.org/classes/String.html#method-i-strip_heredoc
indent = content.scan(/^[ \t]*(?=\S)/).min.size rescue 0
content = content.gsub(/^[ \t]{#{indent}}/, '')
FakeFS do
File.open(path, 'w'){|f| f.print content }
end
end
def instance_with_config(config, options = {})
options = {file: 'config.yml'}.merge(options)
stubbed_file(options[:file], config)
described_class.new(options)
end
describe '#config' do
it 'yields the config as hash' do
instance = instance_with_config(<<-CONFIG)
:csv:
:raw: Some csv
:a: 1
CONFIG
expect(instance.config).to eql({csv: { raw: 'Some csv'}, a: 1})
end
end
describe '#csv' do
it 'yields csv assigned to :raw' do
instance = instance_with_config(<<-CONFIG)
:csv:
:raw: Some csv
CONFIG
expect(instance.csv).to eql(CSV.parse('Some csv'))
end
it 'yields csv from file :file' do
csv_file = stubbed_file('some.csv', 'Some csv')
instance = instance_with_config(<<-CONFIG)
:csv:
:file: some.csv
CONFIG
expect(instance.csv).to eql(CSV.parse('Some csv'))
end
it 'yields csv from :file when both :raw and :file are configured' do
csv_file = stubbed_file('some.csv', 'Some csv')
instance = instance_with_config(<<-CONFIG)
:csv:
:file: some.csv
:raw: Some raw csv
CONFIG
expect(instance.csv).to eql(CSV.parse('Some csv'))
end
specify 'csv-options are passed on to CSV#read' do
instance = instance_with_config(<<-CONFIG)
:csv:
:options:
:option1: true
CONFIG
expect(CSV).to receive(:parse).with(anything(), { option1: true })
instance.csv
end
end
describe '#template' do
it 'yields template assigned to :raw' do
instance = instance_with_config(<<-CONFIG)
:template:
:raw: |-
Raw template
here
CONFIG
expect(instance.template).to eql("Raw template\nhere")
end
it 'yields template from file :file' do
template_file = stubbed_file('template.mustache', 'File template')
instance = instance_with_config(<<-CONFIG)
:template:
:file: template.mustache
CONFIG
expect(instance.template).to eql('File template')
end
it 'yields template from :file when both :raw and :file are configured' do
template_file = stubbed_file('template.mustache', 'File template')
instance = instance_with_config(<<-CONFIG)
:template:
:file: template.mustache
:raw: Raw template
CONFIG
expect(instance.template).to eql('File template')
end
end
describe 'YAML types' do
it 'allows proc-types' do
instance = instance_with_config(<<-CONFIG)
:a: !!proc 1 + 1
:b: !!proc |
1 + 1
CONFIG
expect(instance.config[:a].call).to eq 2
expect(instance.config[:b].call).to eq 2
end
end
describe '#context' do
it 'has a transaction per line of csv' do
instance = instance_with_config(<<-CONFIG)
:csv:
:raw: |-
1
1
:context:
:transactions:
:from: From
:to: !!proc 1 + 1
:amount: !!proc row[0]
CONFIG
transactions = instance.context[:transactions]
expect(transactions.size).to eq 2
transaction = transactions.first
expect(transaction).to match({from: 'From', to: 2, amount: "1"})
end
it 'adds defaults to every transaction' do
instance = instance_with_config(<<-CONFIG)
:csv:
:raw: |-
line 1
line 2
:context:
:transactions:
:__defaults__:
:default: !!proc 1
:from: From
CONFIG
transaction = instance.context[:transactions].first
expect(transaction).to match({from: 'From', default: 1})
end
it 'may contain any other settings' do
instance = instance_with_config(<<-CONFIG)
:csv:
:raw: some csv
:context:
:transactions:
:from: From
:a: 1
CONFIG
expect(instance.context).to match(transactions: [{from: 'From'}], a: 1)
end
end
end