Class: Minitest::Spec

Inherits:
Test
  • Object
show all
Defined in:
lib/minitest/sequel/helpers.rb,
lib/minitest/sequel/plugins.rb

Overview

reopening the class rubocop:disable Style/ClassAndModuleChildren

Class Method Summary collapse

Class Method Details

.ensure_paranoid_model(model) ⇒ Object

Ensure that a given model is properly configured as a paranoid model

This method sets up a series of tests to verify that a model is correctly using the Sequel Paranoid plugin. It checks the behavior of the :deleted_at column in various scenarios.

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Examples:

Testing a paranoid model

class Comment < Sequel::Model
  plugin :paranoid
end

describe Comment do
  ensure_paranoid_model(Comment)
end

Parameters:

  • model (Class)

    The model class to test


315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/minitest/sequel/plugins.rb', line 315

def self.ensure_paranoid_model(model)
  # rubocop:disable Metrics/BlockLength
  describe 'a paranoid model with .plugin(:paranoid)' do
    let(:m) { model.send(:make) }

    describe 'on a new record' do
      it '#:deleted_at should be NULL (empty)' do
        msg = 'EnsureParanoidModel#new - expected #deleted_at to be nil on new record. '
        msg << "Debug: [#{m.inspect}]"

        assert_nil(m.deleted_at, msg)
      end
    end

    describe 'on an updated record' do
      it '#:deleted_at should be NULL (empty)' do
        m.save

        msg = 'EnsureParanoidModel#update - expected #deleted_at to be nil on updated record. '
        msg << "Debug: [#{m.inspect}]"

        assert_nil(m.deleted_at, msg)
      end
    end

    describe 'after a record#destroy' do
      it "#:deleted_at should be NULL (empty) on a delete'd record" do
        m.destroy

        msg = 'EnsureParanoidModel#destroy - expected #deleted_at to be nil on destroyed record. '
        msg << "Debug: [#{m.inspect}]"

        assert_nil(m.deleted_at, msg)
      end
    end

    describe 'after a record#delete' do
      it "#:deleted_at should be NULL (empty) on a delete'd record" do
        m.delete

        msg = 'EnsureParanoidModel#delete - expected #deleted_at to be nil on deleted record. '
        msg << "Debug: [#{m.inspect}]"

        assert_nil(m.deleted_at, msg)
      end
    end

    describe 'after a record#soft_delete' do
      it '#:deleted_at should be a timestamp' do
        refute(m.deleted?)
        assert_no_error { m.soft_delete }
        assert(m.deleted?)

        msg = 'EnsureParanoidModel#destroy - expected #deleted_at to be instance of Time '
        msg << "on deleted model. Debug: [#{m.inspect}]"

        assert_instance_of(Time, m.deleted_at, msg)
      end
    end
  end
  # rubocop:enable Metrics/BlockLength
end

.ensure_timestamped_model(model) ⇒ Object

Ensure that a given model is properly configured as a timestamped model

This method sets up a series of tests to verify that a model is correctly using the aequel-timestamps plugin. It checks the behavior of the :created_at and :updated_at columns in various scenarios.

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Examples:

Testing a timestamped model

class Comment < Sequel::Model
  plugin :timestamps
end

describe Comment do
  ensure_timestamped_model(Comment)
end

Parameters:

  • model (Class)

    The model class to test


397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/minitest/sequel/plugins.rb', line 397

def self.ensure_timestamped_model(model)
  # rubocop:disable Metrics/BlockLength
  describe 'a timestamped model with .plugin(:timestamps)' do
    let(:m) { model.send(:make) }

    describe 'a new record' do
      it '#:created_at should be a timestamp' do
        msg = 'EnsureTimestampedModel#new - expected #created_at to be an instance of Time '
        msg << "on new record. Debug: [#{m.inspect}]"

        assert_instance_of(Time, m.created_at, msg)
      end

      it '#:updated_at should be nil (empty)' do
        msg = 'EnsureTimestampedModel#new - expected #updated_at to be nil on new record. '
        msg << "Debug: [#{m.inspect}]"

        assert_nil(m.updated_at, msg)
      end
    end

    describe 'after an update' do
      it '#:created_at should remain unchanged ' do
        assert_instance_of(Time, m.created_at)
        old_ts = m.created_at

        sleep 1 # TODO: convert this with time_cop or similar one day.
        m.save

        msg = 'EnsureTimestampedModel#update - expected #created_at to be unchanged '
        msg << "on update record. Debug: [#{m.inspect}]"

        assert_equal(old_ts, m.created_at, msg)
      end

      it '#:updated_at should be a timestamp' do
        m.save

        msg = 'EnsureTimestampedModel#new - expected #updated_at to be an instance of Time '
        msg << "on updated record. Debug: [#{m.inspect}]"

        assert_instance_of(Time, m.updated_at, msg)
      end
    end
  end
  # rubocop:enable Metrics/BlockLength
end

.ensure_working_crud(model, attr) ⇒ Object

Enables quick tests to ensure that the basic CRUD functionality is working correctly for a Model

let(:m) { User.make }

ensure_working_CRUD(User, :username)

NOTE!

  • the passed ‘_model` argument must be the actual Model class and NOT a string or symbol

  • the passed attribute ‘_attr` must be a String attribute or the tests will fail

DEPENDENCIES

this test depends upon being able to create a new model instance for each test via using Sequel Factory’s ‘#make()` method

rubocop:disable Metrics/MethodLength, Metrics/AbcSize


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
# File 'lib/minitest/sequel/helpers.rb', line 166

def self.ensure_working_crud(model, attr)
  # rubocop:disable Metrics/BlockLength
  describe 'a valid CRUD model' do
    it "can create a #{model}" do
      m = model.send(:make)

      msg = "CRUD:create - expected #{m.class} to be an instance of #{model}"
      assert_instance_of(model, m, msg)

      msg = "CRUD:create - expected .errors to be empty, but was: [#{m.errors.inspect}]"
      assert_empty(m.errors, msg)
    end

    it "can read a #{model}" do
      m = model.send(:make)
      # res = model.send(:where, id: m.id)
      res = model.send(:first)
      # res = model[m.id]

      msg = "CRUD:read - expected #{res.class} to be an instance of #{model}"
      assert_instance_of(model, m, msg)

      msg = "CRUD:read - expected [#{m.inspect}] to equal [#{res.inspect}]"
      assert_equal(m, res, msg)
    end

    it "can update a #{model}" do
      m = model.send(:make)
      tmp_str = "#{m.send(attr.to_sym)} Updated"
      res = model.send(:first)
      # res = model[m.id]

      msg = "CRUD:update - expected [#{m.inspect}] to equal [#{res.inspect}]"
      assert_equal(m, res, msg)
      res.send(:"#{attr}=", tmp_str)
      res.save

      res2 = model.send(:first)
      # res2 = model[m.id]

      msg = "CRUD:update - expected [#{res2.send(attr.to_sym)}] to equal the "
      msg << "updated string: [#{tmp_str}]"

      assert_equal(tmp_str, res2.send(attr.to_sym), msg)
    end

    it "can destroy a #{model}" do
      m = model.send(:make)

      if m.respond_to?(:deleted_at)
        msg = "CRUD:delete - expected m.deleted_at to be nil, but was [#{m.deleted_at}]"
        assert_nil(m.deleted_at, msg)
      end

      assert(m.destroy, 'CRUD:delete - expected m.destroy to return true')
      res = model.send(:first)

      if m.respond_to?(:deleted_at)
        refute_nil(m.deleted_at, 'CRUD:delete - expected m.deleted_at to be NOT be nil')

        msg = 'CRUD:delete - expected m.deleted_at to be an instance of Time'
        assert_instance_of(Time, m.deleted_at, msg)
      else
        msg = "CRUD:delete - expected #{model}.first to return nil, but was: [#{res.inspect}]"
        assert_nil(res, msg)
      end
    end
  end
  # rubocop:enable Metrics/BlockLength
end