spec/bag_spec.rb in nano-store-0.5.0 vs spec/bag_spec.rb in nano-store-0.5.1

- old
+ new

@@ -12,81 +12,85 @@ after do NanoStore.shared_store = nil end - it "should add objects to bag" do - bag = Bag.bag + describe "#<<" do + it "should add objects to bag" do + bag = Bag.bag - # use << method to add object to bag - page = Page.new - page.text = "Hello" - page.index = 1 - bag << page - - # use + method to add object to bag - page = Page.new - page.text = "World" - page.index = 2 - bag += page + # use << method to add object to bag + bag << Page.new(:text => "Hello", :index => 1) - bag.unsaved.count.should.be == 2 - bag.changed?.should.be.true - bag.save - - bag.unsaved.count.should.be == 0 - bag.saved.count.should.be == 2 - bag.changed?.should.be.false + bag.unsaved.count.should.be == 1 + bag.changed?.should.be.true + bag.save + + bag.unsaved.count.should.be == 0 + bag.saved.count.should.be == 1 + bag.changed?.should.be.false + end end + + describe "#+" do + it "should add objects to bag" do + bag = Bag.bag - it "should delete object from bag" do - bag = Bag.bag + # use + method to add object to bag + bag += Page.new(:text => "World", :index => 2) - # use << method to add object to bag - page = Page.new - page.text = "Hello" - page.index = 1 - bag << page - - page = Page.new - page.text = "Foo Bar" - page.index = 2 - bag << page - - bag.save - bag.saved.count.should.be == 2 - bag.delete(page) - bag.changed?.should.be.true - bag.removed.count.should.be == 1 - bag.save - bag.saved.count.should.be == 1 + bag.unsaved.count.should.be == 1 + bag.changed?.should.be.true + bag.save + + bag.unsaved.count.should.be == 0 + bag.saved.count.should.be == 1 + bag.changed?.should.be.false + end end - it "should add bag to store" do - before_count = NanoStore.shared_store.bags.count + describe "#delete" do + it "should delete object from bag" do + bag = Bag.bag - bag = Bag.bag + page = Page.new(:text => "Hello", :index => 1) + bag << page + bag << Page.new(:text => "World", :index => 2) + bag.save + bag.saved.count.should.be == 2 - # use << method to add object to bag - page = Page.new - page.text = "Hello" - page.index = 1 - bag << page + bag.delete(page) + bag.changed?.should.be.true + bag.removed.count.should.be == 1 + bag.save + bag.saved.count.should.be == 1 + end + end - bag.save - NanoStore.shared_store.bags.count.should.be == before_count + 1 + describe "#store=" do + it "should store bag in a NanoStore" do + store = NanoStore.store + bag = Bag.bag + bag.store = store + bag << Page.new(:text => "1") + bag.save + store.bags.size.should == 1 + store.bags.first.to_a.first.text.should == "1" + end end describe "#to_a" do it "convert a bag to array" do bag = Bag.bag bag << Page.new(:text => "1", :index => 1) bag << Page.new(:text => "2", :index => 2) bag.to_a.is_a?(Array).should.be.true bag.to_a.size.should == 2 - bag.to_a[0].text.should == "1" - bag.to_a[1].text.should == "2" + + # #to_a is not ordered! + ["1", "2"].include?(bag.to_a[0].text).should.be.true + ["1", "2"].include?(bag.to_a[1].text).should.be.true bag.save bag.to_a.size.should == 2 end end end \ No newline at end of file