PageRenderTime 126ms CodeModel.GetById 0ms RepoModel.GetById 2ms app.codeStats 0ms

/test/functional/test_modifiers.rb

http://github.com/jnunemaker/mongomapper
Ruby | 432 lines | 331 code | 101 blank | 0 comment | 45 complexity | 1888a312c2442598eab523d292ea76c1 MD5 | raw file
  1. require 'test_helper'
  2. class ModifierTest < Test::Unit::TestCase
  3. def setup
  4. @page_class = Doc do
  5. key :title, String
  6. key :day_count, Integer, :default => 0
  7. key :week_count, Integer, :default => 0
  8. key :month_count, Integer, :default => 0
  9. key :tags, Array
  10. end
  11. end
  12. def assert_page_counts(page, day_count, week_count, month_count)
  13. page.reload
  14. page.day_count.should == day_count
  15. page.week_count.should == week_count
  16. page.month_count.should == month_count
  17. end
  18. def assert_keys_removed(page, *keys)
  19. keys.each do |key|
  20. doc = @page_class.collection.find_one({:_id => page.id})
  21. doc.keys.should_not include(key)
  22. end
  23. end
  24. context "ClassMethods" do
  25. context "unset" do
  26. setup do
  27. @page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
  28. @page2 = @page_class.create(:title => 'Home')
  29. end
  30. should "work with criteria and keys" do
  31. @page_class.unset({:title => 'Home'}, :title, :tags)
  32. assert_keys_removed @page, :title, :tags
  33. assert_keys_removed @page2, :title, :tags
  34. end
  35. should "work with ids and keys" do
  36. @page_class.unset(@page.id, @page2.id, :title, :tags)
  37. assert_keys_removed @page, :title, :tags
  38. assert_keys_removed @page2, :title, :tags
  39. end
  40. end
  41. context "increment" do
  42. setup do
  43. @page = @page_class.create(:title => 'Home')
  44. @page2 = @page_class.create(:title => 'Home')
  45. end
  46. should "work with criteria and modifier hashes" do
  47. @page_class.increment({:title => 'Home'}, :day_count => 1, :week_count => 2, :month_count => 3)
  48. assert_page_counts @page, 1, 2, 3
  49. assert_page_counts @page2, 1, 2, 3
  50. end
  51. should "work with ids and modifier hash" do
  52. @page_class.increment(@page.id, @page2.id, :day_count => 1, :week_count => 2, :month_count => 3)
  53. assert_page_counts @page, 1, 2, 3
  54. assert_page_counts @page2, 1, 2, 3
  55. end
  56. end
  57. context "decrement" do
  58. setup do
  59. @page = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
  60. @page2 = @page_class.create(:title => 'Home', :day_count => 1, :week_count => 2, :month_count => 3)
  61. end
  62. should "work with criteria and modifier hashes" do
  63. @page_class.decrement({:title => 'Home'}, :day_count => 1, :week_count => 2, :month_count => 3)
  64. assert_page_counts @page, 0, 0, 0
  65. assert_page_counts @page2, 0, 0, 0
  66. end
  67. should "work with ids and modifier hash" do
  68. @page_class.decrement(@page.id, @page2.id, :day_count => 1, :week_count => 2, :month_count => 3)
  69. assert_page_counts @page, 0, 0, 0
  70. assert_page_counts @page2, 0, 0, 0
  71. end
  72. should "decrement with positive or negative numbers" do
  73. @page_class.decrement(@page.id, @page2.id, :day_count => -1, :week_count => 2, :month_count => -3)
  74. assert_page_counts @page, 0, 0, 0
  75. assert_page_counts @page2, 0, 0, 0
  76. end
  77. end
  78. context "set" do
  79. setup do
  80. @page = @page_class.create(:title => 'Home')
  81. @page2 = @page_class.create(:title => 'Home')
  82. end
  83. should "work with criteria and modifier hashes" do
  84. @page_class.set({:title => 'Home'}, :title => 'Home Revised')
  85. @page.reload
  86. @page.title.should == 'Home Revised'
  87. @page2.reload
  88. @page2.title.should == 'Home Revised'
  89. end
  90. should "work with ids and modifier hash" do
  91. @page_class.set(@page.id, @page2.id, :title => 'Home Revised')
  92. @page.reload
  93. @page.title.should == 'Home Revised'
  94. @page2.reload
  95. @page2.title.should == 'Home Revised'
  96. end
  97. should "typecast values before querying" do
  98. @page_class.key :tags, Set
  99. assert_nothing_raised do
  100. @page_class.set(@page.id, :tags => ['foo', 'bar'].to_set)
  101. @page.reload
  102. @page.tags.should == Set.new(['foo', 'bar'])
  103. end
  104. end
  105. should "not typecast keys that are not defined in document" do
  106. assert_raises(BSON::InvalidDocument) do
  107. @page_class.set(@page.id, :colors => ['red', 'green'].to_set)
  108. end
  109. end
  110. should "set keys that are not defined in document" do
  111. @page_class.set(@page.id, :colors => %w[red green])
  112. @page.reload
  113. @page[:colors].should == %w[red green]
  114. end
  115. end
  116. context "push" do
  117. setup do
  118. @page = @page_class.create(:title => 'Home')
  119. @page2 = @page_class.create(:title => 'Home')
  120. end
  121. should "work with criteria and modifier hashes" do
  122. @page_class.push({:title => 'Home'}, :tags => 'foo')
  123. @page.reload
  124. @page.tags.should == %w(foo)
  125. @page2.reload
  126. @page2.tags.should == %w(foo)
  127. end
  128. should "work with ids and modifier hash" do
  129. @page_class.push(@page.id, @page2.id, :tags => 'foo')
  130. @page.reload
  131. @page.tags.should == %w(foo)
  132. @page2.reload
  133. @page2.tags.should == %w(foo)
  134. end
  135. end
  136. context "push_all" do
  137. setup do
  138. @page = @page_class.create(:title => 'Home')
  139. @page2 = @page_class.create(:title => 'Home')
  140. @tags = %w(foo bar)
  141. end
  142. should "work with criteria and modifier hashes" do
  143. @page_class.push_all({:title => 'Home'}, :tags => @tags)
  144. @page.reload
  145. @page.tags.should == @tags
  146. @page2.reload
  147. @page2.tags.should == @tags
  148. end
  149. should "work with ids and modifier hash" do
  150. @page_class.push_all(@page.id, @page2.id, :tags => @tags)
  151. @page.reload
  152. @page.tags.should == @tags
  153. @page2.reload
  154. @page2.tags.should == @tags
  155. end
  156. end
  157. context "pull" do
  158. setup do
  159. @page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
  160. @page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar))
  161. end
  162. should "work with criteria and modifier hashes" do
  163. @page_class.pull({:title => 'Home'}, :tags => 'foo')
  164. @page.reload
  165. @page.tags.should == %w(bar)
  166. @page2.reload
  167. @page2.tags.should == %w(bar)
  168. end
  169. should "be able to pull with ids and modifier hash" do
  170. @page_class.pull(@page.id, @page2.id, :tags => 'foo')
  171. @page.reload
  172. @page.tags.should == %w(bar)
  173. @page2.reload
  174. @page2.tags.should == %w(bar)
  175. end
  176. end
  177. context "pull_all" do
  178. setup do
  179. @page = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
  180. @page2 = @page_class.create(:title => 'Home', :tags => %w(foo bar baz))
  181. end
  182. should "work with criteria and modifier hashes" do
  183. @page_class.pull_all({:title => 'Home'}, :tags => %w(foo bar))
  184. @page.reload
  185. @page.tags.should == %w(baz)
  186. @page2.reload
  187. @page2.tags.should == %w(baz)
  188. end
  189. should "work with ids and modifier hash" do
  190. @page_class.pull_all(@page.id, @page2.id, :tags => %w(foo bar))
  191. @page.reload
  192. @page.tags.should == %w(baz)
  193. @page2.reload
  194. @page2.tags.should == %w(baz)
  195. end
  196. end
  197. context "add_to_set" do
  198. setup do
  199. @page = @page_class.create(:title => 'Home', :tags => 'foo')
  200. @page2 = @page_class.create(:title => 'Home')
  201. end
  202. should "be able to add to set with criteria and modifier hash" do
  203. @page_class.add_to_set({:title => 'Home'}, :tags => 'foo')
  204. @page.reload
  205. @page.tags.should == %w(foo)
  206. @page2.reload
  207. @page2.tags.should == %w(foo)
  208. end
  209. should "be able to add to set with ids and modifier hash" do
  210. @page_class.add_to_set(@page.id, @page2.id, :tags => 'foo')
  211. @page.reload
  212. @page.tags.should == %w(foo)
  213. @page2.reload
  214. @page2.tags.should == %w(foo)
  215. end
  216. end
  217. context "push_uniq" do
  218. setup do
  219. @page = @page_class.create(:title => 'Home', :tags => 'foo')
  220. @page2 = @page_class.create(:title => 'Home')
  221. end
  222. should "be able to push uniq with criteria and modifier hash" do
  223. @page_class.push_uniq({:title => 'Home'}, :tags => 'foo')
  224. @page.reload
  225. @page.tags.should == %w(foo)
  226. @page2.reload
  227. @page2.tags.should == %w(foo)
  228. end
  229. should "be able to push uniq with ids and modifier hash" do
  230. @page_class.push_uniq(@page.id, @page2.id, :tags => 'foo')
  231. @page.reload
  232. @page.tags.should == %w(foo)
  233. @page2.reload
  234. @page2.tags.should == %w(foo)
  235. end
  236. end
  237. context "pop" do
  238. setup do
  239. @page = @page_class.create(:title => 'Home', :tags => %w(foo bar))
  240. end
  241. should "be able to remove the last element the array" do
  242. @page_class.pop(@page.id, :tags => 1)
  243. @page.reload
  244. @page.tags.should == %w(foo)
  245. end
  246. should "be able to remove the first element of the array" do
  247. @page_class.pop(@page.id, :tags => -1)
  248. @page.reload
  249. @page.tags.should == %w(bar)
  250. end
  251. end
  252. end
  253. context "instance methods" do
  254. should "be able to unset with keys" do
  255. page = @page_class.create(:title => 'Foo', :tags => %w(foo))
  256. page.unset(:title, :tags)
  257. assert_keys_removed page, :title, :tags
  258. end
  259. should "be able to increment with modifier hashes" do
  260. page = @page_class.create
  261. page.increment(:day_count => 1, :week_count => 2, :month_count => 3)
  262. assert_page_counts page, 1, 2, 3
  263. end
  264. should "be able to decrement with modifier hashes" do
  265. page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
  266. page.decrement(:day_count => 1, :week_count => 2, :month_count => 3)
  267. assert_page_counts page, 0, 0, 0
  268. end
  269. should "always decrement when decrement is called whether number is positive or negative" do
  270. page = @page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
  271. page.decrement(:day_count => -1, :week_count => 2, :month_count => -3)
  272. assert_page_counts page, 0, 0, 0
  273. end
  274. should "be able to set with modifier hashes" do
  275. page = @page_class.create(:title => 'Home')
  276. page.set(:title => 'Home Revised')
  277. page.reload
  278. page.title.should == 'Home Revised'
  279. end
  280. should "be able to push with modifier hashes" do
  281. page = @page_class.create
  282. page.push(:tags => 'foo')
  283. page.reload
  284. page.tags.should == %w(foo)
  285. end
  286. should "be able to push_all with modifier hashes" do
  287. page = @page_class.create
  288. page.push_all(:tags => %w(foo bar))
  289. page.reload
  290. page.tags.should == %w(foo bar)
  291. end
  292. should "be able to pull with criteria and modifier hashes" do
  293. page = @page_class.create(:tags => %w(foo bar))
  294. page.pull(:tags => 'foo')
  295. page.reload
  296. page.tags.should == %w(bar)
  297. end
  298. should "be able to pull_all with criteria and modifier hashes" do
  299. page = @page_class.create(:tags => %w(foo bar baz))
  300. page.pull_all(:tags => %w(foo bar))
  301. page.reload
  302. page.tags.should == %w(baz)
  303. end
  304. should "be able to add_to_set with criteria and modifier hash" do
  305. page = @page_class.create(:tags => 'foo')
  306. page2 = @page_class.create
  307. page.add_to_set(:tags => 'foo')
  308. page2.add_to_set(:tags => 'foo')
  309. page.reload
  310. page.tags.should == %w(foo)
  311. page2.reload
  312. page2.tags.should == %w(foo)
  313. end
  314. should "be able to push uniq with criteria and modifier hash" do
  315. page = @page_class.create(:tags => 'foo')
  316. page2 = @page_class.create
  317. page.push_uniq(:tags => 'foo')
  318. page2.push_uniq(:tags => 'foo')
  319. page.reload
  320. page.tags.should == %w(foo)
  321. page2.reload
  322. page2.tags.should == %w(foo)
  323. end
  324. should "be able to pop with modifier hashes" do
  325. page = @page_class.create(:tags => %w(foo bar))
  326. page.pop(:tags => 1)
  327. page.reload
  328. page.tags.should == %w(foo)
  329. end
  330. end
  331. end