agenda-libre-ruby/test/models/event_test.rb
2014-09-05 22:15:38 +02:00

130 lines
3.3 KiB
Ruby

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',
start_time: Time.new,
end_time: Time.new + 1,
description: 'et hop!',
city: City.first,
related_region: Region.first,
url: 'http://example.com',
contact: 'contact@example.com',
submitter: 'submitter@example.com'
)
assert @event.save, @event.errors.messages
assert_equal 32, @event.secret.size
assert_equal 32, @event.moderator_mail_id.size
assert_equal 32, @event.submitter_mail_id.size
end
test 'validations' do
@event = Event.new(
title: 'hello world',
start_time: Time.new,
end_time: Time.new + 1,
description: 'et hop!',
city: City.first,
related_region: Region.first,
url: 'http://example.com',
contact: 'contact@example.com'
)
assert @event.valid?, @event.errors.messages
assert_equal @event.contact, @event.submitter
@event.contact = 'hop@@@'
assert !@event.valid?, @event.errors.messages
@event.contact = 'contact@example.com'
assert @event.valid?, @event.errors.messages
# Check invalid url
@event.url = 'htt://truc.com'
assert !@event.valid?, @event.errors.messages
@event.url = 'http:/truc.com'
assert !@event.valid?, @event.errors.messages
end
test 'moderation' do
@event = Event.new(
title: 'hello world',
start_time: Time.new,
end_time: Time.new + 1,
description: 'et hop!',
city: City.first,
related_region: Region.first,
url: 'http://example.com',
contact: 'contact@example.com'
)
assert @event.save, @event.errors.messages
assert !@event.moderated?
@event.update moderated: 1
assert @event.moderated?, @event.errors.messages
end
test 'named scope future.in' do
assert Event.respond_to? :future
assert Event.respond_to? :in
assert_match(/<= end_time/, Event.future.in(nil).where_values[0])
assert_match(/end_time <=/, Event.future.in(nil).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.as_json
assert_equal 'Feature', @event.as_json[:type]
assert_equal 'Point', @event.as_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