Couverture de tests presque à 100%

This commit is contained in:
echarp 2014-09-03 02:14:21 +02:00
parent 7da30d42a5
commit 8f1d374eb5
9 changed files with 103 additions and 24 deletions

View File

@ -5,9 +5,7 @@ class MapsController < ApplicationController
def index def index
respond_to do |format| respond_to do |format|
format.html format.html
format.json do format.json { render json: Event.moderated.future.geo }
render json: Event.moderated.future.geo.map { |event| event.to_json }
end
end end
end end
end end

View File

@ -69,17 +69,15 @@ class Event < ActiveRecord::Base
end end
end end
def to_json def as_json(_options = {})
{ type: 'Feature', properties: { { type: 'Feature', properties: {
name: title, name: title,
popupContent: "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\"" \ popupContent: "<a href=\"/#{self.class.name.downcase.pluralize}/#{id}\"" \
+ ">#{city}: #{title}</a>" + ">#{city}: #{title}</a>"
}, }, geometry: {
geometry: { type: 'Point',
type: 'Point', coordinates: [longitude, latitude]
coordinates: [longitude, latitude] } }
}
}
end end
def full_address def full_address

View File

@ -3,6 +3,8 @@ class Note < ActiveRecord::Base
belongs_to :event belongs_to :event
belongs_to :author, class_name: User belongs_to :author, class_name: User
validates :contents, presence: true
# Setup the magic time stamp so it uses the "date" column # Setup the magic time stamp so it uses the "date" column
def timestamp_attributes_for_create def timestamp_attributes_for_create
super << :date super << :date

View File

@ -10,6 +10,8 @@ class User < ActiveRecord::Base
has_many :notes has_many :notes
validates :login, presence: true
def encrypted_password=(pass) def encrypted_password=(pass)
self[:password] = pass self[:password] = pass
end end

View File

@ -57,6 +57,18 @@ class EventsControllerTest < ActionController::TestCase
assert_redirected_to root_url assert_redirected_to root_url
end end
test 'should not create event' do
assert_no_difference 'Event.count' do
post :create, event: {
title: @event.title,
city: @event.city,
region: @event.related_region
}
assert_not_empty assigns(:event).errors.messages
end
end
test 'should show event' do test 'should show event' do
get :show, id: @event get :show, id: @event
assert_response :success assert_response :success
@ -93,6 +105,14 @@ class EventsControllerTest < ActionController::TestCase
assert_redirected_to :root assert_redirected_to :root
end end
test 'should not update event' do
patch :update, id: @event, secret: 'MyString', event: {
title: nil
}
assert_not_empty assigns(:event).errors.messages
end
test 'should get cancel page' do test 'should get cancel page' do
get :cancel, id: @event, secret: 'MyString' get :cancel, id: @event, secret: 'MyString'
assert_response :success assert_response :success

View File

@ -11,6 +11,12 @@ class ModerationsControllerTest < ActionController::TestCase
sign_in users(:one) sign_in users(:one)
end end
test 'should get index' do
get :index
assert_response :success
assert_not_nil assigns(:events)
end
test 'should preview event' do test 'should preview event' do
patch :preview, id: @moderation, event: { patch :preview, id: @moderation, event: {
title: 'hello world', title: 'hello world',
@ -26,6 +32,13 @@ class ModerationsControllerTest < ActionController::TestCase
assert_redirected_to moderations_path assert_redirected_to moderations_path
end end
test 'should not edit event' do
put :update, id: @moderation, event: {
title: nil
}
assert_not_empty assigns(:moderation).errors
end
test 'should accept event' do test 'should accept event' do
@moderation = events :proposed @moderation = events :proposed
@ -50,21 +63,23 @@ class ModerationsControllerTest < ActionController::TestCase
start_time: @moderation.start_time, start_time: @moderation.start_time,
end_time: @moderation.end_time, end_time: @moderation.end_time,
description: @moderation.description, description: @moderation.description,
city: @moderation.city,
region: @moderation.related_region,
locality: @moderation.locality,
url: @moderation.url, url: @moderation.url,
contact: @moderation.contact, contact: @moderation.contact,
moderated: @moderation.moderated, tags: @moderation.tags
secret: @moderation.secret, }
submission_time: @moderation.submission_time,
submitter: @moderation.submitter,
tags: @moderation.tags }
assert_empty assigns(:moderation).errors assert_empty assigns(:moderation).errors
assert_redirected_to moderations_path assert_redirected_to moderations_path
end end
test 'should not update event' do
patch :update, id: @moderation, secret: 'MyString', event: {
title: nil
}
assert_not_empty assigns(:moderation).errors
end
test 'should reject event' do test 'should reject event' do
assert_difference 'Event.count', -1 do assert_difference 'Event.count', -1 do
delete :destroy, id: @moderation delete :destroy, id: @moderation
@ -74,4 +89,14 @@ class ModerationsControllerTest < ActionController::TestCase
assert_redirected_to moderations_path assert_redirected_to moderations_path
end end
test 'should reject event with a reason' do
assert_difference 'Event.count', -1 do
delete :destroy, id: @moderation, reason: 'r_4'
end
assert_empty assigns(:moderation).errors
assert_redirected_to moderations_path
end
end end

View File

@ -18,12 +18,30 @@ class NotesControllerTest < ActionController::TestCase
test 'should create note' do test 'should create note' do
assert_difference('Note.count') do assert_difference('Note.count') do
post :create, moderation_id: @note.event.id, note: { post :create, moderation_id: @note.event.id, note: {
author: @note.author, contents: @note.contents
contents: @note.contents,
date: @note.date
} }
end end
assert_redirected_to moderations_path assert_redirected_to moderations_path
end end
test 'should send mail' do
assert_difference('Note.count') do
post :create, moderation_id: @note.event.id, envoiParMail: 'oui', note: {
contents: @note.contents
}
end
assert ActionMailer::Base.deliveries.present?
assert_redirected_to moderations_path
end
test 'should not create note' do
assert_no_difference('Note.count') do
post :create, moderation_id: @note.event.id, note: {
nothing: 'almost'
}
end
end
end end

View File

@ -35,6 +35,14 @@ class UsersControllerTest < ActionController::TestCase
assert_redirected_to user_path(assigns(:user)) assert_redirected_to user_path(assigns(:user))
end end
test 'should not create user' do
assert_no_difference('User.count') do
post :create, user: {
login: nil
}
end
end
test 'should show user' do test 'should show user' do
get :show, id: @user get :show, id: @user
assert_response :success assert_response :success
@ -55,6 +63,14 @@ class UsersControllerTest < ActionController::TestCase
assert_redirected_to user_path(assigns(:user)) assert_redirected_to user_path(assigns(:user))
end end
test 'should not update user' do
patch :update, id: @user, user: {
login: nil
}
assert_not_empty assigns(:user).errors
end
test 'should destroy user' do test 'should destroy user' do
assert_difference('User.count', -1) do assert_difference('User.count', -1) do
delete :destroy, id: @user delete :destroy, id: @user

View File

@ -113,10 +113,10 @@ class EventTest < ActiveSupport::TestCase
end end
test 'json transform' do test 'json transform' do
assert_not_nil @event.to_json assert_not_nil @event.as_json
assert_equal 'Feature', @event.to_json[:type] assert_equal 'Feature', @event.as_json[:type]
assert_equal 'Point', @event.to_json[:geometry][:type] assert_equal 'Point', @event.as_json[:geometry][:type]
end end
test 'full address' do test 'full address' do