2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "models/author"
|
|
|
|
require "models/post"
|
|
|
|
require "models/comment"
|
|
|
|
require "models/developer"
|
|
|
|
require "models/computer"
|
|
|
|
require "models/project"
|
|
|
|
require "models/reader"
|
|
|
|
require "models/person"
|
|
|
|
require "models/ship"
|
2005-10-14 20:19:56 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class ReadOnlyTest < ActiveRecord::TestCase
|
2017-04-27 02:44:25 -04:00
|
|
|
fixtures :authors, :author_addresses, :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)
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate dev, :readonly?
|
2005-10-14 20:19:56 -04:00
|
|
|
|
2005-10-20 10:53:04 -04:00
|
|
|
dev.readonly!
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate dev, :readonly?
|
2005-10-14 20:19:56 -04:00
|
|
|
|
|
|
|
assert_nothing_raised do
|
2016-08-06 12:26:20 -04:00
|
|
|
dev.name = "Luscious forbidden fruit."
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not dev.save
|
2016-08-06 12:26:20 -04:00
|
|
|
dev.name = "Forbidden."
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
2014-11-07 03:00:28 -05:00
|
|
|
|
2014-11-06 12:07:29 -05:00
|
|
|
e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save }
|
|
|
|
assert_equal "Developer is marked as readonly", e.message
|
2014-11-07 03:00:28 -05:00
|
|
|
|
2014-11-06 12:07:29 -05:00
|
|
|
e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! }
|
|
|
|
assert_equal "Developer is marked as readonly", e.message
|
2014-11-07 03:00:28 -05:00
|
|
|
|
2014-11-06 12:07:29 -05:00
|
|
|
e = assert_raise(ActiveRecord::ReadOnlyRecord) { dev.destroy }
|
|
|
|
assert_equal "Developer is marked as readonly", e.message
|
2005-10-14 20:19:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_with_readonly_option
|
2018-04-17 18:21:34 -04:00
|
|
|
Developer.all.each { |d| assert_not d.readonly? }
|
|
|
|
Developer.readonly(false).each { |d| assert_not d.readonly? }
|
2009-12-28 05:46:21 -05:00
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
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
|
|
|
|
2016-08-06 12:26:20 -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)
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_not_empty post.comments
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not post.comments.any?(&:readonly?)
|
|
|
|
assert_not 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
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not people.any?(&:readonly?)
|
2006-03-30 17:27:32 -05:00
|
|
|
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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate posts(:welcome).people.find(1), :readonly?
|
2010-08-26 00:22:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_first
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate posts(:welcome).people.first, :readonly?
|
2010-08-26 00:22:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_with_through_is_not_implicitly_marked_readonly_while_finding_last
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate posts(:welcome).people.last, :readonly?
|
2010-08-26 00:22:27 -04:00
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_readonly_scoping
|
2016-08-06 12:26:20 -04:00
|
|
|
Post.where("1=1").scoping do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate Post.find(1), :readonly?
|
|
|
|
assert_predicate Post.readonly(true).find(1), :readonly?
|
|
|
|
assert_not_predicate Post.readonly(false).find(1), :readonly?
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
Post.joins(" ").scoping do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate Post.find(1), :readonly?
|
|
|
|
assert_predicate Post.readonly.find(1), :readonly?
|
|
|
|
assert_not_predicate 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)
|
2016-08-06 12:26:20 -04:00
|
|
|
Post.joins(", developers").scoping do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate Post.find(1), :readonly?
|
|
|
|
assert_predicate Post.readonly.find(1), :readonly?
|
|
|
|
assert_not_predicate 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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Post.find(1), :readonly?
|
|
|
|
assert_predicate Post.readonly.find(1), :readonly?
|
|
|
|
assert_not_predicate 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)
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate developer.projects.all_as_method.first, :readonly?
|
|
|
|
assert_not_predicate developer.projects.all_as_scope.first, :readonly?
|
2011-01-02 15:33:18 -05:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate project.comments.all_as_method.first, :readonly?
|
|
|
|
assert_not_predicate 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
|