# -*- encoding : utf-8 -*- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') 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 def year_range_xml " 01021cam a2200277 a 4500 a1711966 SIRSI 890421s1988 enka 001 0 eng d Schmoe, Joe Main title / Subtitle London : Batsford, 1988-2000 " end def no_date_xml " 01021cam a2200277 a 4500 a1711966 SIRSI 890421s1988 enka 001 0 eng d Schmoe, Joe Main title / Subtitle London : Batsford, n.d. " end def section_title_xml " 01021cam a2200277 a 4500 a1711966 SIRSI 890421s1988 enka 001 0 eng d Schmoe, Joe Main title / Subtitle Number of part. Name of part. London : Batsford, 2001 " end def dissertation_note_xml " 00903nam a2200253 4500 730111s1971 ohu b 000 0 eng Thesis on some subject. 2009 Phd Thesis -- Goodenough College, 2009 " end def special_contributor_with_author_xml " 00903nam a2200253 4500 730111s1971 ohu b 000 0 eng Doe, John Title of item. Publisher Place 2009 Schmoe, Joe trl. Schmoe, Bill edt Schmoe, Susie com. " end def three_authors_xml " 00903nam a2200253 4500 730111s1971 ohu b 000 0 eng Doe, John Title of item. Publisher Place 2009 Schmoe, Joe Schmoe, Bill " end def special_contributor_no_author_xml " 00903nam a2200253 4500 730111s1971 ohu b 000 0 eng Title of item. Publisher Place 2009 Schmoe, Joe trl. Schmoe, Bill edt Schmoe, Susie com. " 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. Doe, John Doe, Jane Smith, John Smith, Jane Smith, James " 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 ) @dissertation_record = dclass.new( dissertation_note_xml ) @record_without_245b = dclass.new( record1_xml ) @three_authors_record = dclass.new( three_authors_xml ) @record_without_authors = dclass.new( record2_xml ) @record_with_10plus_authors = dclass.new( record3_xml ) @year_range_record = dclass.new( year_range_xml ) @no_date_record = dclass.new( no_date_xml ) @section_title_record = dclass.new( section_title_xml ) @special_contributor_record = dclass.new( special_contributor_with_author_xml ) @record_without_citable_data = dclass.new( no_good_data_xml ) @record_with_bad_author = dclass.new( bad_author_xml ) @special_contributor_no_auth_record = dclass.new( special_contributor_no_author_xml ) @record_utf8_decomposed = dclass.new( utf8_decomposed_record_xml ) end describe "export_as_chicago_citation_txt" do it "should handle a typical record correclty" do @typical_record.export_as_chicago_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 record w/o authors correctly" do @record_without_authors.export_as_chicago_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 without a 245b field correctly" do @record_without_245b.export_as_chicago_citation_txt.should == "Janetzky, Kurt., and Bernhard Brüchle. The Horn. London: Batsford, 1988." end it "should format a citation with 4+ authors correctly" do chicago_text = @record_with_10plus_authors.export_as_chicago_citation_txt chicago_text.should == "Greer, Lowell., Steven Lubin, Stephanie Chase, Johannes Brahms, Ludwig van Beethoven, Nikolaus von Krufft, John Doe, et al. Music for Horn. [United States]: Harmonia Mundi USA, 2001." chicago_text.should match(/John Doe, et al\./) chicago_text.should_not match(/Jane Doe/) end it "should handle dissertation data correctly" do @dissertation_record.export_as_chicago_citation_txt.should == "Thesis On Some Subject. Phd Thesis -- Goodenough College, 2009." end it "should handle 3 authors correctly" do @three_authors_record.export_as_chicago_citation_txt.should match(/^Doe, John, Joe Schmoe, and Bill Schmoe\./) end it "should handle editors, translators, and compilers correctly" do @special_contributor_record.export_as_chicago_citation_txt.should == "Doe, John Title of Item. Translated by Joe Schmoe. Edited by Bill Schmoe. Compiled by Susie Schmoe. Publisher: Place, 2009." end it "should handle editors, translators, and compilers correctly when there is no author present" do @special_contributor_no_auth_record.export_as_chicago_citation_txt.should == "Schmoe, Joe trans., Bill Schmoe ed., and Susie Schmoe comp. Title of Item. Publisher: Place, 2009." end it "should handle year ranges properly" do @year_range_record.export_as_chicago_citation_txt.should_not match(/2000/) end it "should handle n.d. in the 260$c properly" do @no_date_record.export_as_chicago_citation_txt.should match(/n\.d\.$/) end it "should handle section title appropriately" do @section_title_record.export_as_chicago_citation_txt.should == "Schmoe, Joe Main Title: Subtitle\.<\/i> Number of Part, Name of Part\.<\/i> London: Batsford, 2001." end it "should not fail if there is no citation data" do @record_without_citable_data.export_as_chicago_citation_txt.should == "" end 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_10plus_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 it "should export in Unicode normalized C form" do @utf8_exported = @record_utf8_decomposed.export_as_refworks_marc_txt if defined? Unicode @utf8_exported.should_not include("\314\204\312\273") # decomposed @utf8_exported.should include("\304\253\312\273") # C-form normalized end 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(["Generic"]) # 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 describe "Citation title transformation" do before(:each) do class CitationTitleTest include Blacklight::Solr::Document::MarcExport def transform_title(text) citation_title(text) end end @citation_title = CitationTitleTest.new end it "should tranform a normal title properlly" do @citation_title.transform_title("This is just a test").should == "This Is Just a Test" end it "should not attempt to capatilize words in all-caps (e.g. acronyms)" do @citation_title.transform_title("IBM PC applications").should == "IBM PC Applications" end it "should capitalize a preposition if it is the first word in a title" do @citation_title.transform_title("of mice and men").should == "Of Mice and Men" end it "should handle hyphenated words properly" do @citation_title.transform_title("Testing hyphenated-words and how they get capitalized").should == "Testing Hyphenated-Words and How They Get Capitalized" end it "should handle normal prepositions properly" do @citation_title.transform_title("A little learning is a dangerous thing").should == "A Little Learning Is a Dangerous Thing" @citation_title.transform_title("Four theories concerning the Gospel according to Matthew").should == "Four Theories Concerning the Gospel According to Matthew" @citation_title.transform_title("Tired but happy").should == "Tired but Happy" @citation_title.transform_title("Mnemonics that work are better than rules that do not").should == "Mnemonics That Work Are Better Than Rules That Do Not" @citation_title.transform_title("This is just a test").should == "This Is Just a Test" @citation_title.transform_title("the mind of the Renaissance").should == "The Mind of the Renaissance" @citation_title.transform_title("Are you considering psychoanalysis?").should == "Are You Considering Psychoanalysis?" @citation_title.transform_title("Not without laughter").should == "Not without Laughter" end end end