spec/toy/attributes_spec.rb in toystore-0.8.3 vs spec/toy/attributes_spec.rb in toystore-0.9.0

- old
+ new

@@ -1,15 +1,10 @@ require 'helper' describe Toy::Attributes do - uses_constants('User', 'Game', 'Move', 'Tile') + uses_objects('User', 'Game') - before do - Game.embedded_list(:moves) - Move.embedded_list(:tiles) - end - describe "including" do it "adds id attribute" do User.attributes.keys.should == ['id'] end end @@ -20,21 +15,19 @@ end end describe "#persisted_attributes" do before do - Game.embedded_list(:moves) @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) @game = Game.new({ :over => true, :creator_score => 20, :rewards => %w(twigs berries).to_set, :ssh => 'h4x', - :moves => [Move.new, Move.new], }) end it "includes persisted attributes" do @game.persisted_attributes.should have_key('over') @@ -46,18 +39,10 @@ it "does not include full names for abbreviated attributes" do @game.persisted_attributes.should_not have_key('super_secret_hash') end - it "includes embedded" do - @game.persisted_attributes.should have_key('moves') - end - - it "includes ids of embedded" do - @game.persisted_attributes['moves'][0].should have_key('id') - 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 @@ -128,54 +113,12 @@ end it "does not fail with nil" do User.new(nil).should be_instance_of(User) end - - it "does guard attributes=" do - attrs = {'age' => 21} - user = User.allocate - user.should_receive(:attributes=).with(attrs, true) - user.send(:initialize, attrs) - end end - describe "#initialize_from_database" do - before do - User.attribute(:age, Integer, :default => 20) - @user = User.allocate - end - - it "sets new record to false" do - @user.initialize_from_database - @user.should_not be_new_record - end - - it "sets attributes" do - @user.initialize_from_database('age' => 21) - end - - it "sets defaults" do - @user.initialize_from_database - @user.age.should == 20 - end - - it "does not fail with nil" do - @user.initialize_from_database(nil).should == @user - end - - it "returns self" do - @user.initialize_from_database.should == @user - end - - it "does not guard attributes=" do - attrs = {'age' => 21} - @user.should_receive(:attributes=).with(attrs, false) - @user.initialize_from_database(attrs) - end - end - describe "#attributes" do it "defaults to hash with id" do attrs = Model().new.attributes attrs.keys.should == ['id'] end @@ -187,15 +130,10 @@ user.attributes.should == { 'id' => user.id, 'active' => true, } end - - it "does not include embedded documents" do - game = Game.new(:moves => [Move.new(:tiles => [Tile.new])]) - game.attributes.should_not have_key('moves') - end end describe "#attributes=" do it "sets attributes if present" do User.attribute :age, Integer @@ -319,99 +257,9 @@ it "aliases writing to abbreviation" do user = User.new user.tat = '1234' user.twitter_access_token.should == '1234' - end - - it "persists to store using abbreviation" do - user = User.create(:twitter_access_token => '1234') - raw = user.store.read(user.id) - raw['tat'].should == '1234' - raw.should_not have_key('twitter_access_token') - end - - it "loads from store correctly" do - user = User.create(:twitter_access_token => '1234') - user = User.get(user.id) - user.twitter_access_token.should == '1234' - user.tat.should == '1234' - end - end - - describe "#reload" do - before do - User.attribute(:name, String) - @user = User.create(:name => 'John') - end - let(:user) { @user } - - it "reloads id from database" do - id = user.id - user.reload - user.id.should == id - end - - it "reloads record from the database" do - user.name = 'Steve' - user.reload - user.name.should == 'John' - end - - it "is still persisted" do - user.should be_persisted - user.reload - user.should be_persisted - end - - it "returns the record" do - user.name = 'Steve' - user.reload.should equal(user) - end - - it "resets instance variables" do - user.instance_variable_set("@foo", true) - user.reload - user.instance_variable_get("@foo").should be_nil - end - - it "resets lists" do - User.list(:games) - game = Game.create - user.update_attributes(:games => [game]) - user.games = [] - user.games.should == [] - user.reload - user.games.should == [game] - end - - it "resets references" do - Game.reference(:user) - game = Game.create(:user => user) - game.user = nil - game.user.should be_nil - game.reload - game.user.should == user - end - - it "raises NotFound if does not exist" do - user.destroy - lambda { user.reload }.should raise_error(Toy::NotFound) - end - - it "reloads defaults" do - User.attribute(:skills, Array) - @user.reload - @user.skills.should == [] - end - - it "reloads attributes protected from mass assignment" do - User.attribute(:admin, Boolean) - User.attr_accessible(:name) - user = User.new(:name => 'John') - user.admin = true - user.save - user.reload.admin.should be_true end end describe "Initialization of array attributes" do before do \ No newline at end of file