-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish match_result_set rspec matchers
- Loading branch information
Showing
5 changed files
with
138 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'rspec/matchers' | ||
|
||
RSpec::Matchers.define :match_result_set do |expected| | ||
match do |actual| | ||
return false if expected.size != actual.size | ||
|
||
expected.each.with_index.all? do |expected_row, i| | ||
if expected_row.respond_to?(:each_pair) | ||
if @partial_match | ||
values_match?(expected_row.values, actual[i].values_at(*expected_row.keys)) | ||
else | ||
values_match?(expected_row, actual[i]) | ||
end | ||
elsif expected_row.respond_to?(:each) | ||
raise ArgumentError, "Can't use partially matcher with Arrays" if @partial_match | ||
values_match?(expected_row, actual[i].values) | ||
else | ||
raise ArgumentError, 'Unknown type' | ||
end | ||
end | ||
end | ||
|
||
chain :partially do | ||
@partial_match = true | ||
end | ||
|
||
diffable | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
require 'spec_helper' | ||
require 'rspec/hive/query_builder_helper' | ||
|
||
RSpec.describe 'match_result_set' do | ||
include RSpec::Hive::QueryBuilderHelper | ||
|
||
let(:john) { {first_name: 'John', last_name: 'Lennon'} } | ||
let(:paul) { {first_name: 'Paul', last_name: 'McCartney'} } | ||
|
||
let(:full_match) { expect(actual_rows).to match_result_set(expected_rows) } | ||
let(:partial_match) { expect(actual_rows).to match_result_set(expected_rows).partially } | ||
let(:full_match_fails) { expect(actual_rows).not_to match_result_set(expected_rows) } | ||
let(:partial_match_fails) { expect(actual_rows).not_to match_result_set(expected_rows).partially } | ||
|
||
context 'when the expected set has only one row' do | ||
context 'but the actual set has more rows' do | ||
let(:actual_rows) { [john, paul] } | ||
|
||
context 'when the row is given as an array' do | ||
let(:expected_rows) { [john.values] } | ||
|
||
specify { full_match_fails } | ||
specify { partial_match_fails } | ||
end | ||
|
||
context 'when the row is given as a hash' do | ||
let(:expected_rows) { [john] } | ||
|
||
specify { full_match_fails } | ||
specify { partial_match_fails } | ||
end | ||
end | ||
|
||
context 'and the actual set has one row' do | ||
let(:actual_rows) { [john] } | ||
|
||
context 'when the row is given as an array' do | ||
context 'when the number of columns differs' do | ||
let(:expected_rows) { [john.values << 'yoko'] } | ||
|
||
specify { full_match_fails } | ||
end | ||
|
||
context 'when the actual and expected have are different' do | ||
let(:expected_rows) { [paul.values] } | ||
|
||
specify { full_match_fails } | ||
end | ||
|
||
context 'when the actual and expected rows are equal' do | ||
let(:expected_rows) { [john.values] } | ||
|
||
specify { full_match } | ||
end | ||
|
||
context 'when the actual and expected rows are equal with rspec matchers' do | ||
let(:expected_rows) { [[a_string_matching('John'), a_string_matching(/lennon/i)]] } | ||
|
||
specify { full_match } | ||
end | ||
end | ||
|
||
context 'when the row is given as a hash' do | ||
context 'when the number of columns differs' do | ||
let(:expected_rows) { [john.dup.tap { |j| j[:ono] = 'yoko' }] } | ||
|
||
specify { full_match_fails } | ||
specify { partial_match_fails } | ||
end | ||
|
||
context 'when the actual and expected have are different' do | ||
let(:expected_rows) { [john.dup.tap { |j| j[:first_name] = 'yoko' }] } | ||
|
||
specify { full_match_fails } | ||
specify { partial_match_fails } | ||
end | ||
|
||
context 'when the actual and expected rows are equal' do | ||
let(:expected_rows) { [john] } | ||
|
||
specify { full_match } | ||
specify { partial_match } | ||
end | ||
|
||
context 'when matching a subset of columns' do | ||
let(:expected_rows) { [{first_name: john[:first_name]}] } | ||
|
||
specify { full_match_fails } | ||
specify { partial_match } | ||
end | ||
end | ||
end | ||
end | ||
end |