Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set spec run order to random #2

Merged
merged 5 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 85 additions & 91 deletions .rubocop_todo.yml

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions spec/lib/annotate/annotate_models_spec_annotate_model_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require 'annotate/annotate_models'
require 'annotate/active_record_patch'
require 'active_support/core_ext/string'
require 'files'
require 'tmpdir'

RSpec.describe AnnotateModels do
unless const_defined?(:MAGIC_COMMENTS)
MAGIC_COMMENTS = [
'# encoding: UTF-8',
'# coding: UTF-8',
'# -*- coding: UTF-8 -*-',
'#encoding: utf-8',
'# encoding: utf-8',
'# -*- encoding : utf-8 -*-',
"# encoding: utf-8\n# frozen_string_literal: true",
"# frozen_string_literal: true\n# encoding: utf-8",
'# frozen_string_literal: true',
'#frozen_string_literal: false',
'# -*- frozen_string_literal : true -*-'
].freeze
end

def mock_index(name, params = {})
double('IndexKeyDefinition',
name: name,
columns: params[:columns] || [],
unique: params[:unique] || false,
orders: params[:orders] || {},
where: params[:where],
using: params[:using])
end

def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
double('ForeignKeyDefinition',
name: name,
column: from_column,
to_table: to_table,
primary_key: to_column,
on_delete: constraints[:on_delete],
on_update: constraints[:on_update])
end

def mock_connection(indexes = [], foreign_keys = [])
double('Conn',
indexes: indexes,
foreign_keys: foreign_keys,
supports_foreign_keys?: true)
end

def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [])
options = {
connection: mock_connection(indexes, foreign_keys),
table_exists?: true,
table_name: table_name,
primary_key: primary_key,
column_names: columns.map { |col| col.name.to_s },
columns: columns,
column_defaults: columns.map { |col| [col.name, col.default] }.to_h,
table_name_prefix: ''
}

double('An ActiveRecord class', options)
end

def mock_column(name, type, options = {})
default_options = {
limit: nil,
null: false,
default: nil,
sql_type: type
}

stubs = default_options.dup
stubs.merge!(options)
stubs[:name] = name
stubs[:type] = type

double('Column', stubs)
end

describe '.annotate_model_file' do
subject do
described_class.annotate_model_file([], 'foo.rb', nil, {})
end

before do
class Foo < ActiveRecord::Base; end
allow(described_class).to receive(:get_model_class).with('foo.rb').and_return(Foo)
allow(Foo).to receive(:table_exists?).and_return(false)
end

after { Object.send :remove_const, 'Foo' }

it 'skips attempt to annotate if no table exists for model' do
expect(subject).to eq nil
end

context 'with a non-class' do
before do
NotAClass = 'foo'.freeze # rubocop:disable Naming/ConstantName
allow(described_class).to receive(:get_model_class).with('foo.rb').and_return(NotAClass)
end

after { Object.send :remove_const, 'NotAClass' }

it "doesn't output an error" do
expect { subject }.not_to output.to_stderr
end
end
end
end
Loading