File: core_test.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (294 lines) | stat: -rw-r--r-- 10,355 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# frozen_string_literal: true

require "cases/helper"
require "models/person"
require "models/topic"
require "pp"
require "models/cpk"

class NonExistentTable < ActiveRecord::Base; end

class CoreTest < ActiveRecord::TestCase
  fixtures :topics

  def test_inspect_class
    assert_equal "ActiveRecord::Base", ActiveRecord::Base.inspect
    assert_equal "LoosePerson(abstract)", LoosePerson.inspect
    assert_match(/^Topic\(id: integer, title: string/, Topic.inspect)
  end

  def test_inspect_instance
    topic = topics(:first)
    assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_fs(:inspect)}", bonus_time: "#{topic.bonus_time.to_fs(:inspect)}", last_read: "#{topic.last_read.to_fs(:inspect)}", content: "Have a nice day", important: nil, binary_content: 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_fs(:inspect)}", updated_at: "#{topic.updated_at.to_fs(:inspect)}">), topic.inspect
  end

  def test_inspect_includes_attributes_from_attributes_for_inspect
    Topic.stub(:attributes_for_inspect, [:id, :title, :author_name]) do
      topic = topics(:first)

      assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David">), topic.inspect
    end
  end

  def test_inspect_instance_with_lambda_date_formatter
    before = Time::DATE_FORMATS[:inspect]

    Topic.stub(:attributes_for_inspect, [:id, :last_read]) do
      Time::DATE_FORMATS[:inspect] = ->(date) { "my_format" }
      topic = topics(:first)

      assert_equal %(#<Topic id: 1, last_read: "2004-04-15">), topic.inspect
    end
  ensure
    Time::DATE_FORMATS[:inspect] = before
  end

  def test_inspect_new_instance
    assert_match(/Topic id: nil/, Topic.new.inspect)
  end

  def test_inspect_singleton_instance
    assert_match(/#<Class:#<Topic:\w+>>/, Topic.new.singleton_class.inspect)
  end

  def test_inspect_limited_select_instance
    Topic.stub(:attributes_for_inspect, [:id, :title]) do
      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
    end
  end

  def test_inspect_instance_with_non_primary_key_id_attribute
    topic = topics(:first).becomes(TitlePrimaryKeyTopic)
    assert_match(/id: 1/, topic.inspect)
  end

  def test_inspect_class_without_table
    assert_equal "NonExistentTable(Table doesn't exist)", NonExistentTable.inspect
  end

  def test_inspect_with_attributes_for_inspect_all_lists_all_attributes
    Topic.stub(:attributes_for_inspect, :all) do
      topic = topics(:first)

      assert_equal <<~STRING.squish, topic.inspect
        #<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_fs(:inspect)}", bonus_time: "#{topic.bonus_time.to_fs(:inspect)}", last_read: "#{topic.last_read.to_fs(:inspect)}", content: "Have a nice day", important: nil, binary_content: 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_fs(:inspect)}", updated_at: "#{topic.updated_at.to_fs(:inspect)}">
      STRING
    end
  end

  def test_inspect_relation_with_virtual_field
    relation = Topic.limit(1).select("1 as virtual_field")
    assert_match(/virtual_field: 1/, relation.first.full_inspect)
  end

  def test_inspect_with_overridden_attribute_for_inspect
    topic = topics(:first)

    topic.instance_eval do
      def attribute_for_inspect(attr_name)
        if attr_name == "title"
          title.upcase.inspect
        else
          super
        end
      end
    end

    assert_match(/title: "THE FIRST TOPIC"/, topic.full_inspect)
  end

  def test_full_inspect_lists_all_attributes
    topic = topics(:first)

    assert_equal <<~STRING.squish, topic.full_inspect
      #<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_fs(:inspect)}", bonus_time: "#{topic.bonus_time.to_fs(:inspect)}", last_read: "#{topic.last_read.to_fs(:inspect)}", content: "Have a nice day", important: nil, binary_content: 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_fs(:inspect)}", updated_at: "#{topic.updated_at.to_fs(:inspect)}">
    STRING
  end

  def test_pretty_print_new
    topic = Topic.new
    actual = +""
    PP.pp(topic, StringIO.new(actual))
    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,
       binary_content: 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>
    PRETTY
    assert actual.start_with?(expected.split("XXXXXX").first)
    assert actual.end_with?(expected.split("XXXXXX").last)
  end

  def test_pretty_print_persisted
    topic = topics(:first)
    actual = +""
    PP.pp(topic, StringIO.new(actual))
    expected = <<~PRETTY
      #<Topic:0x\\w+
       id: 1,
       title: "The First Topic",
       author_name: "David",
       author_email_address: "david@loudthinking.com",
       written_on: "2003-07-16 14:28:11\\.223300000 \\+0000",
       bonus_time: "2000-01-01 14:28:00\\.000000000 \\+0000",
       last_read: "2004-04-15",
       content: "Have a nice day",
       important: nil,
       binary_content: nil,
       approved: false,
       replies_count: 1,
       unique_replies_count: 0,
       parent_id: nil,
       parent_title: nil,
       type: nil,
       group: nil,
       created_at: [^,]+,
       updated_at: [^,>]+>
    PRETTY
    assert_match(/\A#{expected}\z/, actual)
  end

  def test_pretty_print_full
    Topic.stub(:attributes_for_inspect, :all) do
      topic = topics(:first)
      actual = +""
      PP.pp(topic, StringIO.new(actual))
      expected = <<~PRETTY
        #<Topic:0x\\w+
         id: 1,
         title: "The First Topic",
         author_name: "David",
         author_email_address: "david@loudthinking.com",
         written_on: "2003-07-16 14:28:11\\.223300000 \\+0000",
         bonus_time: "2000-01-01 14:28:00\\.000000000 \\+0000",
         last_read: "2004-04-15",
         content: "Have a nice day",
         important: nil,
         binary_content: nil,
         approved: false,
         replies_count: 1,
         unique_replies_count: 0,
         parent_id: nil,
         parent_title: nil,
         type: nil,
         group: nil,
         created_at: [^,]+,
         updated_at: [^,>]+>
      PRETTY
      assert_match(/\A#{expected}\z/, actual)
    end
  end

  def test_pretty_print_uninitialized
    topic = Topic.allocate
    actual = +""
    PP.pp(topic, StringIO.new(actual))
    expected = "#<Topic:XXXXXX not initialized>\n"
    assert actual.start_with?(expected.split("XXXXXX").first)
    assert actual.end_with?(expected.split("XXXXXX").last)
  end

  def test_pretty_print_overridden_by_inspect
    subtopic = Class.new(Topic) do
      def inspect
        "inspecting topic"
      end
    end
    actual = +""
    PP.pp(subtopic.new, StringIO.new(actual))
    assert_equal "inspecting topic\n", actual
  end

  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

  def test_pretty_print_with_overridden_attribute_for_inspect
    topic = topics(:first)

    topic.instance_eval do
      def attribute_for_inspect(attr_name)
        if attr_name == "title"
          title.upcase.inspect
        else
          super
        end
      end
    end

    Topic.stub(:attributes_for_inspect, :all) do
      actual = +""
      PP.pp(topic, StringIO.new(actual))
      assert_match(/title: "THE FIRST TOPIC"/, actual)
    end
  end

  def test_find_by_cache_does_not_duplicate_entries
    Topic.initialize_find_by_cache
    using_prepared_statements = Topic.lease_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

  def test_composite_pk_models_added_to_a_set
    library = Set.new
    # with primary key present
    library << Cpk::Book.new(id: [1, 2])

    # duplicate
    library << Cpk::Book.new(id: [1, 3])
    library << Cpk::Book.new(id: [1, 3])

    # without primary key being set
    library << Cpk::Book.new(title: "Book A")
    library << Cpk::Book.new(title: "Book B")

    assert_equal 4, library.size
  end

  def test_composite_pk_models_equality
    assert Cpk::Book.new(id: [1, 2]) == Cpk::Book.new(id: [1, 2])

    assert_not Cpk::Book.new(id: [1, 2]) == Cpk::Book.new(id: [1, 3])
    assert_not Cpk::Book.new == Cpk::Book.new
    assert_not Cpk::Book.new(title: "Book A") == Cpk::Book.new(title: "Book B")
    assert_not Cpk::Book.new(author_id: 1) == Cpk::Book.new(author_id: 1)
    assert_not Cpk::Book.new(author_id: 1, title: "Same title") == Cpk::Book.new(author_id: 1, title: "Same title")
  end

  def test_composite_pk_models_hash
    assert_equal Cpk::Book.new(id: [1, 2]).hash, Cpk::Book.new(id: [1, 2]).hash

    assert_not_equal Cpk::Book.new(id: [1, 2]).hash, Cpk::Book.new(id: [1, 3]).hash
    assert_not_equal Cpk::Book.new.hash, Cpk::Book.new.hash
    assert_not_equal Cpk::Book.new(title: "Book A").hash, Cpk::Book.new(title: "Book B").hash
    assert_not_equal Cpk::Book.new(author_id: 1).hash, Cpk::Book.new(author_id: 1).hash
    assert_not_equal Cpk::Book.new(author_id: 1, title: "Same title").hash, Cpk::Book.new(author_id: 1, title: "Same title").hash
  end
end