0
0
Fork 0

specs for the index writer, which is responsible for writing the

documents into the index dir
This commit is contained in:
Gibheer 2011-05-26 21:18:46 +02:00
parent b9caf75d53
commit bd8230c4fc
4 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,23 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexWriter#add" do
before :all do
@doc1 = Spec::FooDocument.new :id => 1
@doc2 = Spec::FooDocument.new :id => 2
end
before do
@path = prepare_index_dir
end
it "adds the object to the list of objects" do
w = Polecat::IndexWriter.new @path
w.add @doc1
w.count.should == 1
end
it "raises an error, when the object is not a document" do
lambda { Polecat::IndexWriter.new(@path).add "foo" }.should(
raise_error(ArgumentError))
end
end

View File

@ -0,0 +1,18 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexWriter#count" do
before :all do
@path = prepare_index_dir
end
it "returns 0 on an empty writer" do
w = Polecat::IndexWriter.new @path
w.count.should == 0
end
it "returns the number of documents stored in the storage" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.count.should == 1
end
end

View File

@ -0,0 +1,27 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexWriter#new" do
before :all do
@path = prepare_index_dir
end
it "takes a path as an argument" do
writer = Polecat::IndexWriter.new @path
writer.path.should == @path
end
it "raises an Argument when no path is given" do
lambda { Polecat::IndexWriter.new }.should raise_error(ArgumentError)
end
it "raises an error, when the path is not directory" do
lambda {
Polecat::IndexWriter.new "/dev/null"
}.should raise_error(ArgumentError)
end
it "raises an error when a index.lock file is in the directory" do
FileUtils.touch @path + '/index.lock'
lambda { Polecat::IndexWriter.new @path }.should raise_error(IOError)
end
end

View File

@ -0,0 +1,42 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "IndexWriter#write" do
before do
@path = prepare_index_dir
end
it "sets count to 0" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.write
w.count.should == 0
end
it "returns true when the write was a success" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.write.should == true
end
it "removes the lock after a write" do
w = Polecat::IndexWriter.new @path
w.add Spec::FooDocument.new
w.write
File.exists?(@path + '/index.lock').should == false
end
it "writes a marshalled representation of the document list" do
w = Polecat::IndexWriter.new @path
doc = Spec::FooDocument.new
w.add doc
w.write
File.read(@path + '/ind0.ind').should == Marshal.dump([doc])
end
it "returns false when the directory has an 'index.lock' file" do
w = Polecat::IndexWriter.new @path
FileUtils.touch @path + '/index.lock'
w.add Spec::FooDocument.new
w.write.should == false
end
end