PageRenderTime 70ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/shoulda-2.11.3/lib/shoulda/active_record/macros.rb

http://github.com/IronLanguages/main
Ruby | 457 lines | 184 code | 32 blank | 241 comment | 1 complexity | e7adeec2a698d96fea683c81e658a996 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. module Shoulda # :nodoc:
  2. module ActiveRecord # :nodoc:
  3. # = Macro test helpers for your active record models
  4. #
  5. # These helpers will test most of the validations and associations for your ActiveRecord models.
  6. #
  7. # class UserTest < Test::Unit::TestCase
  8. # should_validate_presence_of :name, :phone_number
  9. # should_not_allow_values_for :phone_number, "abcd", "1234"
  10. # should_allow_values_for :phone_number, "(123) 456-7890"
  11. #
  12. # should_not_allow_mass_assignment_of :password
  13. #
  14. # should_have_one :profile
  15. # should_have_many :dogs
  16. # should_have_many :messes, :through => :dogs
  17. # should_belong_to :lover
  18. # end
  19. #
  20. # For all of these helpers, the last parameter may be a hash of options.
  21. #
  22. module Macros
  23. include Helpers
  24. include Matchers
  25. # Deprecated: use ActiveRecord::Matchers#validate_presence_of instead.
  26. #
  27. # Ensures that the model cannot be saved if one of the attributes listed is not present.
  28. #
  29. # Options:
  30. # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  31. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.blank')</tt>
  32. #
  33. # Example:
  34. # should_validate_presence_of :name, :phone_number
  35. #
  36. def should_validate_presence_of(*attributes)
  37. ::ActiveSupport::Deprecation.warn("use: should validate_presence_of")
  38. message = get_options!(attributes, :message)
  39. attributes.each do |attribute|
  40. should validate_presence_of(attribute).with_message(message)
  41. end
  42. end
  43. # Deprecated: use ActiveRecord::Matchers#validate_uniqueness_of instead.
  44. #
  45. # Ensures that the model cannot be saved if one of the attributes listed is not unique.
  46. # Requires an existing record
  47. #
  48. # Options:
  49. # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  50. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.taken')</tt>
  51. # * <tt>:scoped_to</tt> - field(s) to scope the uniqueness to.
  52. # * <tt>:case_sensitive</tt> - whether or not uniqueness is defined by an
  53. # exact match. Ignored by non-text attributes. Default = <tt>true</tt>
  54. #
  55. # Examples:
  56. # should_validate_uniqueness_of :keyword, :username
  57. # should_validate_uniqueness_of :name, :message => "O NOES! SOMEONE STOELED YER NAME!"
  58. # should_validate_uniqueness_of :email, :scoped_to => :name
  59. # should_validate_uniqueness_of :address, :scoped_to => [:first_name, :last_name]
  60. # should_validate_uniqueness_of :email, :case_sensitive => false
  61. #
  62. def should_validate_uniqueness_of(*attributes)
  63. ::ActiveSupport::Deprecation.warn("use: should validate_uniqueness_of")
  64. message, scope, case_sensitive = get_options!(attributes, :message, :scoped_to, :case_sensitive)
  65. scope = [*scope].compact
  66. case_sensitive = true if case_sensitive.nil?
  67. attributes.each do |attribute|
  68. matcher = validate_uniqueness_of(attribute).
  69. with_message(message).scoped_to(scope)
  70. matcher = matcher.case_insensitive unless case_sensitive
  71. should matcher
  72. end
  73. end
  74. # Deprecated: use ActiveRecord::Matchers#allow_mass_assignment_of instead.
  75. #
  76. # Ensures that the attribute can be set on mass update.
  77. #
  78. # should_allow_mass_assignment_of :first_name, :last_name
  79. #
  80. def should_allow_mass_assignment_of(*attributes)
  81. ::ActiveSupport::Deprecation.warn("use: should allow_mass_assignment_of")
  82. get_options!(attributes)
  83. attributes.each do |attribute|
  84. should allow_mass_assignment_of(attribute)
  85. end
  86. end
  87. # Deprecated: use ActiveRecord::Matchers#allow_mass_assignment_of instead.
  88. #
  89. # Ensures that the attribute cannot be set on mass update.
  90. #
  91. # should_not_allow_mass_assignment_of :password, :admin_flag
  92. #
  93. def should_not_allow_mass_assignment_of(*attributes)
  94. ::ActiveSupport::Deprecation.warn("use: should_not allow_mass_assignment_of")
  95. get_options!(attributes)
  96. attributes.each do |attribute|
  97. should_not allow_mass_assignment_of(attribute)
  98. end
  99. end
  100. # Deprecated: use ActiveRecord::Matchers#have_readonly_attribute instead.
  101. #
  102. # Ensures that the attribute cannot be changed once the record has been created.
  103. #
  104. # should_have_readonly_attributes :password, :admin_flag
  105. #
  106. def should_have_readonly_attributes(*attributes)
  107. ::ActiveSupport::Deprecation.warn("use: should have_readonly_attribute")
  108. get_options!(attributes)
  109. attributes.each do |attribute|
  110. should have_readonly_attribute(attribute)
  111. end
  112. end
  113. # Deprecated: use ActiveRecord::Matchers#allow_value instead.
  114. #
  115. # Ensures that the attribute cannot be set to the given values
  116. #
  117. # Options:
  118. # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  119. # Regexp or string. If omitted, the test will pass if there is ANY error in
  120. # <tt>errors.on(:attribute)</tt>.
  121. #
  122. # Example:
  123. # should_not_allow_values_for :isbn, "bad 1", "bad 2"
  124. #
  125. def should_not_allow_values_for(attribute, *bad_values)
  126. ::ActiveSupport::Deprecation.warn("use: should_not allow_value")
  127. message = get_options!(bad_values, :message)
  128. bad_values.each do |value|
  129. should_not allow_value(value).for(attribute).with_message(message)
  130. end
  131. end
  132. # Deprecated: use ActiveRecord::Matchers#allow_value instead.
  133. #
  134. # Ensures that the attribute can be set to the given values.
  135. #
  136. # Example:
  137. # should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
  138. #
  139. def should_allow_values_for(attribute, *good_values)
  140. ::ActiveSupport::Deprecation.warn("use: should allow_value")
  141. get_options!(good_values)
  142. good_values.each do |value|
  143. should allow_value(value).for(attribute)
  144. end
  145. end
  146. # Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
  147. #
  148. # Ensures that the length of the attribute is in the given range
  149. #
  150. # Options:
  151. # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  152. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % range.first</tt>
  153. # * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  154. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_long') % range.last</tt>
  155. #
  156. # Example:
  157. # should_ensure_length_in_range :password, (6..20)
  158. #
  159. def should_ensure_length_in_range(attribute, range, opts = {})
  160. ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_at_least.is_at_most")
  161. short_message, long_message = get_options!([opts],
  162. :short_message,
  163. :long_message)
  164. should ensure_length_of(attribute).
  165. is_at_least(range.first).
  166. with_short_message(short_message).
  167. is_at_most(range.last).
  168. with_long_message(long_message)
  169. end
  170. # Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
  171. #
  172. # Ensures that the length of the attribute is at least a certain length
  173. #
  174. # Options:
  175. # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  176. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % min_length</tt>
  177. #
  178. # Example:
  179. # should_ensure_length_at_least :name, 3
  180. #
  181. def should_ensure_length_at_least(attribute, min_length, opts = {})
  182. ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_at_least")
  183. short_message = get_options!([opts], :short_message)
  184. should ensure_length_of(attribute).
  185. is_at_least(min_length).
  186. with_short_message(short_message)
  187. end
  188. # Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
  189. #
  190. # Ensures that the length of the attribute is exactly a certain length
  191. #
  192. # Options:
  193. # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  194. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % length</tt>
  195. #
  196. # Example:
  197. # should_ensure_length_is :ssn, 9
  198. #
  199. def should_ensure_length_is(attribute, length, opts = {})
  200. ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_equal_to")
  201. message = get_options!([opts], :message)
  202. should ensure_length_of(attribute).
  203. is_equal_to(length).
  204. with_message(message)
  205. end
  206. # Deprecated: use ActiveRecord::Matchers#ensure_inclusion_of instead.
  207. #
  208. # Ensure that the attribute is in the range specified
  209. #
  210. # Options:
  211. # * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  212. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
  213. # * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  214. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
  215. #
  216. # Example:
  217. # should_ensure_value_in_range :age, (0..100)
  218. #
  219. def should_ensure_value_in_range(attribute, range, opts = {})
  220. ::ActiveSupport::Deprecation.warn("use: should ensure_inclusion_of.in_range")
  221. message, low_message, high_message = get_options!([opts],
  222. :message,
  223. :low_message,
  224. :high_message)
  225. should ensure_inclusion_of(attribute).
  226. in_range(range).
  227. with_message(message).
  228. with_low_message(low_message).
  229. with_high_message(high_message)
  230. end
  231. # Deprecated: use ActiveRecord::Matchers#validate_numericality_of instead.
  232. #
  233. # Ensure that the attribute is numeric
  234. #
  235. # Options:
  236. # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  237. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.not_a_number')</tt>
  238. #
  239. # Example:
  240. # should_validate_numericality_of :age
  241. #
  242. def should_validate_numericality_of(*attributes)
  243. ::ActiveSupport::Deprecation.warn("use: should validate_numericality_of")
  244. message = get_options!(attributes, :message)
  245. attributes.each do |attribute|
  246. should validate_numericality_of(attribute).
  247. with_message(message)
  248. end
  249. end
  250. # Deprecated: use ActiveRecord::Matchers#have_many instead.
  251. #
  252. # Ensures that the has_many relationship exists. Will also test that the
  253. # associated table has the required columns. Works with polymorphic
  254. # associations.
  255. #
  256. # Options:
  257. # * <tt>:through</tt> - association name for <tt>has_many :through</tt>
  258. # * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
  259. #
  260. # Example:
  261. # should_have_many :friends
  262. # should_have_many :enemies, :through => :friends
  263. # should_have_many :enemies, :dependent => :destroy
  264. #
  265. def should_have_many(*associations)
  266. ::ActiveSupport::Deprecation.warn("use: should have_many")
  267. through, dependent = get_options!(associations, :through, :dependent)
  268. associations.each do |association|
  269. should have_many(association).through(through).dependent(dependent)
  270. end
  271. end
  272. # Deprecated: use ActiveRecord::Matchers#have_one instead.
  273. #
  274. # Ensure that the has_one relationship exists. Will also test that the
  275. # associated table has the required columns. Works with polymorphic
  276. # associations.
  277. #
  278. # Options:
  279. # * <tt>:dependent</tt> - tests that the association makes use of the dependent option.
  280. #
  281. # Example:
  282. # should_have_one :god # unless hindu
  283. #
  284. def should_have_one(*associations)
  285. ::ActiveSupport::Deprecation.warn("use: should have_one")
  286. dependent, through = get_options!(associations, :dependent, :through)
  287. associations.each do |association|
  288. should have_one(association).dependent(dependent).through(through)
  289. end
  290. end
  291. # Deprecated: use ActiveRecord::Matchers#have_and_belong_to_many instead.
  292. #
  293. # Ensures that the has_and_belongs_to_many relationship exists, and that the join
  294. # table is in place.
  295. #
  296. # should_have_and_belong_to_many :posts, :cars
  297. #
  298. def should_have_and_belong_to_many(*associations)
  299. ::ActiveSupport::Deprecation.warn("use: should have_and_belong_to_many")
  300. get_options!(associations)
  301. associations.each do |association|
  302. should have_and_belong_to_many(association)
  303. end
  304. end
  305. # Deprecated: use ActiveRecord::Matchers#belong_to instead.
  306. #
  307. # Ensure that the belongs_to relationship exists.
  308. #
  309. # should_belong_to :parent
  310. #
  311. def should_belong_to(*associations)
  312. ::ActiveSupport::Deprecation.warn("use: should belong_to")
  313. dependent = get_options!(associations, :dependent)
  314. associations.each do |association|
  315. should belong_to(association).dependent(dependent)
  316. end
  317. end
  318. # Deprecated.
  319. #
  320. # Ensure that the given class methods are defined on the model.
  321. #
  322. # should_have_class_methods :find, :destroy
  323. #
  324. def should_have_class_methods(*methods)
  325. ::ActiveSupport::Deprecation.warn
  326. get_options!(methods)
  327. klass = described_type
  328. methods.each do |method|
  329. should "respond to class method ##{method}" do
  330. assert_respond_to klass, method, "#{klass.name} does not have class method #{method}"
  331. end
  332. end
  333. end
  334. # Deprecated.
  335. #
  336. # Ensure that the given instance methods are defined on the model.
  337. #
  338. # should_have_instance_methods :email, :name, :name=
  339. #
  340. def should_have_instance_methods(*methods)
  341. ::ActiveSupport::Deprecation.warn
  342. get_options!(methods)
  343. klass = described_type
  344. methods.each do |method|
  345. should "respond to instance method ##{method}" do
  346. assert_respond_to klass.new, method, "#{klass.name} does not have instance method #{method}"
  347. end
  348. end
  349. end
  350. # Deprecated: use ActiveRecord::Matchers#have_db_column instead.
  351. #
  352. # Ensure that the given columns are defined on the models backing SQL table.
  353. # Also aliased to should_have_db_column for readability.
  354. # Takes the same options available in migrations:
  355. # :type, :precision, :limit, :default, :null, and :scale
  356. #
  357. # Examples:
  358. #
  359. # should_have_db_columns :id, :email, :name, :created_at
  360. #
  361. # should_have_db_column :email, :type => "string", :limit => 255
  362. # should_have_db_column :salary, :decimal, :precision => 15, :scale => 2
  363. # should_have_db_column :admin, :default => false, :null => false
  364. #
  365. def should_have_db_columns(*columns)
  366. ::ActiveSupport::Deprecation.warn("use: should have_db_column")
  367. column_type, precision, limit, default, null, scale, sql_type =
  368. get_options!(columns, :type, :precision, :limit,
  369. :default, :null, :scale, :sql_type)
  370. columns.each do |name|
  371. should have_db_column(name).
  372. of_type(column_type).
  373. with_options(:precision => precision, :limit => limit,
  374. :default => default, :null => null,
  375. :scale => scale, :sql_type => sql_type)
  376. end
  377. end
  378. alias_method :should_have_db_column, :should_have_db_columns
  379. # Deprecated: use ActiveRecord::Matchers#have_db_index instead.
  380. #
  381. # Ensures that there are DB indices on the given columns or tuples of columns.
  382. # Also aliased to should_have_db_index for readability
  383. #
  384. # Options:
  385. # * <tt>:unique</tt> - whether or not the index has a unique
  386. # constraint. Use <tt>true</tt> to explicitly test for a unique
  387. # constraint. Use <tt>false</tt> to explicitly test for a non-unique
  388. # constraint. Use <tt>nil</tt> if you don't care whether the index is
  389. # unique or not. Default = <tt>nil</tt>
  390. #
  391. # Examples:
  392. #
  393. # should_have_db_indices :email, :name, [:commentable_type, :commentable_id]
  394. # should_have_db_index :age
  395. # should_have_db_index :ssn, :unique => true
  396. #
  397. def should_have_db_indices(*columns)
  398. ::ActiveSupport::Deprecation.warn("use: should have_db_index")
  399. unique = get_options!(columns, :unique)
  400. columns.each do |column|
  401. should have_db_index(column).unique(unique)
  402. end
  403. end
  404. alias_method :should_have_db_index, :should_have_db_indices
  405. # Deprecated: use ActiveRecord::Matchers#validate_acceptance_of instead.
  406. #
  407. # Ensures that the model cannot be saved if one of the attributes listed is not accepted.
  408. #
  409. # Options:
  410. # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
  411. # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.accepted')</tt>
  412. #
  413. # Example:
  414. # should_validate_acceptance_of :eula
  415. #
  416. def should_validate_acceptance_of(*attributes)
  417. ::ActiveSupport::Deprecation.warn("use: should validate_acceptance_of")
  418. message = get_options!(attributes, :message)
  419. attributes.each do |attribute|
  420. should validate_acceptance_of(attribute).with_message(message)
  421. end
  422. end
  423. end
  424. end
  425. end