# -*- coding: utf-8 -*- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'rubygems' require 'marc' def marc_from_xml(string) reader = MARC::XMLReader.new(StringIO.new(string)) reader.each {|rec| return rec } end def standard_citation " 01182pam a22003014a 4500 a4802615 SIRSI 020828s2003 enkaf b 001 0 eng Bobs Your Uncle Apples : botany, production, and uses / edited by D.C. Ferree and I.J. Warrington. Oxon, U.K. ; Cambridge, MA : CABI Pub., c2003. Ferree, David C. (David Curtis), 1943- Warrington, I. J. (Ian J.) " end def music_record " 01828cjm a2200409 a 4500 a4768316 SIRSI sd fungnnmmned 020117p20011990xxuzz h d Music for horn [sound recording] / Brahms, Beethoven, von Krufft. [United States] : Harmonia Mundi USA, p2001. Greer, Lowell. Lubin, Steven. Chase, Stephanie, 1957- Brahms, Johannes, 1833-1897. Trios, piano, violin, horn, op. 40, E? major. Beethoven, Ludwig van, 1770-1827. Sonatas, horn, piano, op. 17, F major. Krufft, Nikolaus von, 1779-1818. Sonata, horn, piano, F major. " end def utf8_decomposed_record_xml " 01341nam 2200301 a 450 19971120234400.0 890316s1988 caua b 101 0 eng Sharīʻat and ambiguity in South Asian Islam / edited by Katherine P. Ewing. " end # 1:100,1:700,245a,0:245b, def record1_xml " 01021cam a2200277 a 4500 a1711966 SIRSI 890421s1988 enka 001 0 eng d Janetzky, Kurt. The horn / Kurt Janetzky and Bernhard Bruchle ; translated from the German by James Chater. London : Batsford, 1988. Brüchle, Bernhard. " end # 0:100,0:700,245a,0:245b def record2_xml " 00903nam a2200253 4500 a543347 SIRSI 730111s1971 ohu b 000 0 eng Final report to the Honorable John J. Gilligan, Governor. [Columbus, Printed by the State of Ohio, Dept. of Urban Affairs, 1971] " end # 4+:athors def record3_xml " 01828cjm a2200409 a 4500 a4768316 SIRSI sd fungnnmmned 020117p20011990xxuzz h d Music for horn [sound recording] / Brahms, Beethoven, von Krufft. [United States] : Harmonia Mundi USA, p2001. Greer, Lowell. Lubin, Steven. Chase, Stephanie, 1957- Brahms, Johannes, 1833-1897. Trios, piano, violin, horn, op. 40, E? major. Beethoven, Ludwig van, 1770-1827. Sonatas, horn, piano, op. 17, F major. Krufft, Nikolaus von, 1779-1818. Sonata, horn, piano, F major. " end # No elements that can be put into a citation def no_good_data_xml " 01828cjm a2200409 a 4500 a4768316 SIRSI sd fungnnmmned 020117p20011990xxuzz h d 713746703721 HCX 3957037 Harmonia Mundi USA 19901203 19901206 (OCoLC-M)48807235 WC4 WC4 CSt engfre " end # Bad author name def bad_author_xml " 01021cam a2200277 a 4500 a1711966 SIRSI 890421s1988 enka 001 0 eng d The horn / Kurt Janetzky and Bernhard Bruchle ; translated from the German by James Chater. London : Batsford, 1988. Brüchle, Bernhard. " end describe Blacklight::Solr::Document::MarcExport do before(:all) do dclass = Class.new do include Blacklight::Solr::Document::MarcExport attr_accessor :to_marc def initialize(marc_xml_str) self.to_marc = marc_from_xml(marc_xml_str) end end @typical_record = dclass.new( standard_citation ) @music_record = dclass.new( music_record ) @record_without_245b = dclass.new( record1_xml ) @record_without_authors = dclass.new( record2_xml ) @record_with_4plus_authors = dclass.new( record3_xml ) @record_without_citable_data = dclass.new( no_good_data_xml ) @record_with_bad_author = dclass.new( bad_author_xml ) @record_utf8_decomposed = dclass.new( utf8_decomposed_record_xml ) end describe "export_as_apa_citation_txt" do it "should format a standard citation correctly" do @typical_record.export_as_apa_citation_txt.should == "Ferree, D. C, & Warrington, I. J. (2003). Apples : botany, production, and uses. Oxon, U.K.: CABI Pub." end it "should format a citation without a 245b field correctly" do @record_without_245b.export_as_apa_citation_txt.should == "Janetzky, K., & Brüchle, B. (1988). The horn. London: Batsford." end it "should format a citation without any authors correctly" do @record_without_authors.export_as_apa_citation_txt.should == "(1971). Final report to the Honorable John J. Gilligan, Governor. [Columbus: Printed by the State of Ohio, Dept. of Urban Affairs." end it "should not fail if there is no citation data" do @record_without_citable_data.export_as_apa_citation_txt.should == "" end it "should not bomb with a null pointer if there if author data is empty" do @record_with_bad_author.export_as_apa_citation_txt.should == "Brüchle, B. (1988). The horn. London: Batsford." end end describe "export_as_mla_citation_txt" do it "should format a standard citation correctly" do @typical_record.export_as_mla_citation_txt.should == "Ferree, David C, and I. J Warrington. Apples : Botany, Production, and Uses. Oxon, U.K.: CABI Pub., 2003." end it "should format a citation without a 245b field correctly" do @record_without_245b.export_as_mla_citation_txt.should == "Janetzky, Kurt, and Bernhard Brüchle. The Horn. London: Batsford, 1988." end it "should format a citation without any authors correctly" do @record_without_authors.export_as_mla_citation_txt.should == "Final Report to the Honorable John J. Gilligan, Governor. [Columbus: Printed by the State of Ohio, Dept. of Urban Affairs, 1971." end it "should format a citation with 4+ authors correctly" do @record_with_4plus_authors.export_as_mla_citation_txt.should == "Greer, Lowell, et al. Music for Horn. [United States]: Harmonia Mundi USA, 2001." end it "should not fail if there is no citation data" do @record_without_citable_data.export_as_mla_citation_txt.should == "" end end describe "export_as_openurl_ctx_kev" do it "should create the appropriate context object for books" do record = @typical_record.export_as_openurl_ctx_kev('Book') record.should match(/.*mtx%3Abook.*rft.genre=book.*rft.btitle=Apples\+%3A\+botany%2C\+production%2C\+and\+uses.*rft.aucorp=Bobs\+Your\+Uncle.*rft.date=c2003.*rft.place=Oxon%2C\+U.K.*rft.pub=CABI\+Pub.*rft.isbn=/) and record.should_not match(/.*rft.genre=article.*rft.issn=.*/) end it "should create the appropriate context object for journals" do record = @typical_record.export_as_openurl_ctx_kev('Journal') record_journal_other = @typical_record.export_as_openurl_ctx_kev('Journal/Magazine') record.should match(/.*mtx%3Ajournal.*rft.genre=article.*rft.title=Apples\+%3A\+botany%2C\+production%2C\+and\+uses.*rft.atitle=Apples\+%3A\+botany%2C\+production%2C\+and\+uses.*rft.aucorp=Bobs\+Your\+Uncle.*rft.date=c2003.*rft.issn=/) and record_journal_other.should == record and record.should_not match(/.*rft.genre=book.*rft.isbn=.*/) end it "should create the appropriate context object for other content" do record = @typical_record.export_as_openurl_ctx_kev('NotARealFormat') record.should match(/.*mtx%3Adc.*rft.title=Apples\+%3A\+botany%2C\+production%2C\+and\+uses.*rft.creator=.*rft.aucorp=Bobs\+Your\+Uncle.*rft.date=c2003.*rft.place=Oxon%2C\+U.K.*rft.pub=CABI\+Pub.*rft.format=notarealformat/) and record.should_not match(/.*rft.isbn=.*/) and record.should_not match(/.*rft.issn=.*/) end end describe "export_as_marc binary" do it "should export_as_marc" do @typical_record.export_as_marc.should == @typical_record.to_marc.to_marc end end describe "export_as_marcxml" do it "should export_as_marcxml" do marc_from_xml(@typical_record.export_as_marcxml).should == marc_from_xml(@typical_record.to_marc.to_xml.to_s) end end describe "export_as_xml" do it "should export marcxml as xml" do marc_from_xml(@typical_record.export_as_xml).should == marc_from_xml(@typical_record.export_as_marcxml) end end describe "export_as_refworks_marc_txt" do it "should export correctly" do @music_record.export_as_refworks_marc_txt.should == "LEADER 01828cjm a2200409 a 4500001 a4768316\n003 SIRSI\n007 sd fungnnmmned\n008 020117p20011990xxuzz h d\n245 00 Music for horn |h[sound recording] / |cBrahms, Beethoven, von Krufft.\n260 [United States] : |bHarmonia Mundi USA, |cp2001.\n700 1 Greer, Lowell.\n700 1 Lubin, Steven.\n700 1 Chase, Stephanie, |d1957-\n700 12 Brahms, Johannes, |d1833-1897. |tTrios, |mpiano, violin, horn, |nop. 40, |rE? major.\n700 12 Beethoven, Ludwig van, |d1770-1827. |tSonatas, |mhorn, piano, |nop. 17, |rF major.\n700 12 Krufft, Nikolaus von, |d1779-1818. |tSonata, |mhorn, piano, |rF major.\n" end describe "for UTF-8 record" do before do @utf8_exported = @record_utf8_decomposed.export_as_refworks_marc_txt end it "should export in Unicode normalized C form" do @utf8_exported.should_not include("\314\204\312\273") # decomposed @utf8_exported.should include("\304\253\312\273") # C-form normalized end end end describe "export_as_endnote" do it "should export_correctly" do endnote_file = @music_record.export_as_endnote # We have to parse it a bit to check it. endnote_entries = Hash.new {|hash, key| hash[key] = Set.new } endnote_file.each_line do |line| line =~ /\%(..?) (.*)$/ endnote_entries[$1] << $2 end endnote_entries["0"].should == Set.new("Format") # I have no idea WHY this is correct, it is definitely not legal, but taking from earlier test for render_endnote in applicationhelper, the previous version of this. jrochkind. endnote_entries["D"].should == Set.new("p2001. ") endnote_entries["C"].should == Set.new("[United States] : ") endnote_entries["E"].should == Set.new(["Greer, Lowell. ", "Lubin, Steven. ", "Chase, Stephanie, ", "Brahms, Johannes, ", "Beethoven, Ludwig van, ", "Krufft, Nikolaus von, "]) endnote_entries["I"].should == Set.new("Harmonia Mundi USA, ") endnote_entries["T"].should == Set.new("Music for horn ") #nothing extra Set.new(endnote_entries.keys).should == Set.new(["0", "C", "D", "E", "I", "T"]) end end end