-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.