Guest User

Untitled

a guest
Apr 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. it "should validate the uniqueness of a column" do
  2. class User < Sequel::Model
  3. validations.clear
  4. validates do
  5. uniqueness_of :username
  6. end
  7. end
  8. User.dataset.extend(Module.new {
  9. def fetch_rows(sql)
  10. @db << sql
  11.  
  12. if sql == "SELECT * FROM users WHERE (username = 'willy') LIMIT 1" then
  13. yield({:id => 1, :username => "willy", :password => "test"})
  14. end
  15. end
  16. })
  17.  
  18. @user = User.new(:username => "willy", :password => "anothertest")
  19. @user.should_not be_valid
  20. @user.errors.full_messages.should == ['username is already taken']
  21.  
  22. @user.username = "pinky"
  23. @user.should be_valid
  24. end
  25.  
  26. it "should not fail when trying to update a record when the unique value hasn't changed" do
  27. class User < Sequel::Model
  28. validations.clear
  29. validates do
  30. uniqueness_of :username
  31. end
  32. end
  33. User.dataset.extend(Module.new {
  34. def fetch_rows(sql)
  35. @db << sql
  36.  
  37. yield({:id => 1, :username => "willy", :password => "test"})
  38. end
  39. })
  40.  
  41. user = User[1]
  42. user.should be_valid
  43. end
Add Comment
Please, Sign In to add comment