Skip to content

Commit

Permalink
add rspec
Browse files Browse the repository at this point in the history
madmatvey committed May 16, 2024
1 parent e36ea82 commit 0a5dbf7
Showing 11 changed files with 342 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ require:
- rubocop-rails
- rubocop-performance
- rubocop-migration
- rubocop-rspec_rails
- rubocop-factory_bot

inherit_from:
- .rubocop_todo.yml
@@ -45,3 +47,6 @@ Migration/RenameTable:
Enabled: true
Migration/ReservedWordMysql:
Enabled: true

Metrics/BlockLength:
AllowedMethods: ['describe', 'context']
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -59,10 +59,15 @@ group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'bundler-audit'
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'factory_bot_rails'
gem 'faker'
gem 'rspec-rails', '~> 6.1.0'
gem 'rubocop-factory_bot', require: false
gem 'rubocop-migration', require: false
gem 'rubocop-performance', require: false
gem 'rubocop-rails', require: false
gem 'rubocop-rspec', require: false
gem 'rubocop-rspec_rails', require: false
end

group :development do
39 changes: 39 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -82,7 +82,13 @@ GEM
debug (1.9.1)
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
erubi (1.12.0)
factory_bot (6.4.6)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
railties (>= 5.0.0)
faker (3.2.3)
i18n (>= 1.8.11, < 2)
globalid (1.2.1)
@@ -176,6 +182,23 @@ GEM
reline (0.4.2)
io-console (~> 0.5)
rexml (3.2.6)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-rails (6.1.2)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.13)
rspec-expectations (~> 3.13)
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.1)
rubocop (1.63.5)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
@@ -189,6 +212,10 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.25.1)
rubocop (~> 1.41)
rubocop-migration (0.5.1)
activesupport
rubocop (>= 1.34)
@@ -201,6 +228,13 @@ GEM
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rspec (2.29.2)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
rubocop-rspec_rails (~> 2.28)
rubocop-rspec_rails (2.28.3)
rubocop (~> 1.40)
ruby-progressbar (1.13.0)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
@@ -239,6 +273,7 @@ DEPENDENCIES
bundler-audit
cssbundling-rails
debug
factory_bot_rails
faker
interactor (~> 3.0)
jbuilder
@@ -247,9 +282,13 @@ DEPENDENCIES
puma
rails (~> 7.0.8)
redis (~> 4.0)
rspec-rails (~> 6.1.0)
rubocop-factory_bot
rubocop-migration
rubocop-performance
rubocop-rails
rubocop-rspec
rubocop-rspec_rails
sprockets-rails
stimulus-rails
turbo-rails
9 changes: 9 additions & 0 deletions spec/factories/companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

FactoryBot.define do
factory :company do
name { Faker::Company.name }
employee_count { rand(10..1000) }
industry { Faker::Company.industry }
end
end
10 changes: 10 additions & 0 deletions spec/factories/deals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

FactoryBot.define do
factory :deal do
sequence(:name) { |n| "Deal #{n}" }
amount { rand(10..1000) }
status { %w[pending won lost].sample }
association :company
end
end
51 changes: 51 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'

Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f }

begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
abort e.to_s.strip
end
RSpec.configure do |config|
# config.include Requests::JsonHelpers, type: :controller
config.include Requests::JsonHelpers, type: :request
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = Rails.root.join('spec/fixtures')

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# You can uncomment this line to turn off ActiveRecord support entirely.
# config.use_active_record = false

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, type: :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://rspec.info/features/6-0/rspec-rails
config.infer_spec_type_from_file_location!

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
114 changes: 114 additions & 0 deletions spec/requests/filter_companies_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'FilterCompanies', type: :request do
describe 'GET /api/v1/companies' do
subject(:companies_filter_request) { get api_v1_companies_path, params: }

context 'when there are no companies' do
let(:params) {}

it 'works!' do
subject
expect(response).to have_http_status(:ok)
end
end

context 'when there are companies' do
let!(:company1) do
create(:company, name: 'Company1', industry: 'Cosmetics', employee_count: 1000)
end
let!(:company2) do
create(:company, name: 'Company2', industry: 'Test', employee_count: 2000)
end

context 'when filtering by name' do
let(:params) { { name: 'pany1' } }

it 'works!' do
subject
expect(response).to have_http_status(:ok)
names = parsed_json.map { |company| company[:name] }
expect(names).to include('Company1')
expect(names).not_to include('Company2')
end
end

context 'when filtering by name and industry' do
let(:params) { { name: 'Compa', industry: 'test' } }

it 'works!' do
subject
expect(response).to have_http_status(:ok)
names = parsed_json.map { |company| company[:name] }

expect(names).to include('Company2')
expect(names).not_to include('Company1')
end
end

context 'when filtering by min_employee' do
let(:params) { { min_employee: 1500 } }

it 'works!' do
subject
expect(response).to have_http_status(:ok)
names = parsed_json.map { |company| company[:name] }
expect(names).to include('Company2')
expect(names).not_to include('Company1')
end
end

context 'when filtering by minimum_deal_amount' do
let!(:deals1) { create_list(:deal, 5, company: company1, amount: 100) }
let!(:deals2) { create_list(:deal, 4, company: company2, amount: 300) }
let(:params) { { minimum_deal_amount: 1000 } }

it 'works!' do
subject
expect(response).to have_http_status(:ok)
names = parsed_json.map { |company| company[:name] }
expect(names).to include('Company2')
expect(names).not_to include('Company1')
end
end

context 'when filtering by name, industry, min_employee, and minimum_deal_amount' do
let!(:deals1) { create_list(:deal, 5, company: company1, amount: 100) }
let!(:deals2) { create_list(:deal, 4, company: company2, amount: 300) }
let(:params) { { name: 'Compa', industry: 'test', min_employee: 1500, minimum_deal_amount: 1000 } }

it 'works!' do
subject
expect(response).to have_http_status(:ok)
names = parsed_json.map { |company| company[:name] }
expect(names).to include('Company2')
expect(names).not_to include('Company1')
end
end
end

context 'when filtering by invalid minimum_deal_amount' do
let(:params) { { minimum_deal_amount: 'invalid' } }

it 'works!' do
subject
expect(response).to have_http_status(:unprocessable_entity)
expect(parsed_json).to have_key(:error)
expect(parsed_json[:error]).to eq('Minimum deal amount should be an integer')
end
end

context 'when filtering by invalid min_employee' do
let(:params) { { min_employee: 'invalid' } }

it 'works!' do
subject
expect(response).to have_http_status(:unprocessable_entity)
expect(parsed_json).to have_key(:error)
expect(parsed_json[:error]).to eq('Minimum employee count should be an integer')
end
end
end
end
Loading

0 comments on commit 0a5dbf7

Please sign in to comment.