spec/toy/persistence_spec.rb in toystore-0.13.0 vs spec/toy/persistence_spec.rb in toystore-0.13.1
- old
+ new
@@ -1,9 +1,9 @@
require 'helper'
describe Toy::Persistence do
- uses_constants('User')
+ uses_constants('User', 'Game')
let(:klass) do
Class.new { include Toy::Store }
end
@@ -40,10 +40,41 @@
klass.adapter :memory, hash
klass.adapter.client.should equal(hash)
end
end
+ describe ".persisted_attributes" do
+ before do
+ @name = User.attribute(:name, String)
+ @password = User.attribute(:password, String, :virtual => true)
+ end
+
+ it "includes attributes that are not virtual" do
+ User.persisted_attributes.should include(@name)
+ end
+
+ it "excludes attributes that are virtual" do
+ User.persisted_attributes.should_not include(@password)
+ end
+
+ it "memoizes after first call" do
+ User.should_receive(:attributes).once.and_return({
+ 'name' => @name,
+ 'password' => @password,
+ })
+ User.persisted_attributes
+ User.persisted_attributes
+ User.persisted_attributes
+ end
+
+ it "is unmemoized when declaring a new attribute" do
+ User.persisted_attributes
+ age = User.attribute :age, Integer
+ User.persisted_attributes.map(&:name).sort.should eq(%w[age name])
+ end
+ end
+
describe ".create" do
before do
User.attribute :name, String
User.attribute :age, Integer
@doc = User.create(:name => 'John', :age => 50)
@@ -316,10 +347,17 @@
it "should remove the instance" do
doc = User.create
doc.delete
User.key?(doc.id).should be_false
end
+
+ it "uses persisted id for adapter delete" do
+ user = User.new
+ user.stub(:persisted_id => 1)
+ user.adapter.should_receive(:delete).with(1)
+ user.delete
+ end
end
describe "#destroy" do
it "should remove the instance" do
doc = User.create
@@ -350,8 +388,68 @@
it "is never destroyed" do
user = User.create
user.clone.should_not be_destroyed
user.destroy
user.clone.should_not be_destroyed
+ end
+ end
+
+ describe "#persisted_id" do
+ it "returns id attribute value converted for storage" do
+ raw_value = 1
+ typecast_for_store_value = '1'
+ user = User.new(:id => raw_value)
+ user.persisted_id.should eq(typecast_for_store_value)
+ end
+ end
+
+ describe "#persisted_attributes" do
+ before do
+ @over = Game.attribute(:over, Boolean)
+ @score = Game.attribute(:creator_score, Integer, :virtual => true)
+ @abbr = Game.attribute(:super_secret_hash, String, :abbr => :ssh)
+ @rewards = Game.attribute(:rewards, Set)
+ @time = Game.attribute(:time, Time)
+ @game = Game.new({
+ :over => true,
+ :creator_score => 20,
+ :rewards => %w(twigs berries).to_set,
+ :ssh => 'h4x',
+ :time => nil,
+ })
+ end
+
+ it "includes persisted attributes" do
+ @game.persisted_attributes.should have_key('over')
+ end
+
+ it "includes abbreviated names for abbreviated attributes" do
+ @game.persisted_attributes.should have_key('ssh')
+ end
+
+ it "does not include full names for abbreviated attributes" do
+ @game.persisted_attributes.should_not have_key('super_secret_hash')
+ end
+
+ it "does not include virtual attributes" do
+ @game.persisted_attributes.should_not have_key(:creator_score)
+ end
+
+ it "includes to_store values for attributes" do
+ @game.persisted_attributes['rewards'].should be_instance_of(Array)
+ @game.persisted_attributes['rewards'].should == @rewards.to_store(@game.rewards)
+ end
+
+ it "does not include nil attributes" do
+ @game.persisted_attributes.should_not have_key('time')
+ end
+ end
+
+ describe "#persist" do
+ it "calls write on adapter with persisted id and attributes" do
+ user = User.new
+ user.stub(persisted_id: 1, persisted_attributes: {one: 'two'})
+ user.adapter.should_receive(:write).with(1, {one: 'two'})
+ user.persist
end
end
end