Couverture de tests

This commit is contained in:
echarp 2014-09-02 01:02:51 +02:00
parent 242e9f5d40
commit 151e4937c4
8 changed files with 101 additions and 47 deletions

View File

@ -105,6 +105,10 @@ group :development do
gem 'guard-brakeman' gem 'guard-brakeman'
end end
group :test do
gem 'simplecov'
end
group :production do group :production do
# Use mysql as the database for Active Record # Use mysql as the database for Active Record
gem 'mysql2' gem 'mysql2'

View File

@ -1,6 +1,6 @@
GIT GIT
remote: git://github.com/gregbell/active_admin.git remote: git://github.com/gregbell/active_admin.git
revision: 759a9bb7f8dab1b4cb9e23832febf7d0c370ebe4 revision: 28c5ca569acddc162e1179326872da17a934dbb7
specs: specs:
activeadmin (1.0.0.pre) activeadmin (1.0.0.pre)
arbre (~> 1.0, >= 1.0.2) arbre (~> 1.0, >= 1.0.2)
@ -100,6 +100,7 @@ GEM
warden (~> 1.2.3) warden (~> 1.2.3)
devise-i18n (0.11.0) devise-i18n (0.11.0)
differ (0.1.2) differ (0.1.2)
docile (1.1.5)
em-websocket (0.5.1) em-websocket (0.5.1)
eventmachine (>= 0.12.9) eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0) http_parser.rb (~> 0.6.0)
@ -266,6 +267,11 @@ GEM
sexp_processor (4.4.4) sexp_processor (4.4.4)
simple_calendar (1.1.5) simple_calendar (1.1.5)
rails (>= 3.0) rails (>= 3.0)
simplecov (0.9.0)
docile (~> 1.1.0)
multi_json
simplecov-html (~> 0.8.0)
simplecov-html (0.8.0)
slim (2.0.3) slim (2.0.3)
temple (~> 0.6.6) temple (~> 0.6.6)
tilt (>= 1.3.3, < 2.1) tilt (>= 1.3.3, < 2.1)
@ -348,6 +354,7 @@ DEPENDENCIES
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
select2-rails select2-rails
simple_calendar simple_calendar
simplecov
spring spring
sqlite3 sqlite3
therubyracer therubyracer

View File

@ -25,7 +25,7 @@ module EventsHelper
end end
def display_date(event = @event) def display_date(event = @event)
if event.same_day? if event.start_time.to_date == event.end_time.to_date
t 'date.formats.same_day', t 'date.formats.same_day',
date: l(event.start_time.to_date, format: :long), date: l(event.start_time.to_date, format: :long),
start: l(event.start_time, format: :hours), start: l(event.start_time, format: :hours),

View File

@ -22,18 +22,11 @@ class Event < ActiveRecord::Base
scope :moderated, -> { where moderated: true } scope :moderated, -> { where moderated: true }
scope :unmoderated, -> { where moderated: false } scope :unmoderated, -> { where moderated: false }
scope :last_year, -> { where 'start_time >= ?', 360.days.ago } scope :last_year, -> { where '? <= end_time', 360.days.ago }
scope :past, (lambda do scope :past, -> { where 'start_time <= ?', DateTime.now }
where('end_time < ?', DateTime.now).order start_time: :desc scope :future, -> { where '? <= end_time', DateTime.now }
end) scope :future_in, (lambda do |days = 30|
scope :future, (lambda do future.where('end_time <= ?', days.to_i.days.from_now).order :start_time
where('? <= end_time', DateTime.now).order start_time: :asc
end)
scope :future_in, (lambda do |days|
days ||= '30'
where('? <= start_time and end_time <= ?',
DateTime.now, days.to_i.days.from_now)
.order :start_time
end) end)
scope :year, (lambda do |year| scope :year, (lambda do |year|
where '? <= end_time and start_time <= ?', where '? <= end_time and start_time <= ?',
@ -76,10 +69,6 @@ class Event < ActiveRecord::Base
end end
end end
def same_day?
start_time.to_date == end_time.to_date
end
def to_json def to_json
{ type: 'Feature', properties: { { type: 'Feature', properties: {
name: title, name: title,

View File

@ -19,20 +19,17 @@ class EventsControllerTest < ActionController::TestCase
assert_response :success assert_response :success
end end
test 'should preview event' do test 'should preview event creation' do
assert_no_difference 'Event.count' do assert_no_difference 'Event.count' do
patch :preview_create, event: { post :preview_create, event: {
title: @event.title, title: @event.title,
start_time: @event.start_time, start_time: @event.start_time,
end_time: @event.end_time, end_time: @event.end_time,
description: @event.description, description: @event.description,
city: @event.city, city: @event.city,
region: @event.related_region, region: @event.related_region,
locality: @event.locality,
url: @event.url, url: @event.url,
contact: @event.contact, contact: @event.contact
submitter: @event.submitter,
tags: @event.tags
} }
assert_empty assigns(:event).errors assert_empty assigns(:event).errors
@ -50,11 +47,8 @@ class EventsControllerTest < ActionController::TestCase
description: @event.description, description: @event.description,
city: @event.city, city: @event.city,
region: @event.related_region, region: @event.related_region,
locality: @event.locality,
url: @event.url, url: @event.url,
contact: @event.contact, contact: @event.contact
submitter: @event.submitter,
tags: @event.tags
} }
assert_empty assigns(:event).errors.messages assert_empty assigns(:event).errors.messages
@ -78,22 +72,22 @@ class EventsControllerTest < ActionController::TestCase
assert_redirected_to root_url assert_redirected_to root_url
end end
test 'should preview' do
assert_no_difference 'Event.count' do
patch :preview, id: @event, secret: 'MyString', event: {
title: @event.title
}
assert_empty assigns(:event).errors
end
assert_response :success
end
test 'should update event' do test 'should update event' do
patch :update, id: @event, secret: 'MyString', event: { patch :update, id: @event, secret: 'MyString', event: {
title: @event.title, title: @event.title
start_time: @event.start_time, }
end_time: @event.end_time,
description: @event.description,
city: @event.city,
region: @event.related_region,
locality: @event.locality,
url: @event.url,
contact: @event.contact,
moderated: @event.moderated,
secret: @event.secret,
submission_time: @event.submission_time,
submitter: @event.submitter,
tags: @event.tags }
assert_empty assigns(:event).errors.messages assert_empty assigns(:event).errors.messages
assert_redirected_to :root assert_redirected_to :root

View File

@ -3,8 +3,8 @@
one: one:
title: MyString title: MyString
description: MyText description: MyText
start_time: 2013-12-28 16:04:56 start_time: <%= 2.hours.from_now %>
end_time: 2013-12-28 16:04:56 end_time: <%= 2.hours.from_now %>
city: city_one.name city: city_one.name
related_region: region_one related_region: region_one
locality: 1 locality: 1
@ -14,8 +14,8 @@ one:
moderated: 1 moderated: 1
tags: MyString tags: MyString
secret: MyString secret: MyString
decision_time: 2013-12-28 16:04:56 decision_time: <%= 2.days.ago %>
submission_time: 2013-12-28 16:04:56 submission_time: <%= 3.days.ago %>
moderator_mail_id: MyString moderator_mail_id: MyString
submitter_mail_id: MyString submitter_mail_id: MyString

View File

@ -2,6 +2,22 @@ require 'test_helper'
# Test events, which are the application central part # Test events, which are the application central part
class EventTest < ActiveSupport::TestCase class EventTest < ActiveSupport::TestCase
setup do
@event = events :one
Geocoder.configure lookup: :test
Geocoder::Lookup::Test.set_default_stub [{
'latitude' => 40.7143528,
'longitude' => -74.0059731,
'address' => 'New York, NY, USA',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US'
}]
end
test 'basic event' do test 'basic event' do
@event = Event.new( @event = Event.new(
title: 'hello world', title: 'hello world',
@ -65,8 +81,48 @@ class EventTest < ActiveSupport::TestCase
assert @event.save, @event.errors.messages assert @event.save, @event.errors.messages
assert !@event.moderated? assert !@event.moderated?
@event.update(moderated: 1) @event.update moderated: 1
assert @event.moderated?, @event.errors.messages assert @event.moderated?, @event.errors.messages
end end
test 'named scope future_in' do
assert Event.respond_to? :future_in
assert_match(/<= end_time/, Event.future_in.where_values[0])
assert_match(/end_time <=/, Event.future_in.where_values[1])
end
test 'named scope year' do
assert Event.respond_to? :year
assert_match(/<= end_time/, Event.year(2014).where_values[0])
assert_match(/start_time <=/, Event.year(2014).where_values[0])
end
test 'geo data reset' do
# Setup geo data
@event.latitude = 3
@event.longitude = 3
# Change address to ensure geo data is reset
@event.address = 'hello world'
assert @event.valid?
assert @event.save
assert_nil @event.latitude
assert_nil @event.longitude
end
test 'json transform' do
assert_not_nil @event.to_json
assert_equal 'Feature', @event.to_json[:type]
assert_equal 'Point', @event.to_json[:geometry][:type]
end
test 'full address' do
@event.address = 'hello'
@event.city = 'world'
@event.related_region.name = 'all'
assert_equal 'hello, world, all', @event.full_address
end
end end

View File

@ -2,6 +2,10 @@ ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__) require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help' require 'rails/test_help'
require 'simplecov'
SimpleCov.start 'rails'
module ActiveSupport module ActiveSupport
# Standard helper class for almost all tests # Standard helper class for almost all tests
class TestCase class TestCase