forked from kaminari/kaminari
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'datamapper-int' of https://github.com/Ragmaanir/kaminari …
…into Ragmaanir-datamapper-int Conflicts: kaminari.gemspec lib/kaminari/models/data_mapper_extension.rb lib/kaminari/railtie.rb spec/models/data_mapper_spec.rb also, refactored a lot to make the code simple
- Loading branch information
Showing
6 changed files
with
178 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,64 +1,181 @@ | |||
require File.expand_path('../spec_helper', File.dirname(__FILE__)) | require File.expand_path('../spec_helper', File.dirname(__FILE__)) | ||
require 'dm-core' | require 'dm-core' | ||
require 'dm-migrations' | |||
require 'dm-aggregates' | |||
require 'kaminari/models/data_mapper_extension' | require 'kaminari/models/data_mapper_extension' | ||
|
|
||
describe Kaminari::DataMapperExtension do | describe Kaminari::DataMapperExtension do | ||
before :all do | before :all do | ||
DataMapper.setup(:default, 'sqlite::memory:') | DataMapper.setup(:default, 'sqlite::memory:') | ||
class Developer |
|
||
class Worker | |||
include ::DataMapper::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 | include ::DataMapper::Resource | ||
|
|||
property :id, Serial | property :id, Serial | ||
property :salary, Integer | property :name, String, :required => true | ||
|
|||
has n, :workers, :through => Resource | |||
end | |||
|
|||
DataMapper.finalize | |||
DataMapper.auto_migrate! | |||
|
|||
300.times do |i| | |||
Worker.create(:name => "Worker#{i}", :age => i) | |||
end | 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 | end | ||
before do |
|
||
stub(subject).count { 300 } # in order to avoid DB access... | describe 'Model' do | ||
subject{ Worker } | |||
it { should respond_to(:page) } | |||
it { should respond_to(:default_per_page) } | |||
it { should_not respond_to(:per) } | |||
end | end | ||
|
|
||
describe '#page' do | 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 | context 'page 1' do | ||
subject { Developer.page(1) } | subject { Worker.all(:age.gte => 0).page 1 } | ||
it { should be_a DataMapper::Collection } | it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 1 } | its(:current_page) { should == 1 } | ||
its(:limit_value) { should == 25 } | its('query.limit') { should == 25 } | ||
its('query.offset') { should == 0 } | |||
its(:total_count) { should == 300 } | |||
its(:num_pages) { should == 12 } | its(:num_pages) { should == 12 } | ||
it { should offset(0) } | |||
end | end | ||
|
|
||
context 'page 2' do | context 'page 2' do | ||
subject { Developer.page 2 } | subject { Worker.page 2 } | ||
it { should be_a DataMapper::Collection } | it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 2 } | its(:current_page) { should == 2 } | ||
its(:limit_value) { should == 25 } | 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 } | its(:num_pages) { should == 12 } | ||
it { should offset 25 } | |||
end | end | ||
|
|
||
context 'page "foobar"' do | context 'page "foobar"' do | ||
subject { Developer.page 'foobar' } | subject { Worker.page 'foobar' } | ||
it { should be_a DataMapper::Collection } | it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 1 } | its(:current_page) { should == 1 } | ||
its(:limit_value) { should == 25 } | its('query.limit') { should == 25 } | ||
its('query.offset') { should == 0 } | |||
its(:total_count) { should == 300 } | |||
its(:num_pages) { should == 12 } | its(:num_pages) { should == 12 } | ||
it { should offset 0 } | |||
end | end | ||
|
|
||
context 'page 1 with another conditions' do | context 'with criteria before' do | ||
subject { Developer.page(2) } | subject { Worker.all(:age.gt => 100).page 2 } | ||
it { should be_a DataMapper::Collection } | it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 2 } | its(:current_page) { should == 2 } | ||
its(:limit_value) { should == 25 } | its('query.limit') { should == 25 } | ||
its(:num_pages) { should == 12 } | its('query.offset') { should == 25 } | ||
it { should offset(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 | end | ||
|
|
||
describe '#per' do | describe '#per' do | ||
subject { Developer.page(2).per(10) } | context 'on simple query' do | ||
subject { Worker.page(2).per(10) } | |||
it { should be_a DataMapper::Collection } | it { should be_a DataMapper::Collection } | ||
its(:current_page) { should == 2 } | its(:current_page) { should == 2 } | ||
its('query.limit') { should == 10 } | |||
its(:limit_value) { should == 10 } | its(:limit_value) { should == 10 } | ||
its(:num_pages) { should == 30 } | its('query.offset') { should == 10 } | ||
it { should offset 10 } | its(:total_count) { should == 300 } | ||
its(:num_pages) { should == 30 } | |||
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 } | |||
end | |||
end | end | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters