mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Use dedicated Topic
model for SerializedAttributeTest
This fixes both #34555 and #34738. Revert "Merge pull request #34900 from gmcgibbon/fix_test_find_only_some_columns" This reverts commitff807f823b
, reversing changes made to9f1a07af04
. Revert "Merge pull request #34560 from gmcgibbon/fix_decorate_leak_on_serial_attr_test" This reverts commitbd62389307
, reversing changes made toec66c6a2fa
. Revert "Fix unstable `test_serialized_attribute_works_under_concurrent_initial_access` test" This reverts commit65c4b1b50d
.
This commit is contained in:
parent
50226c8fe0
commit
893c647da3
2 changed files with 14 additions and 24 deletions
|
@ -2,27 +2,30 @@
|
||||||
|
|
||||||
require "cases/helper"
|
require "cases/helper"
|
||||||
require "models/topic"
|
require "models/topic"
|
||||||
require "models/reply"
|
|
||||||
require "models/person"
|
require "models/person"
|
||||||
require "models/traffic_light"
|
require "models/traffic_light"
|
||||||
require "models/post"
|
require "models/post"
|
||||||
require "bcrypt"
|
require "bcrypt"
|
||||||
|
|
||||||
class SerializedAttributeTest < ActiveRecord::TestCase
|
class SerializedAttributeTest < ActiveRecord::TestCase
|
||||||
fixtures :topics, :posts
|
fixtures :posts
|
||||||
|
|
||||||
MyObject = Struct.new :attribute1, :attribute2
|
MyObject = Struct.new :attribute1, :attribute2
|
||||||
|
|
||||||
# NOTE: Use a duplicate of Topic so attribute
|
class Topic < ActiveRecord::Base
|
||||||
# changes don't bleed into other tests
|
serialize :content
|
||||||
Topic = ::Topic.dup
|
end
|
||||||
|
|
||||||
|
class ImportantTopic < Topic
|
||||||
|
serialize :important, Hash
|
||||||
|
end
|
||||||
|
|
||||||
teardown do
|
teardown do
|
||||||
Topic.serialize("content")
|
Topic.serialize("content")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_serialize_does_not_eagerly_load_columns
|
def test_serialize_does_not_eagerly_load_columns
|
||||||
reset_column_information_of(Topic)
|
Topic.reset_column_information
|
||||||
assert_no_queries do
|
assert_no_queries do
|
||||||
Topic.serialize(:content)
|
Topic.serialize(:content)
|
||||||
end
|
end
|
||||||
|
@ -53,10 +56,10 @@ class SerializedAttributeTest < ActiveRecord::TestCase
|
||||||
def test_serialized_attributes_from_database_on_subclass
|
def test_serialized_attributes_from_database_on_subclass
|
||||||
Topic.serialize :content, Hash
|
Topic.serialize :content, Hash
|
||||||
|
|
||||||
t = Reply.new(content: { foo: :bar })
|
t = ImportantTopic.new(content: { foo: :bar })
|
||||||
assert_equal({ foo: :bar }, t.content)
|
assert_equal({ foo: :bar }, t.content)
|
||||||
t.save!
|
t.save!
|
||||||
t = Reply.last
|
t = ImportantTopic.last
|
||||||
assert_equal({ foo: :bar }, t.content)
|
assert_equal({ foo: :bar }, t.content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -371,14 +374,13 @@ class SerializedAttributeTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_serialized_attribute_works_under_concurrent_initial_access
|
def test_serialized_attribute_works_under_concurrent_initial_access
|
||||||
model = ::Topic.dup
|
model = Topic.dup
|
||||||
|
|
||||||
topic = model.last
|
topic = model.create!
|
||||||
topic.update group: "1"
|
topic.update group: "1"
|
||||||
|
|
||||||
model.serialize :group, JSON
|
model.serialize :group, JSON
|
||||||
|
model.reset_column_information
|
||||||
reset_column_information_of(model)
|
|
||||||
|
|
||||||
# This isn't strictly necessary for the test, but a little bit of
|
# This isn't strictly necessary for the test, but a little bit of
|
||||||
# knowledge of internals allows us to make failures far more likely.
|
# knowledge of internals allows us to make failures far more likely.
|
||||||
|
@ -398,12 +400,4 @@ class SerializedAttributeTest < ActiveRecord::TestCase
|
||||||
# raw string ("1"), or raise an exception.
|
# raw string ("1"), or raise an exception.
|
||||||
assert_equal [1] * threads.size, threads.map(&:value)
|
assert_equal [1] * threads.size, threads.map(&:value)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def reset_column_information_of(topic_class)
|
|
||||||
topic_class.reset_column_information
|
|
||||||
# reset original topic to undefine attribute methods
|
|
||||||
::Topic.reset_column_information
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -119,10 +119,6 @@ class Topic < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ImportantTopic < Topic
|
|
||||||
serialize :important, Hash
|
|
||||||
end
|
|
||||||
|
|
||||||
class DefaultRejectedTopic < Topic
|
class DefaultRejectedTopic < Topic
|
||||||
default_scope -> { where(approved: false) }
|
default_scope -> { where(approved: false) }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue