-
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 4 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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| module DataMigrate | ||
| include ActiveSupport::Configurable | ||
| class << self | ||
|
|
||
| class << self | ||
| def configure | ||
| yield config | ||
| end | ||
|
|
@@ -12,7 +12,7 @@ def config | |
| end | ||
|
|
||
| class Config | ||
| attr_accessor :data_migrations_table_name, :data_migrations_path, :data_template_path, :db_configuration, :spec_name | ||
| attr_accessor :data_migrations_table_name, :data_migrations_path, :data_template_path, :db_configuration, :spec_name, :test_support_enabled, :test_framework | ||
|
|
||
| DEFAULT_DATA_TEMPLATE_PATH = "data_migration.rb" | ||
|
|
||
|
|
@@ -22,12 +22,20 @@ def initialize | |
| @data_template_path = DEFAULT_DATA_TEMPLATE_PATH | ||
| @db_configuration = nil | ||
| @spec_name = nil | ||
| @test_support_enabled = false | ||
| @test_framework = nil | ||
|
||
| end | ||
|
|
||
| def data_template_path=(value) | ||
| @data_template_path = value.tap do |path| | ||
| raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.exist?(path) | ||
| end | ||
| end | ||
|
|
||
| def test_framework=(value) | ||
| raise ArgumentError, "Invalid test framework: #{value}" unless [:rspec, :minitest].include?(value) | ||
|
|
||
| @test_framework = value | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| module DataMigrate | ||
| module Helpers | ||
| class InferTestSuiteType | ||
| def call | ||
| return DataMigrate.config.test_framework if DataMigrate.config.test_framework.present? | ||
|
|
||
| if File.exist?(Rails.root.join('spec', 'spec_helper.rb')) | ||
|
||
| :rspec | ||
| elsif File.exist?(Rails.root.join('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 |
|---|---|---|
|
|
@@ -15,10 +15,30 @@ class DataMigrationGenerator < Rails::Generators::NamedBase | |
| def create_data_migration | ||
| set_local_assigns! | ||
| migration_template template_path, data_migrations_file_path | ||
| create_data_migration_test | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def create_data_migration_test | ||
| return unless DataMigrate.config.test_support_enabled | ||
|
|
||
| case DataMigrate::Helpers::InferTestSuiteType.new.call | ||
|
||
| when :rspec | ||
| template "data_migration_spec.rb", data_migrations_spec_file_path | ||
| when :minitest | ||
| template "data_migration_test.rb", data_migrations_test_file_path | ||
| end | ||
| end | ||
|
|
||
| def data_migrations_test_file_path | ||
| File.join(Rails.root, 'test', data_migrations_path, "#{migration_file_name}_test.rb") | ||
| end | ||
|
|
||
| def data_migrations_spec_file_path | ||
| File.join(Rails.root, 'spec', data_migrations_path, "#{migration_file_name}_spec.rb") | ||
| end | ||
|
|
||
| def set_local_assigns! | ||
| if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/ | ||
| @migration_action = $1 | ||
|
|
@@ -38,6 +58,10 @@ def data_migrations_file_path | |
| File.join(data_migrations_path, "#{file_name}.rb") | ||
| end | ||
|
|
||
| def data_migrations_file_path_with_version | ||
| File.join(data_migrations_path, "#{migration_number}_#{migration_file_name}.rb") | ||
| end | ||
|
|
||
| # Use the first path in the data_migrations_path as the target directory | ||
| def data_migrations_path | ||
| Array.wrap(DataMigrate.config.data_migrations_path).first | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| require 'rails_helper' | ||
| require './<%= data_migrations_file_path_with_version %>' | ||
|
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. @vprigent Excellent idea here! Much easier and right next to the file being tested |
||
|
|
||
| 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,58 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe DataMigrate::Helpers::InferTestSuiteType 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 test framework is explicitly set' do | ||
| before do | ||
| allow(DataMigrate.config).to receive(:test_framework).and_return(:rspec) | ||
|
|
||
| # Set minitest inference to ensure :rspec if properly set | ||
| allow(File).to receive(:exist?).with(Rails.root.join('spec', 'spec_helper.rb')).and_return(false) | ||
| allow(File).to receive(:exist?).with(Rails.root.join('test', 'test_helper.rb')).and_return(true) | ||
| end | ||
|
|
||
| it 'returns the explicitly set test framework' do | ||
| expect(infer_test_suite.call).to eq(:rspec) | ||
| end | ||
| end | ||
|
|
||
| context 'when RSpec is detected' do | ||
| before do | ||
| allow(File).to receive(:exist?).with(Rails.root.join('spec', 'spec_helper.rb')).and_return(true) | ||
| allow(File).to receive(:exist?).with(Rails.root.join('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(Rails.root.join('spec', 'spec_helper.rb')).and_return(false) | ||
| allow(File).to receive(:exist?).with(Rails.root.join('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(Rails.root.join('spec', 'spec_helper.rb')).and_return(false) | ||
| allow(File).to receive(:exist?).with(Rails.root.join('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 |
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.
Nitpick:
I've read your PR a few times so far. Decided to do one last round from the "does it make me confused if I jump straight into the code without reading the context?". So please consider my comments as ideas / suggestions, and not something authoritative.
In this instance, I feel like the name is a bit confusing. Because it's not that we turn on test support, but we turn on test generation. So I'd prefer to have
test_generator_enabled,generate_tests,generate_specs,generate_test_files, etc.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.
I like this suggested change as it better represents what the configuration value achieves 💯