summaryrefslogtreecommitdiff
path: root/spec/lib/generators/model
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-07-20 15:40:28 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-07-20 15:40:28 +0000
commitb595cb0c1dec83de5bdee18284abe86614bed33b (patch)
tree8c3d4540f193c5ff98019352f554e921b3a41a72 /spec/lib/generators/model
parent2f9104a328fc8a4bddeaa4627b595166d24671d0 (diff)
downloadgitlab-ce-b595cb0c1dec83de5bdee18284abe86614bed33b.tar.gz
Add latest changes from gitlab-org/gitlab@15-2-stable-eev15.2.0-rc42
Diffstat (limited to 'spec/lib/generators/model')
-rw-r--r--spec/lib/generators/model/mocks/migration_file.txt26
-rw-r--r--spec/lib/generators/model/mocks/model_file.txt2
-rw-r--r--spec/lib/generators/model/mocks/spec_file.txt5
-rw-r--r--spec/lib/generators/model/model_generator_spec.rb47
4 files changed, 80 insertions, 0 deletions
diff --git a/spec/lib/generators/model/mocks/migration_file.txt b/spec/lib/generators/model/mocks/migration_file.txt
new file mode 100644
index 00000000000..e92c2d2b534
--- /dev/null
+++ b/spec/lib/generators/model/mocks/migration_file.txt
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+# See https://docs.gitlab.com/ee/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class CreateModelGeneratorTestFoos < Gitlab::Database::Migration[2.0]
+ # When using the methods "add_concurrent_index" or "remove_concurrent_index"
+ # you must disable the use of transactions
+ # as these methods can not run in an existing transaction.
+ # When using "add_concurrent_index" or "remove_concurrent_index" methods make sure
+ # that either of them is the _only_ method called in the migration,
+ # any other changes should go in a separate migration.
+ # This ensures that upon failure _only_ the index creation or removing fails
+ # and can be retried or reverted easily.
+ #
+ # To disable transactions uncomment the following line and remove these
+ # comments:
+ # disable_ddl_transaction!
+
+ def change
+ create_table :model_generator_test_foos do |t|
+
+ t.timestamps null: false
+ end
+ end
+end
diff --git a/spec/lib/generators/model/mocks/model_file.txt b/spec/lib/generators/model/mocks/model_file.txt
new file mode 100644
index 00000000000..066db4bfd76
--- /dev/null
+++ b/spec/lib/generators/model/mocks/model_file.txt
@@ -0,0 +1,2 @@
+class ModelGeneratorTestFoo < ApplicationRecord
+end
diff --git a/spec/lib/generators/model/mocks/spec_file.txt b/spec/lib/generators/model/mocks/spec_file.txt
new file mode 100644
index 00000000000..efd700df0a1
--- /dev/null
+++ b/spec/lib/generators/model/mocks/spec_file.txt
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe ModelGeneratorTestFoo, type: :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
diff --git a/spec/lib/generators/model/model_generator_spec.rb b/spec/lib/generators/model/model_generator_spec.rb
new file mode 100644
index 00000000000..0e770190d25
--- /dev/null
+++ b/spec/lib/generators/model/model_generator_spec.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Model::ModelGenerator do
+ let(:args) { ['ModelGeneratorTestFoo'] }
+ let(:options) { { 'migration' => true, 'timestamps' => true, 'indexes' => true, 'test_framework' => :rspec } }
+ let(:temp_dir) { Dir.mktmpdir }
+ let(:migration_file_path) { Dir.glob(File.join(temp_dir, 'db/migrate/*create_model_generator_test_foos.rb')).first }
+ let(:model_file_path) { File.join(temp_dir, 'app/models/model_generator_test_foo.rb') }
+ let(:spec_file_path) { File.join(temp_dir, 'spec/models/model_generator_test_foo_spec.rb') }
+
+ subject { described_class.new(args, options, { destination_root: temp_dir }) }
+
+ context 'when generating a model' do
+ after do
+ FileUtils.rm_rf(temp_dir)
+ end
+
+ it 'creates the model file with the right content' do
+ subject.invoke_all
+
+ expect(File).to exist(model_file_path)
+ mock_model_file_content = File.read(File.expand_path('./mocks/model_file.txt', __dir__))
+ model_file_content = File.read(model_file_path)
+ expect(model_file_content).to eq(mock_model_file_content)
+ end
+
+ it 'creates the migration file with the right content' do
+ subject.invoke_all
+
+ expect(File).to exist(migration_file_path)
+ mock_migration_file_content = File.read(File.expand_path('./mocks/migration_file.txt', __dir__))
+ migration_file_content = File.read(migration_file_path)
+ expect(migration_file_content).to eq(mock_migration_file_content)
+ end
+
+ it 'creates the spec file with the right content' do
+ subject.invoke_all
+
+ expect(File).to exist(spec_file_path)
+ mock_spec_file_content = File.read(File.expand_path('./mocks/spec_file.txt', __dir__))
+ spec_file_content = File.read(spec_file_path)
+ expect(spec_file_content).to eq(mock_spec_file_content)
+ end
+ end
+end