Skip to content

Commit

Permalink
Add specs for generators
Browse files Browse the repository at this point in the history
Run migration tests only for latest rails. I added also missing index
for faster search and upgrade rails version.
  • Loading branch information
LTe committed Apr 20, 2020
1 parent 58d2cbe commit ab0a098
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 26 deletions.
8 changes: 3 additions & 5 deletions .travis.yml
Expand Up @@ -5,14 +5,15 @@ matrix:
- rvm: 2.3.8
gemfile: gemfiles/rails_3.2.gemfile
- rvm: 2.3.8
gemfile: gemfiles/rails_4.2.11.gemfile
gemfile: gemfiles/rails_4.2.gemfile
- rvm: 2.7
gemfile: gemfiles/rails_5.2.gemfile
- rvm: 2.7
gemfile: gemfiles/rails_6.0.gemfile
- rvm: 2.7
gemfile: gemfiles/rails_5.2.gemfile
- rvm: 2.7
env: RUN_GENERATORS=true
gemfile: gemfiles/rails_6.0.gemfile
- rvm: 2.7
gemfile: gemfiles/rails_master.gemfile
Expand All @@ -26,7 +27,4 @@ matrix:
before_install:
- gem update --system
- gem install bundler
cache:
- bundler
- directories:
- /home/travis/.rvm/
install: bundle install --jobs=3 --retry=3
8 changes: 4 additions & 4 deletions Appraisals
Expand Up @@ -10,10 +10,10 @@ appraise 'rails-3.2' do
end
end

appraise 'rails-4.2.11' do
gem 'activerecord', '~> 4.2.11'
gem 'activesupport', '~> 4.2.11'
gem 'railties', '~> 4.2.11'
appraise 'rails-4.2' do
gem 'activerecord', '~> 4.2'
gem 'activesupport', '~> 4.2'
gem 'railties', '~> 4.2'
gem 'rdoc', '6.2.0'

group :development do
Expand Down
32 changes: 24 additions & 8 deletions acts-as-messageable.gemspec
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
s.require_paths = ['lib']
s.authors = ['Piotr Nielacny']
s.date = '2020-04-18'
s.date = '2020-04-19'
s.email = 'piotr.nielacny@gmail.com'
s.extra_rdoc_files = [
'README.md'
Expand All @@ -22,6 +22,7 @@ Gem::Specification.new do |s|
'.coveralls.yml',
'.rspec',
'.rubocop.yml',
'.rubocop_todo.yml',
'.travis.yml',
'Appraisals',
'Gemfile',
Expand All @@ -32,14 +33,11 @@ Gem::Specification.new do |s|
'VERSION',
'acts-as-messageable.gemspec',
'gemfiles/rails_3.2.gemfile',
'gemfiles/rails_3.2.gemfile.lock',
'gemfiles/rails_4.2.11.gemfile',
'gemfiles/rails_4.2.11.gemfile.lock',
'gemfiles/rails_5.2.gemfile',
'gemfiles/rails_5.2.gemfile.lock',
'gemfiles/rails_6.0.gemfile',
'gemfiles/rails_6.0.gemfile.lock',
'gemfiles/rails_master.gemfile',
'lib/acts-as-messageable.rb',
'lib/acts_as_messageable.rb',
'lib/acts_as_messageable/message.rb',
'lib/acts_as_messageable/model.rb',
Expand All @@ -50,11 +48,14 @@ Gem::Specification.new do |s|
'lib/acts_as_messageable/scopes.rb',
'lib/generators/acts_as_messageable/migration/migration_generator.rb',
'lib/generators/acts_as_messageable/migration/templates/migration.rb',
'lib/generators/acts_as_messageable/migration/templates/migration_indexes.rb',
'lib/generators/acts_as_messageable/migration/templates/migration_opened_as_datetime.rb',
'lib/generators/acts_as_messageable/migration/templates/migration_permanent.rb',
'spec/acts_as_messageable_spec.rb',
'spec/custom_class_spec.rb',
'spec/custom_required_spec.rb',
'spec/group_messages_spec.rb',
'spec/migrations_spec.rb',
'spec/spec_helper.rb',
'spec/support/admin.rb',
'spec/support/send_message.rb',
Expand All @@ -69,26 +70,41 @@ Gem::Specification.new do |s|
s.specification_version = 4

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0')
s.add_runtime_dependency('activerecord', ['>= 0'])
s.add_runtime_dependency('activesupport', ['>= 0'])
s.add_runtime_dependency('ancestry', ['>= 0'])
s.add_runtime_dependency('railties', ['>= 0'])
s.add_development_dependency('appraisal', ['>= 0'])
s.add_development_dependency('coveralls', ['>= 0'])
s.add_development_dependency('jeweler', ['>= 0'])
s.add_development_dependency('rspec', ['>= 0'])
s.add_development_dependency('rubocop', ['>= 0'])
s.add_development_dependency('sqlite3', ['>= 0'])
s.add_development_dependency('timecop', ['>= 0'])
else
s.add_dependency('activerecord', ['>= 0'])
s.add_dependency('activesupport', ['>= 0'])
s.add_dependency('ancestry', ['>= 0'])
s.add_dependency('appraisal', ['>= 0'])
s.add_dependency('coveralls', ['>= 0'])
s.add_dependency('jeweler', ['>= 0'])
s.add_dependency('railties', ['>= 0'])
s.add_dependency('rspec', ['>= 0'])
s.add_dependency('rubocop', ['>= 0'])
s.add_dependency('sqlite3', ['>= 0'])
s.add_dependency('timecop', ['>= 0'])
end
else
s.add_dependency('appraisal', ['>= 0'])
s.add_dependency('coveralls', ['>= 0'])
s.add_dependency('jeweler', ['>= 0'])
s.add_dependency('activerecord', ['>= 0'])
s.add_dependency('activesupport', ['>= 0'])
s.add_dependency('ancestry', ['>= 0'])
s.add_dependency('railties', ['>= 0'])
s.add_dependency('railties', ['>= 0'])
s.add_dependency('railties', ['>= 0'])
s.add_dependency('railties', ['>= 0'])
s.add_dependency('rspec', ['>= 0'])
s.add_dependency('rubocop', ['>= 0'])
s.add_dependency('sqlite3', ['>= 0'])
s.add_dependency('timecop', ['>= 0'])
end
end
6 changes: 3 additions & 3 deletions gemfiles/rails_4.2.11.gemfile → gemfiles/rails_4.2.gemfile
Expand Up @@ -4,10 +4,10 @@

source 'http://rubygems.org'

gem 'activerecord', '~> 4.2.11'
gem 'activesupport', '~> 4.2.11'
gem 'activerecord', '~> 4.2'
gem 'activesupport', '~> 4.2'
gem 'ancestry'
gem 'railties', '~> 4.2.11'
gem 'railties', '~> 4.2'
gem 'rdoc', '6.2.0'

group :development do
Expand Down
Expand Up @@ -7,7 +7,7 @@ module ActsAsMessageable
class MigrationGenerator < Rails::Generators::Base
include Rails::Generators::Migration

namespace 'acts-as-messageable:migration'
namespace 'acts_as_messageable:migration'

source_root File.join(File.dirname(__FILE__), 'templates')
argument :table_name, type: :string, default: 'messages'
Expand All @@ -33,6 +33,11 @@ def create_migration_file
rescue StandardError
nil
end
begin
migration_template 'migration_indexes.rb', 'db/migrate/add_indexes_to_messages.rb'
rescue StandardError
nil
end
end
end
end
@@ -1,4 +1,4 @@
class CreateMessagesTable < ActiveRecord::Migration
class CreateMessagesTable < ActiveRecord::Migration[4.2]
def self.up
create_table :<%= table_name %> do |t|
t.string :topic
Expand Down
@@ -0,0 +1,11 @@
class AddIndexesToMessages < ActiveRecord::Migration[4.2]
def self.up
add_index :<%= table_name %>, [:sent_messageable_id, :sent_messageable_type], :name => "acts_as_messageable_sent"
add_index :<%= table_name %>, [:received_messageable_id, :received_messageable_type], :name => "acts_as_messageable_received"
end

def self.down
remove_index :<%= table_name %>, :name => "acts_as_messageable_sent"
remove_index :<%= table_name %>, :name => "acts_as_messageable_received"
end
end
@@ -1,11 +1,11 @@
class AddOpenedAtToMessages < ActiveRecord::Migration
class AddOpenedAtToMessages < ActiveRecord::Migration[4.2]
def self.up
add_column :<%= table_name %>, :opened_at, :datetime
<%= table_name.classify %>.where(opened:true).update_all(opened_at: DateTime.now)
ActsAsMessageable::Message.where(opened: true).update_all(opened_at: DateTime.now)
end

def self.down
<%= table_name.classify %>.where.not(opened_at:nil).update_all(opened: true)
ActsAsMessageable::Message.where('opened_at is not null').update_all(opened: true)
remove_column :<%= table_name %>, :opened_at
end
end
@@ -1,4 +1,4 @@
class AddRecipientPermanentDeleteAndSenderPermanentDeleteToMessages < ActiveRecord::Migration
class AddRecipientPermanentDeleteAndSenderPermanentDeleteToMessages < ActiveRecord::Migration[4.2]
def self.up
add_column :<%= table_name %>, :recipient_permanent_delete, :boolean, :default => false
add_column :<%= table_name %>, :sender_permanent_delete, :boolean, :default => false
Expand Down
61 changes: 61 additions & 0 deletions spec/migrations_spec.rb
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require 'spec_helper'

def create_dummy_app
system <<-COMMAND
bundle exec rails new dummy --skip-test-unit --skip-spring --skip-webpack-install --skip-bootsnap \
--skip-active-storage --skip-active-job -d sqlite3
COMMAND
end

def add_gem_to_gemfile
run_in_app("echo gem \"'acts-as-messageable', path: '../'\" >> Gemfile; bundle")
end

def run_bundler
run_in_app('bundle update')
end

def run_generators
run_in_app('bundle exec rails g acts_as_messageable:migration')
end

def run_migrations
run_in_app('bundle exec rake db:migrate')
end

def rollback_migrations
run_in_app('bundle exec rake db:migrate VERSION=0')
end

def run_in_app(command)
Bundler.clean_system("cd dummy; BUNDLE_GEMFILE=./Gemfile #{command}")
end

def remove_dummy_app
system 'rm -rf dummy'
end

def skip_generators?
ENV.fetch('RUN_GENERATORS', 'false') == 'false'
end

describe 'migration' do
before do
create_dummy_app
add_gem_to_gemfile
run_bundler
end

after do
remove_dummy_app
end

it 'runs migrations and revert them', skip: skip_generators? do
run_generators

expect(run_migrations).to be_truthy
expect(rollback_migrations).to be_truthy
end
end
3 changes: 3 additions & 0 deletions spec/support/user.rb
Expand Up @@ -6,3 +6,6 @@ class User < ActiveRecord::Base

class Men < User
end

class CustomSearchUser < ActiveRecord::Base
end

0 comments on commit ab0a098

Please sign in to comment.