PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/activerecord/test/mixin_test.rb

http://monkeycharger.googlecode.com/
Ruby | 484 lines | 353 code | 130 blank | 1 comment | 1 complexity | 66d3e484d024a05c876c621a7a44ff24 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'active_record/acts/tree'
  3. require 'active_record/acts/list'
  4. require 'active_record/acts/nested_set'
  5. require 'fixtures/mixin'
  6. # Let us control what Time.now returns for the TouchTest suite
  7. class Time
  8. @@forced_now_time = nil
  9. cattr_accessor :forced_now_time
  10. class << self
  11. def now_with_forcing
  12. if @@forced_now_time
  13. @@forced_now_time
  14. else
  15. now_without_forcing
  16. end
  17. end
  18. alias_method_chain :now, :forcing
  19. end
  20. end
  21. class ListTest < Test::Unit::TestCase
  22. fixtures :mixins
  23. def test_reordering
  24. assert_equal mixins(:list_1, :list_2, :list_3, :list_4),
  25. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  26. mixins(:list_2).move_lower
  27. assert_equal mixins(:list_1, :list_3, :list_2, :list_4),
  28. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  29. mixins(:list_2).move_higher
  30. assert_equal mixins(:list_1, :list_2, :list_3, :list_4),
  31. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  32. mixins(:list_1).move_to_bottom
  33. assert_equal mixins(:list_2, :list_3, :list_4, :list_1),
  34. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  35. mixins(:list_1).move_to_top
  36. assert_equal mixins(:list_1, :list_2, :list_3, :list_4),
  37. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  38. mixins(:list_2).move_to_bottom
  39. assert_equal mixins(:list_1, :list_3, :list_4, :list_2),
  40. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  41. mixins(:list_4).move_to_top
  42. assert_equal mixins(:list_4, :list_1, :list_3, :list_2),
  43. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  44. end
  45. def test_move_to_bottom_with_next_to_last_item
  46. assert_equal mixins(:list_1, :list_2, :list_3, :list_4),
  47. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  48. mixins(:list_3).move_to_bottom
  49. assert_equal mixins(:list_1, :list_2, :list_4, :list_3),
  50. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  51. end
  52. def test_next_prev
  53. assert_equal mixins(:list_2), mixins(:list_1).lower_item
  54. assert_nil mixins(:list_1).higher_item
  55. assert_equal mixins(:list_3), mixins(:list_4).higher_item
  56. assert_nil mixins(:list_4).lower_item
  57. end
  58. def test_injection
  59. item = ListMixin.new("parent_id"=>1)
  60. assert_equal "parent_id = 1", item.scope_condition
  61. assert_equal "pos", item.position_column
  62. end
  63. def test_insert
  64. new = ListMixin.create("parent_id"=>20)
  65. assert_equal 1, new.pos
  66. assert new.first?
  67. assert new.last?
  68. new = ListMixin.create("parent_id"=>20)
  69. assert_equal 2, new.pos
  70. assert !new.first?
  71. assert new.last?
  72. new = ListMixin.create("parent_id"=>20)
  73. assert_equal 3, new.pos
  74. assert !new.first?
  75. assert new.last?
  76. new = ListMixin.create("parent_id"=>0)
  77. assert_equal 1, new.pos
  78. assert new.first?
  79. assert new.last?
  80. end
  81. def test_insert_at
  82. new = ListMixin.create("parent_id" => 20)
  83. assert_equal 1, new.pos
  84. new = ListMixin.create("parent_id" => 20)
  85. assert_equal 2, new.pos
  86. new = ListMixin.create("parent_id" => 20)
  87. assert_equal 3, new.pos
  88. new4 = ListMixin.create("parent_id" => 20)
  89. assert_equal 4, new4.pos
  90. new4.insert_at(3)
  91. assert_equal 3, new4.pos
  92. new.reload
  93. assert_equal 4, new.pos
  94. new.insert_at(2)
  95. assert_equal 2, new.pos
  96. new4.reload
  97. assert_equal 4, new4.pos
  98. new5 = ListMixin.create("parent_id" => 20)
  99. assert_equal 5, new5.pos
  100. new5.insert_at(1)
  101. assert_equal 1, new5.pos
  102. new4.reload
  103. assert_equal 5, new4.pos
  104. end
  105. def test_delete_middle
  106. assert_equal mixins(:list_1, :list_2, :list_3, :list_4),
  107. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  108. mixins(:list_2).destroy
  109. assert_equal mixins(:list_1, :list_3, :list_4, :reload),
  110. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  111. assert_equal 1, mixins(:list_1).pos
  112. assert_equal 2, mixins(:list_3).pos
  113. assert_equal 3, mixins(:list_4).pos
  114. mixins(:list_1).destroy
  115. assert_equal mixins(:list_3, :list_4, :reload),
  116. ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos')
  117. assert_equal 1, mixins(:list_3).pos
  118. assert_equal 2, mixins(:list_4).pos
  119. end
  120. def test_with_string_based_scope
  121. new = ListWithStringScopeMixin.create("parent_id"=>500)
  122. assert_equal 1, new.pos
  123. assert new.first?
  124. assert new.last?
  125. end
  126. def test_nil_scope
  127. new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
  128. new2.move_higher
  129. assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
  130. end
  131. end
  132. class TreeTest < Test::Unit::TestCase
  133. fixtures :mixins
  134. def test_has_child
  135. assert_deprecated 'has_children?' do
  136. assert_equal true, mixins(:tree_1).has_children?
  137. assert_equal true, mixins(:tree_2).has_children?
  138. assert_equal false, mixins(:tree_3).has_children?
  139. assert_equal false, mixins(:tree_4).has_children?
  140. end
  141. end
  142. def test_children
  143. assert_equal mixins(:tree_1).children, mixins(:tree_2, :tree_4)
  144. assert_equal mixins(:tree_2).children, [mixins(:tree_3)]
  145. assert_equal mixins(:tree_3).children, []
  146. assert_equal mixins(:tree_4).children, []
  147. end
  148. def test_has_parent
  149. assert_deprecated 'has_parent?' do
  150. assert_equal false, mixins(:tree_1).has_parent?
  151. assert_equal true, mixins(:tree_2).has_parent?
  152. assert_equal true, mixins(:tree_3).has_parent?
  153. assert_equal true, mixins(:tree_4).has_parent?
  154. end
  155. end
  156. def test_parent
  157. assert_equal mixins(:tree_2).parent, mixins(:tree_1)
  158. assert_equal mixins(:tree_2).parent, mixins(:tree_4).parent
  159. assert_nil mixins(:tree_1).parent
  160. end
  161. def test_delete
  162. assert_equal 6, TreeMixin.count
  163. mixins(:tree_1).destroy
  164. assert_equal 2, TreeMixin.count
  165. mixins(:tree2_1).destroy
  166. mixins(:tree3_1).destroy
  167. assert_equal 0, TreeMixin.count
  168. end
  169. def test_insert
  170. @extra = mixins(:tree_1).children.create
  171. assert @extra
  172. assert_equal @extra.parent, mixins(:tree_1)
  173. assert_equal 3, mixins(:tree_1).children.size
  174. assert mixins(:tree_1).children.include?(@extra)
  175. assert mixins(:tree_1).children.include?(mixins(:tree_2))
  176. assert mixins(:tree_1).children.include?(mixins(:tree_4))
  177. end
  178. def test_ancestors
  179. assert_equal [], mixins(:tree_1).ancestors
  180. assert_equal [mixins(:tree_1)], mixins(:tree_2).ancestors
  181. assert_equal mixins(:tree_2, :tree_1), mixins(:tree_3).ancestors
  182. assert_equal [mixins(:tree_1)], mixins(:tree_4).ancestors
  183. assert_equal [], mixins(:tree2_1).ancestors
  184. assert_equal [], mixins(:tree3_1).ancestors
  185. end
  186. def test_root
  187. assert_equal mixins(:tree_1), TreeMixin.root
  188. assert_equal mixins(:tree_1), mixins(:tree_1).root
  189. assert_equal mixins(:tree_1), mixins(:tree_2).root
  190. assert_equal mixins(:tree_1), mixins(:tree_3).root
  191. assert_equal mixins(:tree_1), mixins(:tree_4).root
  192. assert_equal mixins(:tree2_1), mixins(:tree2_1).root
  193. assert_equal mixins(:tree3_1), mixins(:tree3_1).root
  194. end
  195. def test_roots
  196. assert_equal mixins(:tree_1, :tree2_1, :tree3_1), TreeMixin.roots
  197. end
  198. def test_siblings
  199. assert_equal mixins(:tree2_1, :tree3_1), mixins(:tree_1).siblings
  200. assert_equal [mixins(:tree_4)], mixins(:tree_2).siblings
  201. assert_equal [], mixins(:tree_3).siblings
  202. assert_equal [mixins(:tree_2)], mixins(:tree_4).siblings
  203. assert_equal mixins(:tree_1, :tree3_1), mixins(:tree2_1).siblings
  204. assert_equal mixins(:tree_1, :tree2_1), mixins(:tree3_1).siblings
  205. end
  206. def test_self_and_siblings
  207. assert_equal mixins(:tree_1, :tree2_1, :tree3_1), mixins(:tree_1).self_and_siblings
  208. assert_equal mixins(:tree_2, :tree_4), mixins(:tree_2).self_and_siblings
  209. assert_equal [mixins(:tree_3)], mixins(:tree_3).self_and_siblings
  210. assert_equal mixins(:tree_2, :tree_4), mixins(:tree_4).self_and_siblings
  211. assert_equal mixins(:tree_1, :tree2_1, :tree3_1), mixins(:tree2_1).self_and_siblings
  212. assert_equal mixins(:tree_1, :tree2_1, :tree3_1), mixins(:tree3_1).self_and_siblings
  213. end
  214. end
  215. class TreeTestWithoutOrder < Test::Unit::TestCase
  216. fixtures :mixins
  217. def test_root
  218. assert mixins(:tree_without_order_1, :tree_without_order_2).include?(TreeMixinWithoutOrder.root)
  219. end
  220. def test_roots
  221. assert_equal [], mixins(:tree_without_order_1, :tree_without_order_2) - TreeMixinWithoutOrder.roots
  222. end
  223. end
  224. class TouchTest < Test::Unit::TestCase
  225. fixtures :mixins
  226. def setup
  227. Time.forced_now_time = Time.now
  228. end
  229. def teardown
  230. Time.forced_now_time = nil
  231. end
  232. def test_time_mocking
  233. five_minutes_ago = 5.minutes.ago
  234. Time.forced_now_time = five_minutes_ago
  235. assert_equal five_minutes_ago, Time.now
  236. Time.forced_now_time = nil
  237. assert_not_equal five_minutes_ago, Time.now
  238. end
  239. def test_update
  240. stamped = Mixin.new
  241. assert_nil stamped.updated_at
  242. assert_nil stamped.created_at
  243. stamped.save
  244. assert_equal Time.now, stamped.updated_at
  245. assert_equal Time.now, stamped.created_at
  246. end
  247. def test_create
  248. obj = Mixin.create
  249. assert_equal Time.now, obj.updated_at
  250. assert_equal Time.now, obj.created_at
  251. end
  252. def test_many_updates
  253. stamped = Mixin.new
  254. assert_nil stamped.updated_at
  255. assert_nil stamped.created_at
  256. stamped.save
  257. assert_equal Time.now, stamped.created_at
  258. assert_equal Time.now, stamped.updated_at
  259. old_updated_at = stamped.updated_at
  260. Time.forced_now_time = 5.minutes.from_now
  261. stamped.save
  262. assert_equal Time.now, stamped.updated_at
  263. assert_equal old_updated_at, stamped.created_at
  264. end
  265. def test_create_turned_off
  266. Mixin.record_timestamps = false
  267. assert_nil mixins(:tree_1).updated_at
  268. mixins(:tree_1).save
  269. assert_nil mixins(:tree_1).updated_at
  270. Mixin.record_timestamps = true
  271. end
  272. end
  273. class ListSubTest < Test::Unit::TestCase
  274. fixtures :mixins
  275. def test_reordering
  276. assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4),
  277. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  278. mixins(:list_sub_2).move_lower
  279. assert_equal mixins(:list_sub_1, :list_sub_3, :list_sub_2, :list_sub_4),
  280. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  281. mixins(:list_sub_2).move_higher
  282. assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4),
  283. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  284. mixins(:list_sub_1).move_to_bottom
  285. assert_equal mixins(:list_sub_2, :list_sub_3, :list_sub_4, :list_sub_1),
  286. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  287. mixins(:list_sub_1).move_to_top
  288. assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4),
  289. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  290. mixins(:list_sub_2).move_to_bottom
  291. assert_equal mixins(:list_sub_1, :list_sub_3, :list_sub_4, :list_sub_2),
  292. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  293. mixins(:list_sub_4).move_to_top
  294. assert_equal mixins(:list_sub_4, :list_sub_1, :list_sub_3, :list_sub_2),
  295. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  296. end
  297. def test_move_to_bottom_with_next_to_last_item
  298. assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4),
  299. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  300. mixins(:list_sub_3).move_to_bottom
  301. assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_4, :list_sub_3),
  302. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  303. end
  304. def test_next_prev
  305. assert_equal mixins(:list_sub_2), mixins(:list_sub_1).lower_item
  306. assert_nil mixins(:list_sub_1).higher_item
  307. assert_equal mixins(:list_sub_3), mixins(:list_sub_4).higher_item
  308. assert_nil mixins(:list_sub_4).lower_item
  309. end
  310. def test_injection
  311. item = ListMixin.new("parent_id"=>1)
  312. assert_equal "parent_id = 1", item.scope_condition
  313. assert_equal "pos", item.position_column
  314. end
  315. def test_insert_at
  316. new = ListMixin.create("parent_id" => 20)
  317. assert_equal 1, new.pos
  318. new = ListMixinSub1.create("parent_id" => 20)
  319. assert_equal 2, new.pos
  320. new = ListMixinSub2.create("parent_id" => 20)
  321. assert_equal 3, new.pos
  322. new4 = ListMixin.create("parent_id" => 20)
  323. assert_equal 4, new4.pos
  324. new4.insert_at(3)
  325. assert_equal 3, new4.pos
  326. new.reload
  327. assert_equal 4, new.pos
  328. new.insert_at(2)
  329. assert_equal 2, new.pos
  330. new4.reload
  331. assert_equal 4, new4.pos
  332. new5 = ListMixinSub1.create("parent_id" => 20)
  333. assert_equal 5, new5.pos
  334. new5.insert_at(1)
  335. assert_equal 1, new5.pos
  336. new4.reload
  337. assert_equal 5, new4.pos
  338. end
  339. def test_delete_middle
  340. assert_equal mixins(:list_sub_1, :list_sub_2, :list_sub_3, :list_sub_4),
  341. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  342. mixins(:list_sub_2).destroy
  343. assert_equal mixins(:list_sub_1, :list_sub_3, :list_sub_4, :reload),
  344. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  345. assert_equal 1, mixins(:list_sub_1).pos
  346. assert_equal 2, mixins(:list_sub_3).pos
  347. assert_equal 3, mixins(:list_sub_4).pos
  348. mixins(:list_sub_1).destroy
  349. assert_equal mixins(:list_sub_3, :list_sub_4, :reload),
  350. ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos')
  351. assert_equal 1, mixins(:list_sub_3).pos
  352. assert_equal 2, mixins(:list_sub_4).pos
  353. end
  354. end