-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmatcher_operator_spec.rb
123 lines (108 loc) · 4.03 KB
/
matcher_operator_spec.rb
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
def mop_error?(spec, kind)
unless %w(matcher driver dsl).include?(kind)
raise ArgumentError, "Bogus kind: #{kind}"
end
spec['error'] == true || spec['error'] == kind ||
spec['error'].is_a?(Array) && spec['error'].include?(kind)
end
describe 'Matcher operators' do
config_override :map_big_decimal_to_decimal128, true
Dir[File.join(File.dirname(__FILE__), 'matcher_operator_data', '*.yml')].sort.each do |path|
context File.basename(path) do
permitted_classes = [ BigDecimal,
Date,
Time,
Range,
Regexp,
Symbol,
BSON::Binary,
BSON::Code,
BSON::CodeWithScope,
BSON::DbPointer,
BSON::Decimal128,
BSON::Int32,
BSON::Int64,
BSON::MaxKey,
BSON::MinKey,
BSON::ObjectId,
BSON::Regexp::Raw,
BSON::Symbol::Raw,
BSON::Timestamp,
BSON::Undefined ]
specs = YAML.safe_load(File.read(path), permitted_classes: permitted_classes, aliases: true)
specs.each do |spec|
context spec['name'] do
if spec['pending']
before do
# Cannot use `pending` here because some of the queries may work
# as specified (e.g. when Mongoid and server behavior differ).
skip spec['pending'].to_s
end
end
if spec['min_server_version']
min_server_version spec['min_server_version'].to_s
end
let(:query) { spec.fetch('query') }
let(:result) { spec.fetch('matches') }
context 'embedded matching' do
let(:document) { Mop.new(spec.fetch('document')) }
if mop_error?(spec, 'matcher')
it 'produces an error' do
lambda do
document._matches?(query)
end.should raise_error(Mongoid::Errors::InvalidQuery)
end
else
it 'produces the correct result' do
document._matches?(query).should be result
end
end
end
context 'server query' do
let!(:document) { Mop.create!(spec.fetch('document')) }
context 'via driver' do
if mop_error?(spec, 'driver')
it 'produces an error' do
begin
Mop.collection.find(query).any?
rescue Mongo::Error::OperationFailure
rescue Mongo::Error::InvalidDocument
rescue BSON::Error::UnserializableClass
else
fail "Expected an exception to be raised"
end
end
else
it 'produces the correct result' do
Mop.collection.find(query).any?.should be result
end
end
end
context 'via Mongoid DSL' do
if mop_error?(spec, 'dsl')
it 'produces an error' do
begin
Mop.where(query).any?
rescue Mongo::Error::OperationFailure
rescue BSON::Error::UnserializableClass
rescue Mongoid::Errors::InvalidQuery
rescue Mongoid::Errors::CriteriaArgumentRequired
else
fail "Expected the query to raise an error"
end
end
else
it 'produces the correct result' do
Mop.where(query).any?.should be result
end
end
end
end
end
end
end
end
end