1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

[AR, DM, Mongoid, MongoMapper].each {|orm| run orm specs only if orm is loaded }

This commit is contained in:
Akira Matsuda 2012-05-17 21:08:02 +09:00
parent 53bae068e8
commit 854d04af4b
6 changed files with 547 additions and 533 deletions

View file

@ -1,27 +1,29 @@
require 'spec_helper' require 'spec_helper'
describe Kaminari::ActiveRecordRelationMethods do if defined? ActiveRecord
describe '#total_count' do describe Kaminari::ActiveRecordRelationMethods do
before do describe '#total_count' do
@author = User.create! :name => 'author' before do
@author2 = User.create! :name => 'author2' @author = User.create! :name => 'author'
@author3 = User.create! :name => 'author3' @author2 = User.create! :name => 'author2'
@books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) } @author3 = User.create! :name => 'author3'
@books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) } @books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
@books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) } @books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
@readers = 4.times.map { User.create! :name => 'reader' } @books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
@books.each {|book| book.readers << @readers } @readers = 4.times.map { User.create! :name => 'reader' }
end @books.each {|book| book.readers << @readers }
context "when the scope includes an order which references a generated column" do
it "should successfully count the results" do
@author.readers.by_read_count.page(1).total_count.should == @readers.size
end end
end
context "when the scope use conditions on includes" do context "when the scope includes an order which references a generated column" do
it "should keep includes and successfully count the results" do it "should successfully count the results" do
# Only @author and @author2 have books titled with the title00x partern @author.readers.by_read_count.page(1).total_count.should == @readers.size
User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2 end
end
context "when the scope use conditions on includes" do
it "should keep includes and successfully count the results" do
# Only @author and @author2 have books titled with the title00x partern
User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
end
end end
end end
end end

View file

@ -1,29 +1,32 @@
require 'spec_helper' require 'spec_helper'
describe 'default per_page' do if defined? ActiveRecord
describe 'AR::Base' do
subject { ActiveRecord::Base }
it { should_not respond_to :paginates_per }
end
subject { User.page 0 } describe 'default per_page' do
describe 'AR::Base' do
subject { ActiveRecord::Base }
it { should_not respond_to :paginates_per }
end
context 'by default' do subject { User.page 0 }
its(:limit_value) { should == 25 }
end
context 'when explicitly set via paginates_per' do context 'by default' do
before { User.paginates_per 1326 }
its(:limit_value) { should == 1326 }
after { User.paginates_per nil }
end
describe "default per_page value's independency per model" do
context "when User's default per_page was changed" do
before { User.paginates_per 1326 }
subject { Book.page 0 }
its(:limit_value) { should == 25 } its(:limit_value) { should == 25 }
end
context 'when explicitly set via paginates_per' do
before { User.paginates_per 1326 }
its(:limit_value) { should == 1326 }
after { User.paginates_per nil } after { User.paginates_per nil }
end end
describe "default per_page value's independency per model" do
context "when User's default per_page was changed" do
before { User.paginates_per 1326 }
subject { Book.page 0 }
its(:limit_value) { should == 25 }
after { User.paginates_per nil }
end
end
end end
end end

View file

@ -1,162 +1,165 @@
require 'spec_helper' require 'spec_helper'
shared_examples_for 'the first page' do if defined? ActiveRecord
it { should have(25).users }
its('first.name') { should == 'user001' }
end
shared_examples_for 'blank page' do shared_examples_for 'the first page' do
it { should have(0).users } it { should have(25).users }
end its('first.name') { should == 'user001' }
describe Kaminari::ActiveRecordExtension do
before do
1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
1.upto(100) {|i| GemDefinedModel.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
end end
[User, Admin, GemDefinedModel].each do |model_class| shared_examples_for 'blank page' do
context "for #{model_class}" do it { should have(0).users }
describe '#page' do end
context 'page 1' do
subject { model_class.page 1 } describe Kaminari::ActiveRecordExtension do
it_should_behave_like 'the first page' before do
1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
1.upto(100) {|i| GemDefinedModel.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
end
[User, Admin, GemDefinedModel].each do |model_class|
context "for #{model_class}" do
describe '#page' do
context 'page 1' do
subject { model_class.page 1 }
it_should_behave_like 'the first page'
end
context 'page 2' do
subject { model_class.page 2 }
it { should have(25).users }
its('first.name') { should == 'user026' }
end
context 'page without an argument' do
subject { model_class.page }
it_should_behave_like 'the first page'
end
context 'page < 1' do
subject { model_class.page 0 }
it_should_behave_like 'the first page'
end
context 'page > max page' do
subject { model_class.page 5 }
it_should_behave_like 'blank page'
end
describe 'ensure #order_values is preserved' do
subject { model_class.order('id').page 1 }
its('order_values.uniq') { should == ['id'] }
end
end end
context 'page 2' do describe '#per' do
subject { model_class.page 2 } context 'page 1 per 5' do
it { should have(25).users } subject { model_class.page(1).per(5) }
its('first.name') { should == 'user026' } it { should have(5).users }
its('first.name') { should == 'user001' }
end
end end
context 'page without an argument' do describe '#padding' do
subject { model_class.page } context 'page 1 per 5 padding 1' do
it_should_behave_like 'the first page' subject { model_class.page(1).per(5).padding(1) }
it { should have(5).users }
its('first.name') { should == 'user002' }
end
end end
context 'page < 1' do describe '#num_pages' do
subject { model_class.page 0 } context 'per 25 (default)' do
it_should_behave_like 'the first page' subject { model_class.page }
its(:num_pages) { should == 4 }
end
context 'per 7' do
subject { model_class.page(2).per(7) }
its(:num_pages) { should == 15 }
end
context 'per 65536' do
subject { model_class.page(50).per(65536) }
its(:num_pages) { should == 1 }
end
context 'per 0 (using default)' do
subject { model_class.page(50).per(0) }
its(:num_pages) { should == 4 }
end
context 'per -1 (using default)' do
subject { model_class.page(5).per(-1) }
its(:num_pages) { should == 4 }
end
context 'per "String value that can not be converted into Number" (using default)' do
subject { model_class.page(5).per('aho') }
its(:num_pages) { should == 4 }
end
end end
context 'page > max page' do
subject { model_class.page 5 } describe '#current_page' do
it_should_behave_like 'blank page' context 'page 1' do
subject { model_class.page }
its(:current_page) { should == 1 }
end
context 'page 2' do
subject { model_class.page(2).per 3 }
its(:current_page) { should == 2 }
end
end end
describe 'ensure #order_values is preserved' do describe '#first_page?' do
subject { model_class.order('id').page 1 } context 'on first page' do
its('order_values.uniq') { should == ['id'] } subject { model_class.page(1).per(10) }
end its(:first_page?) { should == true }
end end
describe '#per' do context 'not on first page' do
context 'page 1 per 5' do subject { model_class.page(5).per(10) }
subject { model_class.page(1).per(5) } its(:first_page?) { should == false }
it { should have(5).users } end
its('first.name') { should == 'user001' }
end
end
describe '#padding' do
context 'page 1 per 5 padding 1' do
subject { model_class.page(1).per(5).padding(1) }
it { should have(5).users }
its('first.name') { should == 'user002' }
end
end
describe '#num_pages' do
context 'per 25 (default)' do
subject { model_class.page }
its(:num_pages) { should == 4 }
end end
context 'per 7' do describe '#last_page?' do
subject { model_class.page(2).per(7) } context 'on last page' do
its(:num_pages) { should == 15 } subject { model_class.page(10).per(10) }
its(:last_page?) { should == true }
end
context 'not on last page' do
subject { model_class.page(1).per(10) }
its(:last_page?) { should == false }
end
end end
context 'per 65536' do describe '#count' do
subject { model_class.page(50).per(65536) } context 'page 1' do
its(:num_pages) { should == 1 } subject { model_class.page }
its(:count) { should == 25 }
end
context 'page 2' do
subject { model_class.page 2 }
its(:count) { should == 25 }
end
end end
context 'per 0 (using default)' do context 'chained with .group' do
subject { model_class.page(50).per(0) } subject { model_class.group('age').page(2).per 5 }
its(:num_pages) { should == 4 } # 0..10
its(:total_count) { should == 11 }
its(:num_pages) { should == 3 }
end end
context 'per -1 (using default)' do context 'activerecord descendants' do
subject { model_class.page(5).per(-1) } subject { ActiveRecord::Base.descendants }
its(:num_pages) { should == 4 } its(:length) { should_not == 0 }
end end
context 'per "String value that can not be converted into Number" (using default)' do
subject { model_class.page(5).per('aho') }
its(:num_pages) { should == 4 }
end
end
describe '#current_page' do
context 'page 1' do
subject { model_class.page }
its(:current_page) { should == 1 }
end
context 'page 2' do
subject { model_class.page(2).per 3 }
its(:current_page) { should == 2 }
end
end
describe '#first_page?' do
context 'on first page' do
subject { model_class.page(1).per(10) }
its(:first_page?) { should == true }
end
context 'not on first page' do
subject { model_class.page(5).per(10) }
its(:first_page?) { should == false }
end
end
describe '#last_page?' do
context 'on last page' do
subject { model_class.page(10).per(10) }
its(:last_page?) { should == true }
end
context 'not on last page' do
subject { model_class.page(1).per(10) }
its(:last_page?) { should == false }
end
end
describe '#count' do
context 'page 1' do
subject { model_class.page }
its(:count) { should == 25 }
end
context 'page 2' do
subject { model_class.page 2 }
its(:count) { should == 25 }
end
end
context 'chained with .group' do
subject { model_class.group('age').page(2).per 5 }
# 0..10
its(:total_count) { should == 11 }
its(:num_pages) { should == 3 }
end
context 'activerecord descendants' do
subject { ActiveRecord::Base.descendants }
its(:length) { should_not == 0 }
end end
end end
end end

View file

@ -1,183 +1,183 @@
require 'spec_helper' require 'spec_helper'
require 'dm-core'
require 'dm-migrations'
require 'dm-aggregates'
require 'kaminari/models/data_mapper_extension'
describe Kaminari::DataMapperExtension do if defined? DataMapper
before :all do require 'kaminari/models/data_mapper_extension'
DataMapper.setup(:default, 'sqlite::memory:')
class Worker describe Kaminari::DataMapperExtension do
include ::DataMapper::Resource before :all do
DataMapper.setup(:default, 'sqlite::memory:')
property :id, Serial class Worker
property :name, String, :required => true include ::DataMapper::Resource
property :age, Integer, :required => true
has n, :projects, :through => Resource property :id, Serial
property :name, String, :required => true
property :age, Integer, :required => true
has n, :projects, :through => Resource
end
class Project
include ::DataMapper::Resource
property :id, Serial
property :name, String, :required => true
has n, :workers, :through => Resource
end
DataMapper.finalize
DataMapper.auto_migrate!
end end
class Project before do
include ::DataMapper::Resource 300.times do |i|
Worker.create(:name => "Worker#{i}", :age => i)
end
property :id, Serial worker0 = Worker[0]
property :name, String, :required => true 50.times do |i|
worker0.projects << Project.create(:name => "Project#{i}")
has n, :workers, :through => Resource end
worker0.projects.save
end end
DataMapper.finalize describe 'Collection' do
DataMapper.auto_migrate! subject{ Worker.all }
end it { should respond_to(:page) }
it { should_not respond_to(:per) }
before do
300.times do |i|
Worker.create(:name => "Worker#{i}", :age => i)
end end
worker0 = Worker[0] describe 'Model' do
50.times do |i| subject{ Worker }
worker0.projects << Project.create(:name => "Project#{i}") it { should respond_to(:page) }
end it { should respond_to(:default_per_page) }
worker0.projects.save it { should_not respond_to(:per) }
end
describe 'Collection' do
subject{ Worker.all }
it { should respond_to(:page) }
it { should_not respond_to(:per) }
end
describe 'Model' do
subject{ Worker }
it { should respond_to(:page) }
it { should respond_to(:default_per_page) }
it { should_not respond_to(:per) }
end
describe '#page' do
context 'page 0' do
subject { Worker.all(:age.gte => 200).page 0 }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 1 }
its('query.limit') { should == 25 }
its('query.offset') { should == 0 }
its(:total_count) { should == Worker.count(:age.gte => 200) }
its(:num_pages) { should == 4 }
end end
context 'page 1' do describe '#page' do
subject { Worker.all(:age.gte => 0).page 1 } context 'page 0' do
it { should be_a DataMapper::Collection } subject { Worker.all(:age.gte => 200).page 0 }
its(:current_page) { should == 1 } it { should be_a DataMapper::Collection }
its('query.limit') { should == 25 } its(:current_page) { should == 1 }
its('query.offset') { should == 0 } its('query.limit') { should == 25 }
its(:total_count) { should == 300 } its('query.offset') { should == 0 }
its(:num_pages) { should == 12 } its(:total_count) { should == Worker.count(:age.gte => 200) }
its(:num_pages) { should == 4 }
end
context 'page 1' do
subject { Worker.all(:age.gte => 0).page 1 }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 1 }
its('query.limit') { should == 25 }
its('query.offset') { should == 0 }
its(:total_count) { should == 300 }
its(:num_pages) { should == 12 }
end
context 'page 2' do
subject { Worker.page 2 }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its('query.limit') { should == 25 }
its('query.offset') { should == 25 }
its(:total_count) { should == 300 }
its(:num_pages) { should == 12 }
end
context 'page "foobar"' do
subject { Worker.page 'foobar' }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 1 }
its('query.limit') { should == 25 }
its('query.offset') { should == 0 }
its(:total_count) { should == 300 }
its(:num_pages) { should == 12 }
end
context 'with criteria before' do
subject { Worker.all(:age.gt => 100).page 2 }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its('query.limit') { should == 25 }
its('query.offset') { should == 25 }
its(:total_count) { should == Worker.count(:age.gt => 100) }
its(:num_pages) { should == 8 }
end
context 'with criteria after' do
subject { Worker.page(2).all(:age.gt => 100) }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its('query.limit') { should == 25 }
its('query.offset') { should == 25 }
its(:total_count) { should == Worker.count(:age.gt => 100) }
its(:num_pages) { should == 8 }
end
end end
context 'page 2' do describe '#per' do
subject { Worker.page 2 } context 'on simple query' do
it { should be_a DataMapper::Collection } subject { Worker.page(2).per(10) }
its(:current_page) { should == 2 } it { should be_a DataMapper::Collection }
its(:limit_value) { should == 25 } its(:current_page) { should == 2 }
its('query.limit') { should == 25 } its('query.limit') { should == 10 }
its('query.offset') { should == 25 } its(:limit_value) { should == 10 }
its(:total_count) { should == 300 } its('query.offset') { should == 10 }
its(:num_pages) { should == 12 } its(:total_count) { should == 300 }
end its(:num_pages) { should == 30 }
end
context 'page "foobar"' do context 'on query with condition' do
subject { Worker.page 'foobar' } subject { Worker.page(5).all(:age.lte => 100).per(13) }
it { should be_a DataMapper::Collection } its(:current_page) { should == 5 }
its(:current_page) { should == 1 } its('query.limit') { should == 13 }
its('query.limit') { should == 25 } its('query.offset') { should == 52 }
its('query.offset') { should == 0 } its(:total_count) { should == 101 }
its(:total_count) { should == 300 } its(:num_pages) { should == 8 }
its(:num_pages) { should == 12 } end
end
context 'with criteria before' do context 'on query with order' do
subject { Worker.all(:age.gt => 100).page 2 } subject { Worker.page(5).all(:age.lte => 100, :order => [:age.asc]).per(13) }
it { should be_a DataMapper::Collection } it('includes worker with age 52') { should include(Worker.first(:age => 52)) }
its(:current_page) { should == 2 } it('does not include worker with age 51') { should_not include(Worker.first(:age => 51)) }
its('query.limit') { should == 25 } it('includes worker with age 52') { should include(Worker.first(:age => 64)) }
its('query.offset') { should == 25 } it('does not include worker with age 51') { should_not include(Worker.first(:age => 65)) }
its(:total_count) { should == Worker.count(:age.gt => 100) } its(:current_page) { should == 5 }
its(:num_pages) { should == 8 } its('query.limit') { should == 13 }
end its('query.offset') { should == 52 }
its(:total_count) { should == 101 }
its(:num_pages) { should == 8 }
end
context 'with criteria after' do context 'on chained queries' do
subject { Worker.page(2).all(:age.gt => 100) } subject { Worker.all(:age.gte => 50).page(3).all(:age.lte => 100).per(13) }
it { should be_a DataMapper::Collection } its(:current_page) { should == 3 }
its(:current_page) { should == 2 } its('query.limit') { should == 13 }
its('query.limit') { should == 25 } its('query.offset') { should == 26 }
its('query.offset') { should == 25 } its(:total_count) { should == 51 }
its(:total_count) { should == Worker.count(:age.gt => 100) } its(:num_pages) { should == 4 }
its(:num_pages) { should == 8 } end
end
end
describe '#per' do context 'on query on association' do
context 'on simple query' do subject { Worker[0].projects.page(3).all(:name.like => 'Project%').per(5) }
subject { Worker.page(2).per(10) } its(:current_page) { should == 3 }
it { should be_a DataMapper::Collection } its('query.limit') { should == 5 }
its(:current_page) { should == 2 } its('query.offset') { should == 10 }
its('query.limit') { should == 10 } its(:total_count) { should == 50 }
its(:limit_value) { should == 10 } its(:num_pages) { should == 10 }
its('query.offset') { should == 10 } end
its(:total_count) { should == 300 }
its(:num_pages) { should == 30 }
end
context 'on query with condition' do context 'on query with association conditions' do
subject { Worker.page(5).all(:age.lte => 100).per(13) } subject { Worker.page(3).all(:projects => Project.all).per(5) }
its(:current_page) { should == 5 } its(:current_page) { should == 3 }
its('query.limit') { should == 13 } its('query.limit') { should == 5 }
its('query.offset') { should == 52 } its('query.offset') { should == 10 }
its(:total_count) { should == 101 } its(:total_count) { should == 50 }
its(:num_pages) { should == 8 } its(:num_pages) { should == 10 }
end end
context 'on query with order' do
subject { Worker.page(5).all(:age.lte => 100, :order => [:age.asc]).per(13) }
it('includes worker with age 52') { should include(Worker.first(:age => 52)) }
it('does not include worker with age 51') { should_not include(Worker.first(:age => 51)) }
it('includes worker with age 52') { should include(Worker.first(:age => 64)) }
it('does not include worker with age 51') { should_not include(Worker.first(:age => 65)) }
its(:current_page) { should == 5 }
its('query.limit') { should == 13 }
its('query.offset') { should == 52 }
its(:total_count) { should == 101 }
its(:num_pages) { should == 8 }
end
context 'on chained queries' do
subject { Worker.all(:age.gte => 50).page(3).all(:age.lte => 100).per(13) }
its(:current_page) { should == 3 }
its('query.limit') { should == 13 }
its('query.offset') { should == 26 }
its(:total_count) { should == 51 }
its(:num_pages) { should == 4 }
end
context 'on query on association' do
subject { Worker[0].projects.page(3).all(:name.like => 'Project%').per(5) }
its(:current_page) { should == 3 }
its('query.limit') { should == 5 }
its('query.offset') { should == 10 }
its(:total_count) { should == 50 }
its(:num_pages) { should == 10 }
end
context 'on query with association conditions' do
subject { Worker.page(3).all(:projects => Project.all).per(5) }
its(:current_page) { should == 3 }
its('query.limit') { should == 5 }
its('query.offset') { should == 10 }
its(:total_count) { should == 50 }
its(:num_pages) { should == 10 }
end end
end end
end end

View file

@ -1,86 +1,89 @@
require 'spec_helper' require 'spec_helper'
require 'mongo_mapper'
require 'kaminari/models/mongo_mapper_extension'
describe Kaminari::MongoMapperExtension do if defined? MongoMapper
require 'mongo_mapper'
require 'kaminari/models/mongo_mapper_extension'
before do describe Kaminari::MongoMapperExtension do
begin
MongoMapper.connection = Mongo::Connection.new('localhost', 27017) before do
MongoMapper.database = "kaminari_test" begin
class MongoMapperExtensionDeveloper MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
include ::MongoMapper::Document MongoMapper.database = "kaminari_test"
key :salary, Integer class MongoMapperExtensionDeveloper
include ::MongoMapper::Document
key :salary, Integer
end
rescue Mongo::ConnectionFailure
pending 'can not connect to MongoDB'
end end
rescue Mongo::ConnectionFailure
pending 'can not connect to MongoDB'
end
end
before(:each) do
MongoMapperExtensionDeveloper.destroy_all
41.times { MongoMapperExtensionDeveloper.create!({:salary => 1}) }
end
describe '#page' do
context 'page 1' do
subject { MongoMapperExtensionDeveloper.page(1) }
it { should be_a Plucky::Query }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip(0) }
end end
context 'page 2' do before(:each) do
subject { MongoMapperExtensionDeveloper.page 2 } MongoMapperExtensionDeveloper.destroy_all
it { should be_a Plucky::Query } 41.times { MongoMapperExtensionDeveloper.create!({:salary => 1}) }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
end end
context 'page "foobar"' do describe '#page' do
subject { MongoMapperExtensionDeveloper.page 'foobar' } context 'page 1' do
it { should be_a Plucky::Query } subject { MongoMapperExtensionDeveloper.page(1) }
its(:current_page) { should == 1 } it { should be_a Plucky::Query }
its(:limit_value) { should == 25 } its(:current_page) { should == 1 }
its(:num_pages) { should == 2 } its(:limit_value) { should == 25 }
it { should skip 0 } its(:num_pages) { should == 2 }
end it { should skip(0) }
context 'with criteria before' do
it "should have the proper criteria source" do
MongoMapperExtensionDeveloper.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
end end
subject { MongoMapperExtensionDeveloper.where(:salary => 1).page 2 } context 'page 2' do
its(:current_page) { should == 2 } subject { MongoMapperExtensionDeveloper.page 2 }
its(:limit_value) { should == 25 } it { should be_a Plucky::Query }
its(:num_pages) { should == 2 } its(:current_page) { should == 2 }
it { should skip 25 } its(:limit_value) { should == 25 }
end its(:num_pages) { should == 2 }
it { should skip 25 }
context 'with criteria after' do
it "should have the proper criteria source" do
MongoMapperExtensionDeveloper.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
end end
subject { MongoMapperExtensionDeveloper.page(2).where(:salary => 1) } context 'page "foobar"' do
its(:current_page) { should == 2 } subject { MongoMapperExtensionDeveloper.page 'foobar' }
its(:limit_value) { should == 25 } it { should be_a Plucky::Query }
its(:num_pages) { should == 2 } its(:current_page) { should == 1 }
it { should skip 25 } its(:limit_value) { should == 25 }
end its(:num_pages) { should == 2 }
end it { should skip 0 }
end
describe '#per' do context 'with criteria before' do
subject { MongoMapperExtensionDeveloper.page(2).per(10) } it "should have the proper criteria source" do
it { should be_a Plucky::Query } MongoMapperExtensionDeveloper.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
its(:current_page) { should == 2 } end
its(:limit_value) { should == 10 }
its(:num_pages) { should == 5 } subject { MongoMapperExtensionDeveloper.where(:salary => 1).page 2 }
it { should skip 10 } its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
end
context 'with criteria after' do
it "should have the proper criteria source" do
MongoMapperExtensionDeveloper.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
end
subject { MongoMapperExtensionDeveloper.page(2).where(:salary => 1) }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
end
end
describe '#per' do
subject { MongoMapperExtensionDeveloper.page(2).per(10) }
it { should be_a Plucky::Query }
its(:current_page) { should == 2 }
its(:limit_value) { should == 10 }
its(:num_pages) { should == 5 }
it { should skip 10 }
end
end end
end end

View file

@ -1,145 +1,148 @@
require 'spec_helper' require 'spec_helper'
require 'mongoid'
require 'kaminari/models/mongoid_extension'
describe Kaminari::MongoidExtension do if defined? Mongoid
require 'mongoid'
require 'kaminari/models/mongoid_extension'
before do describe Kaminari::MongoidExtension do
begin
Mongoid.configure do |config| before do
begin
Mongoid.configure do |config|
if Mongoid::VERSION =~ /^3/
config.sessions = { default: { hosts: [ "localhost:27017" ], database: 'kaminari_test' }}
else
config.master = Mongo::Connection.new.db("kaminari_test")
end
end
class MongoidExtensionDeveloper
include ::Mongoid::Document
field :salary, :type => Integer
end
rescue Mongo::ConnectionFailure
pending 'can not connect to MongoDB'
end
end
before(:each) do
MongoidExtensionDeveloper.all.destroy
41.times do
MongoidExtensionDeveloper.create!({:salary => 1})
end
end
describe '#page' do
context 'page 1' do
subject { MongoidExtensionDeveloper.page 1 }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip(0) }
end
context 'page 2' do
subject { MongoidExtensionDeveloper.page 2 }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
end
context 'page "foobar"' do
subject { MongoidExtensionDeveloper.page 'foobar' }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 0 }
end
shared_examples 'complete valid pagination' do
if Mongoid::VERSION =~ /^3/ if Mongoid::VERSION =~ /^3/
config.sessions = { default: { hosts: [ "localhost:27017" ], database: 'kaminari_test' }} its(:selector) { should == {'salary' => 1} }
else else
config.master = Mongo::Connection.new.db("kaminari_test") its(:selector) { should == {:salary => 1} }
end
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
end
context 'with criteria before' do
subject { MongoidExtensionDeveloper.where(:salary => 1).page 2 }
it_should_behave_like 'complete valid pagination'
end
context 'with criteria after' do
subject { MongoidExtensionDeveloper.page(2).where(:salary => 1) }
it_should_behave_like 'complete valid pagination'
end
end
describe '#per' do
subject { MongoidExtensionDeveloper.page(2).per(10) }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:limit_value) { should == 10 }
its(:num_pages) { should == 5 }
it { should skip 10 }
end
describe '#page in embedded documents' do
before :all do
class MongoMongoidExtensionDeveloper
include ::Mongoid::Document
field :salary, :type => Integer
embeds_many :frameworks
end
class Framework
include ::Mongoid::Document
field :name, :type => String
field :language, :type => String
embedded_in :mongo_mongoid_extension_developer
end end
end end
class MongoidExtensionDeveloper
include ::Mongoid::Document
field :salary, :type => Integer
end
rescue Mongo::ConnectionFailure
pending 'can not connect to MongoDB'
end
end
before(:each) do before :all do
MongoidExtensionDeveloper.all.destroy @mongo_developer = MongoMongoidExtensionDeveloper.new
41.times do @mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
MongoidExtensionDeveloper.create!({:salary => 1}) @mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
end @mongo_developer.frameworks.new(:name => "sinatra", :language => "ruby")
end @mongo_developer.frameworks.new(:name => "cakephp", :language => "php")
@mongo_developer.frameworks.new(:name => "tornado", :language => "python")
describe '#page' do
context 'page 1' do
subject { MongoidExtensionDeveloper.page 1 }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip(0) }
end
context 'page 2' do
subject { MongoidExtensionDeveloper.page 2 }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
end
context 'page "foobar"' do
subject { MongoidExtensionDeveloper.page 'foobar' }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 0 }
end
shared_examples 'complete valid pagination' do
if Mongoid::VERSION =~ /^3/
its(:selector) { should == {'salary' => 1} }
else
its(:selector) { should == {:salary => 1} }
end
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
end
context 'with criteria before' do
subject { MongoidExtensionDeveloper.where(:salary => 1).page 2 }
it_should_behave_like 'complete valid pagination'
end
context 'with criteria after' do
subject { MongoidExtensionDeveloper.page(2).where(:salary => 1) }
it_should_behave_like 'complete valid pagination'
end
end
describe '#per' do
subject { MongoidExtensionDeveloper.page(2).per(10) }
it { should be_a Mongoid::Criteria }
its(:current_page) { should == 2 }
its(:limit_value) { should == 10 }
its(:num_pages) { should == 5 }
it { should skip 10 }
end
describe '#page in embedded documents' do
before :all do
class MongoMongoidExtensionDeveloper
include ::Mongoid::Document
field :salary, :type => Integer
embeds_many :frameworks
end end
class Framework context 'page 1' do
include ::Mongoid::Document subject { @mongo_developer.frameworks.page(1).per(1) }
field :name, :type => String it { should be_a Mongoid::Criteria }
field :language, :type => String its(:total_count) { should == 5 }
embedded_in :mongo_mongoid_extension_developer its(:limit_value) { should == 1 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 5 }
end end
end
before :all do context 'with criteria after' do
@mongo_developer = MongoMongoidExtensionDeveloper.new subject { @mongo_developer.frameworks.page(1).per(2).where(:language => "ruby") }
@mongo_developer.frameworks.new(:name => "rails", :language => "ruby") it { should be_a Mongoid::Criteria }
@mongo_developer.frameworks.new(:name => "merb", :language => "ruby") its(:total_count) { should == 3 }
@mongo_developer.frameworks.new(:name => "sinatra", :language => "ruby") its(:limit_value) { should == 2 }
@mongo_developer.frameworks.new(:name => "cakephp", :language => "php") its(:current_page) { should == 1 }
@mongo_developer.frameworks.new(:name => "tornado", :language => "python") its(:num_pages) { should == 2 }
end end
context 'page 1' do context 'with criteria before' do
subject { @mongo_developer.frameworks.page(1).per(1) } subject { @mongo_developer.frameworks.where(:language => "ruby").page(1).per(2) }
it { should be_a Mongoid::Criteria } it { should be_a Mongoid::Criteria }
its(:total_count) { should == 5 } its(:total_count) { should == 3 }
its(:limit_value) { should == 1 } its(:limit_value) { should == 2 }
its(:current_page) { should == 1 } its(:current_page) { should == 1 }
its(:num_pages) { should == 5 } its(:num_pages) { should == 2 }
end end
context 'with criteria after' do
subject { @mongo_developer.frameworks.page(1).per(2).where(:language => "ruby") }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 3 }
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 2 }
end
context 'with criteria before' do
subject { @mongo_developer.frameworks.where(:language => "ruby").page(1).per(2) }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 3 }
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 2 }
end end
end end
end end