File: data_migration_generator_spec.rb

package info (click to toggle)
ruby-data-migrate 6.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 568 kB
  • sloc: ruby: 1,844; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 2,043 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require 'spec_helper'
require 'rails/generators'
require 'rails/generators/migration'
require 'generators/data_migration/data_migration_generator'

describe DataMigrate::Generators::DataMigrationGenerator do
  let(:subject) { DataMigrate::Generators::DataMigrationGenerator }
  describe :next_migration_number do
    it "next migration" do
      Timecop.freeze("2016-12-03 22:15:26 -0800") do
        expect(ActiveRecord::Base).to receive(:timestamped_migrations) { true }
        expect(subject.next_migration_number(1)).to eq("20161204061526")
      end
    end
  end

  describe :migration_base_class_name do
    let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
    it "returns the correct base class name" do
      if ActiveRecord.version >= Gem::Version.new('5.0')
        expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
      else
        expect(subject.send(:migration_base_class_name)).to eq('ActiveRecord::Migration')
      end
    end
  end

  describe :create_data_migration do
    let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
    let(:data_migrations_file_path) { 'abc/my_migration.rb' }

    context 'when custom data migrations path has a trailing slash' do
      before do
        DataMigrate.config.data_migrations_path = 'abc/'
      end

      it 'returns correct file path' do
        expect(subject).to receive(:migration_template).with(
          'data_migration.rb', data_migrations_file_path
        )

        subject.create_data_migration
      end
    end

    context 'when custom data migrations path does not have a trailing slash' do
      before do
        DataMigrate.config.data_migrations_path = 'abc'
      end

      it 'returns correct file path' do
        expect(subject).to receive(:migration_template).with(
          'data_migration.rb', data_migrations_file_path
        )

        subject.create_data_migration
      end
    end
  end
end