2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/person"
|
|
|
|
require "models/topic"
|
|
|
|
require "pp"
|
2013-05-21 13:19:50 -04:00
|
|
|
|
|
|
|
class NonExistentTable < ActiveRecord::Base; end
|
|
|
|
|
|
|
|
class CoreTest < ActiveRecord::TestCase
|
|
|
|
fixtures :topics
|
|
|
|
|
|
|
|
def test_inspect_class
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "ActiveRecord::Base", ActiveRecord::Base.inspect
|
|
|
|
assert_equal "LoosePerson(abstract)", LoosePerson.inspect
|
2013-05-21 13:19:50 -04:00
|
|
|
assert_match(/^Topic\(id: integer, title: string/, Topic.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect_instance
|
|
|
|
topic = topics(:first)
|
2021-12-03 01:16:59 -05:00
|
|
|
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_formatted_s(:inspect)}", bonus_time: "#{topic.bonus_time.to_formatted_s(:inspect)}", last_read: "#{topic.last_read.to_formatted_s(:inspect)}", content: "Have a nice day", important: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "#{topic.created_at.to_formatted_s(:inspect)}", updated_at: "#{topic.updated_at.to_formatted_s(:inspect)}">), topic.inspect
|
2013-05-21 13:19:50 -04:00
|
|
|
end
|
|
|
|
|
2020-05-06 14:39:58 -04:00
|
|
|
def test_inspect_instance_with_lambda_date_formatter
|
2020-05-06 15:50:33 -04:00
|
|
|
before = Time::DATE_FORMATS[:inspect]
|
|
|
|
Time::DATE_FORMATS[:inspect] = ->(date) { "my_format" }
|
2020-05-06 14:39:58 -04:00
|
|
|
topic = topics(:first)
|
|
|
|
|
2020-05-06 15:50:33 -04:00
|
|
|
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "my_format", bonus_time: "my_format", last_read: "2004-04-15", content: "Have a nice day", important: nil, approved: false, replies_count: 1, unique_replies_count: 0, parent_id: nil, parent_title: nil, type: nil, group: nil, created_at: "my_format", updated_at: "my_format">), topic.inspect
|
2020-05-06 14:39:58 -04:00
|
|
|
|
|
|
|
ensure
|
2020-05-06 15:50:33 -04:00
|
|
|
Time::DATE_FORMATS[:inspect] = before
|
2020-05-06 14:39:58 -04:00
|
|
|
end
|
|
|
|
|
2013-05-21 13:19:50 -04:00
|
|
|
def test_inspect_new_instance
|
|
|
|
assert_match(/Topic id: nil/, Topic.new.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect_limited_select_instance
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_equal %(#<Topic id: 1>), Topic.all.merge!(select: "id", where: "id = 1").first.inspect
|
|
|
|
assert_equal %(#<Topic id: 1, title: "The First Topic">), Topic.all.merge!(select: "id, title", where: "id = 1").first.inspect
|
2013-05-21 13:19:50 -04:00
|
|
|
end
|
|
|
|
|
2018-11-05 20:51:52 -05:00
|
|
|
def test_inspect_instance_with_non_primary_key_id_attribute
|
|
|
|
topic = topics(:first).becomes(TitlePrimaryKeyTopic)
|
|
|
|
assert_match(/id: 1/, topic.inspect)
|
|
|
|
end
|
|
|
|
|
2013-05-21 13:19:50 -04:00
|
|
|
def test_inspect_class_without_table
|
|
|
|
assert_equal "NonExistentTable(Table doesn't exist)", NonExistentTable.inspect
|
|
|
|
end
|
2014-05-18 17:27:02 -04:00
|
|
|
|
|
|
|
def test_pretty_print_new
|
|
|
|
topic = Topic.new
|
2018-05-17 04:32:27 -04:00
|
|
|
actual = +""
|
2014-05-18 17:27:02 -04:00
|
|
|
PP.pp(topic, StringIO.new(actual))
|
2018-02-16 19:28:30 -05:00
|
|
|
expected = <<~PRETTY
|
|
|
|
#<Topic:0xXXXXXX
|
|
|
|
id: nil,
|
|
|
|
title: nil,
|
|
|
|
author_name: nil,
|
|
|
|
author_email_address: "test@test.com",
|
|
|
|
written_on: nil,
|
|
|
|
bonus_time: nil,
|
|
|
|
last_read: nil,
|
|
|
|
content: nil,
|
|
|
|
important: nil,
|
|
|
|
approved: true,
|
|
|
|
replies_count: 0,
|
|
|
|
unique_replies_count: 0,
|
|
|
|
parent_id: nil,
|
|
|
|
parent_title: nil,
|
|
|
|
type: nil,
|
|
|
|
group: nil,
|
|
|
|
created_at: nil,
|
|
|
|
updated_at: nil>
|
2014-05-18 17:27:02 -04:00
|
|
|
PRETTY
|
2016-08-06 12:26:20 -04:00
|
|
|
assert actual.start_with?(expected.split("XXXXXX").first)
|
|
|
|
assert actual.end_with?(expected.split("XXXXXX").last)
|
2014-05-18 17:27:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_pretty_print_persisted
|
|
|
|
topic = topics(:first)
|
2018-05-17 04:32:27 -04:00
|
|
|
actual = +""
|
2014-05-18 17:27:02 -04:00
|
|
|
PP.pp(topic, StringIO.new(actual))
|
2018-02-16 19:28:30 -05:00
|
|
|
expected = <<~PRETTY
|
|
|
|
#<Topic:0x\\w+
|
|
|
|
id: 1,
|
|
|
|
title: "The First Topic",
|
|
|
|
author_name: "David",
|
|
|
|
author_email_address: "david@loudthinking.com",
|
2019-09-21 03:57:24 -04:00
|
|
|
written_on: 2003-07-16 14:28:11(?:\.2233)? UTC,
|
2018-02-16 19:28:30 -05:00
|
|
|
bonus_time: 2000-01-01 14:28:00 UTC,
|
|
|
|
last_read: Thu, 15 Apr 2004,
|
|
|
|
content: "Have a nice day",
|
|
|
|
important: nil,
|
|
|
|
approved: false,
|
|
|
|
replies_count: 1,
|
|
|
|
unique_replies_count: 0,
|
|
|
|
parent_id: nil,
|
|
|
|
parent_title: nil,
|
|
|
|
type: nil,
|
|
|
|
group: nil,
|
|
|
|
created_at: [^,]+,
|
|
|
|
updated_at: [^,>]+>
|
2014-05-18 17:27:02 -04:00
|
|
|
PRETTY
|
|
|
|
assert_match(/\A#{expected}\z/, actual)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pretty_print_uninitialized
|
|
|
|
topic = Topic.allocate
|
2018-05-17 04:32:27 -04:00
|
|
|
actual = +""
|
2014-05-18 17:27:02 -04:00
|
|
|
PP.pp(topic, StringIO.new(actual))
|
|
|
|
expected = "#<Topic:XXXXXX not initialized>\n"
|
2016-08-06 12:26:20 -04:00
|
|
|
assert actual.start_with?(expected.split("XXXXXX").first)
|
|
|
|
assert actual.end_with?(expected.split("XXXXXX").last)
|
2014-05-18 17:27:02 -04:00
|
|
|
end
|
2015-01-12 18:11:23 -05:00
|
|
|
|
|
|
|
def test_pretty_print_overridden_by_inspect
|
|
|
|
subtopic = Class.new(Topic) do
|
|
|
|
def inspect
|
|
|
|
"inspecting topic"
|
|
|
|
end
|
|
|
|
end
|
2018-05-17 04:32:27 -04:00
|
|
|
actual = +""
|
2015-01-12 18:11:23 -05:00
|
|
|
PP.pp(subtopic.new, StringIO.new(actual))
|
|
|
|
assert_equal "inspecting topic\n", actual
|
|
|
|
end
|
2018-11-05 20:51:52 -05:00
|
|
|
|
|
|
|
def test_pretty_print_with_non_primary_key_id_attribute
|
|
|
|
topic = topics(:first).becomes(TitlePrimaryKeyTopic)
|
|
|
|
actual = +""
|
|
|
|
PP.pp(topic, StringIO.new(actual))
|
|
|
|
assert_match(/id: 1/, actual)
|
|
|
|
end
|
2021-12-11 22:20:37 -05:00
|
|
|
|
|
|
|
def test_find_by_cache_does_not_duplicate_entries
|
|
|
|
Topic.initialize_find_by_cache
|
|
|
|
using_prepared_statements = Topic.connection.prepared_statements
|
|
|
|
topic_find_by_cache = Topic.instance_variable_get("@find_by_statement_cache")[using_prepared_statements]
|
|
|
|
|
|
|
|
assert_difference -> { topic_find_by_cache.size }, +1 do
|
|
|
|
Topic.find(1)
|
|
|
|
end
|
|
|
|
assert_no_difference -> { topic_find_by_cache.size } do
|
|
|
|
Topic.find_by(id: 1)
|
|
|
|
end
|
|
|
|
end
|
2013-05-21 13:19:50 -04:00
|
|
|
end
|