Skip to content

Commit

Permalink
Initial test to assert migration file (#7)
Browse files Browse the repository at this point in the history
* Fix rubocop offenses

* More generator tests

* Linter offenses and catch up migration template with refactors

* Adds test for postgresql adapter

* Remove unnecesary changes

* Specifies correct version of generator spec

* Updates gemfile.lock

* Removes absolute path from test

* Generates migration timestap programtically

* Fixes failing test about migration content

* Improves time traveling

* reverts workflow changes

* Fix assertion tests

* Reverts changes to CI file

* Initial test to assert migration file

* Remove pry
  • Loading branch information
JoDaBaRo authored Oct 25, 2023
1 parent 2f41559 commit 8471caa
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 20 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gemspec

gem 'byebug', '~> 11.1.3'
gem 'database_cleaner-active_record', '~> 2.1.0'
gem 'generator_spec', '~> 0.9.4'
gem 'pg', '~> 1.5.4'
gem 'pry-rails', '~> 0.3.9'
gem 'reek', '~> 6.1.4'
Expand Down
27 changes: 12 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,17 @@ GEM
concurrent-ruby (~> 1.0)
zeitwerk (~> 2.6)
erubi (1.12.0)
generator_spec (0.9.4)
activesupport (>= 3.0.0)
railties (>= 3.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.3)
kwalify (0.7.2)
language_server-protocol (3.17.0.3)
loofah (2.21.3)
loofah (2.21.4)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mail (2.8.1)
Expand All @@ -106,9 +109,9 @@ GEM
marcel (1.0.2)
method_source (1.0.0)
mini_mime (1.1.5)
mini_portile2 (2.8.4)
mini_portile2 (2.8.5)
minitest (5.20.0)
net-imap (0.3.7)
net-imap (0.4.2)
date
net-protocol
net-pop (0.1.2)
Expand All @@ -121,12 +124,8 @@ GEM
nokogiri (1.15.4)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.15.4-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.4-x86_64-linux)
racc (~> 1.4)
parallel (1.23.0)
parser (3.2.2.3)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
pg (1.5.4)
Expand Down Expand Up @@ -173,7 +172,7 @@ GEM
kwalify (~> 0.7.0)
parser (~> 3.2.0)
rainbow (>= 2.0, < 4.0)
regexp_parser (2.8.1)
regexp_parser (2.8.2)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
Expand All @@ -188,7 +187,7 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubocop (1.56.3)
rubocop (1.56.4)
base64 (~> 0.1.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand Down Expand Up @@ -225,26 +224,24 @@ GEM
activesupport (>= 5.2)
sprockets (>= 3.0.0)
sqlite3 (1.4.2)
thor (1.2.2)
thor (1.3.0)
timeout (0.4.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
unicode-display_width (2.5.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.12)

PLATFORMS
ruby
x86_64-darwin-21
x86_64-darwin-22
x86_64-linux

DEPENDENCIES
active_outbox!
byebug (~> 11.1.3)
database_cleaner-active_record (~> 2.1.0)
generator_spec (~> 0.9.4)
pg (~> 1.5.4)
pry-rails (~> 0.3.9)
reek (~> 6.1.4)
Expand Down
8 changes: 6 additions & 2 deletions lib/active_outbox/generators/active_outbox_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ class ActiveOutboxGenerator < ActiveRecord::Generators::Base
include ActiveOutbox::AdapterHelper
source_root File.expand_path('templates', __dir__)

class_option :root_components_path, type: :string, default: Rails.root
class_option :root_components_path, type: :string

def create_migration_files
migration_path = "#{options['root_components_path']}/db/migrate"
migration_path = "#{root_path}/db/migrate"
migration_template(
'migration.rb',
"#{migration_path}/outbox_create_#{table_name}.rb",
migration_version: migration_version
)
end

def root_path
options['root_components_path'] || Rails.root
end

def migration_version
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end
Expand Down
6 changes: 3 additions & 3 deletions lib/active_outbox/generators/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
class OutboxCreate<%= table_name.camelize.singularize %> < ActiveRecord::Migration<%= migration_version %>
def change
create_table :<%= table_name %> do |t|
t.<%= uuid_type %> :identifier, null: false, index: { unique: true }
t.<%= ActiveOutbox::AdapterHelper.uuid_type %> :identifier, null: false, index: { unique: true }
t.string :event, null: false
t.<%= json_type %> :payload
t.<%= ActiveOutbox::AdapterHelper.json_type %> :payload
t.string :aggregate, null: false
t.<%= uuid_type %> :aggregate_identifier, null: false, index: true
t.<%= ActiveOutbox::AdapterHelper.uuid_type %> :aggregate_identifier, null: false, index: true

t.timestamps
end
Expand Down
110 changes: 110 additions & 0 deletions spec/generators/active_outbox_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# frozen_string_literal: true

require 'spec_helper'
require 'generator_spec'
require 'tempfile'

RSpec.describe ActiveOutboxGenerator, type: :generator do
destination File.expand_path('tmp', __dir__)

before do
prepare_destination
Time.use_zone('UTC') do
travel_to Time.zone.local(2023, 10, 20, 14, 25, 30)
end
end

after do
travel_back
FileUtils.rm_rf(destination_root)
end

let(:table_name) { 'custom_table_name' }
let(:migration_file_path) do
"#{destination_root}/db/migrate/#{timestamp_of_migration}_outbox_create_#{table_name}_outboxes.rb"
end
let(:timestamp_of_migration) { DateTime.now.in_time_zone('UTC').strftime('%Y%m%d%H%M%S') }

context 'without root_component_path' do
before do
allow(Rails).to receive(:root).and_return(destination_root)
end

it 'creates the expected files' do
run_generator [table_name]
assert_file migration_file_path
end
end

context 'with root_component_path' do
it 'creates the expected files' do
run_generator([table_name, "--root_components_path=#{destination_root}"])
assert_file migration_file_path
end
end

describe 'migration content' do
subject(:generate) { run_generator([table_name, "--root_components_path=#{destination_root}"]) }

let(:actual_content) { File.read(migration_file_path) }
let(:active_record_dependency) { ActiveRecord::VERSION::STRING.to_f }

context 'when is not a postgres migration' do
before do
allow(ActiveOutbox::AdapterHelper).to receive(:postgres?).and_return(false)
end

let(:expected_content) do
<<~MIGRATION
class OutboxCreate#{table_name.camelcase}Outbox < ActiveRecord::Migration[#{active_record_dependency}]
def change
create_table :#{table_name}_outboxes do |t|
t.string :identifier, null: false, index: { unique: true }
t.string :event, null: false
t.string :payload
t.string :aggregate, null: false
t.string :aggregate_identifier, null: false, index: true
t.timestamps
end
end
end
MIGRATION
end

it 'creates the migration with the correct content' do
generate
expect(actual_content).to include(expected_content)
end
end

context 'when it is a postgres migration' do
before do
allow(ActiveOutbox::AdapterHelper).to receive(:postgres?).and_return(true)
end

let(:expected_content) do
<<~MIGRATION
class OutboxCreate#{table_name.camelcase}Outbox < ActiveRecord::Migration[#{active_record_dependency}]
def change
create_table :#{table_name}_outboxes do |t|
t.uuid :identifier, null: false, index: { unique: true }
t.string :event, null: false
t.jsonb :payload
t.string :aggregate, null: false
t.uuid :aggregate_identifier, null: false, index: true
t.timestamps
end
end
end
MIGRATION
end

it 'creates the migration with the correct content' do
generate
expect(actual_content).to include(expected_content)
end
end
end
end

0 comments on commit 8471caa

Please sign in to comment.