Skip to content

Commit

Permalink
Add test to default outbox generator
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermoap committed Nov 7, 2023
1 parent b5114da commit be91e95
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions spec/lib/active_outbox/generators/model_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,95 @@
end
let(:timestamp_of_migration) { DateTime.now.in_time_zone('UTC').strftime('%Y%m%d%H%M%S') }

context 'with default outbox name' do
let(:migration_file_path) do
"#{destination_root}/db/migrate/#{timestamp_of_migration}_active_outbox_create_outboxes.rb"
end

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
assert_file migration_file_path
end
end

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

describe 'migration content' do
subject(:generate) { run_generator(["--component_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 ActiveOutboxCreateOutbox < ActiveRecord::Migration[#{active_record_dependency}]
def change
create_table :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 ActiveOutboxCreateOutbox < ActiveRecord::Migration[#{active_record_dependency}]
def change
create_table :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

context 'with custom outbox name' do
let(:table_name) { 'custom_table_name' }

Expand Down

0 comments on commit be91e95

Please sign in to comment.