Guest User

Untitled

a guest
Jul 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. require 'rubygems'
  2. require 'bundler'
  3. Bundler.require
  4.  
  5. MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
  6. MongoMapper.database = "test1-#{RUBY_VERSION.gsub('.', '-')}"
  7. MongoMapper.database.collections.each { |c| c.drop_indexes }
  8.  
  9. class Person
  10. include MongoMapper::Document
  11. key :name, String
  12. key :age, Integer
  13. many :tags
  14.  
  15. def tags
  16. Tag.where(:person_id => self.id).all
  17. end
  18. end
  19.  
  20. class BadPerson < Person
  21. key :killed, Integer
  22. end
  23.  
  24. class GoodPerson < Person
  25. key :saved, Integer
  26. end
  27.  
  28. class Tag
  29. include MongoMapper::Document
  30. key :label, String
  31. belongs_to :person
  32. end
  33.  
  34. describe "One to many relations" do
  35. context 'One person with many tags' do
  36. before do
  37. bp1 = BadPerson.create(:name => 'Some guy', :age => 123, :killed => 123)
  38. Tag.create(:label => 'Ugly', :person => bp1)
  39. Tag.create(:label => 'Deadly', :person => bp1)
  40.  
  41. bp2 = GoodPerson.create(:name => 'Other guy', :age => 45, :killed => 0)
  42. Tag.create(:label => 'Nice', :person => bp2)
  43. Tag.create(:label => 'Benevolent', :person => bp2)
  44. end
  45. after do
  46. MongoMapper.connection.drop_database MongoMapper.database.name
  47. end
  48. it 'bad person should have two tags' do
  49. BadPerson.where(:name => 'Some guy').first.tags.count.should == 2
  50. end
  51. it 'good person should have two tags' do
  52. GoodPerson.where(:name => 'Other guy').first.tags.count.should == 2
  53. end
  54. it 'person should have four tags' do
  55. Person.all.each do |person|
  56. person.tags.count.should == 2
  57. end
  58. end
  59. end
  60. end
Add Comment
Please, Sign In to add comment