-
Notifications
You must be signed in to change notification settings - Fork 198
Feature: Add Rails generator and rake task support for creating data migration test coverage #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5173280
f14dbf8
6b9cdee
7cfcc27
b74b7f9
02b4b12
45e97b1
3fb736a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| module DataMigrate | ||
| module Helpers | ||
| class InferTestFramework | ||
| def call | ||
| if File.exist?(File.expand_path('spec/spec_helper.rb')) | ||
| :rspec | ||
| elsif File.exist?(File.expand_path('test/test_helper.rb')) | ||
| :minitest | ||
| else | ||
| raise StandardError.new('Unable to determine test suite') | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| require 'rails_helper' | ||
| require './<%= data_migrations_file_path_with_version %>' | ||
|
|
||
| describe <%= migration_class_name %>, type: :data_migration do | ||
| let(:migration) { <%= migration_class_name %>.new } | ||
|
|
||
| pending "should test `migration.up`" | ||
|
|
||
| pending "should test `migration.down`" | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| require 'test_helper' | ||
| require './<%= data_migrations_file_path_with_version %>' | ||
|
|
||
| class <%= migration_class_name %>Test < ActiveSupport::TestCase | ||
| def setup | ||
| @migration = <%= migration_class_name %>.new | ||
| end | ||
|
|
||
| def test_migration_up | ||
| skip("Pending test coverage for @migration.up") | ||
| end | ||
|
|
||
| def test_migration_down | ||
| skip("Pending test coverage for @migration.down") | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe DataMigrate::Helpers::InferTestFramework do | ||
| subject(:infer_test_suite) { described_class.new } | ||
|
|
||
| describe '#call' do | ||
| before do | ||
| allow(Rails).to receive(:root).and_return(Pathname.new('/fake/path')) | ||
| end | ||
|
|
||
| context 'when RSpec is detected' do | ||
| before do | ||
| allow(File).to receive(:exist?).with(File.expand_path('spec/spec_helper.rb')).and_return(true) | ||
| allow(File).to receive(:exist?).with(File.expand_path('test/test_helper.rb')).and_return(false) | ||
| end | ||
|
|
||
| it 'returns :rspec' do | ||
| expect(infer_test_suite.call).to eq(:rspec) | ||
| end | ||
| end | ||
|
|
||
| context 'when Minitest is detected' do | ||
| before do | ||
| allow(File).to receive(:exist?).with(File.expand_path('spec/spec_helper.rb')).and_return(false) | ||
| allow(File).to receive(:exist?).with(File.expand_path('test/test_helper.rb')).and_return(true) | ||
| end | ||
|
|
||
| it 'returns :minitest' do | ||
| expect(infer_test_suite.call).to eq(:minitest) | ||
| end | ||
| end | ||
|
|
||
| context 'when no test suite is detected' do | ||
| before do | ||
| allow(File).to receive(:exist?).with(File.expand_path('spec/spec_helper.rb')).and_return(false) | ||
| allow(File).to receive(:exist?).with(File.expand_path('test/test_helper.rb')).and_return(false) | ||
| end | ||
|
|
||
| it 'raises an error' do | ||
| expect { infer_test_suite.call }.to raise_error(StandardError, 'Unable to determine test suite') | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,11 @@ | |
| end | ||
|
|
||
| describe :create_data_migration do | ||
| subject { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) } | ||
| let(:migration_name) { 'my_migration' } | ||
|
|
||
| let(:data_migrations_file_path) { 'abc/my_migration.rb' } | ||
| 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 | ||
|
|
@@ -44,6 +46,8 @@ | |
| 'data_migration.rb', data_migrations_file_path | ||
| ) | ||
|
|
||
| expect(subject).not_to receive(:template) | ||
|
|
||
| subject.create_data_migration | ||
| end | ||
| end | ||
|
|
@@ -58,9 +62,56 @@ | |
| 'data_migration.rb', data_migrations_file_path | ||
| ) | ||
|
|
||
| expect(subject).not_to receive(:template) | ||
|
|
||
| subject.create_data_migration | ||
| end | ||
| end | ||
|
|
||
| context 'when test generation is enabled' do | ||
| let(:rails_root) { 'dummy-app' } | ||
|
|
||
| before do | ||
| DataMigrate.config.data_migrations_path = 'db/data/' | ||
| DataMigrate.config.test_generator_enabled = true | ||
|
|
||
| allow(Rails).to receive(:root).and_return(Pathname.new(rails_root)) | ||
| end | ||
|
|
||
| after(:all) do | ||
| FileUtils.rm_rf(Dir.pwd + '/db/data') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fun fact: Not setting |
||
| end | ||
|
|
||
| context 'and the test suite is RSpec' do | ||
| before do | ||
| DataMigrate.config.test_generator_framework = :rspec | ||
| end | ||
|
|
||
| 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 | ||
| before do | ||
| DataMigrate.config.test_generator_framework = :minitest | ||
| end | ||
|
|
||
| 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 | ||
joshmfrankel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| describe ".source_root" do | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vprigent Excellent idea here! Much easier and right next to the file being tested