/test/test_validations.rb

https://github.com/thedaniel/mongomatic · Ruby · 340 lines · 269 code · 71 blank · 0 comment · 3 complexity · 97492f76f5c6e820005328130ec3dce4 MD5 · raw file

  1. require 'helper'
  2. require 'minitest/autorun'
  3. class TestValidations < MiniTest::Unit::TestCase
  4. def test_array_style_errors
  5. f = Foobar.new
  6. assert !f.valid?
  7. assert_equal ["color must not be blank", "missing style"], f.errors.full_messages
  8. f["color"] = "pink"; f.valid?
  9. assert_equal ["missing style"], f.errors.full_messages
  10. f["style"] = "awesome"; f.valid?
  11. assert_equal [], f.errors.full_messages
  12. end
  13. def test_be_expect
  14. p = Person.new
  15. class << p
  16. def validate
  17. expectations do
  18. be_expected self['alive'], "Alive must be true"
  19. not_be_expected self['dead'], "Dead must be false"
  20. end
  21. end
  22. end
  23. assert !p.valid?
  24. assert_equal ['Alive must be true'], p.errors.full_messages
  25. p['alive'] = true
  26. assert p.valid?
  27. p['dead'] = true
  28. assert !p.valid?
  29. assert_equal ['Dead must be false'], p.errors.full_messages
  30. end
  31. def test_be_expected_with_block
  32. p = Person.new
  33. class << p
  34. def validate
  35. expectations do
  36. be_expected lambda { self['name'].is_a? String }, "Name must be a string"
  37. not_be_expected lambda { self['alive'] && self['dead'] }, "Cannot be alive and dead"
  38. end
  39. end
  40. end
  41. p['name'] = 1
  42. assert !p.valid?
  43. assert_equal p.errors.full_messages, ["Name must be a string"]
  44. p['alive'] = true
  45. p['dead'] = true
  46. p['name'] = "Jordan"
  47. assert !p.valid?
  48. assert_equal p.errors.full_messages, ["Cannot be alive and dead"]
  49. p['dead'] = false
  50. assert p.valid?
  51. end
  52. def test_be_expected_with_method_call
  53. p = Person.new
  54. class << p
  55. def validate
  56. expectations do
  57. be_expected :method_1, "Method 1 must return true"
  58. not_be_expected :method_2, "Method 2 must return false"
  59. end
  60. end
  61. def method_1
  62. (self['name'] == 'Jordan') ? true : false
  63. end
  64. def method_2
  65. (self['age'] == 21) ? false : true
  66. end
  67. end
  68. assert !p.valid?
  69. assert_equal ["Method 1 must return true", "Method 2 must return false"], p.errors.full_messages
  70. p['name'] = 'Jordan'
  71. p['age'] = 21
  72. assert p.valid?
  73. end
  74. def test_be_present
  75. p = Person.new
  76. class << p
  77. def validate
  78. expectations do
  79. be_present self['name'], 'name cannot be blank'
  80. not_be_present self['age'], 'age must be blank'
  81. end
  82. end
  83. end
  84. assert !p.valid?
  85. assert_equal ['name cannot be blank'], p.errors.full_messages
  86. p['name'] = "Jordan"
  87. p['age'] = 21
  88. assert !p.valid?
  89. assert_equal ['age must be blank'], p.errors.full_messages
  90. p['age'] = nil
  91. assert p.valid?
  92. end
  93. def test_be_a_number
  94. p = Person.new
  95. class << p
  96. def validate
  97. expectations do
  98. be_a_number self['age'], 'Age is not a number'
  99. not_be_a_number self['name'], 'Name cannot be a number'
  100. be_a_number self['birth_year'], 'Birth year is not a number', :allow_nil => true
  101. end
  102. end
  103. end
  104. assert !p.valid?
  105. assert_equal ["Age is not a number"], p.errors.full_messages
  106. p['age'] = 21
  107. p['name'] = 65
  108. assert !p.valid?
  109. assert_equal ["Name cannot be a number"], p.errors.full_messages
  110. p['name'] = 'Jordan'
  111. assert p.valid?
  112. end
  113. def test_be_match
  114. p = Person.new
  115. class << p
  116. def validate
  117. expectations do
  118. be_match self['name'], "Name must start with uppercase letter", :with => /[A-Z][a-z]*/
  119. not_be_match self['nickname'], "Nickname cannot start with uppercase letter", :with => /[A-Z][a-z]*/
  120. be_match self['age'], "Age must only contain digits", :with => /\d+/, :allow_nil => true
  121. end
  122. end
  123. end
  124. assert !p.valid?
  125. assert_equal ["Name must start with uppercase letter"], p.errors.full_messages
  126. p['name'] = 'Jordan'
  127. p['nickname'] = 'Jordan'
  128. assert !p.valid?
  129. assert_equal ["Nickname cannot start with uppercase letter"], p.errors.full_messages
  130. p['nickname'] = 'jordan'
  131. assert p.valid?
  132. p['age'] = 'asd'
  133. assert !p.valid?
  134. assert_equal ["Age must only contain digits"], p.errors.full_messages
  135. p['age'] = '21'
  136. assert p.valid?
  137. end
  138. def test_be_of_length
  139. p = Person.new
  140. class << p
  141. def validate
  142. expectations do
  143. be_of_length self['name'], "Name must be 3 characters long", :minimum => 3
  144. be_of_length self['nickname'], "Nickname must not be longer than 5 characters", :maximum => 5
  145. be_of_length self['computers'], "Can only specify between 1 and 3 computers", :range => 1..3
  146. be_of_length self['status'], "Status must be a minimum of 1 character", :minumum => 1, :allow_nil => true
  147. end
  148. end
  149. end
  150. assert !p.valid?
  151. assert_equal ["Name must be 3 characters long",
  152. "Can only specify between 1 and 3 computers"], p.errors.full_messages
  153. p['name'] = 'Jordan'
  154. p['nickname'] = 'Jordan'
  155. assert !p.valid?
  156. assert_equal ["Nickname must not be longer than 5 characters",
  157. "Can only specify between 1 and 3 computers"], p.errors.full_messages
  158. p['nickname'] = 'abc'
  159. p['computers'] = ['comp_a']
  160. assert p.valid?
  161. end
  162. def test_be_reference
  163. id = Person.new('name' => 'jordan').insert
  164. p = Person.new
  165. class << p
  166. def validate
  167. expectations do
  168. be_reference self['friend'], 'friend must be an ObjectId'
  169. end
  170. end
  171. end
  172. assert !p.valid?
  173. assert_equal ["friend must be an ObjectId"], p.errors.full_messages
  174. p['friend'] = id
  175. assert p.valid?
  176. end
  177. def test_expectations_must_be_in_helper_block
  178. p = Person.new
  179. class << p
  180. def validate
  181. be_present self['name'], ''
  182. end
  183. end
  184. assert_raises NoMethodError do
  185. p.valid?
  186. end
  187. class << p
  188. def validate
  189. expectations { }
  190. be_present
  191. end
  192. end
  193. assert_raises NameError do
  194. p.valid?
  195. end
  196. end
  197. def test_errors_on_two_part_error_messages
  198. p = Person.new
  199. class << p
  200. def validate
  201. expectations do
  202. be_expected self['name'], ['name', 'cannot be empty']
  203. be_of_length self['name'], ['name', 'must be at least 3 characters long'], :minimum => 3
  204. be_a_number self['age'], ['age', 'must be a number']
  205. end
  206. end
  207. end
  208. p.valid?
  209. assert_equal ['cannot be empty', 'must be at least 3 characters long'], p.errors.on(:name)
  210. assert_equal ['must be a number'], p.errors.on('age')
  211. p['name'] = 'Jo'
  212. p['age'] = 21
  213. p.valid?
  214. assert_equal ['must be at least 3 characters long'], p.errors.on('name')
  215. assert_equal [], p.errors.on(:age)
  216. p['name'] = 'Jordan'
  217. p.valid?
  218. assert_equal [], p.errors.on(:name)
  219. end
  220. def test_errors_on_one_part_error_message
  221. p = Person.new
  222. class << p
  223. def validate
  224. expectations do
  225. be_expected self['name'], ['name', 'cannot be empty' ]
  226. be_of_length self['name'], ['name', 'must be at least 3 characters long'], :minimum => 3
  227. be_a_number self['age'], ['age','must be a number']
  228. end
  229. end
  230. end
  231. p.valid?
  232. assert_equal ['cannot be empty', 'must be at least 3 characters long'], p.errors.on(:name)
  233. assert_equal ['must be a number'], p.errors.on('age')
  234. p['name'] = 'Jo'
  235. p['age'] = 21
  236. p.valid?
  237. assert_equal ['must be at least 3 characters long'], p.errors.on('name')
  238. assert_equal [], p.errors.on(:age)
  239. p['name'] = 'Jordan'
  240. p.valid?
  241. assert_equal [], p.errors.on(:name)
  242. end
  243. def test_errors_on_case_insensitive
  244. p = Person.new
  245. class << p
  246. def validate
  247. expectations do
  248. be_expected self['name'], ['name', 'cannot be empty']
  249. be_expected self['age'], ['age','cannot be empty']
  250. end
  251. end
  252. end
  253. p.valid?
  254. assert_equal ['cannot be empty'], p.errors.on('name')
  255. assert_equal ['cannot be empty'], p.errors.on(:age)
  256. end
  257. def test_errors_on_multi_word_fields
  258. p = Person.new
  259. class << p
  260. def validate
  261. expectations do
  262. be_expected self['hair_color'], ['hair_color', 'must exist']
  263. end
  264. end
  265. end
  266. p.valid?
  267. assert_equal ['must exist'], p.errors.on(:hair_color)
  268. end
  269. end