Guest User

Untitled

a guest
Apr 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. require 'pathname'
  2. require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
  3.  
  4. describe "DataMapper::IdentityMap" do
  5. before(:all) do
  6. class Cow
  7. include DataMapper::Resource
  8. property :id, Fixnum, :key => true
  9. property :name, String
  10. end
  11.  
  12. class Chicken
  13. include DataMapper::Resource
  14. property :name, String
  15. end
  16.  
  17. class Pig
  18. include DataMapper::Resource
  19. property :id, Fixnum, :key => true
  20. property :composite, Fixnum, :key => true
  21. property :name, String
  22. end
  23. end
  24.  
  25. it "should use a second level cache if created with on"
  26.  
  27. it "should return nil on #get when it does not find the requested instance" do
  28. map = DataMapper::IdentityMap.new
  29. map.get(Cow,[23]).nil?.should == true
  30. end
  31.  
  32. it "should return an instance on #get when it finds the requested instance" do
  33. betsy = Cow.new({:id=>23,:name=>'Betsy'})
  34. map = DataMapper::IdentityMap.new
  35. map.set(betsy)
  36. map.get(Cow,[23]).should == betsy
  37. end
  38.  
  39. it "should store an instance on #set" do
  40. betsy = Cow.new({:id=>23,:name=>'Betsy'})
  41. map = DataMapper::IdentityMap.new
  42. map.set(betsy)
  43. map.get(Cow,[23]).should == betsy
  44. end
  45.  
  46. it "should raise ArgumentError on #set if there is no key property" do
  47. cluck = Chicken.new({:name => 'Cluck'})
  48. map = DataMapper::IdentityMap.new
  49. lambda{map.set(cluck)}.should raise_error(ArgumentError)
  50. end
  51.  
  52. it "should raise ArgumentError on #set if the key property is nil" do
  53. betsy = Cow.new({:name=>'Betsy'})
  54. map = DataMapper::IdentityMap.new
  55. lambda{ map.set(betsy)}.should raise_error(ArgumentError)
  56. end
  57.  
  58. it "should store instances with composite keys on #set" do
  59. pig = Pig.new({:id=>1,:composite=>1,:name=> 'Pig'})
  60. piggy = Pig.new({:id=>1,:composite=>2,:name=>'Piggy'})
  61.  
  62. map = DataMapper::IdentityMap.new
  63. map.set(pig)
  64. map.set(piggy)
  65.  
  66. map.get(Pig,[1,1]).should == pig
  67. map.get(Pig,[1,2]).should == piggy
  68. end
  69.  
  70. it "should remove an instance on #delete" do
  71. betsy = Cow.new({:id=>23,:name=>'Betsy'})
  72. map = DataMapper::IdentityMap.new
  73. map.set(betsy)
  74. map.delete(Cow,[23])
  75. map.get(Cow,[23]).nil?.should == true
  76. end
  77.  
  78. it "should remove all instances of a given class on #clear!" do
  79. betsy = Cow.new({:id=>23,:name=>'Betsy'})
  80. bert = Cow.new({:id=>24,:name=>'Bert'})
  81. piggy = Pig.new({:id=>1,:composite=>2,:name=>'Piggy'})
  82.  
  83. map = DataMapper::IdentityMap.new
  84. map.set(betsy)
  85. map.set(bert)
  86. map.set(piggy)
  87. map.clear!(Cow)
  88. map.get(Cow,[23]).nil?.should == true
  89. map.get(Cow,[24]).nil?.should == true
  90. map.get(Pig,[1,2]).should == piggy
  91. end
  92.  
  93.  
  94.  
  95. end
  96.  
  97. describe "Second Level Caching" do
  98.  
  99. it "should expose a standard API" do
  100.  
  101. cache = Class.new do
  102.  
  103. # Retrieve an instance by it's type and key.
  104. #
  105. # +klass+ is the type you want to retrieve. Should
  106. # map to a mapped class. ie: If you have Salesperson < Person, then
  107. # you should be able to pass in Salesperson, but it should map the
  108. # lookup to it's set os Person instances.
  109. #
  110. # +type+ is an order-specific Array of key-values to identify the object.
  111. # It's always an Array, even when not using a composite-key. ie:
  112. # property :id, Fixnum, :serial => true # => [12]
  113. # Or:
  114. # property :order_id, Fixnum
  115. # property :line_item_number, Fixnum
  116. # # key => [5, 27]
  117. #
  118. # +return+ nil when a matching instance isn't found,
  119. # or the matching instance when present.
  120. def get(type, key); nil end
  121.  
  122. # Store an instance in the map.
  123. #
  124. # +instance+ is the instance you want to store. The cache should
  125. # identify the type-store by instance.class in a naive implementation,
  126. # or if inheritance is allowed, instance.resource_class (not yet implemented).
  127. # The instance must also respond to #key, to return it's key. If key returns nil
  128. # or an empty Array, then #set should raise an error.
  129. def set(instance); instance end
  130.  
  131. # Clear should flush the entire cache.
  132. #
  133. # +type+ if an optional type argument is passed, then
  134. # only the storage for that type should be cleared.
  135. def clear!(type = nil); nil end
  136.  
  137. # Allows you to remove a specific instance from the cache.
  138. #
  139. # +instance+ should respond to the same +resource_class+ and +key+ methods #set does.
  140. def delete(instance); nil end
  141. end.new
  142.  
  143. cache.should respond_to(:get)
  144. cache.should respond_to(:set)
  145. cache.should respond_to(:clear!)
  146. cache.should respond_to(:delete)
  147.  
  148. end
  149.  
  150. end
Add Comment
Please, Sign In to add comment