Skip to content

Commit

Permalink
Merge pull request #629 from projecthydra/issue_48
Browse files Browse the repository at this point in the history
github issue 48 tests and YARD
  • Loading branch information
jcoyne committed Dec 4, 2014
2 parents 0b9fe27 + 0036014 commit d06db81
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 46 deletions.
5 changes: 5 additions & 0 deletions lib/active_fedora/datastreams.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ def add_file_datastream(file, opts={})
end


# Creates a datastream to be added to the object
# @param type [Class] the class of the datastream object to be created (or a String that can eval to a class)
# @param dsid [String] the dsid of the datastream to be created, or a generated value if false
# @param opts [Hash] the ds options to be assigned to the created object, cf. Rubydora::Datastream.DS_ATTRIBUTES
# @return [ActiveFedora::Datastream] a datastream object of the type indicated in the parameters
def create_datastream(type, dsid, opts={})
klass = type.kind_of?(Class) ? type : type.constantize
raise ArgumentError, "Argument dsid must be of type string" if dsid && !dsid.kind_of?(String)
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/active_fedora_dev.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
APP_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../")

require 'jettywrapper'
JETTY_ZIP_BASENAME = 'master'
JETTY_ZIP_BASENAME = '7.x-stable'
Jettywrapper.url = "https://github.com/projecthydra/hydra-jetty/archive/#{JETTY_ZIP_BASENAME}.zip"

namespace :active_fedora do
Expand Down
131 changes: 86 additions & 45 deletions spec/integration/datastreams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
describe "serializing datastreams" do
before :all do
class TestingMetadataSerializing < ActiveFedora::Base
has_metadata :name => "nokogiri_autocreate_on", :autocreate => true, :type => ActiveFedora::OmDatastream
has_metadata :name => "nokogiri_autocreate_off", :autocreate => false, :type => ActiveFedora::OmDatastream
has_metadata name: "nokogiri_autocreate_on", autocreate: true, type: ActiveFedora::OmDatastream
has_metadata name: "nokogiri_autocreate_off", autocreate: false, type: ActiveFedora::OmDatastream
end
end

Expand All @@ -19,7 +19,7 @@ class TestingMetadataSerializing < ActiveFedora::Base
subject { TestingMetadataSerializing.new }

it "should work" do
subject.save(:validate => false)
subject.save(validate: false)
subject.nokogiri_autocreate_on.should_not be_new
subject.nokogiri_autocreate_off.should be_new
end
Expand All @@ -29,104 +29,145 @@ class TestingMetadataSerializing < ActiveFedora::Base
describe "#has_metadata" do
before :all do
class HasMetadata < ActiveFedora::Base
has_metadata :name => "with_versions", :autocreate => true, :label => "Versioned DS", :type => ActiveFedora::SimpleDatastream
has_metadata :name => "without_versions", :autocreate => true, :versionable => false, :type => ActiveFedora::SimpleDatastream
has_metadata name: "with_versions", autocreate: true, label: "Versioned DS", type: ActiveFedora::SimpleDatastream
has_metadata name: "without_versions", autocreate: true, versionable: false, type: ActiveFedora::SimpleDatastream
end
end
after :all do
Object.send(:remove_const, :HasMetadata)
end
before :each do
@base = ActiveFedora::Base.new(:pid=>"test:has_metadata_base")
@base.add_datastream(@base.create_datastream(ActiveFedora::Datastream, "testDS", :dsLabel => "Test DS"))
before do
@base = ActiveFedora::Base.new(pid:"test:has_metadata_base")
@base.add_datastream(@base.create_datastream(ActiveFedora::Datastream, "testDS", dsLabel: "Test DS"))
@base.datastreams["testDS"].content = "blah blah blah"
@base.save
@test = HasMetadata.new(:pid=>"test:has_metadata_model")
@test = HasMetadata.new(pid:"test:has_metadata_model")
@test.save
end

after :each do
after do
@base.delete
@test.delete
end

it "should create datastreams from the spec on new objects" do
@test.without_versions.versionable.should be_false
@test.with_versions.versionable.should be_true
@test.with_versions.dsLabel.should == "Versioned DS"
expect(@test.without_versions.versionable).to be_false
expect(@test.with_versions.versionable).to be_true
expect(@test.with_versions.dsLabel).to eql "Versioned DS"
@test.without_versions.content= "blah blah blah"
@test.save
HasMetadata.find(@test.pid).without_versions.versionable.should be_false
expect(HasMetadata.find(@test.pid).without_versions.versionable).to be_false
end

it "should use ds_specs and preserve existing datastreams on migrated objects" do
test_obj = HasMetadata.find(@base.pid)
test_obj.datastreams["testDS"].dsLabel.should == "Test DS"
test_obj.datastreams["testDS"].new?.should be_false
test_obj.with_versions.dsLabel.should == "Versioned DS"
test_obj.without_versions.versionable.should be_false
test_obj.with_versions.new?.should be_true
expect(test_obj.datastreams["testDS"].dsLabel).to eql "Test DS"
expect(test_obj.datastreams["testDS"].new?).to be_false
expect(test_obj.with_versions.dsLabel).to eql "Versioned DS"
expect(test_obj.without_versions.versionable).to be_false
expect(test_obj.with_versions.new?).to be_true
end

end

describe "#has_file_datastream" do
before :all do
class HasFile < ActiveFedora::Base
has_file_datastream :name => "file_ds", :versionable => false
has_file_datastream :name => "file_ds2", :versionable => false, :autocreate => false
has_file_datastream name: "file_ds", versionable: false
has_file_datastream name: "file_ds2", versionable: false, autocreate: false
end
end
after :all do
Object.send(:remove_const, :HasFile)
end
before :each do
@base = ActiveFedora::Base.new(:pid=>"test:ds_versionable_base")
before do
@base = ActiveFedora::Base.new(pid:"test:ds_versionable_base")
@base.save
@base2 = ActiveFedora::Base.new(:pid=>"test:ds_versionable_base2")
@base2.add_datastream(@base2.create_datastream(ActiveFedora::Datastream,"file_ds", :versionable=>true,:dsLabel=>"Pre-existing DS"))
@base2 = ActiveFedora::Base.new(pid:"test:ds_versionable_base2")
@base2.add_datastream(@base2.create_datastream(ActiveFedora::Datastream,"file_ds", versionable:true,dsLabel:"Pre-existing DS"))
@base2.datastreams["file_ds"].content = "blah blah blah"
@base2.save
@has_file = HasFile.new(:pid=>"test:ds_versionable_has_file")
@has_file = HasFile.new(pid:"test:ds_versionable_has_file")
@has_file.save
end

after :each do
after do
@base.delete
@base2.delete
@has_file.delete
end

it "should create datastreams from the spec on new objects" do
@has_file.file_ds.versionable.should be_false
expect(@has_file.file_ds.versionable).to be_false
@has_file.file_ds.content = "blah blah blah"
@has_file.file_ds.changed?.should be_true
@has_file.file_ds2.changed?.should be_false # no autocreate
@has_file.file_ds2.new?.should be_true
expect(@has_file.file_ds.changed?).to be_true
expect(@has_file.file_ds2.changed?).to be_false # no autocreate
expect(@has_file.file_ds2.new?).to be_true
@has_file.save
@has_file.file_ds.versionable.should be_false
expect(@has_file.file_ds.versionable).to be_false
test_obj = HasFile.find(@has_file.pid)
test_obj.file_ds.versionable.should be_false
test_obj.rels_ext.changed?.should be_false
test_obj.file_ds.changed?.should be_false
test_obj.file_ds2.changed?.should be_false
test_obj.file_ds2.new?.should be_true
expect(test_obj.file_ds.versionable).to be_false
expect(test_obj.rels_ext.changed?).to be_false
expect(test_obj.file_ds.changed?).to be_false
expect(test_obj.file_ds2.changed?).to be_false
expect(test_obj.file_ds2.new?).to be_true
end

it "should use ds_specs on migrated objects" do
test_obj = HasFile.find(@base.pid)
test_obj.file_ds.versionable.should be_false
test_obj.file_ds.new?.should be_true
expect(test_obj.file_ds.versionable).to be_false
expect(test_obj.file_ds.new?).to be_true
test_obj.file_ds.content = "blah blah blah"
test_obj.save
test_obj.file_ds.versionable.should be_false
expect(test_obj.file_ds.versionable).to be_false
# look it up again to check datastream profile
test_obj = HasFile.find(@base.pid)
test_obj.file_ds.versionable.should be_false
test_obj.file_ds.dsLabel.should == "File Datastream"
expect(test_obj.file_ds.versionable).to be_false
expect(test_obj.file_ds.dsLabel).to eql "File Datastream"
test_obj = HasFile.find(@base2.pid)
test_obj.file_ds.versionable.should be_true
test_obj.file_ds.dsLabel.should == "Pre-existing DS"
expect(test_obj.file_ds.versionable).to be_true
expect(test_obj.file_ds.dsLabel).to eql "Pre-existing DS"
end
end
end

describe "#create_datastream" do
before do
@base = ActiveFedora::Base.new(pid:"test:create_datastream")
@base.save
@ds_location = ActiveFedora.solr_config[:url] + "/admin/file/?file=schema.xml"
open(@ds_location) do |f|
@ds_content = f.read
end
end

after do
@base.delete
end
# external file datastreams require changes to XACML policies, but http URIs should work
it "should create external datastreams" do
# we should be albe to reliably download the solr schema
ds_opts = {mimeType:'text/xml',controlGroup:'E',dsLabel:'Some metadata',
dsLocation:@ds_location}
ds = @base.create_datastream('ActiveFedora::Datastream', 'someMetadata', ds_opts)
@base.add_datastream(ds)
@base.save
expect(@base.datastreams.keys.include?('someMetadata')).to be_true
test_obj = ActiveFedora::Base.find(@base.pid)
expect(test_obj.datastreams['someMetadata'].content).to eql @ds_content
expect(test_obj.datastreams['someMetadata'].controlGroup).to eql 'E'
end

# file:// URIs require changes to XACML policies, but http URIs should work
it "should create managed datastreams from a given URL" do
ds_opts = {mimeType:'text/xml',controlGroup:'M',dsLabel:'Some metadata',
dsLocation:@ds_location}
ds = @base.create_datastream('ActiveFedora::Datastream', 'someMetadata', ds_opts)
@base.add_datastream(ds)
@base.save
expect(@base.datastreams.keys.include?('someMetadata')).to be_true
test_obj = ActiveFedora::Base.find(@base.pid)
expect(test_obj.datastreams['someMetadata'].content).to eql @ds_content
expect(test_obj.datastreams['someMetadata'].controlGroup).to eql 'M'
end
end
end

0 comments on commit d06db81

Please sign in to comment.