Skip to content

Commit

Permalink
Fix event_admin specs
Browse files Browse the repository at this point in the history
  • Loading branch information
kigster committed Mar 11, 2024
1 parent 9390137 commit 986efdc
Show file tree
Hide file tree
Showing 12 changed files with 488 additions and 335 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ require:
- rubocop-rake
- rubocop-rspec

Style/IfUnlessModifier:
Enabled: false
4 changes: 4 additions & 0 deletions Brewfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ brew 'yq'
brew 'zlib'
brew 'v8'

# k8s
brew 'minikube'
brew 'kubernetes-cli'

cask 'chromedriver'
cask 'github'

Expand Down
584 changes: 336 additions & 248 deletions Brewfile.lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source 'https://rubygems.org'

gem 'rails', '~> 6.0.2'

gem 'annotate'
gem 'attribute_normalizer'
gem 'awesome_print'
gem 'bootsnap'
Expand Down Expand Up @@ -36,7 +37,6 @@ gem 'twitter-bootstrap-rails'
gem 'uglifier'
gem 'ventable'
gem 'yard'
gem 'annotate'

group :development, :test do
gem 'codecov'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ GEM
thor
tilt
highline (3.0.1)
i18n (1.14.1)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
Expand Down Expand Up @@ -285,7 +285,7 @@ GEM
rspec-expectations (~> 3.10)
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.13.0)
rspec-support (3.13.1)
rubocop (1.60.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand Down
2 changes: 0 additions & 2 deletions app/models/event_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
#
# Indexes
#
# index_event_admins_on_event_id (event_id)
# index_event_admins_on_event_id_and_user_id (event_id,user_id) UNIQUE
# index_event_admins_on_user_id (user_id)
# index_event_admins_on_user_id_only (user_id)
#
class EventAdmin < ApplicationRecord
attr_accessible :event_id, :user_id
Expand Down
27 changes: 27 additions & 0 deletions bin/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rubocop' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)

bundle_binstub = File.expand_path("bundle", __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rubocop", "rubocop")
2 changes: 2 additions & 0 deletions config/initializers/auto_annotate_models.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Annotate.set_defaults(
'active_admin' => 'false',
'additional_file_patterns' => [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class RemoveUnusedIndexesFromEventAdmin < ActiveRecord::Migration[6.0]
def up
if index_exists?(:event_admins, :user_id,
name: 'index_event_admins_on_user_id_only')
remove_index :event_admins, column: [:user_id], name: 'index_event_admins_on_user_id_only'
end

if index_exists?(:event_admins, :event_id, name: 'index_event_admins_on_event_id')
remove_index :event_admins, column: [:event_id], name: 'index_event_admins_on_event_id'
end

execute 'analyze event_admins'
end

def down
unless index_exists?(:event_admins, :user_id, name: 'index_event_admins_on_user_id_only')
add_index(:event_admins, [:user_id], name: 'index_event_admins_on_user_id_only')
end

unless index_exists?(:event_admins, :event_id, name: 'index_event_admins_on_event_id')
add_index(:event_admins, [:event_id], name: 'index_event_admins_on_event_id')
end

execute 'analyze event_admins'
end
end
17 changes: 2 additions & 15 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,6 @@ ALTER TABLE ONLY public.users
CREATE INDEX index_eald_payments_on_event_id ON public.eald_payments USING btree (event_id);


--
-- Name: index_event_admins_on_event_id; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_event_admins_on_event_id ON public.event_admins USING btree (event_id);


--
-- Name: index_event_admins_on_event_id_and_user_id; Type: INDEX; Schema: public; Owner: -
--
Expand All @@ -666,13 +659,6 @@ CREATE UNIQUE INDEX index_event_admins_on_event_id_and_user_id ON public.event_a
CREATE INDEX index_event_admins_on_user_id ON public.event_admins USING btree (user_id);


--
-- Name: index_event_admins_on_user_id_only; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_event_admins_on_user_id_only ON public.event_admins USING btree (user_id);


--
-- Name: index_jobs_on_event_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -788,6 +774,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20140706232217'),
('20150609064608'),
('20160611234315'),
('20180527021019');
('20180527021019'),
('20240311182346');


97 changes: 51 additions & 46 deletions lib/tasks/auto_annotate_models.rake
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
# frozen_string_literal: true

# NOTE: only doing this in development as some production environments (Heroku)
# NOTE: are sensitive to local FS writes, and besides -- it's just not proper
# NOTE: to have a dev-mode tool do its thing in production.
if Rails.env.development?
require 'annotate'
task :set_annotation_options do

# rubocop: disable Rake/Desc
task set_annotation_options: :environment do
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults(
'active_admin' => 'false',
'additional_file_patterns' => [],
'routes' => 'false',
'models' => 'true',
'position_in_routes' => 'before',
'position_in_class' => 'before',
'position_in_test' => 'before',
'position_in_fixture' => 'before',
'position_in_factory' => 'before',
'position_in_serializer' => 'before',
'show_foreign_keys' => 'true',
'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true',
'simple_indexes' => 'false',
'model_dir' => 'app/models',
'root_dir' => '',
'include_version' => 'false',
'require' => '',
'exclude_tests' => 'false',
'exclude_fixtures' => 'false',
'exclude_factories' => 'false',
'exclude_serializers' => 'false',
'exclude_scaffolds' => 'true',
'exclude_controllers' => 'true',
'exclude_helpers' => 'true',
'exclude_sti_subclasses' => 'false',
'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil,
'ignore_routes' => nil,
'ignore_unknown_models' => 'false',
'hide_limit_column_types' => 'integer,bigint,boolean',
'hide_default_column_types' => 'json,jsonb,hstore',
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
'format_yard' => 'false',
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
'frozen' => 'false',
'classified_sort' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
'with_comment' => 'true'
'active_admin' => 'false',
'additional_file_patterns' => [],
'routes' => 'false',
'models' => 'true',
'position_in_routes' => 'before',
'position_in_class' => 'before',
'position_in_test' => 'before',
'position_in_fixture' => 'before',
'position_in_factory' => 'before',
'position_in_serializer' => 'before',
'show_foreign_keys' => 'true',
'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true',
'simple_indexes' => 'false',
'model_dir' => 'app/models',
'root_dir' => '',
'include_version' => 'false',
'require' => '',
'exclude_tests' => 'false',
'exclude_fixtures' => 'false',
'exclude_factories' => 'false',
'exclude_serializers' => 'false',
'exclude_scaffolds' => 'true',
'exclude_controllers' => 'true',
'exclude_helpers' => 'true',
'exclude_sti_subclasses' => 'false',
'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil,
'ignore_routes' => nil,
'ignore_unknown_models' => 'false',
'hide_limit_column_types' => 'integer,bigint,boolean',
'hide_default_column_types' => 'json,jsonb,hstore',
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
'format_yard' => 'false',
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
'frozen' => 'false',
'classified_sort' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
'with_comment' => 'true'
)
end
# rubocop: enable Rake/Desc

Annotate.load_tasks
end
54 changes: 33 additions & 21 deletions spec/models/event_admin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,56 @@
#
# Indexes
#
# index_event_admins_on_event_id (event_id)
# index_event_admins_on_event_id_and_user_id (event_id,user_id) UNIQUE
# index_event_admins_on_user_id (user_id)
# index_event_admins_on_user_id_only (user_id)
#
describe EventAdmin do
describe 'validations' do
describe '#user' do
describe 'valid admin' do
subject(:event_admin) { EventAdmin.make! }
subject(:event_admin) { event_admin_maker[nil_method] }

it { is_expected.to be_valid }
end
let(:nil_method) { nil }
let(:event) { event_admin.event }
let(:user) { event_admin.user }

describe 'nil user id' do
subject(:event_admin_without_user) { EventAdmin.make!.tap { |ea| ea.user = nil }.validate! }
let(:event_admin_maker) do
lambda do |nil_method = nil|
described_class.make!.tap { |ea| nil_method ? ea.send("#{nil_method}=", nil) : ea }
end
end

describe 'validations' do
describe 'valid admin' do
it { is_expected.to be_valid }
end

describe '#user' do
describe 'nil user' do
subject(:event_admin_no_user) { event_admin_maker.call(:user_id) }

it 'invalidates nil user_id' do
expect { event_admin_without_user }.to raise_error(ArgumentError)
expect { event_admin_no_user.validate! }.to raise_error(ArgumentError)
end
end

describe 'nil event id' do
subject(:event_admin_without_event) { EventAdmin.make!.tap { |ea| ea.event = nil }.validate! }
describe 'nil event' do
subject(:event_admin_without_event) { event_admin_maker.call(:event_id) }

it 'invalidates nil event_id' do
expect { event_admin_without_event }.to raise_error(ArgumentError)
it 'invalidates nil user_id' do
expect { event_admin_without_event.validate! }.to raise_error(ArgumentError)
end
end

context 'when the user is already an admin for the event' do
subject { described_class.make event: }
describe 'event admin uniqueness' do
it 'has already event_admin for this event' do
expect(described_class.where(event:, user:).count).to eq(1)
end

let(:event) { Event.make! }
let(:event_admin) { described_class.make! event: }
let(:user) { event_admin.user }
describe 'when the user is already an admin for the event' do
subject(:another_event_admin) { described_class.make!(event:, user:) }

it { is_expected.not_to accept_values_for(:user_id, user.id) }
it 'raises validation error' do
expect { another_event_admin }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end
end
end
Expand Down

0 comments on commit 986efdc

Please sign in to comment.