# -*- encoding : utf-8 -*- require 'spec_helper' require 'active_model' describe "Blacklight::Solr::Document::Marc" do before(:all) do @mock_class = Class.new do include Blacklight::Solr::Document end @mock_class.use_extension( Blacklight::Solr::Document::Marc ) @mock_class.extension_parameters[:marc_source_field] = :marc end describe "marc binary mode" do before(:each) do @mock_class.extension_parameters[:marc_format_type] = :marc21 end it "should read and parse a marc binary file" do document = @mock_class.new(:marc => sample_marc_binary ) document.to_marc.should == marc_from_string(:binary => sample_marc_binary ) end end describe "marcxml mode" do before(:each) do @mock_class.extension_parameters[:marc_format_type] = :marcxml end it "should read and parse a marc xml file" do document = @mock_class.new(:marc => sample_marc_xml) document.to_marc.should == marc_from_string(:xml => sample_marc_xml) end end it "should register all its export formats" do document = @mock_class.new Set.new(document.export_formats.keys).should be_superset(Set.new([:marc, :marcxml, :openurl_ctx_kev, :refworks_marc_txt, :endnote, :xml])) end def sample_marc_xml " 01182pam a22003014a 4500 a4802615 SIRSI 020828s2003 enkaf b 001 0 eng 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 sample_marc_binary "00386pam a22001094a 4500001000900000003000600009008004100015245008900056260005400145700004500199700003200244\036a4802615\036SIRSI\036020828s2003 enkaf b 001 0 eng \03600\037aApples :\037bbotany, production, and uses /\037cedited by D.C. Ferree and I.J. Warrington.\036 \037aOxon, U.K. ;\037aCambridge, MA :\037bCABI Pub.,\037cc2003.\0361 \037aFerree, David C.\037q(David Curtis),\037d1943-\0361 \037aWarrington, I. J.\037q(Ian J.)\036\035" end def marc_from_string(args = {}) if args[:binary] reader = MARC::Reader.new(StringIO.new(args[:binary])) reader.each {|rec| return rec } elsif args[:xml] reader = MARC::XMLReader.new(StringIO.new(args[:xml])) reader.each {|rec| return rec } end return nil end end