Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 162 lines (139 sloc) 3.879 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
require 'helper'

describe Toy::IdentityMap do
  uses_constants('User', 'Skill')

  before do
    Toy::IdentityMap.enabled = true
    Toy::IdentityMap.clear
  end

  after do
    Toy::IdentityMap.enabled = false
  end

  it "adds to map on save" do
    user = User.new
    user.save!
    user.should be_in_identity_map
  end

  it "adds to map on load" do
    user = User.load('1', 'id' => '1')
    user.should be_in_identity_map
  end

  it "removes from map on delete" do
    user = User.create
    user.should be_in_identity_map
    user.delete
    user.should_not be_in_identity_map
  end

  it "removes from map on destroy" do
    user = User.create
    user.should be_in_identity_map
    user.destroy
    user.should_not be_in_identity_map
  end

  describe ".get" do
    it "adds to map if not in map" do
      user = User.create
      Toy::IdentityMap.clear
      user.should_not be_in_identity_map
      user = User.get(user.id)
      user.should be_in_identity_map
    end

    it "returns from map if in map" do
      user = User.create
      user.should be_in_identity_map
      User.get(user.id).should equal(user)
    end

    it "does not query if in map" do
      user = User.create
      user.should be_in_identity_map
      user.adapter.should_not_receive(:read)
      User.get(user.id).should equal(user)
    end

    it "returns nil if not found in map or adapter" do
      User.get(1).should be_nil
    end
  end

  describe "#reload" do
    it "forces new query each time and skips the identity map" do
      user = User.create
      user.should be_in_identity_map
      User.adapter.should_receive(:read).with(user.id).and_return({})
      user.reload
    end
  end

  describe "identity map off" do
    it "does not add to map on save" do
      Toy::IdentityMap.enabled = false
      user = User.new
      user.save!
      user.should_not be_in_identity_map
    end

    it "does not add to map on load" do
      Toy::IdentityMap.enabled = false
      user = User.load('1', 'name' => 'John')
      user.should_not be_in_identity_map
    end

    it "does not remove from map on delete" do
      user = User.create
      user.should be_in_identity_map
      Toy::IdentityMap.enabled = false
      user.delete
      user.should be_in_identity_map
    end

    it "does not remove from map on destroy" do
      user = User.create
      user.should be_in_identity_map
      Toy::IdentityMap.enabled = false
      user.destroy
      user.should be_in_identity_map
    end

    describe ".get" do
      it "does not add to map if not in map" do
        Toy::IdentityMap.enabled = false
        user = User.create
        user.should_not be_in_identity_map
        user = User.get(user.id)
        user.should_not be_in_identity_map
      end

      it "does not load from map if in map" do
        user = User.create
        user.should be_in_identity_map
        Toy::IdentityMap.enabled = false
        user.adapter.should_receive(:read).with(user.id, nil).and_return(user.persisted_attributes)
        User.get(user.id)
      end
    end
  end

  describe ".without" do
    before do
      @user = User.create
      Toy::IdentityMap.clear
    end
    let(:user) { @user }

    it "skips the map" do
      Toy::IdentityMap.without do
        User.get(user.id).should_not equal(user.id)
      end
    end
  end

  describe ".use" do
    before do
      @user = User.create
      Toy::IdentityMap.clear
    end
    let(:user) { @user }

    it "uses the map" do
      Toy::IdentityMap.enabled = false
      Toy::IdentityMap.use do
        User.get(user.id).should equal(User.get(user.id))
      end
    end

    it "clears the map" do
      Toy::IdentityMap.enabled = false
      Toy::IdentityMap.repository['hello'] = 'world'
      Toy::IdentityMap.use do
        User.get(user.id)
      end
      Toy::IdentityMap.repository.should be_empty
    end
  end
end
Something went wrong with that request. Please try again.