summaryrefslogtreecommitdiff
path: root/vendor/plugins/globalize2/test
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-02-25 22:37:22 +0100
committerhukl <contact@smyck.org>2009-02-25 22:37:22 +0100
commitf4c73763e5b3d65f3377fb1238a94122a1c272a4 (patch)
tree8f6b276f9432e31ae0fe7bffe010e680081a1e0a /vendor/plugins/globalize2/test
parent19b07ff63c626f76ad512b95ddbf16cddd2bf855 (diff)
updated globalize2
Diffstat (limited to 'vendor/plugins/globalize2/test')
-rw-r--r--vendor/plugins/globalize2/test/data/post.rb7
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/migration_test.rb2
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb75
-rw-r--r--vendor/plugins/globalize2/test/model/active_record/translated_test.rb85
-rw-r--r--vendor/plugins/globalize2/test/test_helper.rb4
5 files changed, 169 insertions, 4 deletions
diff --git a/vendor/plugins/globalize2/test/data/post.rb b/vendor/plugins/globalize2/test/data/post.rb
index 20a2495d..fdfb7c0e 100644
--- a/vendor/plugins/globalize2/test/data/post.rb
+++ b/vendor/plugins/globalize2/test/data/post.rb
@@ -6,3 +6,10 @@ end
6class Blog < ActiveRecord::Base 6class Blog < ActiveRecord::Base
7 has_many :posts, :order => 'id ASC' 7 has_many :posts, :order => 'id ASC'
8end 8end
9
10class Parent < ActiveRecord::Base
11 translates :content
12end
13
14class Child < Parent
15end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
index 09804c7d..c539fb6e 100644
--- a/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
+++ b/vendor/plugins/globalize2/test/model/active_record/migration_test.rb
@@ -10,7 +10,7 @@ require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
10 10
11class MigrationTest < ActiveSupport::TestCase 11class MigrationTest < ActiveSupport::TestCase
12 def setup 12 def setup
13 reset_db! 'no_globalize_schema' 13 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'no_globalize_schema.rb'))
14 end 14 end
15 15
16 test 'globalize table added' do 16 test 'globalize table added' do
diff --git a/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb
new file mode 100644
index 00000000..14d7d0f9
--- /dev/null
+++ b/vendor/plugins/globalize2/test/model/active_record/sti_translated_test.rb
@@ -0,0 +1,75 @@
1require File.join( File.dirname(__FILE__), '..', '..', 'test_helper' )
2require 'active_record'
3require 'globalize/model/active_record'
4
5# Hook up model translation
6ActiveRecord::Base.send(:include, Globalize::Model::ActiveRecord::Translated)
7
8# Load Post model
9require File.join( File.dirname(__FILE__), '..', '..', 'data', 'post' )
10
11class StiTranslatedTest < ActiveSupport::TestCase
12 def setup
13 I18n.locale = :'en-US'
14 I18n.fallbacks.clear
15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
16 end
17
18 def teardown
19 I18n.fallbacks.clear
20 end
21
22 test "works with simple dynamic finders" do
23 foo = Child.create :content => 'foo'
24 Child.create :content => 'bar'
25 child = Child.find_by_content('foo')
26 assert_equal foo, child
27 end
28
29 test 'change attribute on globalized model' do
30 child = Child.create :content => 'foo'
31 assert_equal [], child.changed
32 child.content = 'bar'
33 assert_equal [ 'content' ], child.changed
34 child.content = 'baz'
35 assert_member 'content', child.changed
36 end
37
38 test 'change attribute on globalized model after locale switching' do
39 child = Child.create :content => 'foo'
40 assert_equal [], child.changed
41 child.content = 'bar'
42 I18n.locale = :de
43 assert_equal [ 'content' ], child.changed
44 end
45
46 test 'fallbacks with lots of locale switching' do
47 I18n.fallbacks.map :'de-DE' => [ :'en-US' ]
48 child = Child.create :content => 'foo'
49
50 I18n.locale = :'de-DE'
51 assert_equal 'foo', child.content
52
53 I18n.locale = :'en-US'
54 child.update_attribute :content, 'bar'
55
56 I18n.locale = :'de-DE'
57 assert_equal 'bar', child.content
58 end
59
60 test "saves all locales, even after locale switching" do
61 child = Child.new :content => 'foo'
62 I18n.locale = 'de-DE'
63 child.content = 'bar'
64 I18n.locale = 'he-IL'
65 child.content = 'baz'
66 child.save
67 I18n.locale = 'en-US'
68 child = Child.first
69 assert_equal 'foo', child.content
70 I18n.locale = 'de-DE'
71 assert_equal 'bar', child.content
72 I18n.locale = 'he-IL'
73 assert_equal 'baz', child.content
74 end
75end \ No newline at end of file
diff --git a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb
index ec507e03..6bb7b26a 100644
--- a/vendor/plugins/globalize2/test/model/active_record/translated_test.rb
+++ b/vendor/plugins/globalize2/test/model/active_record/translated_test.rb
@@ -12,7 +12,8 @@ class TranslatedTest < ActiveSupport::TestCase
12 def setup 12 def setup
13 I18n.locale = :'en-US' 13 I18n.locale = :'en-US'
14 I18n.fallbacks.clear 14 I18n.fallbacks.clear
15 reset_db! 15 reset_db! File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'data', 'schema.rb'))
16 ActiveRecord::Base.locale = nil
16 end 17 end
17 18
18 def teardown 19 def teardown
@@ -92,6 +93,13 @@ class TranslatedTest < ActiveSupport::TestCase
92 assert_equal 'baz', Post.first.subject 93 assert_equal 'baz', Post.first.subject
93 end 94 end
94 95
96 test "update_attributes failure" do
97 post = Post.create :subject => 'foo', :content => 'bar'
98 assert !post.update_attributes( { :subject => '' } )
99 assert_nil post.reload.attributes['subject']
100 assert_equal 'foo', post.subject
101 end
102
95 test "validates presence of :subject" do 103 test "validates presence of :subject" do
96 post = Post.new 104 post = Post.new
97 assert !post.save 105 assert !post.save
@@ -255,6 +263,81 @@ class TranslatedTest < ActiveSupport::TestCase
255 I18n.locale = :'de-DE' 263 I18n.locale = :'de-DE'
256 assert_equal 'bar', post.subject 264 assert_equal 'bar', post.subject
257 end 265 end
266
267 test 'reload' do
268 post = Post.create :subject => 'foo', :content => 'bar'
269 post.subject = 'baz'
270 assert_equal 'foo', post.reload.subject
271 end
272
273 test 'complex writing and stashing' do
274 post = Post.create :subject => 'foo', :content => 'bar'
275 post.subject = nil
276 assert_nil post.subject
277 assert !post.valid?
278 end
279
280 test 'translated class locale setting' do
281 assert ActiveRecord::Base.respond_to?(:locale)
282 assert_equal :'en-US', I18n.locale
283 assert_equal :'en-US', ActiveRecord::Base.locale
284 I18n.locale = :de
285 assert_equal :de, I18n.locale
286 assert_equal :de, ActiveRecord::Base.locale
287 ActiveRecord::Base.locale = :es
288 assert_equal :de, I18n.locale
289 assert_equal :es, ActiveRecord::Base.locale
290 I18n.locale = :fr
291 assert_equal :fr, I18n.locale
292 assert_equal :es, ActiveRecord::Base.locale
293 end
294
295 test "untranslated class responds to locale" do
296 assert Blog.respond_to?(:locale)
297 end
298
299 test "to ensure locales in different classes are the same" do
300 ActiveRecord::Base.locale = :de
301 assert_equal :de, ActiveRecord::Base.locale
302 assert_equal :de, Parent.locale
303 Parent.locale = :es
304 assert_equal :es, ActiveRecord::Base.locale
305 assert_equal :es, Parent.locale
306 end
307
308 test "attribute saving goes by content locale and not global locale" do
309 ActiveRecord::Base.locale = :de
310 assert_equal :'en-US', I18n.locale
311 Post.create :subject => 'foo'
312 assert_equal :de, Post.first.globalize_translations.first.locale
313 end
314
315 test "attribute loading goes by content locale and not global locale" do
316 post = Post.create :subject => 'foo'
317 assert_equal :'en-US', ActiveRecord::Base.locale
318 ActiveRecord::Base.locale = :de
319 assert_equal :'en-US', I18n.locale
320 post.update_attribute :subject, 'foo [de]'
321 assert_equal 'foo [de]', Post.first.subject
322 ActiveRecord::Base.locale = :'en-US'
323 assert_equal 'foo', Post.first.subject
324 end
325
326 test "access content locale before setting" do
327 Globalize::Model::ActiveRecord::Translated::ActMethods.class_eval "remove_class_variable(:@@locale)"
328 assert_nothing_raised { ActiveRecord::Base.locale }
329 end
330
331 test "translated_locales" do
332 Post.locale = :de
333 post = Post.create :subject => 'foo'
334 Post.locale = :es
335 post.update_attribute :subject, 'bar'
336 Post.locale = :fr
337 post.update_attribute :subject, 'baz'
338 assert_equal [ :de, :es, :fr ], post.translated_locales
339 assert_equal [ :de, :es, :fr ], Post.first.translated_locales
340 end
258end 341end
259 342
260# TODO should validate_presence_of take fallbacks into account? maybe we need 343# TODO should validate_presence_of take fallbacks into account? maybe we need
diff --git a/vendor/plugins/globalize2/test/test_helper.rb b/vendor/plugins/globalize2/test/test_helper.rb
index 58b047d6..956b352f 100644
--- a/vendor/plugins/globalize2/test/test_helper.rb
+++ b/vendor/plugins/globalize2/test/test_helper.rb
@@ -7,14 +7,14 @@ require 'mocha'
7$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' ) 7$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )
8 8
9class ActiveSupport::TestCase 9class ActiveSupport::TestCase
10 def reset_db!( schema = 'schema' ) 10 def reset_db!( schema_path )
11 ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine 11 ::ActiveRecord::Migration.verbose = false # Quiet down the migration engine
12 ::ActiveRecord::Base.establish_connection({ 12 ::ActiveRecord::Base.establish_connection({
13 :adapter => 'sqlite3', 13 :adapter => 'sqlite3',
14 :dbfile => ':memory:' 14 :dbfile => ':memory:'
15 }) 15 })
16 ::ActiveRecord::Base.silence do 16 ::ActiveRecord::Base.silence do
17 load File.expand_path(File.join(File.dirname(__FILE__), 'data', schema + '.rb')) 17 load schema_path
18 end 18 end
19 end 19 end
20 20