require "spec_helper"
describe Model do
it "should be created with default options" do
lambda { Model.new }.should_not raise_exception
end
describe "statements" do
before :each do
@model = Model.new
end
it do
@model.statements.should be_an_instance_of(ModelProxy)
end
it "should be created in the model" do
lambda {
@model.statements.create(statement_attributes)
}.should change(@model.statements, :size).by(1)
end
it "should be iterated over" do
statement = @model.statements.create(statement_attributes)
statements = []
lambda {
@model.statements.each do |s|
statements << s
end
}.should change(statements, :size).by(1)
statements.first.should eql(statement)
end
it "should be added to the model" do
statement = Statement.new(statement_attributes)
lambda {
@model.statements.add(statement)
}.should change(@model.statements, :size).by(1)
@model.statements.should include(statement)
end
it "should be found without a block" do
statement = @model.statements.create(statement_attributes)
@model.statements.find(:first).should eql(statement)
@model.statements.first.should eql(statement)
@model.statements.all.should eql([statement])
end
it "should be found with a block" do
statements = []
statement = @model.statements.create(statement_attributes)
lambda {
@model.statements.each do |st|
statements << st
end
}.should change(statements, :size).by(1)
statements.first.should eql(statement)
end
it "should be found by an attribute" do
statements = []
statement = @model.statements.create(statement_attributes)
lambda {
@model.statements.find(:all, :object => statement.object).each do |st|
statements << st
end
}.should change(statements, :size).by(1)
statements.first.should eql(statement)
end
it "should not be found with a block" do
statements = []
statement = @model.statements.create(statement_attributes)
lambda {
@model.statements.find(:all, :object => "another object").each do |st|
statements << st
end
}.should_not change(statements, :size)
end
it "should be removed from the model" do
statement = @model.statements.create(statement_attributes)
lambda {
@model.statements.delete(statement)
}.should change(@model.statements, :size).by(-1)
@model.statements.should_not include(statement)
end
private
def statement_attributes
s = URI.parse('http://example.com/concepts#subject')
p = URI.parse('http://example.com/concepts#label')
o = "subject!"
{
:subject => s,
:predicate => p,
:object => o
}
end
end
describe "serialization" do
before :each do
@model = Model.new
s = URI.parse("http://example.com/concepts#two-dimensional_seismic_imaging")
p = URI.parse("http://www.w3.org/2000/01/rdf-schema#label")
o = "2-D seismic imaging@en"
@model.statements.create(:subject => s, :predicate => p, :object => o)
end
it "should produce RDF/XML content" do
content = @model.to_rdfxml
content.should be_an_instance_of(String)
content.should include('2-D seismic imaging')
end
it "should produce N-Triples content" do
content = @model.to_ntriples
content.should be_an_instance_of(String)
content.should include('2-D seismic imaging@en')
end
it "should produce Turtle content" do
content = @model.to_turtle
content.should be_an_instance_of(String)
content.should include('2-D seismic imaging@en')
end
it "should produce JSON content" do
content = @model.to_json
content.should be_an_instance_of(String)
content.should include('2-D seismic imaging@en')
end
it "should produce DOT content" do
content = @model.to_dot
content.should be_an_instance_of(String)
content.should include('2-D seismic imaging@en')
end
describe "file source" do
before :each do
content = '2-D seismic imaging'
reference_model = Model.new
reference_model.from_rdfxml(content, :base_uri => 'http://example.com/concepts')
reference_model.to_file(filename)
end
after :each do
cleanup
end
it "should be loaded from a file" do
lambda {
@model.from_file(filename, :base_uri => 'http://example.com/concepts')
}.should change(@model.statements, :size).by(1)
end
end
describe "file destination" do
before :each do
cleanup
end
after :each do
cleanup
end
it "should produce a file" do
@model.to_file(filename)
File.should be_exists(filename)
File.size(filename).should_not be_zero
end
end
private
def cleanup
File.delete(filename) if File.exists?(filename)
end
def filename
"test_model.rdf"
end
end
describe "deserialization" do
before :each do
@model = Model.new
end
it "should be successful for RDF/XML data" do
content = '2-D seismic imaging'
lambda {
@model.from_rdfxml(content, :base_uri => 'http://example.com/concepts')
}.should change(@model.statements, :size).by(1)
end
it "should be successful for N-Triples data" do
content = ' .'
lambda {
@model.from_ntriples(content)
}.should change(@model.statements, :size).by(1)
end
it "should be successful for Turtle data" do
content = '# this is a complete turtle document
@prefix foo: .
@prefix : .
foo:bar foo: : .'
lambda {
@model.from_turtle(content, :base_uri => 'http://example.com/concepts')
}.should change(@model.statements, :size).by(1)
end
end
end