Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

198 lines (152 sloc) 3.526 kb
require "spec_helper"
describe Mongoid::QueryCache do
around do |spec|
Mongoid::QueryCache.clear_cache
Mongoid::QueryCache.cache { spec.run }
end
context "when querying for a single document" do
[ :first, :one ].each do |method|
before do
Band.all.send(method)
end
context "when query cache disable" do
before do
Mongoid::QueryCache.enabled = false
end
it "queries again" do
expect_query(1) do
Band.all.send(method)
end
end
end
context "with same selector" do
it "does not query again" do
expect_no_queries do
Band.all.send(method)
end
end
end
context "with different selector" do
it "queries again" do
expect_query(1) do
Band.where(id: 1).send(method)
end
end
end
end
end
context "when querying in the same collection" do
before do
Band.all.to_a
end
context "when query cache disable" do
before do
Mongoid::QueryCache.enabled = false
end
it "queries again" do
expect_query(1) do
Band.all.to_a
end
end
end
context "with same selector" do
it "does not query again" do
expect_no_queries do
Band.all.to_a
end
end
end
context "with different selector" do
it "queries again" do
expect_query(1) do
Band.where(id: 1).to_a
end
end
end
end
context "when querying in different collection" do
before do
Person.all.to_a
end
it "queries again" do
expect_query(1) do
Band.all.to_a
end
end
end
context "when inserting a new document" do
before do
Band.all.to_a
Band.create!
end
it "queries again" do
expect_query(1) do
Band.all.to_a
end
end
end
context "when deleting all documents" do
before do
Band.create!
Band.all.to_a
Band.delete_all
end
it "queries again" do
expect_query(1) do
Band.all.to_a
end
end
end
context "when destroying all documents" do
before do
Band.create!
Band.all.to_a
Band.destroy_all
end
it "queries again" do
expect_query(1) do
Band.all.to_a
end
end
end
context "when inserting an index" do
it "does not cache the query" do
expect(Mongoid::QueryCache).to receive(:cache_table).never
Band.collection.indexes.create(name: 1)
end
end
end
describe Mongoid::QueryCache::Middleware do
let :middleware do
Mongoid::QueryCache::Middleware.new(app)
end
context "when not touching mongoid on the app" do
let(:app) do
->(env) { @enabled = Mongoid::QueryCache.enabled?; [200, env, "app"] }
end
it "returns success" do
code, _ = middleware.call({})
expect(code).to eq(200)
end
it "enableds the query cache" do
middleware.call({})
expect(@enabled).to be true
end
end
context "when querying on the app" do
let(:app) do
->(env) {
Band.all.to_a
[200, env, "app"]
}
end
it "returns success" do
code, _ = middleware.call({})
expect(code).to eq(200)
end
it "cleans the query cache after reponds" do
middleware.call({})
expect(Mongoid::QueryCache.cache_table).to be_empty
end
end
end
Jump to Line
Something went wrong with that request. Please try again.