Class: ODDB::TestTextInfoPlugin

Inherits:
Test::Unit::TestCase
  • Object
show all
Includes:
FlexMock::TestCase
Defined in:
test/test_plugin/text_info.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) setup



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'test/test_plugin/text_info.rb', line 23

def setup
  super
  @app = flexmock 'application'
  @datadir = File.expand_path '../data/html/text_info', File.dirname(__FILE__)
  @vardir = File.expand_path '../var/', File.dirname(__FILE__)
  FileUtils.mkdir_p @vardir
  ODDB.config.data_dir = @vardir
  ODDB.config.log_dir = @vardir
  ODDB.config.text_info_searchform = 'http://textinfo.ch/Search.aspx'
  ODDB.config.text_info_newssource = 'http://textinfo.ch/news.aspx'
  @parser = flexmock 'parser (simulates ext/fiparse)'
  @plugin = TextInfoPlugin.new @app
  @plugin.parser = @parser
end

- (Object) setup_fachinfo_document(heading, text)



84
85
86
87
88
89
90
# File 'test/test_plugin/text_info.rb', line 84

def setup_fachinfo_document heading, text
  fi = FachinfoDocument.new
  fi.iksnrs = Text::Chapter.new
  fi.iksnrs.heading << heading
  fi.iksnrs.next_section.next_paragraph << text
  fi
end

- (Object) setup_mechanize(mapping = [])



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
# File 'test/test_plugin/text_info.rb', line 41

def setup_mechanize mapping=[]
  agent = flexmock Mechanize.new
  @pages = Hash.new(0)
  @actions = {}
  mapping.each do |page, method, url, formname, page2|
    path = File.join @datadir, page
    page = setup_page url, path, agent
    if formname
      form = flexmock page.form(formname)
      action = form.action
      page = flexmock page
      page.should_receive(:form).with(formname).and_return(form)
      path2 = File.join @datadir, page2
      page2 = setup_page action, path2, agent
      agent.should_receive(:submit).and_return page2
    end
    case method
    when :get, :post
      agent.should_receive(method).with(url).and_return do |*args|
        @pages[[method, url, *args]] += 1
        page
      end
    when :submit
      @actions[url] = page
      agent.should_receive(method).and_return do |form, *args|
        action = form.action
        @pages[[method, action, *args]] += 1
        @actions[action]
      end
    else
      agent.should_receive(method).and_return do |*args|
        @pages[[method, *args]] += 1
        page
      end
    end
  end
  agent
end

- (Object) setup_page(url, path, agent)



79
80
81
82
83
# File 'test/test_plugin/text_info.rb', line 79

def setup_page url, path, agent
  response = {'content-type' => 'text/html'}
  Mechanize::Page.new(URI.parse(url), response,
                      File.read(path), 200, agent)
end

- (Object) teardown



37
38
39
40
# File 'test/test_plugin/text_info.rb', line 37

def teardown
  FileUtils.rm_r @vardir
  super
end

- (Object) test_detect_session_failure__failure



510
511
512
513
514
515
# File 'test/test_plugin/text_info.rb', line 510

def test_detect_session_failure__failure
  agent = setup_mechanize
  path = File.join @datadir, 'SearchForm.html'
  page = setup_page 'CompanyProdukte.aspx?lang=de', path, agent
  assert_equal true, @plugin.detect_session_failure(page)
end

- (Object) test_detect_session_failure__fine



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'test/test_plugin/text_info.rb', line 516

def test_detect_session_failure__fine
  agent = setup_mechanize
  path = File.join @datadir, 'Companies.html'
  page = setup_page 'Search.aspx', path, agent
  assert_equal false, @plugin.detect_session_failure(page)
  path = File.join @datadir, 'ResultEmpty.html'
  page = setup_page 'Result.aspx?lang=de', path, agent
  assert_equal false, @plugin.detect_session_failure(page)
  path = File.join @datadir, 'Result.html'
  page = setup_page 'Result.aspx?lang=de', path, agent
  assert_equal false, @plugin.detect_session_failure(page)
  path = File.join @datadir, 'Aclasta.de.html'
  page = setup_page 'CompanyProdukte.aspx?lang=de', path, agent
  assert_equal false, @plugin.detect_session_failure(page)
end

- (Object) test_download_info



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'test/test_plugin/text_info.rb', line 262

def test_download_info
  mapping = [
    [ 'Aclasta.de.html',
      :submit,
      'CompanyProdukte.aspx?lang=de',
    ],
    [ 'Companies.html',
      :get,
      'CompanyProdukte.aspx?lang=fr',
    ]
  ]
  agent = setup_mechanize mapping
  path = File.join @datadir, 'Result.html'
  page = setup_page 'http://textinfo.ch/CompanyProdukte.aspx?lang=de', path, agent
  form = page.form_with :name => 'frmResultProdukte'
  eventtarget = 'dtgFachinformationen$_ctl3$btnFachinformation'
  paths, flags = @plugin.download_info :fachinfo, 'Aclasta',
                                       agent, form, eventtarget
  expected = {}
  path = File.join @vardir, 'html', 'fachinfo', 'de', 'Aclasta.html'
  expected.store :de, path
  assert File.exist?(path)
  path = File.join @vardir, 'html', 'fachinfo', 'fr', 'Aclasta.html'
  expected.store :fr, path
  assert File.exist?(path)
  assert_equal expected, paths
  assert_equal({}, flags)
  paths, flags = @plugin.download_info :fachinfo, 'Aclasta',
                                       agent, form, eventtarget
  ## existing identical files are flagged as up-to-date
  assert_equal expected, paths
  assert_equal({:fr => :up_to_date, :de => :up_to_date}, flags)
end

- (Object) test_extract_iksnrs



295
296
297
298
299
# File 'test/test_plugin/text_info.rb', line 295

def test_extract_iksnrs
  de = setup_fachinfo_document 'Zulassungsnummer', '57363 (Swissmedic).'
  fr = setup_fachinfo_document 'Numéro d’autorisation', '57364 (Swissmedic).'
  assert_equal %w{57363 57364}, @plugin.extract_iksnrs(:de => de, :fr => fr).sort
end

- (Object) test_fachinfo_news



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'test/test_plugin/text_info.rb', line 608

def test_fachinfo_news
  mapping = [
    [ 'News.html',
      :get,
      ODDB.config.text_info_newssource,
    ],
  ]
  agent = setup_mechanize mapping
  news = nil
  assert_nothing_raised do
    news = @plugin.fachinfo_news agent
  end
  assert_equal 7, news.size
  assert_equal "Abilify\302\256", news.first
end

- (Object) test_fachinfo_news__unconfigured



601
602
603
604
605
606
607
# File 'test/test_plugin/text_info.rb', line 601

def test_fachinfo_news__unconfigured
  agent = setup_mechanize
  ODDB.config.text_info_newssource = nil
  assert_raises RuntimeError do
    @plugin.fachinfo_news agent
  end
end

- (Object) test_identify_eventtargets



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'test/test_plugin/text_info.rb', line 225

def test_identify_eventtargets
  agent = setup_mechanize
  path = File.join @datadir, 'Result.html'
  page = setup_page 'http://textinfo.ch/Search.aspx', path, agent
  targets = @plugin.identify_eventtargets page, /btnFachinformation/
  assert_equal 77, targets.size
  assert_equal "dtgFachinformationen$_ctl2$btnFachinformation", targets['Alca-C®']
  assert_equal "dtgFachinformationen$_ctl78$btnFachinformation",
               targets['Zymafluor®']
  targets = @plugin.identify_eventtargets page, /btnPatientenn?information/
  assert_equal 79, targets.size
  assert_equal "dtgPatienteninformationen$_ctl2$btnPatientenninformation",
               targets['Alca-C®']
  assert_equal "dtgPatienteninformationen$_ctl80$btnPatientenninformation",
               targets['Zymafluor®']
end

- (Object) test_import_companies



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'test/test_plugin/text_info.rb', line 136

def test_import_companies
  ## we return an empty result here, to contain testing the import_companies method
  mapping = [
    [ 'ResultEmpty.html',
      :submit,
      'Result.aspx?lang=de',
    ],
  ]
  agent = setup_mechanize mapping
  path = File.join @datadir, 'Companies.html'
  result = setup_page 'http://textinfo.ch/Search.aspx', path, agent
  page = nil
  assert_nothing_raised do
    @plugin.import_companies result, agent
  end
  ## we've touched only one page here, because we returned ResultEmpty.html
  assert_equal 1, @pages.size
end

- (Object) test_import_company



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
# File 'test/test_plugin/text_info.rb', line 154

def test_import_company
  mapping = [
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
    ],
    [ 'Companies.html',
      :submit,
      'Search.aspx',
    ],
    [ 'ResultAlcaC.html',
      :submit,
      'Result.aspx?lang=de',
    ],
    [ 'Aclasta.de.html',
      :submit,
      'CompanyProdukte.aspx?lang=de',
    ],
    [ 'Aclasta.fr.html',
      :get,
      'CompanyProdukte.aspx?lang=fr',
    ],
  ]
  agent = setup_mechanize mapping
  page = nil
  @parser.should_receive(:parse_fachinfo_html).and_return FachinfoDocument.new
  @parser.should_receive(:parse_patinfo_html).and_return PatinfoDocument.new
  assert_nothing_raised do
    @plugin.import_company 'novartis', agent
  end
  assert_equal 5, @pages.size
  ## we didn't set up @parser to return a FachinfoDocument with an iksnr.
  #  the rest of the process is tested in test_update_product
  assert_equal ['Alca-C®'], @plugin.iksless.uniq
end

- (Object) test_import_company__session_failure



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
219
220
221
222
223
224
# File 'test/test_plugin/text_info.rb', line 189

def test_import_company__session_failure
  mapping = [
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
    ],
    [ 'Companies.html',
      :submit,
      'Search.aspx',
    ],
    [ 'ResultAlcaC.html',
      :submit,
      'Result.aspx?lang=de',
    ],
    [ 'SearchForm.html',
      :submit,
      'CompanyProdukte.aspx?lang=de',
    ],
    [ 'SearchForm.html',
      :get,
      'CompanyProdukte.aspx?lang=fr',
    ],
  ]
  agent = setup_mechanize mapping
  page = nil
  @parser.should_receive(:parse_fachinfo_html).and_return FachinfoDocument.new
  @parser.should_receive(:parse_patinfo_html).and_return PatinfoDocument.new
  assert_nothing_raised do
    @plugin.import_company 'novartis', agent
  end
  assert_equal 5, @pages.size
  ## we didn't set up @parser to return a FachinfoDocument with an iksnr.
  #  the rest of the process is tested in test_update_product
  assert_equal ['Alca-C®'], @plugin.iksless.uniq
  assert_equal 8, @plugin.session_failures
end

- (Object) test_import_fulltext



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'test/test_plugin/text_info.rb', line 570

def test_import_fulltext
  mapping = [
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
    ],
    [ 'ResultFulltext.html',
      :submit,
      'Search.aspx',
    ],
    [ 'Aclasta.de.html',
      :submit,
      'Result.aspx?lang=de',
    ],
    [ 'Aclasta.fr.html',
      :get,
      'Result.aspx?lang=fr',
    ],
  ]
  agent = setup_mechanize mapping
  page = nil
  @parser.should_receive(:parse_fachinfo_html).and_return FachinfoDocument.new
  @parser.should_receive(:parse_patinfo_html).and_return PatinfoDocument.new
  assert_nothing_raised do
    @plugin.import_fulltext '53537', agent
  end
  assert_equal 4, @pages.size
  ## we didn't set up @parser to return a FachinfoDocument with an iksnr.
  #  the rest of the process is tested in test_update_product
  assert_equal ['Topamax®'], @plugin.iksless.uniq
end

- (Object) test_import_name



704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'test/test_plugin/text_info.rb', line 704

def test_import_name
  mapping = [
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
    ],
    [ 'ResultProduct.html',
      :submit,
      'Search.aspx',
    ],
    [ 'Aclasta.de.html',
      :submit,
      'Result.aspx?lang=de',
    ],
    [ 'Aclasta.fr.html',
      :get,
      'Result.aspx?lang=fr',
    ],
  ]
  agent = setup_mechanize mapping
  page = nil
  @parser.should_receive(:parse_fachinfo_html).and_return FachinfoDocument.new
  @parser.should_receive(:parse_patinfo_html).and_return PatinfoDocument.new
  assert_nothing_raised do
    @plugin.import_fulltext 'Trittico® retard', agent
  end
  assert_equal 4, @pages.size
  ## we didn't set up @parser to return a FachinfoDocument with an iksnr.
  #  the rest of the process is tested in test_update_product
  assert_equal ['Trittico® retard'], @plugin.iksless.uniq
end

- (Object) test_import_news



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'test/test_plugin/text_info.rb', line 735

def test_import_news
  logfile = File.join @vardir, 'fachinfo.txt'
  File.open logfile, 'w' do |fh|
    fh.puts "8a7f708c-c738-4425-a9a5-5ad294f20be4 Aclasta\302\256"
  end
  mapping = [
    [ 'News.html',
      :get,
      ODDB.config.text_info_newssource,
    ],
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
    ],
    [ 'ResultProduct.html',
      :submit,
      'Search.aspx',
    ],
    [ 'Aclasta.de.html',
      :submit,
      'Result.aspx?lang=de',
    ],
    [ 'Aclasta.fr.html',
      :get,
      'Result.aspx?lang=fr',
    ],
  ]
  agent = setup_mechanize mapping
  @parser.should_receive(:parse_fachinfo_html).and_return FachinfoDocument.new
  @parser.should_receive(:parse_patinfo_html).and_return PatinfoDocument.new
  @app.should_receive(:sorted_fachinfos).and_return []
  success = @plugin.import_news agent
  expected = "Abilify\302\256\nAbilify\302\256 Injektionsl\303\266sung\nAbseamed\302\256\nAceril\302\256- mite\nAcetaPhos\302\256 750 mg\nAcimethin\302\256\nAclasta\302\256"
  assert_equal 5, @pages.size
  assert_equal expected, File.read(logfile)
  assert_equal true, success
end

- (Object) test_import_products



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'test/test_plugin/text_info.rb', line 241

def test_import_products
  mapping = [
    [ 'Aclasta.de.html',
      :submit,
      'http://textinfo.ch/MonographieTxt.aspx?lang=de&MonType=fi',
    ],
    [ 'Aclasta.fr.html',
      :get,
      'http://textinfo.ch/MonographieTxt.aspx?lang=fr&MonType=fi',
    ]
  ]
  agent = setup_mechanize mapping
  path = File.join @datadir, 'ResultEmpty.html'
  result = setup_page 'http://textinfo.ch/Search.aspx', path, agent
  page = nil
  @parser.should_receive(:parse_fachinfo_html).and_return FachinfoDocument.new
  @parser.should_receive(:parse_patinfo_html).and_return PatinfoDocument.new
  assert_nothing_raised do
    @plugin.import_products result, agent
  end
end

- (Object) test_init_agent



91
92
93
94
95
# File 'test/test_plugin/text_info.rb', line 91

def test_init_agent
  agent = @plugin.init_agent
  assert_instance_of Mechanize, agent
  assert /Mozilla/.match(agent.user_agent)
end

- (Object) test_init_searchform__accept



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'test/test_plugin/text_info.rb', line 103

def test_init_searchform__accept
  mapping = [
    [ 'AcceptForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
      'frmNutzungsbedingungen',
      'SearchForm.html',
    ],
  ]
  agent = setup_mechanize mapping
  page = nil
  assert_nothing_raised do
    page = @plugin.init_searchform agent
  end
  assert_not_nil page.form_with(:name => 'frmSearchForm')
end

- (Object) test_init_searchform__not_configured



96
97
98
99
100
101
102
# File 'test/test_plugin/text_info.rb', line 96

def test_init_searchform__not_configured
  ODDB.config.text_info_searchform = nil
  agent = setup_mechanize
  assert_raises RuntimeError do
    @plugin.init_searchform agent
  end
end

- (Object) test_old_fachinfo_news



623
624
625
626
627
628
629
630
631
632
633
# File 'test/test_plugin/text_info.rb', line 623

def test_old_fachinfo_news
  ## no file means no news
  assert_equal [], @plugin.old_fachinfo_news
  File.open File.join(@vardir, 'fachinfo.txt'), 'w' do |fh|
    fh.puts "Amiodarone Winthrop\\302\\256/- Mite\nAcetaPhos\\302\\256 750 mg\n"
  end
  ## the file is parsed properly
  news = @plugin.old_fachinfo_news
  assert_equal 2, news.size
  assert_equal "Amiodarone Winthrop\302\256/- Mite", news.first
end

- (Object) test_rebuild_resultlist



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'test/test_plugin/text_info.rb', line 531

def test_rebuild_resultlist
  mapping = [
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
    ],
    [ 'Companies.html',
      :submit,
      'Search.aspx',
    ],
    [ 'ResultAlcaC.html',
      :submit,
      'Result.aspx?lang=de',
    ],
  ]
  agent = setup_mechanize mapping
  @plugin.current_search = [:search_company, 'Company Name']
  @plugin.current_eventtarget = "dtgFachinformationen$_ctl2$btnFachinformation"
  form = @plugin.rebuild_resultlist agent
  assert_instance_of Mechanize::Form, form
  assert_equal 'CompanyProdukte.aspx?lang=de', form.action
end

- (Object) test_search_company



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'test/test_plugin/text_info.rb', line 119

def test_search_company
  mapping = [
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
      'frmSearchForm',
      'Companies.html',
    ],
  ]
  agent = setup_mechanize mapping
  page = nil
  assert_nothing_raised do
    page = @plugin.search_company 'novartis', agent
  end
  assert_not_nil page.form_with(:name => 'frmResulthForm')
  assert_equal 1, @pages.size
end

- (Object) test_search_fulltext



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'test/test_plugin/text_info.rb', line 553

def test_search_fulltext
  mapping = [
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
      'frmSearchForm',
      'ResultFulltext.html',
    ],
  ]
  agent = setup_mechanize mapping
  page = nil
  assert_nothing_raised do
    page = @plugin.search_fulltext '53537', agent
  end
  assert_not_nil page.form_with(:name => 'frmResulthForm')
  assert_equal 1, @pages.size
end

- (Object) test_search_product



687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'test/test_plugin/text_info.rb', line 687

def test_search_product
  mapping = [
    [ 'SearchForm.html',
      :get,
      'http://textinfo.ch/Search.aspx',
      'frmSearchForm',
      'ResultProduct.html',
    ],
  ]
  agent = setup_mechanize mapping
  page = nil
  assert_nothing_raised do
    page = @plugin.search_product 'Trittico® retard', agent
  end
  assert_not_nil page.form_with(:name => 'frmResulthForm')
  assert_equal 1, @pages.size
end

- (Object) test_true_news



638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'test/test_plugin/text_info.rb', line 638

def test_true_news
  ## there are no news
  news = [
    "Abseamed\302\256",
    "Aclasta\302\256",
    "Alcacyl\302\256 500 Instant-Pulver",
    "Aldurazyme\302\256",
    "Allopur\302\256",
    "Allopurinol - 1 A Pharma100 mg/300 mg",
    "Amavita Acetylcystein 600",
    "Amavita Carbocistein",
    "Amavita Ibuprofen 400",
    "Amavita Paracetamol 500"
  ]
  old_news = [
    "Abseamed\302\256",
    "Aclasta\302\256",
  ]
  expected_news = [
    "Alcacyl\302\256 500 Instant-Pulver",
    "Aldurazyme\302\256",
    "Allopur\302\256",
    "Allopurinol - 1 A Pharma100 mg/300 mg",
    "Amavita Acetylcystein 600",
    "Amavita Carbocistein",
    "Amavita Ibuprofen 400",
    "Amavita Paracetamol 500"
  ]
  assert_equal expected_news, @plugin.true_news(news, old_news)
  ## clean disection
  old_news = [
    "Allopurinol - 1 A Pharma100 mg/300 mg",
    "Amavita Acetylcystein 600",
    "Amavita Carbocistein",
    "Amavita Ibuprofen 400",
    "Amavita Paracetamol 500"
  ]
  expected = [
    "Abseamed\302\256",
    "Aclasta\302\256",
    "Alcacyl\302\256 500 Instant-Pulver",
    "Aldurazyme\302\256",
    "Allopur\302\256",
  ]
  assert_equal expected, @plugin.true_news(news, old_news)
  ## recorded news don't appear on the news-page
  old_news = ["Amiodarone Winthrop\302\256/- Mite"]
  assert_equal news, @plugin.true_news(news, old_news)
end

- (Object) test_update_product__existing_infos



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'test/test_plugin/text_info.rb', line 375

def test_update_product__existing_infos
  de = setup_fachinfo_document 'Zulassungsnummer', '57363 (Swissmedic).'
  fr = setup_fachinfo_document 'Numéro d’autorisation', '57363 (Swissmedic).'
  fi_path_de = File.join(@datadir, 'Aclasta.de.html')
  fi_path_fr = File.join(@datadir, 'Aclasta.fr.html')
  fi_paths = { :de => fi_path_de, :fr => fi_path_fr }
  pi_path_de = File.join(@datadir, 'Aclasta.pi.de.html')
  pi_path_fr = File.join(@datadir, 'Aclasta.pi.fr.html')
  pi_paths = { :de => pi_path_de, :fr => pi_path_fr }
  pi_de = PatinfoDocument.new
  pi_fr = PatinfoDocument.new
  @parser.should_receive(:parse_fachinfo_html).with(fi_path_de).and_return de
  @parser.should_receive(:parse_fachinfo_html).with(fi_path_fr).and_return fr
  @parser.should_receive(:parse_patinfo_html).with(pi_path_de).and_return pi_de
  @parser.should_receive(:parse_patinfo_html).with(pi_path_fr).and_return pi_fr

  fi = flexmock 'fachinfo'
  fi.should_receive(:pointer).and_return Persistence::Pointer.new([:fachinfo,1])
  fi.should_receive(:empty?).and_return(true)
  pi = flexmock 'patinfo'
  pi.should_receive(:pointer).and_return Persistence::Pointer.new([:patinfo,1])
  ## this is conceptually a bit of a leap, but it tests all the code: even though
  #  pi is used to update the patinfo, I'm making it claim empty?, so that the
  #  deletion-code is triggered
  pi.should_receive(:empty?).and_return(true)
  reg = flexmock 'registration'
  reg.should_receive(:fachinfo).and_return fi
  ptr = Persistence::Pointer.new([:registration, '57363'])
  reg.should_receive(:pointer).and_return ptr
  seq = flexmock 'sequence'
  seq.should_receive(:patinfo).and_return pi
  seq.should_receive(:pointer).and_return ptr + [:sequence, '01']
  reg.should_receive(:each_sequence).and_return do |block| block.call seq end
  reg.should_receive(:sequences).and_return({'01' => seq})
  @app.should_receive(:registration).with('57363').and_return reg
  @app.should_receive(:update).and_return do |pointer, data|
    case pointer.to_s
    when ':!create,:!fachinfo..'
      assert_equal({:de => de, :fr => fr}, data)
      fi
    ## existing patinfos are handled differently than fachinfos!
    when ':!patinfo,1.'
      assert_equal({:de => pi_de, :fr => pi_fr}, data)
      pi
    when ':!registration,57363.'
      assert_equal({:fachinfo => fi.pointer}, data)
      reg
    when ':!registration,57363!sequence,01.'
      assert_equal({:patinfo => pi.pointer}, data)
      seq
    else
      flunk "unhandled call to update(#{pointer})"
    end
  end
  @app.should_receive(:delete).and_return do |pointer, data|
    case pointer.to_s
    when ':!fachinfo,1.'
      assert true
      fi
    when ':!patinfo,1.'
      assert true
      pi
    else
      flunk "unhandled call to delete(#{pointer})"
    end
  end
  result = @plugin.update_product 'Aclasta', fi_paths, pi_paths
end

- (Object) test_update_product__new_infos



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'test/test_plugin/text_info.rb', line 300

def test_update_product__new_infos
  de = setup_fachinfo_document 'Zulassungsnummer', '57363 (Swissmedic).'
  fr = setup_fachinfo_document 'Numéro d’autorisation', '57363 (Swissmedic).'
  fi_path_de = File.join(@datadir, 'Aclasta.de.html')
  fi_path_fr = File.join(@datadir, 'Aclasta.fr.html')
  fi_paths = { :de => fi_path_de, :fr => fi_path_fr }
  pi_path_de = File.join(@datadir, 'Aclasta.pi.de.html')
  pi_path_fr = File.join(@datadir, 'Aclasta.pi.fr.html')
  pi_paths = { :de => pi_path_de, :fr => pi_path_fr }
  pi_de = PatinfoDocument.new
  pi_fr = PatinfoDocument.new
  @parser.should_receive(:parse_fachinfo_html).with(fi_path_de).and_return de
  @parser.should_receive(:parse_fachinfo_html).with(fi_path_fr).and_return fr
  @parser.should_receive(:parse_patinfo_html).with(pi_path_de).and_return pi_de
  @parser.should_receive(:parse_patinfo_html).with(pi_path_fr).and_return pi_fr

  reg = flexmock 'registration'
  reg.should_receive(:fachinfo)
  ptr = Persistence::Pointer.new([:registration, '57363'])
  reg.should_receive(:pointer).and_return ptr
  seq = flexmock 'sequence'
  seq.should_receive(:patinfo)
  seq.should_receive(:pointer).and_return ptr + [:sequence, '01']
  reg.should_receive(:each_sequence).and_return do |block| block.call seq end
  reg.should_receive(:sequences).and_return({'01' => seq})
  @app.should_receive(:registration).with('57363').and_return reg
  fi = flexmock 'fachinfo'
  fi.should_receive(:pointer).and_return Persistence::Pointer.new([:fachinfo,1])
  pi = flexmock 'patinfo'
  pi.should_receive(:pointer).and_return Persistence::Pointer.new([:patinfo,1])
  @app.should_receive(:update).and_return do |pointer, data|
    case pointer.to_s
    when ':!create,:!fachinfo..'
      assert_equal({:de => de, :fr => fr}, data)
      fi
    when ':!create,:!patinfo..'
      assert_equal({:de => pi_de, :fr => pi_fr}, data)
      pi
    when ':!registration,57363.'
      assert_equal({:fachinfo => fi.pointer}, data)
      reg
    when ':!registration,57363!sequence,01.'
      assert_equal({:patinfo => pi.pointer}, data)
      seq
    else
      flunk "unhandled call to update(#{pointer})"
    end
  end
  result = @plugin.update_product 'Aclasta', fi_paths, pi_paths
  assert_equal "Searched for\nStored 1 Fachinfos\nIgnored 0 Pseudo-Fachinfos\nIgnored 0 up-to-date Fachinfo-Texts\nStored 1 Patinfos\nIgnored 0 up-to-date Patinfo-Texts\n\nChecked 0 companies\n\n\nUnknown Iks-Numbers: 0\n\n\nFachinfos without iksnrs: 0\n\n\nSession failures: 0\n\nDownload errors: 0\n\n\nParse Errors: 0\n", @plugin.report
end

- (Object) test_update_product__orphaned_infos



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'test/test_plugin/text_info.rb', line 443

def test_update_product__orphaned_infos
  de = setup_fachinfo_document 'Zulassungsnummer', '57363 (Swissmedic).'
  fr = setup_fachinfo_document 'Numéro d’autorisation', '57363 (Swissmedic).'
  fi_path_de = File.join(@datadir, 'Aclasta.de.html')
  fi_path_fr = File.join(@datadir, 'Aclasta.fr.html')
  fi_paths = { :de => fi_path_de, :fr => fi_path_fr }
  pi_path_de = File.join(@datadir, 'Aclasta.pi.de.html')
  pi_path_fr = File.join(@datadir, 'Aclasta.pi.fr.html')
  pi_paths = { :de => pi_path_de, :fr => pi_path_fr }
  pi_de = PatinfoDocument.new
  pi_fr = PatinfoDocument.new
  @parser.should_receive(:parse_fachinfo_html).with(fi_path_de).and_return de
  @parser.should_receive(:parse_fachinfo_html).with(fi_path_fr).and_return fr
  @parser.should_receive(:parse_patinfo_html).with(pi_path_de).and_return pi_de
  @parser.should_receive(:parse_patinfo_html).with(pi_path_fr).and_return pi_fr

  @app.should_receive(:registration).with('57363')
  @app.should_receive(:update).and_return do |pointer, data|
    case pointer.to_s
    when ":!create,:!orphaned_fachinfo.."
      expected = {
        :key => '57363',
        :languages => { :de => de, :fr => fr },
      }
      assert_equal expected, data
    when ":!create,:!orphaned_patinfo.."
      expected = {
        :key => '57363',
        :languages => { :de => pi_de, :fr => pi_fr },
      }
      assert_equal expected, data
    else
      flunk "unhandled call to update(#{pointer})"
    end
  end
  result = @plugin.update_product 'Aclasta', fi_paths, pi_paths
end

- (Object) test_update_product__up_to_date_infos



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'test/test_plugin/text_info.rb', line 480

def test_update_product__up_to_date_infos
  de = setup_fachinfo_document 'Zulassungsnummer', '57363 (Swissmedic).'
  fr = setup_fachinfo_document 'Numéro d’autorisation', '57363 (Swissmedic).'
  fi_path_de = File.join(@datadir, 'Aclasta.de.html')
  fi_path_fr = File.join(@datadir, 'Aclasta.fr.html')
  fi_paths = { :de => fi_path_de, :fr => fi_path_fr }
  pi_path_de = File.join(@datadir, 'Aclasta.pi.de.html')
  pi_path_fr = File.join(@datadir, 'Aclasta.pi.fr.html')
  pi_paths = { :de => pi_path_de, :fr => pi_path_fr }
  @parser.should_receive(:parse_fachinfo_html).with(fi_path_de).times(1).and_return de
  @parser.should_receive(:parse_fachinfo_html).with(fi_path_fr).times(1).and_return fr

  reg = flexmock 'registration'
  reg.should_receive(:fachinfo)
  ptr = Persistence::Pointer.new([:registration, '57363'])
  reg.should_receive(:pointer).and_return ptr
  seq = flexmock 'sequence'
  seq.should_receive(:patinfo)
  seq.should_receive(:pointer).and_return ptr + [:sequence, '01']
  reg.should_receive(:each_sequence).and_return do |block| block.call seq end
  reg.should_receive(:sequences).and_return({'01' => seq})
  @app.should_receive(:registration).with('57363').and_return reg
  fi = flexmock 'fachinfo'
  fi.should_receive(:pointer).and_return Persistence::Pointer.new([:fachinfo,1])
  pi = flexmock 'patinfo'
  pi.should_receive(:pointer).and_return Persistence::Pointer.new([:patinfo,1])
  flags = {:de => :up_to_date, :fr => :up_to_date}
  result = @plugin.update_product 'Aclasta', fi_paths, pi_paths, flags, flags
  assert true # no call to parse_patinfo or @app.update has been made
end