-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtest_helper.rb
82 lines (66 loc) · 1.88 KB
/
test_helper.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
require 'minitest/autorun'
require 'json_translate'
require 'database_cleaner'
DatabaseCleaner.strategy = :transaction
I18n.available_locales = [:en, :fr]
class Post < ActiveRecord::Base
has_many :tags
translates :title, :body_1
scope :tagged, -> (tag_title) { joins(:tags).merge(Tag.with_title_translation(tag_title)) }
end
class Tag < ActiveRecord::Base
belongs_to :post
translates :title
end
class PostDetailed < Post
translates :comment, allow_blank: true
end
class JSONTranslate::Test < Minitest::Test
class << self
def prepare_database
create_database
create_table
end
private
def adapter
@adapter ||= ENV['DB'] || 'postgres'
end
def db_config
@db_config ||= begin
filepath = File.join('test', 'database.yml')
YAML.load_file(filepath)[adapter]
end
end
def establish_connection(config)
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection
end
def create_database
system_config = db_config
connection = establish_connection(system_config)
connection.create_database(db_config['database']) rescue nil
end
def create_table
column_type = adapter == 'mysql' ? 'json' : 'jsonb'
connection = establish_connection(db_config)
connection.create_table(:posts, :force => true) do |t|
t.column :title_translations, column_type
t.column :body_1_translations, column_type
t.column :comment_translations, column_type
end
connection.create_table(:tags, :force => true) do |t|
t.column :title_translations, column_type
t.column :post_id, :integer
end
end
end
prepare_database
def setup
I18n.available_locales = ['en', 'en-US', 'fr']
I18n.config.enforce_available_locales = true
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
end