diff --git a/spec/index_writer/add_spec.rb b/spec/index_writer/add_spec.rb new file mode 100644 index 0000000..5179b6b --- /dev/null +++ b/spec/index_writer/add_spec.rb @@ -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 diff --git a/spec/index_writer/count_spec.rb b/spec/index_writer/count_spec.rb new file mode 100644 index 0000000..6f58b20 --- /dev/null +++ b/spec/index_writer/count_spec.rb @@ -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 diff --git a/spec/index_writer/new_spec.rb b/spec/index_writer/new_spec.rb new file mode 100644 index 0000000..0458b64 --- /dev/null +++ b/spec/index_writer/new_spec.rb @@ -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 diff --git a/spec/index_writer/write_spec.rb b/spec/index_writer/write_spec.rb new file mode 100644 index 0000000..dbf151f --- /dev/null +++ b/spec/index_writer/write_spec.rb @@ -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