2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2013-05-27 10:52:42 -04:00
|
|
|
require 'models/author'
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/post'
|
|
|
|
require 'models/comment'
|
|
|
|
require 'models/developer'
|
|
|
|
require 'models/project'
|
|
|
|
require 'models/reader'
|
|
|
|
require 'models/person'
|
2005-10-14 20:19:56 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class ReadOnlyTest < ActiveRecord::TestCase
|
2013-05-27 10:52:42 -04:00
|
|
|
fixtures :authors, :posts, :comments, :developers, :projects, :developers_projects, :people, :readers
|
2005-10-14 20:19:56 -04:00
|
|
|
|
|
|
|
def test_cant_save_readonly_record
|
2005-10-28 00:53:22 -04:00
|
|
|
dev = Developer.find(1)
|
2005-10-20 10:53:04 -04:00
|
|
|
assert !dev.readonly?
|
2005-10-14 20:19:56 -04:00
|
|
|
|
2005-10-20 10:53:04 -04:00
|
|
|
dev.readonly!
|
|
|
|
assert dev.readonly?
|
2005-10-14 20:19:56 -04:00
|
|
|
|
|
|
|
assert_nothing_raised do
|
2005-10-20 10:53:04 -04:00
|
|
|
dev.name = 'Luscious forbidden fruit.'
|
|
|
|
assert !dev.save
|
|
|
|
dev.name = 'Forbidden.'
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
2005-10-20 10:53:04 -04:00
|
|
|
assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save }
|
|
|
|
assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! }
|
2012-05-10 07:47:50 -04:00
|
|
|
assert_raise(ActiveRecord::ReadOnlyRecord) { dev.destroy }
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
|
|
|
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2005-10-14 20:19:56 -04:00
|
|
|
def test_find_with_readonly_option
|
2012-08-03 06:51:52 -04:00
|
|
|
Developer.all.each { |d| assert !d.readonly? }
|
2009-12-28 05:46:21 -05:00
|
|
|
Developer.readonly(false).each { |d| assert !d.readonly? }
|
|
|
|
Developer.readonly(true).each { |d| assert d.readonly? }
|
|
|
|
Developer.readonly.each { |d| assert d.readonly? }
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
|
|
|
|
2013-05-27 10:52:42 -04:00
|
|
|
def test_find_with_joins_option_does_not_imply_readonly
|
|
|
|
Developer.joins(' ').each { |d| assert_not d.readonly? }
|
|
|
|
Developer.joins(' ').readonly(true).each { |d| assert d.readonly? }
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2013-05-27 10:52:42 -04:00
|
|
|
Developer.joins(', projects').each { |d| assert_not d.readonly? }
|
|
|
|
Developer.joins(', projects').readonly(true).each { |d| assert d.readonly? }
|
2005-10-20 10:53:04 -04:00
|
|
|
end
|
|
|
|
|
2005-10-28 00:53:22 -04:00
|
|
|
def test_has_many_find_readonly
|
|
|
|
post = Post.find(1)
|
|
|
|
assert !post.comments.empty?
|
2006-03-30 17:27:32 -05:00
|
|
|
assert !post.comments.any?(&:readonly?)
|
2012-07-27 07:01:25 -04:00
|
|
|
assert !post.comments.to_a.any?(&:readonly?)
|
2009-12-28 05:46:21 -05:00
|
|
|
assert post.comments.readonly(true).all?(&:readonly?)
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2006-03-30 17:27:32 -05:00
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly
|
|
|
|
assert people = Post.find(1).people
|
|
|
|
assert !people.any?(&:readonly?)
|
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2010-08-26 00:22:27 -04:00
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_by_id
|
|
|
|
assert !posts(:welcome).people.find(1).readonly?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_first
|
|
|
|
assert !posts(:welcome).people.first.readonly?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_last
|
|
|
|
assert !posts(:welcome).people.last.readonly?
|
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_readonly_scoping
|
2012-07-27 12:55:43 -04:00
|
|
|
Post.where('1=1').scoping do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert !Post.find(1).readonly?
|
2009-12-28 05:46:21 -05:00
|
|
|
assert Post.readonly(true).find(1).readonly?
|
|
|
|
assert !Post.readonly(false).find(1).readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2012-07-27 12:55:43 -04:00
|
|
|
Post.joins(' ').scoping do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert !Post.find(1).readonly?
|
2009-12-28 05:46:21 -05:00
|
|
|
assert Post.readonly.find(1).readonly?
|
|
|
|
assert !Post.readonly(false).find(1).readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2005-10-29 14:40:49 -04:00
|
|
|
# Oracle barfs on this because the join includes unqualified and
|
|
|
|
# conflicting column names
|
2006-03-01 11:01:53 -05:00
|
|
|
unless current_adapter?(:OracleAdapter)
|
2012-07-27 12:55:43 -04:00
|
|
|
Post.joins(', developers').scoping do
|
2013-05-27 10:52:42 -04:00
|
|
|
assert_not Post.find(1).readonly?
|
2009-12-28 05:46:21 -05:00
|
|
|
assert Post.readonly.find(1).readonly?
|
|
|
|
assert !Post.readonly(false).find(1).readonly?
|
2005-10-29 14:40:49 -04:00
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2012-07-27 12:55:43 -04:00
|
|
|
Post.readonly(true).scoping do
|
2005-10-28 00:53:22 -04:00
|
|
|
assert Post.find(1).readonly?
|
2009-12-28 05:46:21 -05:00
|
|
|
assert Post.readonly.find(1).readonly?
|
|
|
|
assert !Post.readonly(false).find(1).readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_association_collection_method_missing_scoping_not_readonly
|
2011-01-02 15:33:18 -05:00
|
|
|
developer = Developer.find(1)
|
|
|
|
project = Post.find(1)
|
|
|
|
|
|
|
|
assert !developer.projects.all_as_method.first.readonly?
|
|
|
|
assert !developer.projects.all_as_scope.first.readonly?
|
|
|
|
|
|
|
|
assert !project.comments.all_as_method.first.readonly?
|
|
|
|
assert !project.comments.all_as_scope.first.readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|