1 require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
2 require 'active_record'
3 require 'globalize/model/active_record'
5 # Hook up model translation
6 ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
9 require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
11 class MigrationTest < ActiveSupport::TestCase
13 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb'))
16 test 'globalize table added' do
17 assert !Post.connection.table_exists?( :post_translations )
18 Post.create_translation_table! :subject => :string, :content => :text
19 assert Post.connection.table_exists?( :post_translations )
20 columns = Post.connection.columns( :post_translations )
21 assert locale = columns.detect {|c| c.name == 'locale' }
22 assert_equal :string, locale.type
23 assert subject = columns.detect {|c| c.name == 'subject' }
24 assert_equal :string, subject.type
25 assert content = columns.detect {|c| c.name == 'content' }
26 assert_equal :text, content.type
27 assert post_id = columns.detect {|c| c.name == 'post_id' }
28 assert_equal :integer, post_id.type
29 assert created_at = columns.detect {|c| c.name == 'created_at' }
30 assert_equal :datetime, created_at.type
31 assert updated_at = columns.detect {|c| c.name == 'updated_at' }
32 assert_equal :datetime, updated_at.type
35 test 'globalize table dropped' do
36 assert !Post.connection.table_exists?( :post_translations )
37 Post.create_translation_table! :subject => :string, :content => :text
38 assert Post.connection.table_exists?( :post_translations )
39 Post.drop_translation_table!
40 assert !Post.connection.table_exists?( :post_translations )
43 test 'exception on untranslated field inputs' do
44 assert_raise Globalize::Model::UntranslatedMigrationField do
45 Post.create_translation_table! :subject => :string, :content => :text, :bogus => :string
49 test 'exception on missing field inputs' do
50 assert_raise Globalize::Model::MigrationMissingTranslatedField do
51 Post.create_translation_table! :content => :text
55 test 'exception on bad input type' do
56 assert_raise Globalize::Model::BadMigrationFieldType do
57 Post.create_translation_table! :subject => :string, :content => :integer
61 test 'create_translation_table! should not be called on non-translated models' do
62 assert_raise NoMethodError do
63 Blog.create_translation_table! :name => :string
67 test 'drop_translation_table! should not be called on non-translated models' do
68 assert_raise NoMethodError do
69 Blog.drop_translation_table!