Skip to content

Commit

Permalink
Add Rspec tests for ApiController
Browse files Browse the repository at this point in the history
Remove unused game_id model, add scaffold for api spec
Add factory_girl instead of using rails fixtures
Remove the rest of the test dir
  • Loading branch information
djds23 committed Jan 5, 2016
1 parent 0af8b22 commit c3d9f7e
Show file tree
Hide file tree
Showing 20 changed files with 346 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
trebek.psd
*.swp
.DS_Store

.byebug_history
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
32 changes: 15 additions & 17 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,17 @@ source 'https://rubygems.org'

gem 'rails', '4.0.2'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

#gem 'sqlite3'
gem 'pg'

# Gems used only for assets and not required
# in production environments by default.
group :assets do

gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer'
gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

gem 'sass-rails', '>=4.0.0'
gem 'bootstrap-sass', '~> 3.1.0'

gem 'rails_12factor', group: :production

#for scraping
gem 'nokogiri'
gem 'nokogiri'
gem 'chronic'

#for search
Expand All @@ -36,7 +21,21 @@ gem 'pg_search'
#this mimics the rails 3 syntax, fix that and remove this at some point.
gem 'protected_attributes'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer'
gem 'uglifier', '>= 1.0.3'
end

group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
gem 'byebug'
end
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

Expand All @@ -52,4 +51,3 @@ gem 'protected_attributes'
# To use debugger
# gem 'debugger'


30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ GEM
bootstrap-sass (3.1.1.1)
sass (~> 3.2)
builder (3.1.4)
byebug (8.2.1)
chronic (0.10.2)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
Expand All @@ -37,8 +38,14 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.8.0)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.2.2)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
hike (1.2.3)
i18n (0.7.0)
jquery-rails (3.1.2)
Expand Down Expand Up @@ -86,6 +93,23 @@ GEM
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
ref (1.0.5)
rspec-core (3.4.1)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-rails (3.4.0)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
sass (3.4.10)
sass-rails (5.0.1)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -121,15 +145,21 @@ PLATFORMS

DEPENDENCIES
bootstrap-sass (~> 3.1.0)
byebug
chronic
coffee-rails (~> 4.0.0)
factory_girl_rails
jquery-rails
nokogiri
pg
pg_search
protected_attributes
rails (= 4.0.2)
rails_12factor
rspec-rails (~> 3.0)
sass-rails (>= 4.0.0)
therubyracer
uglifier (>= 1.0.3)

BUNDLED WITH
1.10.6
10 changes: 5 additions & 5 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def clues
def categories
offset = params[:offset].present? ? params[:offset] : 0
count = params[:count].present? ? params[:count] : 1

if(count.to_f > 100)
count = 100
end
@categories = Category.all(:limit => count, :offset => offset)

respond_to do |format|
format.json { render json: @categories }
end
Expand All @@ -56,9 +56,9 @@ def mark_invalid
@clue = Clue.find(params[:id])
@clue.increment(:invalid_count,1)
@clue.save

respond_to do |format|
format.json {render :json => @clue.to_json() }
end
format.json {render :json => @clue.to_json() }
end
end
end
5 changes: 2 additions & 3 deletions app/models/clue.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class Clue < ActiveRecord::Base
attr_accessible :airdate, :answer, :question, :value, :category_id, :category
belongs_to :category, :counter_cache => true


def to_json(options={})
def to_json(options={})
options[:except] ||= [:updated_at, :created_at]
super(options)
end
end
end
2 changes: 0 additions & 2 deletions app/models/game_id.rb

This file was deleted.

8 changes: 6 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ class Application < Rails::Application

# Enable the asset pipeline
config.assets.enabled = true

# Disable this to get it to not precompile on heroku when it wont succeed
#config.assets.initialize_on_precompile = true


# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.1'

config.generators do |g|
g.test_framework :rspec
end
end
end
7 changes: 3 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
JArchive::Application.routes.draw do

get "search" => "search#index"
get "search/:query" => "search#index", :as => "search_term"



get "popular" => "popular#index"
get "popular/:category_id" => "popular#list", :as => "popular_list"
get "api/random" => "api#random", defaults: {format: :json}
get "api/clues" => "api#clues", defaults: {format: :json}
get "api/categories" => "api#categories", defaults: {format: :json}
get "api/category" => "api#single_category", defaults: {format: :json}
post "api/invalid" => "api#mark_invalid", defaults: {format: :json}

get "index/home", :as => "home"
get "index/about"
resources :game_ids
Expand Down
9 changes: 2 additions & 7 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)

Category.create(title: "Placeholder")
112 changes: 112 additions & 0 deletions spec/controllers/api_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require 'rails_helper'

RSpec.describe ApiController, type: :controller do
describe '#clues' do
let!(:recent_clue) { FactoryGirl.create(:clue, airdate: Time.now - 1.week ) }
let!(:cheap_clue) { FactoryGirl.create(:clue, value: 100 ) }
let!(:new_clue ) { FactoryGirl.create(:clue, airdate: Time.now ) }

it 'only returns the proper valued clues' do
get :clues, value: cheap_clue.value, format: :json
expect(response).to be_success

body = JSON.parse(response.body)
expect(body.count).to eq(1)

clue = body.first
expect(clue["id"]).to eq(cheap_clue.id)
end

it 'only returns clues within the proper airdates' do
get :clues, min_date: recent_clue.airdate.to_s, max_date: (new_clue.airdate + 1.day).to_s, format: :json
expect(response).to be_success

body = JSON.parse(response.body)
expect(body.count).to eq(2)

response_ids = body.map { |h| h["id"] }
expect(response_ids).to include(recent_clue.id)
expect(response_ids).to include(new_clue.id)
end

it 'only returns clues from the proper category' do
get :clues, category: cheap_clue.category_id, format: :json
expect(response).to be_success
body = JSON.parse(response.body)
expect(body.count).to eq(1)

clue = body.first
expect(clue["id"]).to eq(cheap_clue.id)
end

it 'follows the correct offset' do
get :clues, offset: 2, format: :json
expect(response).to be_success
body = JSON.parse(response.body)
expect(body.count).to eq(1)
end
end

describe '#random' do
let!(:clue1) { FactoryGirl.create(:clue) }
let!(:clue2) { FactoryGirl.create(:clue) }

it 'returns the default of one clue' do
get :random, format: :json
expect(response).to be_success
body = JSON.parse(response.body)
expect(body.count).to eq(1)
end

it 'returns the passed count of clues' do
get :random, count: 2, format: :json
expect(response).to be_success
body = JSON.parse(response.body)
expect(body.count).to eq(2)
end
end

describe '#categories' do
let!(:category1) { FactoryGirl.create(:category) }
let!(:category2) { FactoryGirl.create(:category) }

it 'returns the correct count' do
get :categories, count: 1, format: :json
expect(response).to be_success
body = JSON.parse(response.body)
expect(body.count).to eq(1)
end

it 'returns the correct offset' do
get :categories, offset: 1, format: :json
expect(response).to be_success
body = JSON.parse(response.body)
expect(body.count).to eq(1)
end
end

describe '#single_category' do
let(:category) { FactoryGirl.create(:category) }
it 'returns the right category for id' do
get :single_category, id: category.id, format: :json
expect(response).to be_success

body = JSON.parse(response.body)
expect(body["id"]).to eq(category.id)
expect(body["title"]).to eq(category.title)
expect(body["clues_count"]).to eq(category.clues_count)
end
end

describe '#mark_invalid' do
let!(:clue) { FactoryGirl.create(:clue) }

it 'increments the invalid_count on selected clue' do
expect{
get :mark_invalid, id: clue.id, format: :json
expect(response).to be_success
}.to change{ clue.reload.invalid_count }.from(nil).to(1)
end
end
end

6 changes: 6 additions & 0 deletions spec/factories/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryGirl.define do
factory :category do
title "Names of things"
clues_count 1
end
end
9 changes: 9 additions & 0 deletions spec/factories/clue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryGirl.define do
factory :clue do
answer "John"
question "Last name Doe first name"
value 400
airdate Chronic.parse('1985-04-10 17:00:00')
category
end
end
Loading

0 comments on commit c3d9f7e

Please sign in to comment.