spec/lib/ezid/metadata_spec.rb in ezid-client-0.2.0 vs spec/lib/ezid/metadata_spec.rb in ezid-client-0.3.0

- old
+ new

@@ -1,97 +1,97 @@ module Ezid RSpec.describe Metadata do - - describe "method missing" do - context "for an internal element name w/o leading underscore" do - it "should call the reader method" do - expect(subject).to receive(:reader).with("_status") - subject.status - end + + let(:elements) do + { "_updated" => "1416507086", + "_target" => "http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87", + "_profile" => "erc", + "_ownergroup" => "apitest", + "_owner" => "apitest", + "_export" => "yes", + "_created" => "1416507086", + "_status" => "public" } + end + before { subject.instance_variable_set(:@elements, elements) } + describe "#status" do + it "should return the status" do + expect(subject.status).to eq("public") end - context "for an internal writable element name + '=' and w/o leading underscore" do - it "should call the writer method" do - expect(subject).to receive(:writer).with("_status", "public") - subject.status = "public" - end + end + describe "#target" do + it "should return the target URL" do + expect(subject.target).to eq("http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87") end - context "for an internal readonly element name + '=' and w/o leading underscore" do - it "should not call the writer method" do - expect { subject.created = "1416507086" }.to raise_error - end + end + describe "#profile" do + it "should return the profile" do + expect(subject.profile).to eq("erc") end end - - describe "internal element reader" do - context "for a datetime element" do - before { subject["_created"] = "1416507086" } - it "should return a Time" do - expect(subject.created).to be_a(Time) - end + describe "#created" do + it "should return the creation time" do + expect(subject.created).to be_a(Time) end - context "for a non-datetime element" do - before { subject["_status"] = "public" } - it "should return the value" do - expect(subject.status).to eq("public") - end - end end - - describe "internal element writer" do - before { subject["_status"] = "reserved" } - it "should set the element" do - expect { subject.status = "public" }.to change { subject["_status"] }.from("reserved").to("public") + describe "#updated" do + it "should return the last update time" do + expect(subject.updated).to be_a(Time) end end describe "ANVL output" do - let(:metadata) { described_class.new(_updated: "1416507086", - _target: "http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87", - _profile: "erc", - _ownergroup: "apitest", - _owner: "apitest", - _export: "yes", - _created: "1416507086", - _status: "public") } it "should output the proper format" do - expect(metadata.to_anvl).to eq("\ + expect(subject.to_anvl).to eq("\ _updated: 1416507086 _target: http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87 _profile: erc _ownergroup: apitest _owner: apitest _export: yes _created: 1416507086 _status: public") end + describe "encoding" do + before do + subject.each_key { |k| subject[k] = subject[k].force_encoding("US_ASCII") } + end + end + it "should be encoded in UTF-8" do + expect(subject.to_anvl.encoding).to eq(Encoding::UTF_8) + end describe "escaping" do - context "of element names" do - it "should escape a colon" - it "should escape a line feed" - it "should escape a carriage return" - it "should escape a percent sign" + before do + subject["_target"] = "http://example.com/path%20with%20spaces" + subject["dc.title"] = "A really long title\nneeds a line feed" + subject["dc.creator"] = "David Chandek-Stark\r\nJim Coble" end - context "of element values" do - it "should escape a line feed" - it "should escape a carriage return" - it "should escape a percent sign" + it "should escape a line feed" do + expect(subject.to_anvl).to match(/dc.title: A really long title%0Aneeds a line feed/) end + it "should escape a carriage return" do + expect(subject.to_anvl).to match(/dc.creator: David Chandek-Stark%0D%0AJim Coble/) + end + it "should escape a percent sign" do + expect(subject.to_anvl).to match(/_target: http:\/\/example.com\/path%2520with%2520spaces/) + end end end describe "coercion" do + subject { described_class.new(data) } context "of a string" do - let(:data) { "\ + let(:data) do <<-EOS _updated: 1416507086 _target: http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87 _profile: erc _ownergroup: apitest _owner: apitest _export: yes _created: 1416507086 -_status: public" } - subject { described_class.new(data) } +_status: public +EOS + end it "should coerce the data into a hash" do expect(subject.elements).to eq({"_updated" => "1416507086", "_target" => "http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87", "_profile" => "erc", "_ownergroup" => "apitest", @@ -100,13 +100,29 @@ "_created" => "1416507086", "_status" => "public"}) end end context "of a hash" do - it "should stringify the keys" - end - context "of an Ezid::Metadata instance" do - it "should return the elements hash" + let(:data) do + { _updated: "1416507086", + _target: "http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87", + _profile: "erc", + _ownergroup: "apitest", + _owner: "apitest", + _export: "yes", + _created: "1416507086", + _status: "public" } + end + it "should stringify the keys" do + expect(subject.elements).to eq({"_updated" => "1416507086", + "_target" => "http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87", + "_profile" => "erc", + "_ownergroup" => "apitest", + "_owner" => "apitest", + "_export" => "yes", + "_created" => "1416507086", + "_status" => "public"}) + end end end end end