PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/test_validations.rb

http://github.com/jnunemaker/mongomapper
Ruby | 549 lines | 455 code | 93 blank | 1 comment | 13 complexity | 90a4d55721786fececa43c8dfdbabc37 MD5 | raw file
  1. require 'test_helper'
  2. class ValidationsTest < Test::Unit::TestCase
  3. context "Validations" do
  4. context "on a Document" do
  5. setup do
  6. @document = Doc('John')
  7. end
  8. context "Validating acceptance of" do
  9. should "work with validates_acceptance_of macro" do
  10. @document.key :terms, String
  11. @document.validates_acceptance_of :terms
  12. doc = @document.new(:terms => '')
  13. doc.should have_error_on(:terms)
  14. doc.terms = '1'
  15. doc.should_not have_error_on(:terms)
  16. end
  17. end
  18. context "validating confirmation of" do
  19. should "work with validates_confirmation_of macro" do
  20. @document.key :password, String
  21. @document.validates_confirmation_of :password
  22. # NOTE: Api change as ActiveModel passes if password_confirmation is nil
  23. doc = @document.new
  24. doc.password = 'foobar'
  25. doc.password_confirmation = 'foobar1'
  26. doc.should have_error_on(:password)
  27. doc.password_confirmation = 'foobar'
  28. doc.should_not have_error_on(:password)
  29. end
  30. end
  31. context "validating format of" do
  32. should "work with validates_format_of macro" do
  33. @document.key :name, String
  34. @document.validates_format_of :name, :with => /.+/
  35. doc = @document.new
  36. doc.should have_error_on(:name)
  37. doc.name = 'John'
  38. doc.should_not have_error_on(:name)
  39. end
  40. should "work with :format shorcut key" do
  41. @document.key :name, String, :format => /.+/
  42. doc = @document.new
  43. doc.should have_error_on(:name)
  44. doc.name = 'John'
  45. doc.should_not have_error_on(:name)
  46. end
  47. end
  48. context "validating length of" do
  49. should "work with validates_length_of macro" do
  50. @document.key :name, String
  51. @document.validates_length_of :name, :minimum => 5
  52. doc = @document.new
  53. doc.should have_error_on(:name)
  54. end
  55. context "with :length => integer shortcut" do
  56. should "set maximum of integer provided" do
  57. @document.key :name, String, :length => 5
  58. doc = @document.new
  59. doc.name = '123456'
  60. doc.should have_error_on(:name)
  61. doc.name = '12345'
  62. doc.should_not have_error_on(:name)
  63. end
  64. end
  65. context "with :length => range shortcut" do
  66. setup do
  67. @document.key :name, String, :length => 5..7
  68. end
  69. should "set minimum of range min" do
  70. doc = @document.new
  71. doc.should have_error_on(:name)
  72. doc.name = '123456'
  73. doc.should_not have_error_on(:name)
  74. end
  75. should "set maximum of range max" do
  76. doc = @document.new
  77. doc.should have_error_on(:name)
  78. doc.name = '12345678'
  79. doc.should have_error_on(:name)
  80. doc.name = '123456'
  81. doc.should_not have_error_on(:name)
  82. end
  83. end
  84. context "with :length => hash shortcut" do
  85. should "pass options through" do
  86. @document.key :name, String, :length => {:minimum => 2}
  87. doc = @document.new
  88. doc.should have_error_on(:name)
  89. doc.name = '12'
  90. doc.should_not have_error_on(:name)
  91. end
  92. end
  93. end # validates_length_of
  94. context "Validating numericality of" do
  95. should "work with validates_numericality_of macro" do
  96. @document.key :age, Integer
  97. @document.validates_numericality_of :age
  98. doc = @document.new
  99. doc.age = 'String'
  100. doc.should have_error_on(:age)
  101. doc.age = 23
  102. doc.should_not have_error_on(:age)
  103. end
  104. context "with :numeric shortcut" do
  105. should "work with integer or float" do
  106. @document.key :weight, Float, :numeric => true
  107. doc = @document.new
  108. doc.weight = 'String'
  109. doc.should have_error_on(:weight)
  110. doc.weight = 23.0
  111. doc.should_not have_error_on(:weight)
  112. doc.weight = 23
  113. doc.should_not have_error_on(:weight)
  114. end
  115. end
  116. context "with :numeric shortcut on Integer key" do
  117. should "only work with integers" do
  118. @document.key :age, Integer, :numeric => true
  119. doc = @document.new
  120. doc.age = 'String'
  121. doc.should have_error_on(:age)
  122. doc.age = 23.1
  123. doc.should have_error_on(:age)
  124. doc.age = 23
  125. doc.should_not have_error_on(:age)
  126. end
  127. end
  128. end # numericality of
  129. context "validating presence of" do
  130. should "work with validates_presence_of macro" do
  131. @document.key :name, String
  132. @document.validates_presence_of :name
  133. doc = @document.new
  134. doc.should have_error_on(:name)
  135. end
  136. should "work with :required shortcut on key definition" do
  137. @document.key :name, String, :required => true
  138. doc = @document.new
  139. doc.should have_error_on(:name)
  140. end
  141. end
  142. context "validating exclusion of" do
  143. should "throw error if enumerator not provided" do
  144. @document.key :action, String
  145. lambda {
  146. @document.validates_exclusion_of :action
  147. }.should raise_error(ArgumentError)
  148. end
  149. should "work with validates_exclusion_of macro" do
  150. @document.key :action, String
  151. @document.validates_exclusion_of :action, :in => %w(kick run)
  152. doc = @document.new
  153. doc.should_not have_error_on(:action)
  154. doc.action = 'fart'
  155. doc.should_not have_error_on(:action)
  156. doc.action = 'kick'
  157. doc.should have_error_on(:action, 'is reserved')
  158. end
  159. should "work with :not_in shortcut on key definition" do
  160. @document.key :action, String, :not_in => %w(kick run)
  161. doc = @document.new
  162. doc.should_not have_error_on(:action)
  163. doc.action = 'fart'
  164. doc.should_not have_error_on(:action)
  165. doc.action = 'kick'
  166. doc.should have_error_on(:action, 'is reserved')
  167. end
  168. should "not have error if allow nil is true and value is nil" do
  169. @document.key :action, String
  170. @document.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
  171. doc = @document.new
  172. doc.should_not have_error_on(:action)
  173. end
  174. should "not have error if allow blank is true and value is blank" do
  175. @document.key :action, String
  176. @document.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
  177. doc = @document.new(:action => '')
  178. doc.should_not have_error_on(:action)
  179. end
  180. end
  181. context "validating inclusion of" do
  182. should "throw error if enumerator not provided" do
  183. @document.key :action, String
  184. lambda {
  185. @document.validates_inclusion_of :action
  186. }.should raise_error(ArgumentError)
  187. end
  188. should "work with validates_inclusion_of macro" do
  189. @document.key :action, String
  190. @document.validates_inclusion_of :action, :in => %w(kick run)
  191. doc = @document.new
  192. doc.should have_error_on(:action, 'is not included in the list')
  193. doc.action = 'fart'
  194. doc.should have_error_on(:action, 'is not included in the list')
  195. doc.action = 'kick'
  196. doc.should_not have_error_on(:action)
  197. end
  198. should "work with :in shortcut on key definition" do
  199. @document.key :action, String, :in => %w(kick run)
  200. doc = @document.new
  201. doc.should have_error_on(:action, 'is not included in the list')
  202. doc.action = 'fart'
  203. doc.should have_error_on(:action, 'is not included in the list')
  204. doc.action = 'kick'
  205. doc.should_not have_error_on(:action)
  206. end
  207. should "not have error if allow nil is true and value is nil" do
  208. @document.key :action, String
  209. @document.validates_inclusion_of :action, :in => %w(kick run), :allow_nil => true
  210. doc = @document.new
  211. doc.should_not have_error_on(:action)
  212. end
  213. should "not have error if allow blank is true and value is blank" do
  214. @document.key :action, String
  215. @document.validates_inclusion_of :action, :in => %w(kick run), :allow_blank => true
  216. doc = @document.new(:action => '')
  217. doc.should_not have_error_on(:action)
  218. end
  219. end
  220. end # End on a Document
  221. context "On an EmbeddedDocument" do
  222. setup do
  223. @embedded_doc = EDoc()
  224. end
  225. context "Validating acceptance of" do
  226. should "work with validates_acceptance_of macro" do
  227. @embedded_doc.key :terms, String
  228. @embedded_doc.validates_acceptance_of :terms
  229. doc = @embedded_doc.new(:terms => '')
  230. doc.should have_error_on(:terms)
  231. doc.terms = '1'
  232. doc.should_not have_error_on(:terms)
  233. end
  234. end
  235. context "validating confirmation of" do
  236. should "work with validates_confirmation_of macro" do
  237. @embedded_doc.key :password, String
  238. @embedded_doc.validates_confirmation_of :password
  239. doc = @embedded_doc.new
  240. doc.password = 'foobar'
  241. doc.password_confirmation = 'foobar1'
  242. doc.should have_error_on(:password)
  243. doc.password_confirmation = 'foobar'
  244. doc.should_not have_error_on(:password)
  245. end
  246. end
  247. context "validating format of" do
  248. should "work with validates_format_of macro" do
  249. @embedded_doc.key :name, String
  250. @embedded_doc.validates_format_of :name, :with => /.+/
  251. doc = @embedded_doc.new
  252. doc.should have_error_on(:name)
  253. doc.name = 'John'
  254. doc.should_not have_error_on(:name)
  255. end
  256. should "work with :format shorcut key" do
  257. @embedded_doc.key :name, String, :format => /.+/
  258. doc = @embedded_doc.new
  259. doc.should have_error_on(:name)
  260. doc.name = 'John'
  261. doc.should_not have_error_on(:name)
  262. end
  263. end
  264. context "validating length of" do
  265. should "work with validates_length_of macro" do
  266. @embedded_doc.key :name, String
  267. @embedded_doc.validates_length_of :name, :minimum => 5
  268. doc = @embedded_doc.new
  269. doc.should have_error_on(:name)
  270. end
  271. context "with :length => integer shortcut" do
  272. should "set maximum of integer provided" do
  273. @embedded_doc.key :name, String, :length => 5
  274. doc = @embedded_doc.new
  275. doc.name = '123456'
  276. doc.should have_error_on(:name)
  277. doc.name = '12345'
  278. doc.should_not have_error_on(:name)
  279. end
  280. end
  281. context "with :length => range shortcut" do
  282. setup do
  283. @embedded_doc.key :name, String, :length => 5..7
  284. end
  285. should "set minimum of range min" do
  286. doc = @embedded_doc.new
  287. doc.should have_error_on(:name)
  288. doc.name = '123456'
  289. doc.should_not have_error_on(:name)
  290. end
  291. should "set maximum of range max" do
  292. doc = @embedded_doc.new
  293. doc.should have_error_on(:name)
  294. doc.name = '12345678'
  295. doc.should have_error_on(:name)
  296. doc.name = '123456'
  297. doc.should_not have_error_on(:name)
  298. end
  299. end
  300. context "with :length => hash shortcut" do
  301. should "pass options through" do
  302. @embedded_doc.key :name, String, :length => {:minimum => 2}
  303. doc = @embedded_doc.new
  304. doc.should have_error_on(:name)
  305. doc.name = '12'
  306. doc.should_not have_error_on(:name)
  307. end
  308. end
  309. end # validates_length_of
  310. context "Validating numericality of" do
  311. should "work with validates_numericality_of macro" do
  312. @embedded_doc.key :age, Integer
  313. @embedded_doc.validates_numericality_of :age
  314. doc = @embedded_doc.new
  315. doc.age = 'String'
  316. doc.should have_error_on(:age)
  317. doc.age = 23
  318. doc.should_not have_error_on(:age)
  319. end
  320. context "with :numeric shortcut" do
  321. should "work with integer or float" do
  322. @embedded_doc.key :weight, Float, :numeric => true
  323. doc = @embedded_doc.new
  324. doc.weight = 'String'
  325. doc.should have_error_on(:weight)
  326. doc.weight = 23.0
  327. doc.should_not have_error_on(:weight)
  328. doc.weight = 23
  329. doc.should_not have_error_on(:weight)
  330. end
  331. end
  332. context "with :numeric shortcut on Integer key" do
  333. should "only work with integers" do
  334. @embedded_doc.key :age, Integer, :numeric => true
  335. doc = @embedded_doc.new
  336. doc.age = 'String'
  337. doc.should have_error_on(:age)
  338. doc.age = 23.1
  339. doc.should have_error_on(:age)
  340. doc.age = 23
  341. doc.should_not have_error_on(:age)
  342. end
  343. end
  344. end # numericality of
  345. context "validating presence of" do
  346. should "work with validates_presence_of macro" do
  347. @embedded_doc.key :name, String
  348. @embedded_doc.validates_presence_of :name
  349. doc = @embedded_doc.new
  350. doc.should have_error_on(:name)
  351. end
  352. should "work with :required shortcut on key definition" do
  353. @embedded_doc.key :name, String, :required => true
  354. doc = @embedded_doc.new
  355. doc.should have_error_on(:name)
  356. end
  357. end
  358. context "validating exclusion of" do
  359. should "throw error if enumerator not provided" do
  360. @embedded_doc.key :action, String
  361. lambda {
  362. @embedded_doc.validates_exclusion_of :action
  363. }.should raise_error(ArgumentError)
  364. end
  365. should "work with validates_exclusion_of macro" do
  366. @embedded_doc.key :action, String
  367. @embedded_doc.validates_exclusion_of :action, :in => %w(kick run)
  368. doc = @embedded_doc.new
  369. doc.should_not have_error_on(:action)
  370. doc.action = 'fart'
  371. doc.should_not have_error_on(:action)
  372. doc.action = 'kick'
  373. doc.should have_error_on(:action, 'is reserved')
  374. end
  375. should "work with :not_in shortcut on key definition" do
  376. @embedded_doc.key :action, String, :not_in => %w(kick run)
  377. doc = @embedded_doc.new
  378. doc.should_not have_error_on(:action)
  379. doc.action = 'fart'
  380. doc.should_not have_error_on(:action)
  381. doc.action = 'kick'
  382. doc.should have_error_on(:action, 'is reserved')
  383. end
  384. should "not have error if allow nil is true and value is nil" do
  385. @embedded_doc.key :action, String
  386. @embedded_doc.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
  387. doc = @embedded_doc.new
  388. doc.should_not have_error_on(:action)
  389. end
  390. should "not have error if allow blank is true and value is blank" do
  391. @embedded_doc.key :action, String
  392. @embedded_doc.validates_exclusion_of :action, :in => %w(kick run), :allow_nil => true
  393. doc = @embedded_doc.new(:action => '')
  394. doc.should_not have_error_on(:action)
  395. end
  396. end
  397. context "validating inclusion of" do
  398. should "throw error if enumerator not provided" do
  399. @embedded_doc.key :action, String
  400. lambda {
  401. @embedded_doc.validates_inclusion_of :action
  402. }.should raise_error(ArgumentError)
  403. end
  404. should "work with validates_inclusion_of macro" do
  405. @embedded_doc.key :action, String
  406. @embedded_doc.validates_inclusion_of :action, :in => %w(kick run)
  407. doc = @embedded_doc.new
  408. doc.should have_error_on(:action, 'is not included in the list')
  409. doc.action = 'fart'
  410. doc.should have_error_on(:action, 'is not included in the list')
  411. doc.action = 'kick'
  412. doc.should_not have_error_on(:action)
  413. end
  414. should "work with :in shortcut on key definition" do
  415. @embedded_doc.key :action, String, :in => %w(kick run)
  416. doc = @embedded_doc.new
  417. doc.should have_error_on(:action, 'is not included in the list')
  418. doc.action = 'fart'
  419. doc.should have_error_on(:action, 'is not included in the list')
  420. doc.action = 'kick'
  421. doc.should_not have_error_on(:action)
  422. end
  423. should "not have error if allow nil is true and value is nil" do
  424. @embedded_doc.key :action, String
  425. @embedded_doc.validates_inclusion_of :action, :in => %w(kick run), :allow_nil => true
  426. doc = @embedded_doc.new
  427. doc.should_not have_error_on(:action)
  428. end
  429. should "not have error if allow blank is true and value is blank" do
  430. @embedded_doc.key :action, String
  431. @embedded_doc.validates_inclusion_of :action, :in => %w(kick run), :allow_blank => true
  432. doc = @embedded_doc.new(:action => '')
  433. doc.should_not have_error_on(:action)
  434. end
  435. end
  436. end # End on an EmbeddedDocument
  437. end # Validations
  438. context "Adding validation errors" do
  439. setup do
  440. @document = Doc do
  441. key :action, String
  442. def action_present
  443. errors.add(:action, 'is invalid') if action.blank?
  444. end
  445. end
  446. end
  447. should "work with validate callback" do
  448. @document.validate :action_present
  449. doc = @document.new
  450. doc.action = nil
  451. doc.should have_error_on(:action)
  452. doc.action = 'kick'
  453. doc.should_not have_error_on(:action)
  454. end
  455. end
  456. end