Skip to content

Commit

Permalink
Minor associations spec improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
yrashk committed Apr 30, 2008
1 parent 8db3a1c commit 6c34648
Showing 1 changed file with 62 additions and 60 deletions.
122 changes: 62 additions & 60 deletions spec/lib/strokedb/document/associations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "Playlist.has_many :songs association" do

before(:each) do
setup_default_store
setup_index
Expand All @@ -12,7 +12,7 @@
end
Song = Meta.new
end

it "should convert :songs to Song and Playlist to playlist to compute foreign reference slot name" do
playlist = Playlist.create!
song = Song.create!(:playlist => playlist)
Expand All @@ -24,13 +24,13 @@
song = Song.create!
playlist.songs.should be_empty
end

it "should have association owner defined" do
playlist = Playlist.create!
song = Song.create!
playlist.songs.association_owner.should == playlist
end

it "should work well with multiple metas" do
Object.send!(:remove_const,'RockPlaylist') if defined?(RockPlaylist)
RockPlaylist = Meta.new do
Expand All @@ -44,7 +44,7 @@
playlist.songs.should == [rock_song]
playlist.rock_songs.should == [rock_song]
end

it "should fetch head versions of associated documents if association owner is a head" do
playlist = Playlist.create!
playlist.should be_head
Expand All @@ -60,21 +60,22 @@
end

it "should fetch specific versions of associated documents if association owner is a not a head" do
pending
playlist = Playlist.create!
song = Song.create!(:playlist => playlist)
playlist.name = "My playlist"
playlist.save!
playlist = playlist.versions.previous
playlist.should_not be_head
song.name = "My song"
song.save!
song = song.versions.previous
playlist.songs.should == [song]
playlist.songs.each do |s|
s.should_not be_head
s.should be_a_kind_of(VersionedDocument)
s.should_not have_slot(:name)
pending do
playlist = Playlist.create!
song = Song.create!(:playlist => playlist)
playlist.name = "My playlist"
playlist.save!
playlist = playlist.versions.previous
playlist.should_not be_head
song.name = "My song"
song.save!
song = song.versions.previous
playlist.songs.should == [song]
playlist.songs.each do |s|
s.should_not be_head
s.should be_a_kind_of(VersionedDocument)
s.should_not have_slot(:name)
end
end
end

Expand All @@ -94,33 +95,34 @@
end

it "should fetch head versions of associated documents if association owner wasn't saved when associated doc were created and now it is not a head" do
pending
playlist = Playlist.new
song = Song.create!(:playlist => playlist)
song.name = "My song"
song.save!
playlist.save!
playlist.name = "My playlist"
playlist.save!
playlist = playlist.versions.previous
playlist.songs.should == [song]
playlist.songs.each do |s|
s.should be_head
s.should_not be_a_kind_of(VersionedDocument)
s.should have_slot(:name)
pending do
playlist = Playlist.new
song = Song.create!(:playlist => playlist)
song.name = "My song"
song.save!
playlist.save!
playlist.name = "My playlist"
playlist.save!
playlist = playlist.versions.previous
playlist.songs.should == [song]
playlist.songs.each do |s|
s.should be_head
s.should_not be_a_kind_of(VersionedDocument)
s.should have_slot(:name)
end
end
end

it "should be able to filter associated documents" do
playlist = Playlist.create!
rock_song = Song.create!(:playlist => playlist, :genre => 'Rock')
pop_song = Song.create!(:playlist => playlist, :genre => 'Pop')
pending("filtering is not ready") do
playlist.songs.find(:genre => 'Rock').should == [rock_song]
playlist.songs.find(:genre => 'Pop').should == [pop_song]
end
playlist.songs.find(:genre => 'Rock').should == [rock_song]
playlist.songs.find(:genre => 'Pop').should == [pop_song]
end
end

it "should be able to instantiate new document with #new" do
playlist = Playlist.create!
song = playlist.songs.new(:name => 'My song')
Expand Down Expand Up @@ -153,11 +155,11 @@
song.should_not be_new
playlist.songs.should == [song]
end

end

describe "Namespace::Playlist.has_many :songs association" do

before(:each) do
setup_default_store
setup_index
Expand All @@ -171,18 +173,18 @@
end
Namespace::Song = Meta.new
end

it "should convert :songs to Song and Playlist to playlist to compute foreign reference slot name" do
playlist = Namespace::Playlist.create!
song = Namespace::Song.create!(:playlist => playlist)
playlist.songs.should == [song]
end


end

describe "Playlist.has_many :rock_songs, :through => :songs, :conditions => { :genre => 'Rock' } association" do

before(:each) do
setup_default_store
setup_index
Expand All @@ -193,7 +195,7 @@
end
Song = Meta.new
end

it "should convert :songs to Song and Playlist to playlist to compute foreign reference slot name" do
playlist = Playlist.create!
rock_song = Song.create!(:playlist => playlist, :genre => 'Rock')
Expand All @@ -202,11 +204,11 @@
playlist.rock_songs.should == [rock_song]
end
end

end

describe "Playlist.has_many :songs, :foreign_reference => :belongs_to_playlist association" do

before(:each) do
setup_default_store
setup_index
Expand All @@ -217,17 +219,17 @@
end
Song = Meta.new
end

it "should convert :songs to Song and use foreign_reference to compute foreign reference slot name" do
playlist = Playlist.create!
song = Song.create!(:belongs_to_playlist => playlist)
playlist.songs.should == [song]
end

end

describe "Playlist.has_many :all_songs, :through => :songs association" do

before(:each) do
setup_default_store
setup_index
Expand All @@ -239,17 +241,17 @@
end
Song = Meta.new
end

it "should use through's :songs to find out Song and convert Playlist to playlist" do
playlist = Playlist.create!
song = Song.create!(:playlist => playlist)
playlist.all_songs.should == [song]
end

end

describe "Playlist.has_many :authors, :through => [:songs,:authors] association" do

before(:each) do
setup_default_store
setup_index
Expand All @@ -261,7 +263,7 @@
end
Song = Meta.new
end

it "should use through's :songs to find out Song and convert Playlist to playlist" do
playlist = Playlist.create!
song = Song.create!(:playlist => playlist, :author => "John Doe")
Expand All @@ -273,7 +275,7 @@
song = Song.create!(:playlist => playlist)
playlist.authors.should be_empty
end

end

describe "Playlist.has_many :songs, :extend => MyExt association" do
Expand All @@ -290,12 +292,12 @@
end
Song = Meta.new
end

it "should extend result with MyExt" do
playlist = Playlist.create!
playlist.songs.should be_a_kind_of(MyExt)
end

end

describe "Playlist.has_many :songs do .. end association" do
Expand All @@ -312,11 +314,11 @@ def some_method
end
Song = Meta.new
end

it "should extend result with given block" do
playlist = Playlist.create!
playlist.songs.should be_a_kind_of(Playlist::HasManySongs)
playlist.songs.should respond_to(:some_method)
end

end

0 comments on commit 6c34648

Please sign in to comment.