mongoid-rspec icon indicating copy to clipboard operation
mongoid-rspec copied to clipboard

Matcher to test that a query is covered by an index (covered query)

Open Startouf opened this issue 5 years ago • 1 comments

Would it be possible to make make a matcher to ensure that a given request is a covered query ?

We could take advantage of mongodb's explain feature to check this.

I have written some basic tests that make sure the winning plan relies on an IXSCAN, I believe it would be possible to build more advanced matchers based on this ?

I know it mainly depends on what data you need to retrieve, but it could prove very useful to ensure the code is fast. Note that it may be slightly more difficult for special queries like count

let(:winning_plan) { criteria.explain.dig('queryPlanner', 'winningPlan') }
expect(winning_plan).to include('stage' => 'IXSCAN')

Startouf avatar Nov 13 '20 11:11 Startouf

Good idea.

dblock avatar Nov 13 '20 15:11 dblock