File: active_record_spec.rb

package info (click to toggle)
ruby-awesome-print 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 720 kB
  • sloc: ruby: 3,787; sh: 4; makefile: 2
file content (270 lines) | stat: -rw-r--r-- 8,248 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'
require 'active_record_helper'

RSpec.describe 'AwesomePrint/ActiveRecord', skip: -> { !ExtVerifier.has_rails? }.call do
  describe 'ActiveRecord instance, attributes only (default)' do
    before do
      ActiveRecord::Base.default_timezone = :utc
      @diana = User.new(name: 'Diana', rank: 1, admin: false, created_at: '1992-10-10 12:30:00')
      @laura = User.new(name: 'Laura', rank: 2, admin: true,  created_at: '2003-05-26 14:15:00')
      @ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true)
    end

    it 'display single record' do
      out = @ap.awesome(@diana)
      str = <<-EOS.strip
#<User:placeholder_id> {
         :admin => false,
    :created_at => ?,
            :id => nil,
          :name => "Diana",
          :rank => 1
}
      EOS
      if RUBY_VERSION < '1.9'
        str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
      else
        str.sub!('?', '1992-10-10 12:30:00 UTC')
      end
      expect(out).to be_similar_to(str)
    end

    it 'display multiple records' do
      out = @ap.awesome([@diana, @laura])
      str = <<-EOS.strip
[
    [0] #<User:placeholder_id> {
             :admin => false,
        :created_at => ??,
                :id => nil,
              :name => "Diana",
              :rank => 1
    },
    [1] #<User:placeholder_id> {
             :admin => true,
        :created_at => ?!,
                :id => nil,
              :name => "Laura",
              :rank => 2
    }
]
      EOS
      if RUBY_VERSION < '1.9'
        str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
        str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
      else
        str.sub!('??', '1992-10-10 12:30:00 UTC')
        str.sub!('?!', '2003-05-26 14:15:00 UTC')
      end
      expect(out).to be_similar_to(str)
    end

    it 'display multiple records on a relation' do
      @diana.save
      @laura.save
      out = @ap.awesome(User.all)
      str = <<-EOS.strip
[
    [0] #<User:placeholder_id> {
             :admin => false,
        :created_at => ??,
                :id => 1,
              :name => "Diana",
              :rank => 1
    },
    [1] #<User:placeholder_id> {
             :admin => true,
        :created_at => ?!,
                :id => 2,
              :name => "Laura",
              :rank => 2
    }
]
      EOS
      if RUBY_VERSION < '1.9'
        str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
        str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
      else
        str.sub!('??', '1992-10-10 12:30:00 UTC')
        str.sub!('?!', '2003-05-26 14:15:00 UTC')
      end
      expect(out).to be_similar_to(str)
    end
  end

  describe 'Linked records (joins)' do
    before do
      @ap = AwesomePrint::Inspector.new(plain: true)
    end

    it 'should show the entire record' do
      e = Email.create(email_address: 'foo@bar.com')
      u = User.last
      u.emails << e
      email_record = User.joins(:emails).select('users.id, emails.email_address').last
      out = @ap.awesome(email_record)
      raw_object_string = <<-EOS.strip
#<User:placeholder_id> {
               "id" => #{u.id},
    "email_address" => "#{e.email_address}"
}
EOS
      expect(out).to be_similar_to(raw_object_string)
    end
  end

  #------------------------------------------------------------------------------
  describe 'ActiveRecord instance (raw)' do
    before do
      ActiveRecord::Base.default_timezone = :utc
      @diana = User.new(name: 'Diana', rank: 1, admin: false, created_at: '1992-10-10 12:30:00')
      @laura = User.new(name: 'Laura', rank: 2, admin: true,  created_at: '2003-05-26 14:15:00')
      @ap = AwesomePrint::Inspector.new(plain: true, sort_keys: true, raw: true)
    end

    it 'display single record' do
      out = @ap.awesome(@diana)

      raw_object_string =
        if activerecord_5_2?
          ActiveRecordData.raw_5_2_diana
        elsif activerecord_5_1?
          ActiveRecordData.raw_5_1_diana
        elsif activerecord_5_0?
          ActiveRecordData.raw_5_0_diana
        elsif activerecord_4_2?
          if RUBY_VERSION > '1.9.3'
            ActiveRecordData.raw_4_2_diana
          else
            ActiveRecordData.raw_4_2_diana_legacy
          end
        elsif activerecord_4_1?
          ActiveRecordData.raw_4_1_diana
        elsif activerecord_4_0?
          ActiveRecordData.raw_4_0_diana
        elsif activerecord_3_2?
          if RUBY_VERSION > '1.9.3'
            ActiveRecordData.raw_3_2_diana
          else
            ActiveRecordData.raw_3_2_diana_legacy
          end
        end
      raw_object_string.sub!('?', '1992-10-10 12:30:00')
      expect(out).to be_similar_to(raw_object_string)
    end

    it 'display multiple records' do
      out = @ap.awesome([@diana, @laura])

      raw_object_string =
        if activerecord_5_2?
          ActiveRecordData.raw_5_2_multi
        elsif activerecord_5_1?
          ActiveRecordData.raw_5_1_multi
        elsif activerecord_5_0?
          ActiveRecordData.raw_5_0_multi
        elsif activerecord_4_2?
          if RUBY_VERSION > '1.9.3'
            ActiveRecordData.raw_4_2_multi
          else
            ActiveRecordData.raw_4_2_multi_legacy
          end
        elsif activerecord_4_1?
          ActiveRecordData.raw_4_1_multi
        elsif activerecord_4_0?
          ActiveRecordData.raw_4_0_multi
        elsif activerecord_3_2?
          if RUBY_VERSION > '1.9.3'
            ActiveRecordData.raw_3_2_multi
          else
            ActiveRecordData.raw_3_2_multi_legacy
          end
        end
      raw_object_string.sub!('?', '1992-10-10 12:30:00')
      raw_object_string.sub!('?', '2003-05-26 14:15:00')
      expect(out).to be_similar_to(raw_object_string)
    end
  end

  #------------------------------------------------------------------------------
  describe 'ActiveRecord class' do
    before do
      @ap = AwesomePrint::Inspector.new(plain: true)
    end

    it 'should print the class' do
      expect(@ap.awesome(User)).to eq <<-EOS.strip
class User < ActiveRecord::Base {
            :id => :integer,
          :name => :string,
          :rank => :integer,
         :admin => :boolean,
    :created_at => :datetime
}
      EOS
    end

    it 'should print the class for non-direct subclasses of ActiveRecord::Base' do
      out = @ap.awesome(SubUser)
      expect(out).to eq <<-EOS.strip
class SubUser < User {
            :id => :integer,
          :name => :string,
          :rank => :integer,
         :admin => :boolean,
    :created_at => :datetime
}
      EOS
    end

    it 'should print ActiveRecord::Base objects (ex. ancestors)' do
      expect { @ap.awesome(User.ancestors) }.not_to raise_error
    end
  end

  #------------------------------------------------------------------------------
  describe 'ActiveRecord methods formatting' do
    before do
      @ap = AwesomePrint::Inspector.new(plain: true)
    end

    it 'should format class methods properly' do
      # spec 1
      out = @ap.awesome(User.methods.grep(/first/))

      if ActiveRecord::VERSION::STRING >= '3.2'
        if RUBY_VERSION >= '2.5'
          expect(out).to match(/\sfirst\(\*arg.*?\)\s+User/)
        elsif RUBY_VERSION >= '1.9'
          expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/)
        else
          expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/)
        end
      else
        expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
      end

      # spec 2
      out = @ap.awesome(User.methods.grep(/primary_key/))
      if RUBY_VERSION >= '2.5'
        expect(out).to match(/\sprimary_key\(.*?\)\s+User/)
      else
        expect(out).to match(/\sprimary_key\(.*?\)\s+Class \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/)
      end

      # spec 3
      out = @ap.awesome(User.methods.grep(/validate/))

      if ActiveRecord::VERSION::MAJOR < 3
        expect(out).to match(/\svalidate\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
      else
        if RUBY_VERSION >= '2.5'
          expect(out).to match(/\svalidate\(\*arg.*?\)\s+User/)
        else
          expect(out).to match(/\svalidate\(\*arg.*?\)\s+Class \(ActiveModel::Validations::ClassMethods\)/)
        end
      end

    end
  end
end