#!/usr/bin/env ruby # TestExporter -- oddb -- 07.02.2011 -- mhatakeyama@ywesee.com $: << File.expand_path("../../src", File.dirname(__FILE__)) require 'test/unit' require 'flexmock' require 'util/exporter' require 'util/log' require 'date' # Redefine sleep method, otherwise you have to much time to do tests def sleep(*args) end module ODDB =begin # Redifine Schedule methods # These tests should be done in test/test_util/schedule.rb module Util module Schedule def run_on_weekday(*args) yield end def run_on_monthday(*args) yield end end end =end class StubDRbObject def clear end end class Exporter remove_const :EXPORT_SERVER EXPORT_SERVER = StubDRbObject.new @@today = Date.new(2011,2,3) end class TestExporter < Test::Unit::TestCase include FlexMock::TestCase def setup app = flexmock('app') @exporter = ODDB::Exporter.new(app) @log = flexmock('log') do |log| log.should_receive(:report) log.should_receive(:notify) log.should_receive(:report=) end @export = flexmock('exporter') do |exp| exp.should_receive(:export_fachinfos) end flexstub(OdbaExporter::OddbDatExport) do |oddb| oddb.should_receive(:new).and_return(@export) end end def test_export_oddbdat flexstub(Log) do |logclass| # white box test: Log.new is never called # if dose_missing_list is not empty or an error raises, # Log.new will be called logclass.should_receive(:new).times(0).and_return(@log) end flexstub(@export) do |exp| exp.should_receive(:export).and_return([]) # this is the key point end assert_equal(nil, @exporter.export_oddbdat) end def test_export_oddbdat__dose_missing flexstub(Log) do |logclass| # white box test: Log.new is once called because of dose data missing logclass.should_receive(:new).times(1).and_return(@log) end flexstub(@export) do |exp| exp.should_receive(:export).and_return(['dose_missing']) # this is the key point end assert_equal(nil, @exporter.export_oddbdat) end def test_safe_export flexstub(Log) do |logclass| # white box test: Log.new is never called if there is no error logclass.should_receive(:new).times(0).and_return(@log) end @exporter.safe_export('test_safe_export') do 'no error' end end def test_safe_export__error flexstub(Log) do |logclass| # white box test: Log.new is never called if there is no error logclass.should_receive(:new).times(1).and_return(@log) end @exporter.safe_export('test_safe_export') do raise end end def test_run__on_1st_day # totally whilte box test Exporter.__send__(:class_variable_set, :@@today, Date.new(2011,1,1)) # Saturday flexstub(@exporter) do |exp| exp.should_receive(:mail_patinfo_invoices).once.with_no_args exp.should_receive(:mail_fachinfo_log).once.with_no_args exp.should_receive(:mail_download_invoices).once.with_no_args exp.should_receive(:mail_download_stats).times(0).with_no_args exp.should_receive(:mail_feedback_stats).times(0).with_no_args exp.should_receive(:export_sl_pcodes).once.with_no_args exp.should_receive(:export_yaml).once.with_no_args exp.should_receive(:export_oddbdat).once.with_no_args exp.should_receive(:export_csv).once.with_no_args exp.should_receive(:export_doc_csv).once.with_no_args exp.should_receive(:export_index_therapeuticus_csv).once.with_no_args exp.should_receive(:export_price_history_csv).once.with_no_args end assert_equal(nil, @exporter.run) end def test_run__on_15th_day Exporter.__send__(:class_variable_set, :@@today, Date.new(2011,1,15)) # Saturday flexstub(@exporter) do |exp| exp.should_receive(:mail_patinfo_invoices).once.with_no_args exp.should_receive(:mail_fachinfo_log).once.with_no_args exp.should_receive(:mail_download_invoices).once.with_no_args exp.should_receive(:mail_download_stats).times(0).with_no_args exp.should_receive(:mail_feedback_stats).times(0).with_no_args exp.should_receive(:export_sl_pcodes).once.with_no_args exp.should_receive(:export_yaml).once.with_no_args exp.should_receive(:export_oddbdat).once.with_no_args exp.should_receive(:export_csv).once.with_no_args exp.should_receive(:export_doc_csv).once.with_no_args exp.should_receive(:export_index_therapeuticus_csv).once.with_no_args exp.should_receive(:export_price_history_csv).once.with_no_args end assert_equal(nil, @exporter.run) end def test_run__on_sunday Exporter.__send__(:class_variable_set, :@@today, Date.new(2011,1,2)) # Sunday flexstub(@exporter) do |exp| exp.should_receive(:mail_patinfo_invoices).once.with_no_args exp.should_receive(:mail_fachinfo_log).once.with_no_args exp.should_receive(:mail_download_invoices).times(0).with_no_args exp.should_receive(:mail_download_stats).once.with_no_args exp.should_receive(:mail_feedback_stats).once.with_no_args exp.should_receive(:export_sl_pcodes).once.with_no_args exp.should_receive(:export_yaml).once.with_no_args exp.should_receive(:export_oddbdat).once.with_no_args exp.should_receive(:export_csv).once.with_no_args exp.should_receive(:export_doc_csv).once.with_no_args exp.should_receive(:export_index_therapeuticus_csv).once.with_no_args exp.should_receive(:export_price_history_csv).once.with_no_args end assert_equal(nil, @exporter.run) end def test_export_helper assert_equal(nil, false) end end end