This repository was archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpuret_test.rb
135 lines (116 loc) · 3.52 KB
/
puret_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require 'test_helper'
describe 'puret' do
before do
setup_db
I18n.locale = I18n.default_locale = :en
Post.create(:title => 'English title')
end
after do
teardown_db
end
it "should have the correct database setup" do
assert_equal 1, Post.count
end
it "allow translation" do
I18n.locale = :de
Post.first.update_attribute :title, 'Deutscher Titel'
assert_equal 'Deutscher Titel', Post.first.title
I18n.locale = :en
assert_equal 'English title', Post.first.title
end
it "assert fallback to default locale" do
post = Post.first
I18n.locale = :sv
post.title = 'Svensk titel'
I18n.locale = :en
assert_equal 'English title', post.title
I18n.locale = :de
assert_equal 'English title', post.title
end
it "assert fallback to saved default locale defined on instance" do
post = Post.first
def post.default_locale() :sv; end
assert_equal :sv, post.puret_default_locale
I18n.locale = :sv
post.title = 'Svensk titel'
post.save!
I18n.locale = :en
assert_equal 'English title', post.title
I18n.locale = :de
assert_equal 'Svensk titel', post.title
end
it "assert fallback to saved default locale defined on class level" do
post = Post.first
def Post.default_locale() :sv; end
assert_equal :sv, post.puret_default_locale
I18n.locale = :sv
post.title = 'Svensk titel'
post.save!
I18n.locale = :en
assert_equal 'English title', post.title
I18n.locale = :de
assert_equal 'Svensk titel', post.title
end
it "assert separate fallback for each attribute" do
post = Post.first
I18n.locale = :de
post.text = 'Deutsche text'
post.save!
I18n.locale = :sv
assert_equal 'English title', post.title
assert_equal 'Deutsche text', post.text
end
it "post has_many translations" do
assert_equal PostTranslation, Post.first.translations.first.class
end
it "translations are deleted when parent is destroyed" do
I18n.locale = :de
Post.first.update_attribute :title, 'Deutscher Titel'
assert_equal 2, PostTranslation.count
Post.destroy_all
assert_equal 0, PostTranslation.count
end
it 'validates_presence_of should work' do
post = Post.new
assert_equal false, post.valid?
post.title = 'English title'
assert_equal true, post.valid?
end
it 'temporary locale switch should not clear changes' do
I18n.locale = :de
post = Post.first
post.text = 'Deutscher Text'
assert !post.title.blank?
assert_equal 'Deutscher Text', post.text
end
it 'temporary locale switch should work like expected' do
post = Post.new
post.title = 'English title'
I18n.locale = :de
post.title = 'Deutscher Titel'
post.save
assert_equal 'Deutscher Titel', post.title
I18n.locale = :en
assert_equal 'English title', post.title
end
it 'translation model should validate presence of model' do
t = PostTranslation.new
t.valid?
refute_nil t.errors[:post]
end
it 'translation model should validate presence of locale' do
t = PostTranslation.new
t.valid?
refute_nil t.errors[:locale]
end
it 'translation model should validate uniqueness of locale in model scope' do
post = Post.first
t1 = PostTranslation.new :post => post, :locale => "de"
t1.save!
t2 = PostTranslation.new :post => post, :locale => "de"
refute_nil t2.errors[:locale]
end
it 'model should provide attribute_before_type_cast' do
assert_equal Post.first.title, Post.first.title_before_type_cast
end
end