Skip to content

Commit

Permalink
Adding has_many :through tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagopradi committed Jun 22, 2010
1 parent 0d3cb89 commit b222837
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 44 deletions.
13 changes: 13 additions & 0 deletions Rakefile
Expand Up @@ -135,6 +135,19 @@ namespace :db do
u.integer :role_id
u.integer :permission_id
end

ActiveRecord::Base.using(shard_symbol).connection.create_table(:assignments) do |u|
u.integer :programmer_id
u.integer :project_id
end

ActiveRecord::Base.using(shard_symbol).connection.create_table(:programmers) do |u|
u.string :name
end

ActiveRecord::Base.using(shard_symbol).connection.create_table(:projects) do |u|
u.string :name
end
end
end

Expand Down
6 changes: 5 additions & 1 deletion lib/octopus/association_collection.rb
Expand Up @@ -5,7 +5,11 @@ def should_wrap_the_connection?

def count(column_name = nil, options = {})
if @reflection.options[:counter_sql]
@reflection.klass.count_by_sql(@counter_sql)
if should_wrap_the_connection?
@owner.using(@owner.current_shard) { @reflection.klass.count_by_sql(@counter_sql) }
else
@reflection.klass.count_by_sql(@counter_sql)
end
else
column_name, options = nil, column_name if column_name.is_a?(Hash)

Expand Down
15 changes: 15 additions & 0 deletions spec/database_models.rb
Expand Up @@ -36,4 +36,19 @@ class Role < ActiveRecord::Base

class Permission < ActiveRecord::Base
has_and_belongs_to_many :roles, :before_remove => :set_connection, :before_add => :set_connection
end

class Assignment < ActiveRecord::Base
belongs_to :programmer
belongs_to :project
end

class Programmer < ActiveRecord::Base
has_many :assignments, :before_remove => :set_connection, :before_add => :set_connection
has_many :projects, :through => :assignments, :before_remove => :set_connection, :before_add => :set_connection
end

class Project < ActiveRecord::Base
has_many :assignments, :before_remove => :set_connection, :before_add => :set_connection
has_many :programmers, :through => :assignments, :before_remove => :set_connection, :before_add => :set_connection
end
200 changes: 158 additions & 42 deletions spec/octopus/association_spec.rb
Expand Up @@ -90,7 +90,7 @@
@permission_brazil.role_ids.should == [new_brazil_role.id]
@permission_brazil.roles().should == [new_brazil_role]
end

it "should works for build method" do
new_brazil_role = Role.using(:brazil).create!(:name => "Brazil Role")
c = new_brazil_role.permissions.create(:name => "new Permission")
Expand All @@ -99,7 +99,7 @@
c.roles().should == [new_brazil_role]
new_brazil_role.permissions.should == [c]
end

describe "it should works when using" do
before(:each) do
@permission_brazil_2 = Permission.using(:brazil).create!(:name => "Brazil Item 2")
Expand All @@ -115,36 +115,6 @@
@permission_brazil_2.update_attribute(:role_ids, [@role.id])
@permission_brazil_2.roles.to_set.should == [@role].to_set
end

it "increment" do
u = User.using(:brazil).create!(:name => "Teste", :number => 10)
u = User.using(:brazil).find_by_number(10)
u.increment(:number)
u.save()
u = User.using(:brazil).find_by_number(11).should_not be_nil
end

it "increment!" do
u = User.using(:brazil).create!(:name => "Teste", :number => 10)
u = User.using(:brazil).find_by_number(10)
u.increment!(:number)
u = User.using(:brazil).find_by_number(11).should_not be_nil
end

it "toggle" do
u = User.using(:brazil).create!(:name => "Teste", :admin => false)
u = User.using(:brazil).find_by_name('Teste')
u.toggle(:admin)
u.save()
u = User.using(:brazil).find_by_name('Teste').admin.should be_true
end

it "toggle!" do
u = User.using(:brazil).create!(:name => "Teste", :admin => false)
u = User.using(:brazil).find_by_name('Teste')
u.toggle!(:admin)
u = User.using(:brazil).find_by_name('Teste').admin.should be_true
end

it "<<" do
@permission_brazil_2.roles << @role
Expand All @@ -153,23 +123,23 @@
@permission_brazil_2.reload
@permission_brazil_2.roles.to_set.should == [@role].to_set
end

it "build" do
role = @permission_brazil_2.roles.build(:name => "Builded Role")
@permission_brazil_2.save()
@permission_brazil_2.roles.to_set.should == [role].to_set
end

it "create" do
role = @permission_brazil_2.roles.create(:name => "Builded Role")
@permission_brazil_2.roles.to_set.should == [role].to_set
end

it "create" do
role = @permission_brazil_2.roles.create!(:name => "Builded Role")
@permission_brazil_2.roles.to_set.should == [role].to_set
end

it "count" do
@permission_brazil_2.roles.count.should == 0
role = @permission_brazil_2.roles.create(:name => "Builded Role")
Expand Down Expand Up @@ -200,7 +170,7 @@
role = @permission_brazil_2.roles.create(:name => "Builded Role")
@permission_brazil_2.roles.empty?.should be_false
end

it "delete_all" do
role = @permission_brazil_2.roles.create(:name => "Builded Role")
@permission_brazil_2.roles.empty?.should be_false
Expand All @@ -214,7 +184,7 @@
@permission_brazil_2.roles.destroy_all
@permission_brazil_2.roles.empty?.should be_true
end

it "find" do
role = @permission_brazil_2.roles.create(:name => "Builded Role")
@permission_brazil_2.roles.find(:first).should == role
Expand All @@ -228,14 +198,14 @@
@permission_brazil_2.roles.destroy_all
@permission_brazil_2.roles.exists?(role).should be_false
end

it "clear" do
role = @permission_brazil_2.roles.create(:name => "Builded Role")
@permission_brazil_2.roles.empty?.should be_false
@permission_brazil_2.roles.clear
@permission_brazil_2.roles.empty?.should be_true
end

it "delete" do
role = @permission_brazil_2.roles.create(:name => "Builded Role")
@permission_brazil_2.roles.empty?.should be_false
Expand All @@ -249,8 +219,154 @@
end

describe "when you have has_many :through" do
it "should be implemented" do
pending()
before(:each) do
@programmer = Programmer.using(:brazil).create!(:name => "Thiago")
@project = Project.using(:brazil).create!(:name => "RubySoc")
@project2 = Project.using(:brazil).create!(:name => "Cobol Application")
@programmer.projects << @project
@programmer.save()
end

it "should find all models in the specified shard" do
@programmer.project_ids().should == [ @project.id]
@programmer.projects().should == [@project]
end


it "should update the attribute for the item" do
new_brazil_programmer = Programmer.using(:brazil).create!(:name => "Joao")
@project.programmers = [new_brazil_programmer]
@project.programmers.should == [new_brazil_programmer]
@project.save()
@project.reload
@project.programmer_ids.should == [new_brazil_programmer.id]
@project.programmers().should == [new_brazil_programmer]
end

it "should works for create method" do
new_brazil_programmer = Programmer.using(:brazil).create!(:name => "Joao")
c = new_brazil_programmer.projects.create(:name => "new Project")
c.save()
new_brazil_programmer.save()
c.programmers().should == [new_brazil_programmer]
new_brazil_programmer.projects.should == [c]
end

describe "it should works when using" do
before(:each) do
@new_brazil_programmer = Programmer.using(:brazil).create!(:name => "Jose")
@project = Project.using(:brazil).create!(:name => "VB Application :-(")
end

it "update_attributes" do
@new_brazil_programmer.update_attributes(:project_ids => [@project.id])
@new_brazil_programmer.projects.to_set.should == [@project].to_set
end

it "update_attribute" do
@new_brazil_programmer.update_attribute(:project_ids, [@project.id])
@new_brazil_programmer.projects.to_set.should == [@project].to_set
end

it "<<" do
@new_brazil_programmer.projects << @project
@project.save()
@new_brazil_programmer.save()
@new_brazil_programmer.reload
@new_brazil_programmer.projects.to_set.should == [@project].to_set
end

it "build" do
role = @new_brazil_programmer.projects.build(:name => "New VB App :-/")
@new_brazil_programmer.save()
@new_brazil_programmer.projects.to_set.should == [role].to_set
end

it "create" do
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.to_set.should == [role].to_set
end

it "create" do
role = @new_brazil_programmer.projects.create!(:name => "New VB App :-/")
@new_brazil_programmer.projects.to_set.should == [role].to_set
end

it "count" do
@new_brazil_programmer.projects.count.should == 0
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.count.should == 1
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.count.should == 2
end

it "size" do
@new_brazil_programmer.projects.size.should == 0
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.size.should == 1
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.size.should == 2
end

it "length" do
@new_brazil_programmer.projects.length.should == 0
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.length.should == 1
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.length.should == 2
end


it "empty?" do
@new_brazil_programmer.projects.empty?.should be_true
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.empty?.should be_false
end

it "delete_all" do
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.empty?.should be_false
@new_brazil_programmer.projects.delete_all
@new_brazil_programmer.projects.empty?.should be_true
end

it "destroy_all" do
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.empty?.should be_false
@new_brazil_programmer.projects.destroy_all
@new_brazil_programmer.projects.empty?.should be_true
end

it "find" do
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.find(:first).should == role
@new_brazil_programmer.projects.destroy_all
@new_brazil_programmer.projects.find(:first).should be_nil
end

it "exists?" do
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.exists?(role).should be_true
@new_brazil_programmer.projects.destroy_all
@new_brazil_programmer.projects.exists?(role).should be_false
end

it "clear" do
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.empty?.should be_false
@new_brazil_programmer.projects.clear
@new_brazil_programmer.projects.empty?.should be_true
end

it "delete" do
role = @new_brazil_programmer.projects.create(:name => "New VB App :-/")
@new_brazil_programmer.projects.empty?.should be_false
@new_brazil_programmer.projects.delete(role)
@new_brazil_programmer.reload
@project.reload
@project.programmers.should == []
@new_brazil_programmer.projects.should == []
end
end
end

Expand Down
32 changes: 32 additions & 0 deletions spec/octopus/model_spec.rb
Expand Up @@ -102,7 +102,39 @@
User.using(:alone_shard).find(:all).should == []
end
end

describe "AR basic methods" do
it "increment" do
u = User.using(:brazil).create!(:name => "Teste", :number => 10)
u = User.using(:brazil).find_by_number(10)
u.increment(:number)
u.save()
u = User.using(:brazil).find_by_number(11).should_not be_nil
end

it "increment!" do
u = User.using(:brazil).create!(:name => "Teste", :number => 10)
u = User.using(:brazil).find_by_number(10)
u.increment!(:number)
u = User.using(:brazil).find_by_number(11).should_not be_nil
end

it "toggle" do
u = User.using(:brazil).create!(:name => "Teste", :admin => false)
u = User.using(:brazil).find_by_name('Teste')
u.toggle(:admin)
u.save()
u = User.using(:brazil).find_by_name('Teste').admin.should be_true
end

it "toggle!" do
u = User.using(:brazil).create!(:name => "Teste", :admin => false)
u = User.using(:brazil).find_by_name('Teste')
u.toggle!(:admin)
u = User.using(:brazil).find_by_name('Teste').admin.should be_true
end
end

describe "#replicated_model method" do
it "should be replicated" do
using_enviroment :production_replicated do
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -22,7 +22,7 @@

def clean_all_shards()
ActiveRecord::Base.using(:master).connection.shards.keys.each do |shard_symbol|
['schema_migrations', 'users', 'clients', 'cats', 'items', 'keyboards', 'computers', 'permissions_roles', 'roles', 'permissions'].each do |tables|
['schema_migrations', 'users', 'clients', 'cats', 'items', 'keyboards', 'computers', 'permissions_roles', 'roles', 'permissions', 'assignments', 'projects', 'programmers'].each do |tables|
ActiveRecord::Base.using(shard_symbol).connection.execute("DELETE FROM #{tables};")
end
end
Expand Down

0 comments on commit b222837

Please sign in to comment.