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'
describe Kaminari::ActiveRecordRelationMethods do
describe '#total_count' do
before do
@author = User.create! :name => 'author'
@author2 = User.create! :name => 'author2'
@author3 = User.create! :name => 'author3'
@books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
@books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
@books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
@readers = 4.times.map { User.create! :name => 'reader' }
@books.each {|book| book.readers << @readers }
end
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
if defined? ActiveRecord
describe Kaminari::ActiveRecordRelationMethods do
describe '#total_count' do
before do
@author = User.create! :name => 'author'
@author2 = User.create! :name => 'author2'
@author3 = User.create! :name => 'author3'
@books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
@books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
@books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
@readers = 4.times.map { User.create! :name => 'reader' }
@books.each {|book| book.readers << @readers }
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
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
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

View file

@ -1,29 +1,32 @@
require 'spec_helper'
describe 'default per_page' do
describe 'AR::Base' do
subject { ActiveRecord::Base }
it { should_not respond_to :paginates_per }
end
if defined? ActiveRecord
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
its(:limit_value) { should == 25 }
end
subject { User.page 0 }
context 'when explicitly set via paginates_per' 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 }
context 'by default' do
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 }
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

View file

@ -1,162 +1,165 @@
require 'spec_helper'
shared_examples_for 'the first page' do
it { should have(25).users }
its('first.name') { should == 'user001' }
end
if defined? ActiveRecord
shared_examples_for 'blank page' do
it { should have(0).users }
end
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)}
shared_examples_for 'the first page' do
it { should have(25).users }
its('first.name') { should == 'user001' }
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'
shared_examples_for 'blank page' do
it { should have(0).users }
end
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
[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
context 'page 2' do
subject { model_class.page 2 }
it { should have(25).users }
its('first.name') { should == 'user026' }
describe '#per' do
context 'page 1 per 5' do
subject { model_class.page(1).per(5) }
it { should have(5).users }
its('first.name') { should == 'user001' }
end
end
context 'page without an argument' do
subject { model_class.page }
it_should_behave_like 'the first page'
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
context 'page < 1' do
subject { model_class.page 0 }
it_should_behave_like 'the first page'
describe '#num_pages' do
context 'per 25 (default)' do
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
context 'page > max page' do
subject { model_class.page 5 }
it_should_behave_like 'blank page'
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 'ensure #order_values is preserved' do
subject { model_class.order('id').page 1 }
its('order_values.uniq') { should == ['id'] }
end
end
describe '#first_page?' do
context 'on first page' do
subject { model_class.page(1).per(10) }
its(:first_page?) { should == true }
end
describe '#per' do
context 'page 1 per 5' do
subject { model_class.page(1).per(5) }
it { should have(5).users }
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 }
context 'not on first page' do
subject { model_class.page(5).per(10) }
its(:first_page?) { should == false }
end
end
context 'per 7' do
subject { model_class.page(2).per(7) }
its(:num_pages) { should == 15 }
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
context 'per 65536' do
subject { model_class.page(50).per(65536) }
its(:num_pages) { should == 1 }
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 'per 0 (using default)' do
subject { model_class.page(50).per(0) }
its(:num_pages) { should == 4 }
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 'per -1 (using default)' do
subject { model_class.page(5).per(-1) }
its(:num_pages) { should == 4 }
context 'activerecord descendants' do
subject { ActiveRecord::Base.descendants }
its(:length) { should_not == 0 }
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

View file

@ -1,183 +1,183 @@
require 'spec_helper'
require 'dm-core'
require 'dm-migrations'
require 'dm-aggregates'
require 'kaminari/models/data_mapper_extension'
describe Kaminari::DataMapperExtension do
before :all do
DataMapper.setup(:default, 'sqlite::memory:')
if defined? DataMapper
require 'kaminari/models/data_mapper_extension'
class Worker
include ::DataMapper::Resource
describe Kaminari::DataMapperExtension do
before :all do
DataMapper.setup(:default, 'sqlite::memory:')
property :id, Serial
property :name, String, :required => true
property :age, Integer, :required => true
class Worker
include ::DataMapper::Resource
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
class Project
include ::DataMapper::Resource
before do
300.times do |i|
Worker.create(:name => "Worker#{i}", :age => i)
end
property :id, Serial
property :name, String, :required => true
has n, :workers, :through => Resource
worker0 = Worker[0]
50.times do |i|
worker0.projects << Project.create(:name => "Project#{i}")
end
worker0.projects.save
end
DataMapper.finalize
DataMapper.auto_migrate!
end
before do
300.times do |i|
Worker.create(:name => "Worker#{i}", :age => i)
describe 'Collection' do
subject{ Worker.all }
it { should respond_to(:page) }
it { should_not respond_to(:per) }
end
worker0 = Worker[0]
50.times do |i|
worker0.projects << Project.create(:name => "Project#{i}")
end
worker0.projects.save
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 }
describe 'Model' do
subject{ Worker }
it { should respond_to(:page) }
it { should respond_to(:default_per_page) }
it { should_not respond_to(:per) }
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 }
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
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
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
describe '#per' do
context 'on simple query' do
subject { Worker.page(2).per(10) }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its('query.limit') { should == 10 }
its(:limit_value) { should == 10 }
its('query.offset') { should == 10 }
its(:total_count) { should == 300 }
its(:num_pages) { should == 30 }
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 'on query with condition' do
subject { Worker.page(5).all(:age.lte => 100).per(13) }
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 '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 '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 '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
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
describe '#per' do
context 'on simple query' do
subject { Worker.page(2).per(10) }
it { should be_a DataMapper::Collection }
its(:current_page) { should == 2 }
its('query.limit') { should == 10 }
its(:limit_value) { should == 10 }
its('query.offset') { should == 10 }
its(:total_count) { should == 300 }
its(:num_pages) { should == 30 }
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 condition' do
subject { Worker.page(5).all(:age.lte => 100).per(13) }
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 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 }
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

View file

@ -1,86 +1,89 @@
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
begin
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "kaminari_test"
class MongoMapperExtensionDeveloper
include ::MongoMapper::Document
key :salary, Integer
describe Kaminari::MongoMapperExtension do
before do
begin
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "kaminari_test"
class MongoMapperExtensionDeveloper
include ::MongoMapper::Document
key :salary, Integer
end
rescue Mongo::ConnectionFailure
pending 'can not connect to MongoDB'
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
context 'page 2' do
subject { MongoMapperExtensionDeveloper.page 2 }
it { should be_a Plucky::Query }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
before(:each) do
MongoMapperExtensionDeveloper.destroy_all
41.times { MongoMapperExtensionDeveloper.create!({:salary => 1}) }
end
context 'page "foobar"' do
subject { MongoMapperExtensionDeveloper.page 'foobar' }
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
context 'with criteria before' do
it "should have the proper criteria source" do
MongoMapperExtensionDeveloper.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
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
subject { MongoMapperExtensionDeveloper.where(:salary => 1).page 2 }
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}
context 'page 2' do
subject { MongoMapperExtensionDeveloper.page 2 }
it { should be_a Plucky::Query }
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
it { should skip 25 }
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
context 'page "foobar"' do
subject { MongoMapperExtensionDeveloper.page 'foobar' }
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
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 }
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
subject { MongoMapperExtensionDeveloper.where(:salary => 1).page 2 }
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

View file

@ -1,145 +1,148 @@
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
begin
Mongoid.configure do |config|
describe Kaminari::MongoidExtension do
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/
config.sessions = { default: { hosts: [ "localhost:27017" ], database: 'kaminari_test' }}
its(:selector) { should == {'salary' => 1} }
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
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/
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
before :all do
@mongo_developer = MongoMongoidExtensionDeveloper.new
@mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
@mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
@mongo_developer.frameworks.new(:name => "sinatra", :language => "ruby")
@mongo_developer.frameworks.new(:name => "cakephp", :language => "php")
@mongo_developer.frameworks.new(:name => "tornado", :language => "python")
end
class Framework
include ::Mongoid::Document
field :name, :type => String
field :language, :type => String
embedded_in :mongo_mongoid_extension_developer
context 'page 1' do
subject { @mongo_developer.frameworks.page(1).per(1) }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 5 }
its(:limit_value) { should == 1 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 5 }
end
end
before :all do
@mongo_developer = MongoMongoidExtensionDeveloper.new
@mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
@mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
@mongo_developer.frameworks.new(:name => "sinatra", :language => "ruby")
@mongo_developer.frameworks.new(:name => "cakephp", :language => "php")
@mongo_developer.frameworks.new(:name => "tornado", :language => "python")
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 'page 1' do
subject { @mongo_developer.frameworks.page(1).per(1) }
it { should be_a Mongoid::Criteria }
its(:total_count) { should == 5 }
its(:limit_value) { should == 1 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 5 }
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 }
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