require 'test/spec' require 'ferret' require 'pyrosoma/database' context "Pyrosoma::Database" do setup do @db = Pyrosoma::Database.new(:dir => Ferret::Store::RAMDirectory.new) @frame = Pyrosoma::Frame.new({ :id => "frame", :foo => "bar", :master => "@master", }) end specify "should be able to add frames" do @db << Pyrosoma::Frame.new({:id => "quux", :bar => "huh"}) end specify "should notify observers on adding frames" do changed = false obj = Object.new (class << obj; self; end).send(:define_method, :update) { changed = true } @db.add_observer obj changed.should.be false @db << @frame changed.should.be true end specify "should be able to retrieve frames" do @db << @frame @db["frame"].should.be.kind_of Pyrosoma::Frame @db["frame"][:foo].should.equal "bar" end specify "should be able to retrieve all known ids" do @db << @frame @db << Pyrosoma::Frame.new({:id => "quux", :bar => "huh"}) @db.ids.sort.should.equal ["frame", "quux"] end specify "should deal with non-existant frames" do lambda { @db["bad luck"] }.should.raise(IndexError) end specify "should be able to load RFC2822-like frames" do @db.import < "a", :n => "9") @db << Pyrosoma::Frame.new(:id => "b", :n => "8") @db << Pyrosoma::Frame.new(:id => "c", :n => "2") @db << Pyrosoma::Frame.new(:id => "d", :n => "33") @db.first(:n).should.equal @db["c"] @db.first(:n, 3).should.equal [@db["c"], @db["b"], @db["a"]] @db.last(:n).should.equal @db["d"] @db.last(:n, 4).should.equal [@db["d"], @db["a"], @db["b"], @db["c"]] end end