Skip to content

Instantly share code, notes, and snippets.

@Ragmaanir
Created June 1, 2011 16:02
Show Gist options
  • Save Ragmaanir/1002632 to your computer and use it in GitHub Desktop.
Save Ragmaanir/1002632 to your computer and use it in GitHub Desktop.
require "dm_spec"
require 'dm-core'
require 'dm-migrations'
require 'dm-types'
require 'dm-validations'
require 'dm-timestamps'
describe DmSpec do
before(:all) do
DataMapper.setup(:default, 'sqlite::memory:')
class User
include DataMapper::Resource
property :id, Serial
has n, :friendship_requests, 'Friendship', :child_key => [:applicant_id]
has n, :friendship_offers, 'Friendship', :child_key => [:friend_id]
def friendships
friendship_requests + friendship_offers
end
end
class Friendship
include DataMapper::Resource
belongs_to :applicant, 'User', :key => true
belongs_to :friend, 'User', :key => true
def participants
[applicant,friend]
end
validates_with_block do
if applicant_id == friend_id
[false,'Friendship.errors.model.self_referential']
else
true
end
end
end
DataMapper.auto_migrate!
end
describe 'User' do
let(:applicant) { friendship.applicant }
let(:friend) { friendship.friend }
let(:friendship) { Friendship.create(:applicant => User.create,:friend => User.create) }
describe '#friendship_offers' do
context 'on friend' do
it 'includes friendship' do
friend.friendship_offers.should include(friendship)
end
end
context 'on applicant' do
it 'does not include friendship' do
applicant.friendship_offers.should_not include(friendship)
end
end
end
describe '#friendship_requests' do
context 'on friend' do
it 'does not include friendship' do
friend.friendship_requests.should_not include(friendship)
end
end
context 'on applicant' do
it 'does include friendship' do
applicant.friendship_requests.should include(friendship)
end
end
end
describe '#friendships' do
let(:applicant) { friendship.applicant }
let(:friend) { friendship.friend }
let(:friendship) { Friendship.create(:applicant => User.create,:friend => User.create) }
context 'on applicant' do
it 'returns friendship requests and offers' do
applicant.friendships.should == (applicant.friendship_requests + applicant.friendship_offers)
end
it 'includes a friendship' do
applicant.friendships.should have(1).entry
end
it 'includes the new friendship' do
applicant.friendships.should include(friendship)
end
it 'includes valid friendship' do
fs = applicant.friendships.first
fs.applicant.should_not == fs.friend
end
end
context 'on friend' do
it 'returns friendship requests and offers' do
p friend.friendship_requests | friend.friendship_offers
p (friend.friendship_requests + friend.friendship_offers).map{|fs| fs.participants.map(&:id)}
p friend.friendships
p Friendship.all.include?(friend.friendships.first)
friend.friendships.should == (friend.friendship_requests + friend.friendship_offers)
end
it 'includes a friendship' do
friend.friendships.should have(1).entry
end
it 'includes the new friendship' do
friend.friendships.should include(friendship)
end
it 'includes valid friendship' do
fs = friend.friendships.first
fs.applicant.should_not == fs.friend
end
end
end
end
end
@Ragmaanir
Copy link
Author

Two specs are failing, because:

Quote dkubb:
ragmaanir: you should probably call .to_a on the obejcts before trying to union them together
ragmaanir: in DM you can union two collections that have the same target, but since your model + key are different, it can't be done yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment