forked from zdavatz/gtin2atc
/
builder_spec.rb
218 lines (190 loc) · 6.8 KB
/
builder_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# encoding: utf-8
require 'spec_helper'
require "rexml/document"
include REXML
module Kernel
def buildr_capture(stream)
begin
stream = stream.to_s
eval "$#{stream} = StringIO.new"
yield
result = eval("$#{stream}").string
ensure
eval "$#{stream} = #{stream.upcase}"
end
result
end
end
describe Gtin2atc::Builder do
include ServerMockHelper
CSV_NAME = 'gtin2atc.csv'
before(:each) do
@savedDir = Dir.pwd
FileUtils.makedirs Gtin2atc::WorkDir
Dir.chdir Gtin2atc::WorkDir
Gtin2atc::Util.set_archive_dir(Gtin2atc::WorkDir)
cleanup_directories_before_run
setup_server_mocks
{ 'XMLPublications' => '.zip',
'swissindex_Pharma_DE' => '.xml',
'swissmedic_package' => '.xlsx',
}.each {
|name, extension|
use4test = File.expand_path(File.join( __FILE__, '../data/'+name + extension))
latest, dated = Gtin2atc::Util.get_latest_and_dated_name(name, extension)
FileUtils.cp(use4test, latest, :verbose => false)
}
end
after(:each) do
Dir.chdir @savedDir if @savedDir and File.directory?(@savedDir)
end
after(:all) do
Dir.chdir @savedDir if @savedDir and File.directory?(@savedDir)
end
def check_csv(filename)
File.exists?(filename).should eq true
inhalt = IO.readlines(filename)
/^\d{13};\w{4}/.should match inhalt[1]
# Packungsgrösse, Dosierung, DDD, Route of Administration
/^gtin;ATC;pharmacode;description;selling units;name;qty;unit;ddd:qty;ddd:unit;ddd:full_text/.should match inhalt.first
/^7680316440115;B03AA07;20244;FERRO-GRADUMET Depottabl;30;Ferrum(ii);105.0;mg;0.2;g;O Fe2+;Fe2+/.should match inhalt.join("\n")
end
context 'when 20273 41803 (Pharmacodes) is given' do
let(:cli) do
options = Gtin2atc::Options.new
options.parser.parse!('20273 41803'.split(' '))
Gtin2atc::Builder.new(options.opts)
end
it 'should produce a correct csv' do
# @res = buildr_capture(:stdout){ cli.run }
cli.run
check_csv(CSV_NAME)
end
end
context 'when --log is given' do
let(:cli) do
options = Gtin2atc::Options.new
options.parser.parse!('--log'.split(' '))
Gtin2atc::Builder.new(options.opts)
end
it 'should produce a correct csv' do
@res = buildr_capture(:stdout){ cli.run }
check_csv(CSV_NAME)
end
it 'should produce more log output' do
@res = buildr_capture(:stdout){ cli.run }
@res.match(/swissindex_xml_extractor/).should_not == nil
@res.match(/swissindex: Extracted/).should_not == nil
end
end
context 'when 20273 41803 (Pharmacodes) is given' do
let(:cli) do
options = Gtin2atc::Options.new
options.parser.parse!('20273 41803'.split(' '))
Gtin2atc::Builder.new(options.opts)
end
it 'should produce a csv with a two GTIN' do
@res = buildr_capture(:stdout){ cli.run(["20273", "41803"]) }
check_csv(CSV_NAME)
inhalt = IO.readlines(CSV_NAME)
inhalt.size.should eq 2+1 # one header lines + two items
inhalt[1].chomp.should eq '7680147690482;N07BC02;41803;KETALGIN Inj Lös 10 mg/ml;;;;;;;25 mg O,P'
inhalt[2].chomp.should eq '7680353660163;B03AE10;20273;KENDURAL Depottabl;;;;;;;'
end
end
context 'when 7680147690482 7680353660163 is given' do
let(:cli) do
options = Gtin2atc::Options.new
options.parser.parse!('7680147690482 7680353660163'.split(' '))
Gtin2atc::Builder.new(options.opts)
end
it 'should produce a csv with a two GTIN' do
@res = buildr_capture(:stdout){ cli.run(["7680147690482", "7680353660163"]) }
check_csv(CSV_NAME)
inhalt = IO.readlines(CSV_NAME)
inhalt.size.should eq 2+1 # one header lines + two items
/7680147690482/.match(inhalt[1]).should_not == nil
/7680353660163/.match(inhalt[1]).should == nil
/7680147690482/.match(inhalt[2]).should == nil
/7680353660163/.match(inhalt[2]).should_not == nil
/7680353660163;B03AE10;20273;KENDURAL Depottabl/.match(inhalt[2]).should_not == nil
end
end
def check_csv(filename)
File.exists?(filename).should eq true
inhalt = IO.readlines(filename)
/^gtin;ATC/.match(inhalt.first).should_not == nil
/^\d{13};\w{4}/.match(inhalt[1]).should_not == nil
end
context 'when --compare is given' do
let(:cli) do
options = Gtin2atc::Options.new
options.parser.parse!('--compare'.split(' '))
Gtin2atc::Builder.new(options.opts)
end
it 'should produce three correct csv' do
@res = buildr_capture(:stdout){ cli.run }
check_csv('gtin2atc_bag.csv')
check_csv('gtin2atc_swissmedic.csv')
check_csv('gtin2atc_swissindex.csv')
end
it 'should produce a good logging output' do
@res = buildr_capture(:stdout){ cli.run }
[ /Found infos/,
/Fetched from/,
/Matching/,
/not in bag/,
/not in swissmedic/,
/not in swissindex/,
/atc are different/,
].each {
|pattern|
unless pattern.match(@res)
puts "Looking for #{pattern} in #{@res}"
end
pattern.match(@res).should_not == nil
}
end
end
context 'when --compare --full is given' do
let(:cli) do
options = Gtin2atc::Options.new
options.parser.parse!('--compare --full'.split(' '))
Gtin2atc::Builder.new(options.opts)
end
it 'should produce many report files' do
@res = buildr_capture(:stdout){ cli.run }
Dir.glob(Gtin2atc::WorkDir + '/compare_all_*.txt').size.should == 5
Dir.glob(Gtin2atc::WorkDir + '/pharmacode_gtin_not_found.txt').size.should == 1
Dir.glob(Gtin2atc::WorkDir + '/compare_swissmedic_to_swisssindex_*.txt').size.should == 5
Dir.glob(Gtin2atc::WorkDir + '/compare_bag_to_swissindex_*.txt').size.should == 5
Dir.glob(Gtin2atc::WorkDir + '/compare_bag_to_swissmedic_*.txt').size.should == 5
end
end
context 'when outputfile is given' do
let(:cli) do
options = Gtin2atc::Options.new
options.parser.parse!('7680316440115 tst.csv'.split(' '))
Gtin2atc::Builder.new(options.opts)
end
it 'should produce a correct tst.csv' do
tst_name = 'tst.csv'
buildr_capture(:stdout){ cli.run(["7680316440115"], tst_name) }
check_csv(tst_name)
end
end
context 'when producing csv' do
let(:cli) do
options = Gtin2atc::Options.new
Gtin2atc::Builder.new(options.opts)
end
it 'should contain dose for FERRO-GRADUMET Depottab' do
oddb_calc_xml = File.expand_path(File.join( __FILE__, '../data/oddb_calc.xml'))
FileUtils.cp(oddb_calc_xml, Gtin2atc::WorkDir, :verbose => false)
@res = buildr_capture(:stdout){ cli.run() }
check_csv(CSV_NAME)
inhalt = IO.read(CSV_NAME)
inhalt.index("\n7680316440115;B03AA07;20244;FERRO-GRADUMET Depottabl;;;30;Ferrum(ii);105.0;mg;0,2 g O Fe2+\n").should_not == nil
end
end
end