Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #15 from rmm5t/compound-indexes

have_index matcher doesn't support compound indexes
  • Loading branch information...
commit d22c20a7bab6f9cdec2bf27e946342bf486d52dc 2 parents 9aca4ae + 8f7f058
@frodsan authored
View
1  README.md
@@ -104,6 +104,7 @@ See the following examples:
it { must have_fields(:name, :login).of_type(String).with_default_value("me") }
it { must have_index_for(:name) }
+ it { must have_index_for(:account_id, :email) }
end
### Validation Matchers
View
29 lib/matchers/document/have_index.rb
@@ -1,39 +1,36 @@
module Mongoid
module Matchers
class HaveIndexMatcher < Matcher
- def initialize(field)
- @field = field.to_s
+ def initialize(*fields)
+ @fields = fields.map(&:to_sym)
end
def matches?(subject)
@klass = class_of(subject)
-
- @klass.index_options.each do |key, options|
- unless key[@field.to_sym] || key[:"#{@field}_id"]
- @error = "no index for #{@field.inspect}"
-
- return false
- end
+ @klass.index_options.any? do |index, options|
+ index.keys == @fields
end
-
- true
end
def failure_message
- "#{@klass} to #{description}, got #{@error}"
+ "#{@klass} to #{description}, but only found indexes #{indexes.inspect}"
end
def negative_failure_message
- "#{@klass} to not #{description}, got #{@klass} to #{description}"
+ "#{@klass} to not #{description}, but found an index for #{@fields.inspect}"
end
def description
- "have an index for #{@field.inspect}"
+ "have an index for #{@fields.inspect}"
+ end
+
+ def indexes
+ @klass.index_options.keys.map(&:keys)
end
end
- def have_index_for(field)
- HaveIndexMatcher.new(field)
+ def have_index_for(*fields)
+ HaveIndexMatcher.new(*fields)
end
end
end
View
3  test/matchers/document_test.rb
@@ -15,11 +15,12 @@
it { must have_fields(:name, :login).of_type(String).with_default_value('me') }
it { must have_index_for(:name) }
+ it { must have_index_for(:email, :login) }
end
describe Pet do
subject { Pet }
- it { must have_index_for(:owner) }
+ it { must have_index_for(:owner_id) }
end
end
View
1  test/models/models.rb
@@ -13,6 +13,7 @@ class Person
field :site, type: String
index({ name: 1 }, { name: 'name' })
+ index({ email: 1, login: 1 })
has_one :account
has_many :pets, inverse_of: :owner
Please sign in to comment.
Something went wrong with that request. Please try again.