Tests for courses

master
Alinson S. Xavier 10 years ago
parent effa6f2f4d
commit e2d754a96e

@ -22,6 +22,11 @@ gem 'icalendar', '~>1.0'
gem 'jquery-rails'
gem 'shoulda', "~>2.11"
gem 'will_paginate', "~>3.0"
gem 'simplecov'
group :test do
gem 'turn'
end
#group :development, :production do
# gem 'brazilian-rails', '3.3.0', :path => './vendor/gems/brazilian-rails/brI18n/'

@ -44,6 +44,7 @@ GEM
activerecord (>= 3.0)
acts_as_paranoid (0.4.2)
activerecord (~> 3.2)
ansi (1.5.0)
arel (3.0.2)
builder (3.0.4)
calendar_helper (0.2.5)
@ -55,6 +56,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.6.3)
docile (1.1.5)
dynamic_form (1.1.4)
erubis (2.7.0)
execjs (1.4.0)
@ -73,6 +75,7 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.23)
minitest (4.7.5)
multi_json (1.7.7)
open4 (1.3.0)
polyglot (0.3.3)
@ -107,6 +110,11 @@ GEM
sass (>= 3.1.10)
tilt (~> 1.3)
shoulda (2.11.3)
simplecov (0.10.0)
docile (~> 1.1.0)
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
sprockets (2.2.2)
hike (~> 1.2)
multi_json (~> 1.0)
@ -118,6 +126,9 @@ GEM
treetop (1.4.14)
polyglot
polyglot (>= 0.3.1)
turn (0.9.7)
ansi
minitest (~> 4)
tzinfo (0.3.37)
uglifier (2.1.2)
execjs (>= 0.3.0)
@ -143,7 +154,9 @@ DEPENDENCIES
rails (~> 3.2)
sass-rails (~> 3.2.3)
shoulda (~> 2.11)
simplecov
sqlite3
turn
uglifier (>= 1.0.3)
will_paginate (~> 3.0)

@ -17,59 +17,63 @@
class Course < ActiveRecord::Base
# Plugins
acts_as_paranoid
# Associacoes
has_many :attachments,
:order => "path is not null, path, file_name",
:dependent => :destroy
acts_as_paranoid
has_many :events,
:order => "time asc",
:dependent => :destroy
has_many :attachments,
:order => 'path is not null, path, file_name',
:dependent => :destroy
has_many :news,
:foreign_key => "receiver_id",
:order => "id desc",
:dependent => :destroy
has_many :events,
:order => 'time asc',
:dependent => :destroy
has_many :log_entries,
:order => "created_at desc",
:dependent => :destroy
has_many :news,
:foreign_key => 'receiver_id',
:order => 'id desc',
:class_name => 'News',
:dependent => :destroy
has_many :wiki_pages,
:order => "position",
:dependent => :destroy
has_many :log_entries,
:order => 'created_at desc',
:dependent => :destroy
has_and_belongs_to_many :users,
:order => "last_seen desc"
# Validacao
validates_presence_of :short_name
validates_presence_of :full_name
validates_presence_of :code
validates_numericality_of :grade, :only_integer => true
validates_inclusion_of :hidden, :in => [true, false], :allow_nil => false
validates_format_of :short_name, :with => /^[^0-9]/
has_many :wiki_pages,
:order => 'position',
:dependent => :destroy
def related_courses
Course.find(:all, :conditions => [ 'short_name = ?', self.short_name], :limit => 4, :order => 'period desc')
end
has_and_belongs_to_many :users,
:order => 'last_seen desc'
def recent_news
self.news.find(:all, :conditions => [ 'timestamp > ?', 7.days.ago ])
end
def after_create
App.inital_wiki_pages.each do |page_title|
wiki_page = WikiPage.new(:title => page_title, :version => 1, :content => App.initial_wiki_page_content)
self.wiki_pages << wiki_page
end
end
validates_presence_of :short_name
validates_presence_of :full_name
validates_presence_of :code
validates_numericality_of :grade, :only_integer => true
validates_inclusion_of :hidden, :in => [true, false], :allow_nil => false
validates_format_of :short_name, :with => /^[^0-9]/
def to_param
return self.short_name if self.period == App.current_period
return self.id.to_s
end
after_create :add_initial_wiki_pages
def related_courses
Course.all(:conditions => ['short_name = ?', self.short_name], :limit => 4,
:order => 'period desc')
end
def recent_news
self.news.all(:conditions => ['timestamp > ?', 7.days.ago])
end
def add_initial_wiki_pages
App.initial_wiki_pages.each do |page_title|
wiki_page = WikiPage.new(:title => page_title, :description => 'New course',
:content => App.initial_wiki_page_content)
wiki_page.user = User.first
self.wiki_pages << wiki_page
wiki_page.save!
end
end
def to_param
return self.short_name if self.period == App.current_period
return self.id.to_s
end
end

@ -17,25 +17,25 @@
class Message < ActiveRecord::Base
# Plugins
acts_as_paranoid
acts_as_versioned :if_changed => [ :title, :body ]
self.non_versioned_columns << 'deleted_at'
acts_as_paranoid
acts_as_versioned :if_changed => [:title, :body]
self.non_versioned_columns << 'deleted_at'
#acts_as_paranoid_versioned
self.versioned_class.class_eval do
def self.delete_all(conditions = nil); return; end
end
# Associacoes
belongs_to :user,
:foreign_key => "sender_id",
:with_deleted => true
#acts_as_paranoid_versioned
self.versioned_class.class_eval do
def self.delete_all(conditions = nil)
return
end
end
belongs_to :user,
:foreign_key => "sender_id",
:with_deleted => true
end
class News < Message
validates_presence_of :title
belongs_to :course,
:foreign_key => "receiver_id"
validates_presence_of :title
belongs_to :course,
:foreign_key => "receiver_id"
end

@ -29,3 +29,11 @@ old_course:
description: Description goes here
period: 1970.1
hidden: false
related_course:
id: 3
short_name: course1
full_name: Third course
description: Hello world
period: 1970.2
hidden: false

@ -14,15 +14,47 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#require File.dirname(__FILE__) + '/../test_helper'
#
#class CourseTest < ActiveSupport::TestCase
#
# fixtures :courses
#
# def test_truth
# assert true
# end
#
#end
#
require File.dirname(__FILE__) + '/../test_helper'
class CourseTest < ActiveSupport::TestCase
fixtures :courses
def test_related_courses
course = courses(:course_1)
related = courses(:related_course)
assert course.related_courses.include?(related)
end
def test_initial_wiki_pages
Course.create!(:short_name => 'course999',
:full_name => 'Course 999', :code => 123, :grade => 1)
course = Course.find_by_short_name('course999')
assert course.wiki_pages.length == App.initial_wiki_pages.length
end
def test_to_param
current_course = courses(:course_1)
old_course = courses(:related_course)
assert current_course.to_param == current_course.short_name
assert old_course.to_param == old_course.id.to_s
end
def test_recent_news
course = courses(:related_course)
user = User.first
assert course.news.length == 0
news = course.news.create!(:title => 'hello', :body => 'hello',
:timestamp => 1.hour.ago, :sender_id => user.id,
:receiver_id => course.id, :version => 1)
assert course.recent_news.include?(news)
news = course.news.create!(:title => 'hello', :body => 'hello',
:timestamp => 1.month.ago, :sender_id => user.id,
:receiver_id => course.id, :version => 1)
assert !course.recent_news.include?(news)
end
end