Skip to content

Commit

Permalink
Corrected parsing orphan/fridges. Added rspec tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed May 6, 2014
1 parent 2109b57 commit 5a28847
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/oddb2xml/cli.rb
Expand Up @@ -185,6 +185,7 @@ def download(what, type=nil, lang=nil)
"@#{var}".intern,
SwissmedicExtractor.new(bin, what).to_arry
)
# Oddb2xml.log("SwissmedicExtractor added #{self.instance_variable_get("@#{var}".intern).size} #{var}. File #{bin} was #{File.size(bin)} bytes")
end
when :interaction
Thread.new do
Expand All @@ -210,7 +211,7 @@ def download(what, type=nil, lang=nil)
bin = downloader.download
@mutex.synchronize do
@packs = SwissmedicExtractor.new(bin, :package).to_hash
Oddb2xml.log("SwissmedicExtractor added #{@packs.size} packs")
# Oddb2xml.log("SwissmedicExtractor added #{@packs.size} packs. File #{bin} was #{File.size(bin)} bytes")
end
end
when :bm_update
Expand Down
10 changes: 5 additions & 5 deletions lib/oddb2xml/downloader.rb
Expand Up @@ -12,7 +12,7 @@ def download_as(file, option='r')
file2save = File.join(Downloads, File.basename(file))
Oddb2xml.log "download_as file #{file2save} via #{tempFile} from #{@url}"
data = nil
FileUtils.rm_f(tempFile)
FileUtils.rm_f(tempFile, :verbose => false)
if Oddb2xml.skip_download(file)
io = File.open(file, option)
data = io.read
Expand Down Expand Up @@ -199,7 +199,7 @@ def download
end
end
content = read_xml_from_zip(/Preparations.xml/, File.join(Downloads, File.basename(file)))
FileUtils.rm_f(file) unless defined?(Rspec)
FileUtils.rm_f(file, :verbose => false) unless defined?(Rspec)
content
end
end
Expand All @@ -225,7 +225,7 @@ def download
filename = "swissindex_#{@type}_#{@lang}.xml"
file2save = File.join(Downloads, "swissindex_#{@type}_#{@lang}.xml")
return IO.read(file2save) if Oddb2xml.skip_download? and File.exists?(file2save)
FileUtils.rm_f(file2save)
FileUtils.rm_f(file2save, :verbose => false)
soap = <<XML
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
Expand Down Expand Up @@ -287,7 +287,7 @@ def download
retrievable? ? retry : raise
ensure
Oddb2xml.download_finished(file, false)
end # unless Oddb2xml.skip_download(file)
end
return File.expand_path(file)
end
end
Expand All @@ -299,7 +299,7 @@ def init
end
def download
file = File.join(Downloads, "swissmedic_info.zip")
FileUtils.rm_f(file) unless Oddb2xml.skip_download?
FileUtils.rm_f(file, :verbose => false) unless Oddb2xml.skip_download?
begin
response = nil
if home = @agent.get(@url)
Expand Down
7 changes: 4 additions & 3 deletions lib/oddb2xml/extractor.rb
Expand Up @@ -215,10 +215,11 @@ def to_hash
end
class SwissmedicExtractor < Extractor
def initialize(filename, type)
@filename = filename
@filename = File.join(Downloads, File.basename(filename))
@filename = File.join(SpecData, File.basename(filename)) if defined?(RSpec) and not File.exists?(@filename)
@type = type
return unless File.exists?(filename)
@sheet = RubyXL::Parser.parse(File.expand_path(filename)).worksheets[0]
return unless File.exists?(@filename)
@sheet = RubyXL::Parser.parse(File.expand_path(@filename)).worksheets[0]
end
def to_arry
data = []
Expand Down
4 changes: 2 additions & 2 deletions lib/oddb2xml/util.rb
Expand Up @@ -35,7 +35,7 @@ def Oddb2xml.download_finished(file, remove_file = true)
#return unless File.exists?(file)
return unless file and File.exists?(file)
return if File.expand_path(file).eql?(dest)
FileUtils.mv(src, dest, :verbose => false)
Oddb2xml.log("download_finished saved as #{dest}")
FileUtils.cp(src, dest, :verbose => false)
Oddb2xml.log("download_finished saved as #{dest} #{File.size(dest)} bytes.")
end
end
7 changes: 6 additions & 1 deletion spec/builder_spec.rb
Expand Up @@ -129,6 +129,8 @@ def buildr_capture(stream)
dscrds = XPath.match( doc, "//ART" )
XPath.match( doc, "//PHAR" ).find_all{|x| x.text.match('5366964') }.size.should == 1
dscrds.size.should == NrExtendedArticles
XPath.match( doc, "//PRODNO" ).find_all{|x| true}.size.should == 1
XPath.match( doc, "//PRODNO" ).find_all{|x| x.text.match('620691') }.size.should == 1
end

it 'should load correct number of nonpharma' do
Expand Down Expand Up @@ -220,7 +222,6 @@ def buildr_capture(stream)

it 'should emit a correct oddb_product.xml' do
res = buildr_capture(:stdout){ cli.run }

res.should match(/products/)
product_filename = File.expand_path(File.join(Oddb2xml::WorkDir, 'oddb_product.xml'))
File.exists?(product_filename).should be_true
Expand All @@ -236,6 +237,10 @@ def buildr_capture(stream)
product_xml.should match(/7680555580054/) # ZYVOXID
product_xml.should_not match(/ZYVOXID/i)
end
doc = REXML::Document.new File.new(product_filename)
XPath.match( doc, "//PRD" ).find_all{|x| true}.size.should == 3
XPath.match( doc, "//GTIN" ).find_all{|x| true}.size.should == 3
XPath.match( doc, "//PRODNO" ).find_all{|x| true}.size.should == 1
end
end
end
Binary file not shown.
Binary file removed spec/data/swissmedic_fridges.xls
Binary file not shown.
Binary file modified spec/data/swissmedic_orphan.xlsx
Binary file not shown.
File renamed without changes.
1 change: 1 addition & 0 deletions spec/downloader_spec.rb
Expand Up @@ -126,6 +126,7 @@ def common_after
before(:each) do
setup_swissmedic_server_mock
@downloader = Oddb2xml::SwissmedicDownloader.new(:fridge)
common_before
end
context 'download_by for fridge xls' do
let(:bin) { @downloader.download }
Expand Down
14 changes: 7 additions & 7 deletions spec/extractor_spec.rb
Expand Up @@ -63,9 +63,9 @@
subject { Oddb2xml::SwissmedicInfoExtractor.new("") }
it { expect(subject.to_hash).to be_empty }
end
context 'can parse swissmedic_packages.xlsx' do
context 'can parse swissmedic_package.xlsx' do
it {
filename = File.join(File.dirname(__FILE__), 'data/swissmedic_packages.xlsx')
filename = File.join(Oddb2xml::SpecData, 'swissmedic_package.xlsx')
@packs = Oddb2xml::SwissmedicExtractor.new(filename, :package).to_hash
expect(@packs.size).to eq(14)
serocytol = nil
Expand All @@ -79,9 +79,9 @@
expect(serocytol[:substance_swissmedic]).to eq('globulina equina (immunisé avec coeur, tissu pulmonaire, reins de porcins)')
}
end
context 'can parse swissmedic_fridges.xlsx' do
context 'can parse swissmedic_fridge.xlsx' do
it {
filename = File.join(File.dirname(__FILE__), 'data/swissmedic_fridges.xlsx')
filename = File.join(Oddb2xml::SpecData, 'swissmedic_fridge.xlsx')
@packs = Oddb2xml::SwissmedicExtractor.new(filename, :fridge).to_arry
expect(@packs.size).to eq(17)
expect(@packs[0]).to eq("58618")
Expand All @@ -90,10 +90,10 @@
end
context 'can parse swissmedic_orphans.xls' do
it {
filename = File.join(File.dirname(__FILE__), 'data/swissmedic_orphan.xlsx')
filename = File.join(Oddb2xml::SpecData, 'swissmedic_orphan.xlsx')
expect(File.exists?(filename)).to eq(true), "File #{filename} must exists"
@packs = Oddb2xml::SwissmedicExtractor.new(filename, :orphan).to_arry
expect(@packs.size).to eq(79)
expect(@packs.size).to eq(78)
expect(@packs.first).to eq("62132")
expect(@packs[7]).to eq("00687")
}
Expand All @@ -103,7 +103,7 @@
describe Oddb2xml::EphaExtractor do
context 'can parse epha_interactions.csv' do
it {
filename = File.join(File.dirname(__FILE__), 'data/epha_interactions.csv')
filename = File.join(Oddb2xml::SpecData, 'epha_interactions.csv')
string = IO.read(filename)
@actions = Oddb2xml::EphaExtractor.new(string).to_arry
expect(@actions.size).to eq(2)
Expand Down

0 comments on commit 5a28847

Please sign in to comment.