-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathdata_migration_generator_spec.rb
More file actions
149 lines (116 loc) · 4.52 KB
/
data_migration_generator_spec.rb
File metadata and controls
149 lines (116 loc) · 4.52 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'spec_helper'
require 'rails/generators'
require 'rails/generators/migration'
require 'generators/data_migration/data_migration_generator'
describe DataMigrate::Generators::DataMigrationGenerator do
subject { DataMigrate::Generators::DataMigrationGenerator }
describe :next_migration_number do
it "next migration" do
Timecop.freeze("2016-12-03 22:15:26 -0800") do
if ActiveRecord.version >= Gem::Version.new('7.0')
expect(ActiveRecord).to receive(:timestamped_migrations) { true }
else
expect(ActiveRecord::Base).to receive(:timestamped_migrations) { true }
end
expect(subject.next_migration_number(1).to_s).to eq("20161204061526")
end
end
end
describe :migration_base_class_name do
subject { generator.send(:migration_base_class_name) }
let(:generator) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
it "returns the correct base class name" do
is_expected.to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
end
end
describe :create_data_migration do
let(:migration_name) { 'my_migration' }
subject { DataMigrate::Generators::DataMigrationGenerator.new([migration_name]) }
let(:data_migrations_file_path) { "abc/#{migration_name}.rb" }
context 'when custom data migrations path has a trailing slash' do
before do
DataMigrate.config.data_migrations_path = 'abc/'
end
it 'returns correct file path' do
is_expected.to receive(:migration_template).with(
'data_migration.rb', data_migrations_file_path
)
expect(subject).not_to receive(:template)
subject.create_data_migration
end
end
context 'when custom data migrations path does not have a trailing slash' do
before do
DataMigrate.config.data_migrations_path = 'abc'
end
it 'returns correct file path' do
is_expected.to receive(:migration_template).with(
'data_migration.rb', data_migrations_file_path
)
expect(subject).not_to receive(:template)
subject.create_data_migration
end
end
context 'when test support is enabled' do
let(:rails_root) { 'dummy-app' }
before do
DataMigrate.config.data_migrations_path = 'db/data/'
DataMigrate.config.test_support_enabled = true
allow(Rails).to receive(:root).and_return(Pathname.new(rails_root))
expect(DataMigrate::Helpers::InferTestSuiteType).to receive(:new).and_return(infer_double)
end
after(:all) do
FileUtils.rm_rf(Dir.glob("#{DataMigrate.config.data_migrations_path}/*"))
end
context 'and the test suite is RSpec' do
let(:infer_double) { instance_double(DataMigrate::Helpers::InferTestSuiteType, call: :rspec) }
it 'creates a spec file' do
expect(subject).to receive(:template).with(
'data_migration_spec.rb',
"#{rails_root}/spec/db/data/#{migration_name}_spec.rb"
)
subject.create_data_migration
end
end
context 'and the test suite is Minitest' do
let(:infer_double) { instance_double(DataMigrate::Helpers::InferTestSuiteType, call: :minitest) }
it 'creates a test file' do
expect(subject).to receive(:template).with(
'data_migration_test.rb',
"#{rails_root}/test/db/data/#{migration_name}_test.rb"
)
subject.create_data_migration
end
end
end
end
describe ".source_root" do
subject { described_class.source_root }
let(:default_source_root) do
File.expand_path(
File.dirname(File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb"))
)
end
it { is_expected.to eq default_source_root }
context "when DateMigrate.config.data_template_path is set" do
before do
@before = DataMigrate.config.data_template_path
DataMigrate.configure do |config|
config.data_template_path = data_template_path
end
end
let(:data_template_path) do
File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
end
let(:expected_source_root) { File.dirname(data_template_path) }
after do
DataMigrate.configure do |config|
config.data_template_path = @before
end
end
it "reads directory from config data template path" do
is_expected.to eq expected_source_root
end
end
end
end