Skip to content

Commit

Permalink
added rspec, and wrote tests for all models
Browse files Browse the repository at this point in the history
  • Loading branch information
srinjoyc committed Mar 22, 2018
1 parent 67aea3b commit 4235f81
Show file tree
Hide file tree
Showing 18 changed files with 360 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'rspec-rails', '>= 3.5.0'
gem 'shoulda-matchers', '~> 3.1'
gem 'database_cleaner'
gem 'factory_girl'
gem 'faker'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ GEM
byebug (10.0.0)
concurrent-ruby (1.0.5)
crass (1.0.3)
database_cleaner (1.6.2)
diff-lcs (1.3)
erubi (1.7.1)
factory_girl (4.9.0)
activesupport (>= 3.0.0)
faker (1.8.7)
i18n (>= 0.7)
ffi (1.9.23)
globalid (0.4.1)
activesupport (>= 4.2.0)
Expand Down Expand Up @@ -97,7 +103,26 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-rails (3.7.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
ruby_dep (1.5.0)
shoulda-matchers (3.1.2)
activesupport (>= 4.0.0)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand All @@ -123,10 +148,15 @@ PLATFORMS

DEPENDENCIES
byebug
database_cleaner
factory_girl
faker
listen (>= 3.0.5, < 3.2)
pg (~> 0.18)
puma (~> 3.7)
rails (~> 5.1.1)
rspec-rails (>= 3.5.0)
shoulda-matchers (~> 3.1)
spring
spring-watcher-listen (~> 2.0.0)
tzinfo-data
Expand Down
2 changes: 2 additions & 0 deletions app/models/guest.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Guest < ApplicationRecord
validates :name, :email, presence: true
validates :email, uniqueness: true, format: { with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i }
end
9 changes: 9 additions & 0 deletions app/models/reservation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ class Reservation < ApplicationRecord
belongs_to :guest
belongs_to :restaurant_table
belongs_to :restaurant_shift

validates_associated :restaurant, :guest, :restaurant_table, :restaurant_shift
validates :guest_count, :reservation_time, :restaurant_id, :restaurant_shift_id, :restaurant_table_id, presence: true
before_save :valid_guest_count

private
def valid_guest_count
guest_count > 0
end
end
2 changes: 2 additions & 0 deletions app/models/restaurant.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Restaurant < ApplicationRecord
validates :name, :email, :phone_number, presence: true
validates :email, uniqueness: true, format: { with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i }
end
9 changes: 9 additions & 0 deletions app/models/restaurant_shift.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
class RestaurantShift < ApplicationRecord
belongs_to :restaurant

validates_associated :restaurant
validates :start_time, :end_time, :name, :restaurant_id, presence: true
before_save :has_no_conflicts

private
def has_no_conflicts
return true
end
end
10 changes: 10 additions & 0 deletions app/models/restaurant_table.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
class RestaurantTable < ApplicationRecord
belongs_to :restaurant

validates_associated :restaurant
validates :minimum_count, :maximum_count, :restaurant_id, presence: true
before_save :valid_table_count

private
def valid_table_count
maximum_count > minimum_count && minimum_count > 0
end

end
4 changes: 2 additions & 2 deletions db/migrate/20180321014957_create_restaurant_shifts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class CreateRestaurantShifts < ActiveRecord::Migration[5.1]
def change
create_table :restaurant_shifts do |t|
t.references :restaurant, foreign_key: true
t.date :start_time
t.date :end_time
t.time :start_time
t.time :end_time
t.string :name

t.timestamps
Expand Down
36 changes: 36 additions & 0 deletions spec/factories/factories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "faker"
FactoryGirl.define do

factory :guest do
name Faker::Name.name
email Faker::Internet.email
end

factory :restaurant do
name Faker::Name.name
email Faker::Internet.email
phone_number Faker::PhoneNumber.phone_number
end

factory :restaurant_table do
maximum_count Faker::Number.between(3, 10)
minimum_count Faker::Number.between(1,2)
restaurant { Restaurant.first || association(:restaurant) }
end

factory :restaurant_shift do
name Faker::Name.name
start_time Faker::Time.between(2.days.ago, Date.today, :morning)
end_time Faker::Time.between(2.days.ago, Date.today, :night)
restaurant { Restaurant.first || association(:restaurant) }
end

factory :reservation do
guest_count Faker::Number.between(1, 100)
reservation_time Faker::Time.forward(2, :morning)
restaurant { Restaurant.first || association(:restaurant) }
guest { Guest.first || association(:guest) }
restaurant_table { RestaurantTable.first || association(:restaurant_table) }
restaurant_shift { RestaurantShift.first || association(:restaurant_shift) }
end
end
14 changes: 14 additions & 0 deletions spec/models/guest_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe Guest, type: :model do

it 'has a valid factory' do
expect(FactoryGirl.create(:guest)).to be_valid
end

context 'validations' do
it { is_expected.to validate_presence_of :email }
it { is_expected.to validate_uniqueness_of :email }
it { is_expected.to validate_presence_of :name }
end
end
16 changes: 16 additions & 0 deletions spec/models/reservations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

RSpec.describe Reservation, type: :model do

it 'has a valid factory' do
expect(FactoryGirl.create(:reservation)).to be_valid
end

context 'validations' do
it { is_expected.to validate_presence_of :guest_count }
it { is_expected.to validate_presence_of :reservation_time }
it { is_expected.to validate_presence_of :restaurant_id }
it { is_expected.to validate_presence_of :restaurant_table_id }
it { is_expected.to validate_presence_of :restaurant_shift_id }
end
end
15 changes: 15 additions & 0 deletions spec/models/restaurant_shift_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe RestaurantShift, type: :model do

it 'has a valid factory' do
expect(FactoryGirl.create(:restaurant_shift)).to be_valid
end

context 'validations' do
it { is_expected.to validate_presence_of :start_time }
it { is_expected.to validate_presence_of :end_time }
it { is_expected.to validate_presence_of :name }
it { is_expected.to validate_presence_of :restaurant_id }
end
end
15 changes: 15 additions & 0 deletions spec/models/restaurant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe Restaurant, type: :model do

it 'has a valid factory' do
expect(FactoryGirl.create(:restaurant)).to be_valid
end

context 'validations' do
it { is_expected.to validate_presence_of :email }
it { is_expected.to validate_uniqueness_of :email }
it { is_expected.to validate_presence_of :name }
it { is_expected.to validate_presence_of :phone_number }
end
end
14 changes: 14 additions & 0 deletions spec/models/restaurant_table_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe RestaurantTable, type: :model do

it 'has a valid factory' do
expect(FactoryGirl.create(:restaurant_table)).to be_valid
end

context 'validations' do
it { is_expected.to validate_presence_of :maximum_count }
it { is_expected.to validate_presence_of :minimum_count }
it { is_expected.to validate_presence_of :restaurant_id }
end
end
64 changes: 64 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
require 'shoulda/matchers'
require 'faker'
require 'database_cleaner'
require 'factory_girl'
require 'support/shoulda'
require 'support/database_cleaner'
require 'factories/factories.rb'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/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

# 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://relishapp.com/rspec/rspec-rails/docs
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
Loading

0 comments on commit 4235f81

Please sign in to comment.