Couverture de tests

recurrent
echarp 8 years ago
parent 242e9f5d40
commit 151e4937c4
  1. 4
      Gemfile
  2. 9
      Gemfile.lock
  3. 2
      app/helpers/events_helper.rb
  4. 21
      app/models/event.rb
  5. 42
      test/controllers/events_controller_test.rb
  6. 8
      test/fixtures/events.yml
  7. 58
      test/models/event_test.rb
  8. 4
      test/test_helper.rb

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

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

@ -25,7 +25,7 @@ module EventsHelper
end
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',
date: l(event.start_time.to_date, format: :long),
start: l(event.start_time, format: :hours),

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

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

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

@ -2,6 +2,22 @@ require 'test_helper'
# Test events, which are the application central part
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
@event = Event.new(
title: 'hello world',
@ -65,8 +81,48 @@ class EventTest < ActiveSupport::TestCase
assert @event.save, @event.errors.messages
assert !@event.moderated?
@event.update(moderated: 1)
@event.update moderated: 1
assert @event.moderated?, @event.errors.messages
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

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

Loading…
Cancel
Save