diff options
| author | Teddy Wing | 2017-11-14 15:19:51 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2017-11-14 16:04:50 +0100 | 
| commit | 081522bbd459742e81ba8733be2198622c92fb87 (patch) | |
| tree | 29a99eb3ae762ad700a0794bd3759f789e081692 /spec/db | |
| parent | d851ee1375aaaf2f3add1c48d75a0667b1b2bff5 (diff) | |
| download | chouette-core-081522bbd459742e81ba8733be2198622c92fb87.tar.bz2 | |
schema_spec: Move spec to a custom RSpec matcher
This enables us to add custom formatting to the failure message. For
now, the logic works the same way as before. The offending lines are
compared to an empty array and appear in the output. We're missing line
numbers. I think we'll want to actually compare to an entirely new
in-memory version of the schema ideally so that it can output the right
line numbers and corrections.
The differ is based on Alexey Shin's:
https://stackoverflow.com/questions/32477104/rspec-custom-diffable-matcher#32479025
Refs #4951
Diffstat (limited to 'spec/db')
| -rw-r--r-- | spec/db/schema_spec.rb | 33 | 
1 files changed, 26 insertions, 7 deletions
| diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index 8111988ee..0e0fe780a 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -1,21 +1,40 @@  RSpec.describe ActiveRecord::Schema do    it "uses type `bigint` for primary and foreign keys" do -    non_bigint_primary_keys = [] -    non_bigint_foreign_keys = [] +    expect('db/schema.rb').to use_bigint_keys +  end +end + + +RSpec::Matchers.define :use_bigint_keys do +  match do |filename| +    @non_bigint_primary_keys = [] +    @non_bigint_foreign_keys = [] -    File.open('db/schema.rb', 'r') do |f| -      non_bigint_primary_keys = f +    File.open(filename, 'r') do |f| +      @non_bigint_primary_keys = f          .grep(/create_table /)          .grep_v(/id: :bigserial/)        f.rewind -      non_bigint_foreign_keys = f +      @non_bigint_foreign_keys = f          .grep(/t\.integer +"\w+_id"/)          .grep_v(/limit: 8/)      end -    expect(non_bigint_primary_keys).to be_empty -    expect(non_bigint_foreign_keys).to be_empty +    @non_bigint_primary_keys.empty? && @non_bigint_foreign_keys.empty? +  end + +  failure_message do |filename| +    <<~EOS +      expected #{filename.inspect} to use bigint keys +      Diff: #{diff} +    EOS +  end + +  def diff +    RSpec::Support::Differ.new( +      color: RSpec::Matchers.configuration.color? +    ).diff(@non_bigint_primary_keys + @non_bigint_foreign_keys, [])    end  end | 
