Sha256: 546ebbd491e795477ce871ea290ee13c800fbba31a7b041e0ee0aa75db9b966e

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe DirtySeed::Sorter do
  let(:alfa) { build_model(Alfa) }
  let(:bravo) { build_model(Bravo) }
  let(:charlie) { build_model(Charlie) }

  describe '#initialize' do
    it 'builds an instance' do
      expect(described_class.new([alfa, bravo])).to be_a described_class
    end
  end

  describe '#sort' do
    it 'returns an empty array if models is empty' do
      sorted = described_class.new([]).sort
      expect(sorted).to be_empty
    end

    it 'sorts models by dependencies [1]' do
      sorted = described_class.new([alfa, charlie]).sort
      expect(sorted).to eq([alfa, charlie])
    end

    it 'sorts models by dependencies [2]' do
      sorted = described_class.new([charlie, alfa]).sort
      expect(sorted).to eq([alfa, charlie])
    end

    it 'sorts models by dependencies [3]' do # rubocop:disable RSpec/ExampleLength
      delta = build_model(Delta)
      echo = build_model(Echo)
      hotel = build_model(Hotel)
      india = build_model(India)
      active_record_models = [alfa, charlie, delta, echo, hotel, india]
      10.times do
        sorted = described_class.new(active_record_models.shuffle).sort
        expect(sorted.index(alfa)).to be < sorted.index(delta)
        expect(sorted.index(alfa)).to be < sorted.index(echo)
        expect(sorted.index(hotel)).to be < sorted.index(india)
      end
    end

    context 'when infinite loop could happens' do
      it 'does not raise error' do
        hotel = build_model(Hotel)
        india = build_model(India)
        sorted = described_class.new([india, hotel]).sort
        expect(sorted).to eq([hotel, india])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dirty_seed-0.2.1 spec/lib/dirty_seed/sorter_spec.rb
dirty_seed-0.2.0 spec/lib/dirty_seed/sorter_spec.rb