Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 90 lines (68 sloc) 2.987 kb
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
require 'spec_helper'
require 'consistency_fail/reporter'
require 'consistency_fail/index'

describe ConsistencyFail::Reporter do
  before(:each) do
    @real_out = $stdout
    @fake_out = StringIO.new
    $stdout = @fake_out
  end
  after(:each) do
    $stdout = @real_out
  end

  context "validates_uniqueness_of" do
    it "says everything's good" do
      subject.report_validates_uniqueness_problems([])

      @fake_out.string.should =~ /Hooray!/
    end

    it "shows a missing single-column index on a single model" do
      missing_indexes = [ConsistencyFail::Index.new(double('model'), "users", ["email"])]

      subject.report_validates_uniqueness_problems(fake_ar_model("User", :table_name => "users") => missing_indexes)

      @fake_out.string.should =~ /users\s+\(email\)/
    end

    it "shows a missing multiple-column index on a single model" do
      missing_indexes = [ConsistencyFail::Index.new(double('model'),"addresses", ["number", "street", "zip"])]

      subject.report_validates_uniqueness_problems(fake_ar_model("Address", :table_name => "addresses") => missing_indexes)

      @fake_out.string.should =~ /addresses\s+\(number, street, zip\)/
    end

    context "with problems on multiple models" do
      before(:each) do
        subject.report_validates_uniqueness_problems(
          fake_ar_model("User", :table_name => "users") =>
            [ConsistencyFail::Index.new(double('model'),"users", ["email"])],
          fake_ar_model("Citizen", :table_name => "citizens") =>
            [ConsistencyFail::Index.new(double('model'),"citizens", ["ssn"])]
        )
      end

      it "shows all problems" do
        @fake_out.string.should =~ /users\s+\(email\)/m
        @fake_out.string.should =~ /citizens\s+\(ssn\)/m
      end

      it "orders the models alphabetically" do
        @fake_out.string.should =~ /citizens\s+\(ssn\).*users\s+\(email\)/m
      end
    end
  end

  context "has_one" do
    it "says everything's good" do
      subject.report_has_one_problems([])

      @fake_out.string.should =~ /Hooray!/
    end

    it "shows a missing single-column index on a single model" do
      missing_indexes = [ConsistencyFail::Index.new(double('model'),"users", ["email"])]

      subject.report_has_one_problems(fake_ar_model("Friend", :table_name => "users") => missing_indexes)

      @fake_out.string.should =~ /Friend\s+users\s+\(email\)/m
    end
  end

  context "polymorphic" do
    it "says everything's good" do
      subject.report_polymorphic_problems([])

      @fake_out.string.should =~ /Hooray!/
    end

    it "shows a missing compound index on a single model" do
      missing_indexes = [ConsistencyFail::Index.new(double('model'), "addresses", ["addressable_type", "addressable_id"])]

      subject.report_polymorphic_problems(fake_ar_model("Address", :table_name => "addresses") => missing_indexes)

      @fake_out.string.should =~ /Address\s+addresses\s+\(addressable_type, addressable_id\)/m
    end
  end
end
Something went wrong with that request. Please try again.