diff options
Diffstat (limited to 'spec/lib/generators')
5 files changed, 103 insertions, 0 deletions
diff --git a/spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb b/spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb index 4cba9732c22..b6e1d59f6c0 100644 --- a/spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb +++ b/spec/lib/generators/gitlab/usage_metric_definition/redis_hll_generator_spec.rb @@ -38,6 +38,29 @@ RSpec.describe Gitlab::UsageMetricDefinition::RedisHllGenerator, :silence_stdout expect(monthly_metric_definition["instrumentation_class"]).to eq('RedisHLLMetric') end + context 'with multiple events', :aggregate_failures do + let(:event_2) { 'i_test_event_2' } + let(:args) { [category, event, event_2] } + + it 'creates metric definition files' do + described_class.new(args).invoke_all + + [event, event_2].each do |event| + weekly_metric_definition_path = Dir.glob(File.join(temp_dir, "metrics/counts_7d/*#{event}_weekly.yml")).first + monthly_metric_definition_path = Dir.glob(File.join(temp_dir, "metrics/counts_28d/*#{event}_monthly.yml")).first + + weekly_metric_definition = YAML.safe_load(File.read(weekly_metric_definition_path)) + monthly_metric_definition = YAML.safe_load(File.read(monthly_metric_definition_path)) + + expect(weekly_metric_definition).to include("key_path" => "redis_hll_counters.test_category.#{event}_weekly") + expect(monthly_metric_definition).to include("key_path" => "redis_hll_counters.test_category.#{event}_monthly") + + expect(weekly_metric_definition["instrumentation_class"]).to eq('RedisHLLMetric') + expect(monthly_metric_definition["instrumentation_class"]).to eq('RedisHLLMetric') + end + end + end + context 'with ee option' do let(:weekly_metric_definition_path) { Dir.glob(File.join(temp_dir, 'ee/config/metrics/counts_7d/*i_test_event_weekly.yml')).first } let(:monthly_metric_definition_path) { Dir.glob(File.join(temp_dir, 'ee/config/metrics/counts_28d/*i_test_event_monthly.yml')).first } 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 |