Guest User

Untitled

a guest
Mar 11th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #
  3. # A one file test to show ...
  4. require 'rubygems'
  5. require 'dm-core'
  6. require 'dm-validations'
  7.  
  8. # setup the logger
  9. DataMapper::Logger.new(STDOUT, :debug)
  10.  
  11. # connect to the DB
  12. DataMapper.setup(:default, 'sqlite3::memory:')
  13.  
  14. class User
  15. include DataMapper::Resource
  16.  
  17. # properties
  18. property :id, Serial
  19. property :username, String
  20. property :password, String, :length => 10..50
  21.  
  22. has 1..n, :pages
  23.  
  24. end
  25.  
  26. class Page
  27. include DataMapper::Resource
  28.  
  29. # properties
  30. property :id, Serial
  31. property :name, String
  32.  
  33. belongs_to :user
  34. end
  35.  
  36. DataMapper.auto_migrate!
  37.  
  38. def save_without_transaction(username, password)
  39. @user = User.new(:username => username, :password => password, :pages => [ Page.new(:name => 'a'*51) ])
  40.  
  41. if @user.save
  42. puts "YAY!"
  43. true
  44. else
  45. puts "BOO!"
  46. puts @user.errors.full_messages.join("\n")
  47. @user.pages.map { |p| puts(p.errors.full_messages.join("\n")) }
  48. end
  49. end
  50.  
  51.  
  52. puts "All users:"
  53. p User.all
  54. puts "All pages:"
  55. p Page.all
  56.  
  57.  
  58. save_without_transaction('namelessjon', 'rlyrlysecret!')
  59.  
  60. puts "All users:"
  61. p User.all
  62. puts "All pages:"
  63. p Page.all
  64.  
  65.  
  66. save_without_transaction('namedjon', 'notsogood')
  67.  
  68. puts "All users:"
  69. p User.all
  70. puts "All pages:"
  71. p Page.all
Add Comment
Please, Sign In to add comment