Skip to content

Commit

Permalink
[mssql] add a has_many :though tests + re-arrange tests a bit (closes j…
Browse files Browse the repository at this point in the history
…ruby#171)

NOTE: a failure commit fd27edf fixes was only reproducable with 3.0 and 3.1 while running in JRuby --1.8 mode
  • Loading branch information
kares committed Feb 13, 2013
1 parent fd27edf commit a5f05f8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 78 deletions.
76 changes: 13 additions & 63 deletions test/has_many_through.rb
Original file line number Diff line number Diff line change
@@ -1,86 +1,35 @@
class CreateRbac < ActiveRecord::Migration
def self.up
create_table :role_assignments do |t|
t.column :role_id, :integer
t.column :user_id, :integer
end

create_table :roles do |t|
t.column :name, :string
t.column :description, :string
end

create_table :permission_groups do |t|
t.column :right_id, :integer, :null => false
t.column :role_id, :integer, :null => false
end

create_table :rights do |t|
t.column :name, :string
t.column :controller_name, :string
t.column :actions, :string
t.column :hours, :float, :null => false
end
end

def self.down
drop_table :role_assignments
drop_table :roles
drop_table :permission_groups
drop_table :rights
end
end

class Right < ActiveRecord::Base
has_many :permission_groups, :dependent => :destroy
has_many :roles, :through => :permission_groups
end

class Role < ActiveRecord::Base
has_many :permission_groups, :dependent => :destroy
has_many :rights, :through => :permission_groups
has_many :role_assignments, :dependent => :destroy

def has_right?(right)
rights.include? right
end
end

class PermissionGroup < ActiveRecord::Base
belongs_to :right
belongs_to :role
end

class RoleAssignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
require 'models/rights_and_roles'

module HasManyThroughMethods

def setup
CreateRbac.up
CreateRightsAndRoles.up
end

def teardown
CreateRbac.down
CreateRightsAndRoles.down
end

def test_has_many_through
admin_role = Role.create( {:name => "Administrator", :description => "System defined super user - access to right and role management."} )
admin_role = Role.create :name => "Administrator",
:description => "System defined super user - access to right and role management."
admin_role.save!

assert_equal(0, admin_role.rights.sum(:hours))

role_rights = Right.create( {:name => "Administrator - Full Access To Roles", :actions => "*", :controller_name => "Admin::RolesController", :hours => 0} )
right_rights = Right.create( {:name => "Administrator - Full Access To Rights", :actions => "*", :controller_name => "Admin::RightsController", :hours => 1.5} )
role_rights = Right.create :name => "Administrator - Full Access To Roles",
:actions => "*", :controller_name => "Admin::RolesController", :hours => 0
right_rights = Right.create :name => "Administrator - Full Access To Rights",
:actions => "*", :controller_name => "Admin::RightsController", :hours => 1.5

admin_role.rights << role_rights
admin_role.rights << right_rights
admin_role.save!

assert_equal(1.5, admin_role.rights.sum(:hours))

rights_only_role = Role.create!(:name => "Rights Manager", :description => "access to rights management")
rights_only_role = Role.create! :name => "Rights Manager",
:description => "access to rights management"
rights_only_role.rights << right_rights
rights_only_role.save!
rights_only_role.reload
Expand All @@ -90,4 +39,5 @@ def test_has_many_through
assert admin_role.reload.has_right?(role_rights)
assert ! rights_only_role.has_right?(role_rights)
end

end
57 changes: 57 additions & 0 deletions test/models/rights_and_roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class CreateRightsAndRoles < ActiveRecord::Migration
def self.up
create_table :role_assignments do |t|
t.column :role_id, :integer
t.column :user_id, :integer
end

create_table :roles do |t|
t.column :name, :string
t.column :description, :string
end

create_table :permission_groups do |t|
t.column :right_id, :integer, :null => false
t.column :role_id, :integer, :null => false
end

create_table :rights do |t|
t.column :name, :string
t.column :controller_name, :string
t.column :actions, :string
t.column :hours, :float, :null => false
end
end

def self.down
drop_table :role_assignments
drop_table :roles
drop_table :permission_groups
drop_table :rights
end
end

class Right < ActiveRecord::Base
has_many :permission_groups, :dependent => :destroy
has_many :roles, :through => :permission_groups
end

class Role < ActiveRecord::Base
has_many :permission_groups, :dependent => :destroy
has_many :rights, :through => :permission_groups
has_many :role_assignments, :dependent => :destroy

def has_right?(right)
rights.include? right
end
end

class PermissionGroup < ActiveRecord::Base
belongs_to :right
belongs_to :role
end

class RoleAssignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
14 changes: 0 additions & 14 deletions test/mssql_null_test.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/mssql_serialize_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'db/mssql'
require 'serialize'

class MsSQLSerializeTest < Test::Unit::TestCase
class MSSQLSerializeTest < Test::Unit::TestCase
include SerializeTestMethods
end
13 changes: 13 additions & 0 deletions test/mssql_simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,23 @@ def test_change_column_nullability
assert(!title_column.null)
end

[nil, "NULL", "null", "(null)", "(NULL)"].each_with_index do |v, i|
define_method "test_null_#{i}" do
entry = Entry.create!(:title => v, :content => v)
entry = Entry.find(entry.id)
assert_equal [v, v], [entry.title, entry.content], "writing #{v.inspect} " +
"should read back as #{v.inspect} for both string and text columns"
end
end

# ACTIVERECORD_JDBC-124
def test_model_does_not_have_row_num_column
entry = Entry.first
assert_false entry.attributes.keys.include?("_row_num")
assert_false entry.respond_to?(:_row_num)
end
end

class MSSQLHasManyThroughTest < Test::Unit::TestCase
include HasManyThroughMethods
end

0 comments on commit a5f05f8

Please sign in to comment.