Module: Cuprum::Collections::RSpec::Contracts::QueryContracts::WithQueryContextsContract

Extended by:
RSpec::SleepingKingStudios::Contract
Defined in:
lib/cuprum/collections/rspec/contracts/query_contracts.rb

Overview

Contract defining contexts for validating query behavior.

Instance Method Summary collapse

Instance Method Details

#apply(example_group) ⇒ Object

Adds the contract to the example group.

Parameters:

  • example_group (RSpec::Core::ExampleGroup)

    the example group to which the contract is applied.



944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'lib/cuprum/collections/rspec/contracts/query_contracts.rb', line 944

contract do
  let(:filter)    { nil }
  let(:strategy)  { nil }
  let(:limit)     { nil }
  let(:offset)    { nil }
  let(:order)     { nil }

  shared_context 'when the query has limit: value' do
    let(:limit)         { 3 }
    let(:matching_data) { super()[0...limit] }
  end

  shared_context 'when the query has offset: value' do
    let(:offset)        { 2 }
    let(:matching_data) { super()[offset..] || [] }
  end

  shared_context 'when the query has order: a simple ordering' do
    let(:order)         { :title }
    let(:matching_data) { super().sort_by { |item| item['title'] } }
  end

  shared_context 'when the query has order: a complex ordering' do
    let(:order) do
      {
        author: :asc,
        title:  :desc
      }
    end
    let(:matching_data) do
      super().sort do |u, v|
        cmp = u['author'] <=> v['author']

        cmp.zero? ? (v['title'] <=> u['title']) : cmp
      end
    end
  end

  shared_context 'when the query has where: a simple block filter' do
    let(:filter) { -> { { author: 'Ursula K. LeGuin' } } }
    let(:matching_data) do
      super().select { |item| item['author'] == 'Ursula K. LeGuin' }
    end
  end

  shared_context 'when the query has where: a complex block filter' do
    let(:filter) do
      lambda do
        {
          author: equals('Ursula K. LeGuin'),
          series: not_equal('Earthsea')
        }
      end
    end
    let(:matching_data) do
      super()
        .select { |item| item['author'] == 'Ursula K. LeGuin' }
        .reject { |item| item['series'] == 'Earthsea' }
    end
  end

  shared_context 'when the query has where: a greater_than filter' do
    let(:filter) { -> { { published_at: greater_than('1970-12-01') } } }
    let(:matching_data) do
      super().select { |item| item['published_at'] > '1970-12-01' }
    end
  end

  shared_context 'when the query has where: a greater_than_or_equal_to ' \
                 'filter' \
  do
    let(:filter) do
      -> { { published_at: greater_than_or_equal_to('1970-12-01') } }
    end
    let(:matching_data) do
      super().select { |item| item['published_at'] >= '1970-12-01' }
    end
  end

  shared_context 'when the query has where: a less_than filter' do
    let(:filter) { -> { { published_at: less_than('1970-12-01') } } }
    let(:matching_data) do
      super().select { |item| item['published_at'] < '1970-12-01' }
    end
  end

  shared_context 'when the query has where: a ' \
                 'less_than_or_equal_to filter' \
  do
    let(:filter) do
      -> { { published_at: less_than_or_equal_to('1970-12-01') } }
    end
    let(:matching_data) do
      super().select { |item| item['published_at'] <= '1970-12-01' }
    end
  end

  shared_context 'when the query has where: an equal block filter' do
    let(:filter) { -> { { author: equals('Ursula K. LeGuin') } } }
    let(:matching_data) do
      super().select { |item| item['author'] == 'Ursula K. LeGuin' }
    end
  end

  shared_context 'when the query has where: a not_equal block filter' do
    let(:filter) { -> { { author: not_equal('Ursula K. LeGuin') } } }
    let(:matching_data) do
      super().reject { |item| item['author'] == 'Ursula K. LeGuin' }
    end
  end

  shared_context 'when the query has where: a not_one_of block filter' do
    let(:filter) do
      -> { { series: not_one_of(['Earthsea', 'The Lord of the Rings']) } }
    end
    let(:matching_data) do
      super().reject do |item|
        ['Earthsea', 'The Lord of the Rings'].include?(item['series'])
      end
    end
  end

  shared_context 'when the query has where: a one_of block filter' do
    let(:filter) do
      -> { { series: one_of(['Earthsea', 'The Lord of the Rings']) } }
    end
    let(:matching_data) do
      super().select do |item|
        ['Earthsea', 'The Lord of the Rings'].include?(item['series'])
      end
    end
  end

  shared_context 'when the query has multiple query options' do
    let(:filter)    { -> { { author: 'Ursula K. LeGuin' } } }
    let(:strategy)  { nil }
    let(:order)     { { title: :desc } }
    let(:limit)     { 2 }
    let(:offset)    { 1 }
    let(:matching_data) do
      super()
        .select { |item| item['author'] == 'Ursula K. LeGuin' }
        .sort { |u, v| v['title'] <=> u['title'] }
        .slice(1, 2) || []
    end
  end
end