2017-05-30 00:58:26 -04:00
|
|
|
require "spec_helper"
|
2016-03-05 17:07:32 -05:00
|
|
|
require "generator_spec/test_case"
|
|
|
|
require File.expand_path("../../../lib/generators/paper_trail/install_generator", __FILE__)
|
2014-03-07 16:06:18 -05:00
|
|
|
|
2017-04-01 00:33:33 -04:00
|
|
|
RSpec.describe PaperTrail::InstallGenerator, type: :generator do
|
2014-03-07 16:06:18 -05:00
|
|
|
include GeneratorSpec::TestCase
|
2016-03-05 17:07:32 -05:00
|
|
|
destination File.expand_path("../tmp", __FILE__)
|
2014-03-07 16:06:18 -05:00
|
|
|
|
|
|
|
after(:all) { prepare_destination } # cleanup the tmp directory
|
|
|
|
|
|
|
|
describe "no options" do
|
|
|
|
before(:all) do
|
|
|
|
prepare_destination
|
|
|
|
run_generator
|
|
|
|
end
|
2015-12-24 01:50:14 -05:00
|
|
|
|
2014-03-07 16:06:18 -05:00
|
|
|
it "generates a migration for creating the 'versions' table" do
|
2017-04-10 11:10:15 -04:00
|
|
|
expected_parent_class = lambda {
|
|
|
|
old_school = "ActiveRecord::Migration"
|
|
|
|
ar_version = ActiveRecord::VERSION
|
|
|
|
if ar_version::MAJOR >= 5
|
|
|
|
format("%s[%d.%d]", old_school, ar_version::MAJOR, ar_version::MINOR)
|
|
|
|
else
|
|
|
|
old_school
|
|
|
|
end
|
|
|
|
}.call
|
2017-04-10 11:35:18 -04:00
|
|
|
expected_create_table_options = lambda {
|
|
|
|
if described_class::MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
|
|
|
|
', { options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }'
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
}.call
|
2017-04-01 00:33:33 -04:00
|
|
|
expect(destination_root).to(
|
|
|
|
have_structure {
|
|
|
|
directory("db") {
|
|
|
|
directory("migrate") {
|
|
|
|
migration("create_versions") {
|
2017-04-10 11:10:15 -04:00
|
|
|
contains("class CreateVersions < " + expected_parent_class)
|
2017-04-01 00:33:33 -04:00
|
|
|
contains "def change"
|
2017-04-10 11:35:18 -04:00
|
|
|
contains "create_table :versions#{expected_create_table_options}"
|
2017-04-01 00:33:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-03-07 16:06:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "`--with-changes` option set to `true`" do
|
|
|
|
before(:all) do
|
|
|
|
prepare_destination
|
2017-05-21 02:40:23 -04:00
|
|
|
run_generator %w[--with-changes]
|
2014-03-07 16:06:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "generates a migration for creating the 'versions' table" do
|
2017-04-01 00:33:33 -04:00
|
|
|
expect(destination_root).to(
|
|
|
|
have_structure {
|
|
|
|
directory("db") {
|
|
|
|
directory("migrate") {
|
|
|
|
migration("create_versions") {
|
|
|
|
contains "class CreateVersions"
|
|
|
|
contains "def change"
|
|
|
|
contains "create_table :versions"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-03-07 16:06:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "generates a migration for adding the 'object_changes' column to the 'versions' table" do
|
2017-04-01 00:33:33 -04:00
|
|
|
expect(destination_root).to(
|
|
|
|
have_structure {
|
|
|
|
directory("db") {
|
|
|
|
directory("migrate") {
|
|
|
|
migration("add_object_changes_to_versions") {
|
|
|
|
contains "class AddObjectChangesToVersions"
|
|
|
|
contains "def change"
|
|
|
|
contains "add_column :versions, :object_changes, :text"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-03-07 16:06:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|