Permalink
Browse files

More tests (STI, AR::Base.find)

  • Loading branch information...
1 parent 9c16ff8 commit 4d540e322518ba1dfc3972a9d08cb996e48ddda5 Elad Meidar committed Sep 13, 2009
@@ -14,7 +14,10 @@ def index
# GET /users/1.xml
def show
@user = User.find(params[:id])
-
+ @user_by_name = User.find_by_name("elad")
+ @users_by_name = User.find_all_by_name("elad")
+ @users_by_name_and_company_id = User.find_all_by_name_and_company_id("elad",1)
+
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
View
@@ -0,0 +1,3 @@
+class SuperUser < User
+ set_inheritance_column 'sub_type'
+end
@@ -0,0 +1,9 @@
+class AddInheritanceColumnToUser < ActiveRecord::Migration
+ def self.up
+ add_column :users, :sub_type, :string
+ end
+
+ def self.down
+ remove_column :users, :sub_type
+ end
+end
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090912232753) do
+ActiveRecord::Schema.define(:version => 20090913003404) do
create_table "companies", :force => true do |t|
t.string "name"
@@ -39,6 +39,7 @@
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "sub_type"
end
end
View
@@ -9,3 +9,10 @@ two:
company_id:
name: MyString
works_for_company_id:
+
+three:
+ company_id:
+ sub_type: SuperUser
+ name: MyString
+ works_for_company_id:
+
@@ -32,15 +32,14 @@ def self.check_for_indexes(migration_format = false)
model_classes.each do |class_name|
- # check if this is an STI child instance
- if class_name.base_class.name != class_name.name && class_name.column_names.include?(class_name.base_class.inheritance_column)
+ # check if this is an STI child instance
+ if class_name.base_class.name != class_name.name && (class_name.column_names.include?(class_name.base_class.inheritance_column) || class_name.column_names.include?(class_name.inheritance_column))
# add the inharitance column on the parent table
-
if !(migration_format)
- @indexes_required[class_name.base_class.table_name] += [class_name.base_class.inheritance_column].sort unless @indexes_required[class_name.base_class.table_name].include?([class_name.base_class.inheritance_column].sort)
+ @indexes_required[class_name.base_class.table_name] += [class_name.inheritance_column].sort unless @indexes_required[class_name.base_class.table_name].include?([class_name.inheritance_column].sort)
else
# index migration for STI should require both the primary key and the inheritance_column in a composite index.
- @index_migrations[class_name.base_class.table_name] += [[class_name.base_class.inheritance_column, class_name.base_class.primary_key]] unless @index_migrations[class_name.base_class.table_name].include?([class_name.base_class.inheritance_column].sort)
+ @index_migrations[class_name.base_class.table_name] += [[class_name.inheritance_column, class_name.base_class.primary_key].sort] unless @index_migrations[class_name.base_class.table_name].include?([class_name.base_class.inheritance_column].sort)
end
end
@@ -0,0 +1,29 @@
+require 'test_helper'
+
+class ActiveRecordFindsTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+
+ def setup
+ @rake_output = `rake db:show_me_ar_find_indexes`
+ end
+
+ test "should add index on :id on :courses, :companies and :users" do
+ assert_match /add_index :courses, :id/, @rake_output
+ assert_match /add_index :companies, :id/, @rake_output
+ assert_match /add_index :users, :id/, @rake_output
+
+ assert_match /remove_index :courses, :id/, @rake_output
+ assert_match /remove_index :companies, :id/, @rake_output
+ assert_match /remove_index :users, :id/, @rake_output
+ end
+
+ test "should add indexes for :name and [:company_id, :name] for :users due to finds on UsersController#show" do
+ assert_match /add_index :users, :name/, @rake_output
+ assert_match /add_index :users, \[:company_id, :name\]/, @rake_output
+
+ assert_match /remove_index :users, :name/, @rake_output
+ assert_match /remove_index :users, :column =\> \[:company_id, :name\]/, @rake_output
+
+ end
+
+end
@@ -0,0 +1,15 @@
+require 'test_helper'
+
+class SingleTableInheritanceTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+
+ def setup
+ @rake_output = `rake db:show_me_a_migration`
+ end
+
+ test "should add an index on [:id, :sub_type] in the :users table when there's an STI relationship (SuperUser) with a custom inheritance_column" do
+ assert_match /add_index :users, [:id, :sub_type]/, @rake_output
+ assert_match /remove_index :users, :column =\> \[:id, :sub_type\]/, @rake_output
+ end
+
+end

0 comments on commit 4d540e3

Please sign in to comment.