mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
refactor AR, DM, mongoid, and mongo_mapper specs
extract model definitions for each ORM under spec/fake_app, and load them only if target ORM is loaded by this mechanism, all ORMs share a model name "User", and the other specs such as helper specs and request specs will run polymorphically
This commit is contained in:
parent
a90504de4c
commit
d0cb1837a1
14 changed files with 181 additions and 188 deletions
3
spec/fake_app/active_record/config.rb
Normal file
3
spec/fake_app/active_record/config.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# database
|
||||||
|
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
||||||
|
ActiveRecord::Base.establish_connection('test')
|
|
@ -1,23 +1,3 @@
|
||||||
require 'active_record'
|
|
||||||
require 'action_controller/railtie'
|
|
||||||
require 'action_view/railtie'
|
|
||||||
|
|
||||||
# database
|
|
||||||
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
|
||||||
ActiveRecord::Base.establish_connection('test')
|
|
||||||
|
|
||||||
# config
|
|
||||||
app = Class.new(Rails::Application)
|
|
||||||
app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
|
|
||||||
app.config.session_store :cookie_store, :key => "_myapp_session"
|
|
||||||
app.config.active_support.deprecation = :log
|
|
||||||
app.initialize!
|
|
||||||
|
|
||||||
# routes
|
|
||||||
app.routes.draw do
|
|
||||||
resources :users
|
|
||||||
end
|
|
||||||
|
|
||||||
# models
|
# models
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
has_many :authorships
|
has_many :authorships
|
||||||
|
@ -62,30 +42,6 @@ class User::Address < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
end
|
end
|
||||||
|
|
||||||
# controllers
|
|
||||||
class ApplicationController < ActionController::Base; end
|
|
||||||
class UsersController < ApplicationController
|
|
||||||
def index
|
|
||||||
@users = User.page params[:page]
|
|
||||||
render :inline => <<-ERB
|
|
||||||
<%= @users.map(&:name).join("\n") %>
|
|
||||||
<%= paginate @users %>
|
|
||||||
ERB
|
|
||||||
end
|
|
||||||
end
|
|
||||||
class AddressesController < ApplicationController
|
|
||||||
def index
|
|
||||||
@addresses = User::Address.page params[:page]
|
|
||||||
render :inline => <<-ERB
|
|
||||||
<%= @addresses.map(&:street).join("\n") %>
|
|
||||||
<%= paginate @addresses %>
|
|
||||||
ERB
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# helpers
|
|
||||||
Object.const_set(:ApplicationHelper, Module.new)
|
|
||||||
|
|
||||||
#migrations
|
#migrations
|
||||||
class CreateAllTables < ActiveRecord::Migration
|
class CreateAllTables < ActiveRecord::Migration
|
||||||
def self.up
|
def self.up
|
||||||
|
@ -97,3 +53,4 @@ class CreateAllTables < ActiveRecord::Migration
|
||||||
create_table(:user_addresses) {|t| t.string :street; t.integer :user_id }
|
create_table(:user_addresses) {|t| t.string :street; t.integer :user_id }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
CreateAllTables.up
|
1
spec/fake_app/data_mapper/config.rb
Normal file
1
spec/fake_app/data_mapper/config.rb
Normal file
|
@ -0,0 +1 @@
|
||||||
|
DataMapper.setup(:default, 'sqlite::memory:')
|
27
spec/fake_app/data_mapper/models.rb
Normal file
27
spec/fake_app/data_mapper/models.rb
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
class User
|
||||||
|
include ::DataMapper::Resource
|
||||||
|
|
||||||
|
property :id, Serial
|
||||||
|
property :name, String, :required => true
|
||||||
|
property :age, Integer
|
||||||
|
|
||||||
|
has n, :projects, :through => Resource
|
||||||
|
end
|
||||||
|
|
||||||
|
class User::Address
|
||||||
|
include ::DataMapper::Resource
|
||||||
|
|
||||||
|
property :id, Serial
|
||||||
|
end
|
||||||
|
|
||||||
|
class Project
|
||||||
|
include ::DataMapper::Resource
|
||||||
|
|
||||||
|
property :id, Serial
|
||||||
|
property :name, String, :required => true
|
||||||
|
|
||||||
|
has n, :users, :through => Resource
|
||||||
|
end
|
||||||
|
|
||||||
|
DataMapper.finalize
|
||||||
|
DataMapper.auto_migrate!
|
53
spec/fake_app/fake_app.rb
Normal file
53
spec/fake_app/fake_app.rb
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# require 'rails/all'
|
||||||
|
require 'action_controller/railtie'
|
||||||
|
require 'action_view/railtie'
|
||||||
|
|
||||||
|
require 'fake_app/active_record/config' if defined? ActiveRecord
|
||||||
|
require 'fake_app/data_mapper/config' if defined? DataMapper
|
||||||
|
require 'fake_app/mongoid/config' if defined? Mongoid
|
||||||
|
require 'fake_app/mongo_mapper/config' if defined? MongoMapper
|
||||||
|
# config
|
||||||
|
app = Class.new(Rails::Application)
|
||||||
|
app.config.secret_token = '3b7cd727ee24e8444053437c36cc66c4'
|
||||||
|
app.config.session_store :cookie_store, :key => '_myapp_session'
|
||||||
|
app.config.active_support.deprecation = :log
|
||||||
|
Rails.backtrace_cleaner.remove_silencers!
|
||||||
|
app.initialize!
|
||||||
|
|
||||||
|
# routes
|
||||||
|
app.routes.draw do
|
||||||
|
resources :users
|
||||||
|
end
|
||||||
|
|
||||||
|
#models
|
||||||
|
require 'fake_app/active_record/models' if defined? ActiveRecord
|
||||||
|
require 'fake_app/data_mapper/models' if defined? DataMapper
|
||||||
|
require 'fake_app/mongoid/models' if defined? Mongoid
|
||||||
|
require 'fake_app/mongo_mapper/models' if defined? MongoMapper
|
||||||
|
|
||||||
|
# controllers
|
||||||
|
class ApplicationController < ActionController::Base; end
|
||||||
|
class UsersController < ApplicationController
|
||||||
|
def index
|
||||||
|
@users = User.page params[:page]
|
||||||
|
render :inline => <<-ERB
|
||||||
|
<%= @users.map(&:name).join("\n") %>
|
||||||
|
<%= paginate @users %>
|
||||||
|
ERB
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if defined? ActiveRecord
|
||||||
|
class AddressesController < ApplicationController
|
||||||
|
def index
|
||||||
|
@addresses = User::Address.page params[:page]
|
||||||
|
render :inline => <<-ERB
|
||||||
|
<%= @addresses.map(&:street).join("\n") %>
|
||||||
|
<%= paginate @addresses %>
|
||||||
|
ERB
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# helpers
|
||||||
|
Object.const_set(:ApplicationHelper, Module.new)
|
2
spec/fake_app/mongo_mapper/config.rb
Normal file
2
spec/fake_app/mongo_mapper/config.rb
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
MongoMapper.connection = Mongo::Connection.new 'localhost', 27017
|
||||||
|
MongoMapper.database = 'kaminari_test'
|
9
spec/fake_app/mongo_mapper/models.rb
Normal file
9
spec/fake_app/mongo_mapper/models.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
class User
|
||||||
|
include ::MongoMapper::Document
|
||||||
|
key :name, String
|
||||||
|
key :age, Integer
|
||||||
|
end
|
||||||
|
|
||||||
|
class User::Address
|
||||||
|
include ::MongoMapper::Document
|
||||||
|
end
|
16
spec/fake_app/mongoid/config.rb
Normal file
16
spec/fake_app/mongoid/config.rb
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# Ensure we use 'syck' instead of 'psych' in 1.9.2
|
||||||
|
# RubyGems >= 1.5.0 uses 'psych' on 1.9.2, but
|
||||||
|
# Psych does not yet support YAML 1.1 merge keys.
|
||||||
|
# Merge keys is often used in mongoid.yml
|
||||||
|
# See: http://redmine.ruby-lang.org/issues/show/4300
|
||||||
|
if RUBY_VERSION >= '1.9.2'
|
||||||
|
YAML::ENGINE.yamler = 'syck'
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
22
spec/fake_app/mongoid/models.rb
Normal file
22
spec/fake_app/mongoid/models.rb
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
class User
|
||||||
|
include ::Mongoid::Document
|
||||||
|
field :name, :type => String
|
||||||
|
field :age, :type => Integer
|
||||||
|
end
|
||||||
|
|
||||||
|
class User::Address
|
||||||
|
include ::Mongoid::Document
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
|
@ -1,6 +1,4 @@
|
||||||
# Simulate a gem providing a subclass of ActiveRecord::Base before the Railtie is loaded.
|
# Simulate a gem providing a subclass of ActiveRecord::Base before the Railtie is loaded.
|
||||||
|
|
||||||
require 'active_record'
|
|
||||||
|
|
||||||
class GemDefinedModel < ActiveRecord::Base
|
class GemDefinedModel < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,41 +1,13 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
if defined? DataMapper
|
if defined? DataMapper
|
||||||
require 'kaminari/models/data_mapper_extension'
|
|
||||||
|
|
||||||
describe Kaminari::DataMapperExtension do
|
describe Kaminari::DataMapperExtension do
|
||||||
before :all do
|
|
||||||
DataMapper.setup(:default, 'sqlite::memory:')
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
property :id, Serial
|
|
||||||
property :name, String, :required => true
|
|
||||||
|
|
||||||
has n, :workers, :through => Resource
|
|
||||||
end
|
|
||||||
|
|
||||||
DataMapper.finalize
|
|
||||||
DataMapper.auto_migrate!
|
|
||||||
end
|
|
||||||
|
|
||||||
before do
|
before do
|
||||||
300.times do |i|
|
300.times do |i|
|
||||||
Worker.create(:name => "Worker#{i}", :age => i)
|
User.create(:name => "User#{i}", :age => i)
|
||||||
end
|
end
|
||||||
|
|
||||||
worker0 = Worker[0]
|
worker0 = User[0]
|
||||||
50.times do |i|
|
50.times do |i|
|
||||||
worker0.projects << Project.create(:name => "Project#{i}")
|
worker0.projects << Project.create(:name => "Project#{i}")
|
||||||
end
|
end
|
||||||
|
@ -43,13 +15,13 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Collection' do
|
describe 'Collection' do
|
||||||
subject{ Worker.all }
|
subject{ User.all }
|
||||||
it { should respond_to(:page) }
|
it { should respond_to(:page) }
|
||||||
it { should_not respond_to(:per) }
|
it { should_not respond_to(:per) }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Model' do
|
describe 'Model' do
|
||||||
subject{ Worker }
|
subject{ User }
|
||||||
it { should respond_to(:page) }
|
it { should respond_to(:page) }
|
||||||
it { should respond_to(:default_per_page) }
|
it { should respond_to(:default_per_page) }
|
||||||
it { should_not respond_to(:per) }
|
it { should_not respond_to(:per) }
|
||||||
|
@ -57,17 +29,17 @@ if defined? DataMapper
|
||||||
|
|
||||||
describe '#page' do
|
describe '#page' do
|
||||||
context 'page 0' do
|
context 'page 0' do
|
||||||
subject { Worker.all(:age.gte => 200).page 0 }
|
subject { User.all(:age.gte => 200).page 0 }
|
||||||
it { should be_a DataMapper::Collection }
|
it { should be_a DataMapper::Collection }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its('query.limit') { should == 25 }
|
its('query.limit') { should == 25 }
|
||||||
its('query.offset') { should == 0 }
|
its('query.offset') { should == 0 }
|
||||||
its(:total_count) { should == Worker.count(:age.gte => 200) }
|
its(:total_count) { should == User.count(:age.gte => 200) }
|
||||||
its(:num_pages) { should == 4 }
|
its(:num_pages) { should == 4 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page 1' do
|
context 'page 1' do
|
||||||
subject { Worker.all(:age.gte => 0).page 1 }
|
subject { User.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('query.limit') { should == 25 }
|
its('query.limit') { should == 25 }
|
||||||
|
@ -77,7 +49,7 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page 2' do
|
context 'page 2' do
|
||||||
subject { Worker.page 2 }
|
subject { User.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 }
|
||||||
|
@ -88,7 +60,7 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page "foobar"' do
|
context 'page "foobar"' do
|
||||||
subject { Worker.page 'foobar' }
|
subject { User.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('query.limit') { should == 25 }
|
its('query.limit') { should == 25 }
|
||||||
|
@ -98,29 +70,29 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria before' do
|
context 'with criteria before' do
|
||||||
subject { Worker.all(:age.gt => 100).page 2 }
|
subject { User.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('query.limit') { should == 25 }
|
its('query.limit') { should == 25 }
|
||||||
its('query.offset') { should == 25 }
|
its('query.offset') { should == 25 }
|
||||||
its(:total_count) { should == Worker.count(:age.gt => 100) }
|
its(:total_count) { should == User.count(:age.gt => 100) }
|
||||||
its(:num_pages) { should == 8 }
|
its(:num_pages) { should == 8 }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria after' do
|
context 'with criteria after' do
|
||||||
subject { Worker.page(2).all(:age.gt => 100) }
|
subject { User.page(2).all(:age.gt => 100) }
|
||||||
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 == 25 }
|
its('query.limit') { should == 25 }
|
||||||
its('query.offset') { should == 25 }
|
its('query.offset') { should == 25 }
|
||||||
its(:total_count) { should == Worker.count(:age.gt => 100) }
|
its(:total_count) { should == User.count(:age.gt => 100) }
|
||||||
its(:num_pages) { should == 8 }
|
its(:num_pages) { should == 8 }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#per' do
|
describe '#per' do
|
||||||
context 'on simple query' do
|
context 'on simple query' do
|
||||||
subject { Worker.page(2).per(10) }
|
subject { User.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('query.limit') { should == 10 }
|
||||||
|
@ -131,7 +103,7 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'on query with condition' do
|
context 'on query with condition' do
|
||||||
subject { Worker.page(5).all(:age.lte => 100).per(13) }
|
subject { User.page(5).all(:age.lte => 100).per(13) }
|
||||||
its(:current_page) { should == 5 }
|
its(:current_page) { should == 5 }
|
||||||
its('query.limit') { should == 13 }
|
its('query.limit') { should == 13 }
|
||||||
its('query.offset') { should == 52 }
|
its('query.offset') { should == 52 }
|
||||||
|
@ -140,11 +112,11 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'on query with order' do
|
context 'on query with order' do
|
||||||
subject { Worker.page(5).all(:age.lte => 100, :order => [:age.asc]).per(13) }
|
subject { User.page(5).all(:age.lte => 100, :order => [:age.asc]).per(13) }
|
||||||
it('includes worker with age 52') { should include(Worker.first(:age => 52)) }
|
it('includes worker with age 52') { should include(User.first(:age => 52)) }
|
||||||
it('does not include worker with age 51') { should_not include(Worker.first(:age => 51)) }
|
it('does not include worker with age 51') { should_not include(User.first(:age => 51)) }
|
||||||
it('includes worker with age 52') { should include(Worker.first(:age => 64)) }
|
it('includes worker with age 52') { should include(User.first(:age => 64)) }
|
||||||
it('does not include worker with age 51') { should_not include(Worker.first(:age => 65)) }
|
it('does not include worker with age 51') { should_not include(User.first(:age => 65)) }
|
||||||
its(:current_page) { should == 5 }
|
its(:current_page) { should == 5 }
|
||||||
its('query.limit') { should == 13 }
|
its('query.limit') { should == 13 }
|
||||||
its('query.offset') { should == 52 }
|
its('query.offset') { should == 52 }
|
||||||
|
@ -153,7 +125,7 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'on chained queries' do
|
context 'on chained queries' do
|
||||||
subject { Worker.all(:age.gte => 50).page(3).all(:age.lte => 100).per(13) }
|
subject { User.all(:age.gte => 50).page(3).all(:age.lte => 100).per(13) }
|
||||||
its(:current_page) { should == 3 }
|
its(:current_page) { should == 3 }
|
||||||
its('query.limit') { should == 13 }
|
its('query.limit') { should == 13 }
|
||||||
its('query.offset') { should == 26 }
|
its('query.offset') { should == 26 }
|
||||||
|
@ -162,7 +134,7 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'on query on association' do
|
context 'on query on association' do
|
||||||
subject { Worker[0].projects.page(3).all(:name.like => 'Project%').per(5) }
|
subject { User[0].projects.page(3).all(:name.like => 'Project%').per(5) }
|
||||||
its(:current_page) { should == 3 }
|
its(:current_page) { should == 3 }
|
||||||
its('query.limit') { should == 5 }
|
its('query.limit') { should == 5 }
|
||||||
its('query.offset') { should == 10 }
|
its('query.offset') { should == 10 }
|
||||||
|
@ -171,7 +143,7 @@ if defined? DataMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'on query with association conditions' do
|
context 'on query with association conditions' do
|
||||||
subject { Worker.page(3).all(:projects => Project.all).per(5) }
|
subject { User.page(3).all(:projects => Project.all).per(5) }
|
||||||
its(:current_page) { should == 3 }
|
its(:current_page) { should == 3 }
|
||||||
its('query.limit') { should == 5 }
|
its('query.limit') { should == 5 }
|
||||||
its('query.offset') { should == 10 }
|
its('query.offset') { should == 10 }
|
||||||
|
|
|
@ -1,32 +1,15 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
if defined? MongoMapper
|
if defined? MongoMapper
|
||||||
require 'mongo_mapper'
|
|
||||||
require 'kaminari/models/mongo_mapper_extension'
|
|
||||||
|
|
||||||
describe Kaminari::MongoMapperExtension do
|
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
|
|
||||||
end
|
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
MongoMapperExtensionDeveloper.destroy_all
|
User.destroy_all
|
||||||
41.times { MongoMapperExtensionDeveloper.create!({:salary => 1}) }
|
41.times { User.create!({:salary => 1}) }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#page' do
|
describe '#page' do
|
||||||
context 'page 1' do
|
context 'page 1' do
|
||||||
subject { MongoMapperExtensionDeveloper.page(1) }
|
subject { User.page(1) }
|
||||||
it { should be_a Plucky::Query }
|
it { should be_a Plucky::Query }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:limit_value) { should == 25 }
|
its(:limit_value) { should == 25 }
|
||||||
|
@ -35,7 +18,7 @@ if defined? MongoMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page 2' do
|
context 'page 2' do
|
||||||
subject { MongoMapperExtensionDeveloper.page 2 }
|
subject { User.page 2 }
|
||||||
it { should be_a Plucky::Query }
|
it { should be_a Plucky::Query }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
its(:limit_value) { should == 25 }
|
||||||
|
@ -44,7 +27,7 @@ if defined? MongoMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page "foobar"' do
|
context 'page "foobar"' do
|
||||||
subject { MongoMapperExtensionDeveloper.page 'foobar' }
|
subject { User.page 'foobar' }
|
||||||
it { should be_a Plucky::Query }
|
it { should be_a Plucky::Query }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:limit_value) { should == 25 }
|
its(:limit_value) { should == 25 }
|
||||||
|
@ -54,10 +37,10 @@ if defined? MongoMapper
|
||||||
|
|
||||||
context 'with criteria before' do
|
context 'with criteria before' do
|
||||||
it "should have the proper criteria source" do
|
it "should have the proper criteria source" do
|
||||||
MongoMapperExtensionDeveloper.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
|
User.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
|
||||||
end
|
end
|
||||||
|
|
||||||
subject { MongoMapperExtensionDeveloper.where(:salary => 1).page 2 }
|
subject { User.where(:salary => 1).page 2 }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
its(:limit_value) { should == 25 }
|
||||||
its(:num_pages) { should == 2 }
|
its(:num_pages) { should == 2 }
|
||||||
|
@ -66,10 +49,10 @@ if defined? MongoMapper
|
||||||
|
|
||||||
context 'with criteria after' do
|
context 'with criteria after' do
|
||||||
it "should have the proper criteria source" do
|
it "should have the proper criteria source" do
|
||||||
MongoMapperExtensionDeveloper.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
|
User.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
|
||||||
end
|
end
|
||||||
|
|
||||||
subject { MongoMapperExtensionDeveloper.page(2).where(:salary => 1) }
|
subject { User.page(2).where(:salary => 1) }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
its(:limit_value) { should == 25 }
|
||||||
its(:num_pages) { should == 2 }
|
its(:num_pages) { should == 2 }
|
||||||
|
@ -78,7 +61,7 @@ if defined? MongoMapper
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#per' do
|
describe '#per' do
|
||||||
subject { MongoMapperExtensionDeveloper.page(2).per(10) }
|
subject { User.page(2).per(10) }
|
||||||
it { should be_a Plucky::Query }
|
it { should be_a Plucky::Query }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 10 }
|
its(:limit_value) { should == 10 }
|
||||||
|
|
|
@ -1,40 +1,17 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
if defined? Mongoid
|
if defined? Mongoid
|
||||||
require 'mongoid'
|
|
||||||
require 'kaminari/models/mongoid_extension'
|
|
||||||
|
|
||||||
describe Kaminari::MongoidExtension do
|
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
|
before(:each) do
|
||||||
MongoidExtensionDeveloper.all.destroy
|
|
||||||
41.times do
|
41.times do
|
||||||
MongoidExtensionDeveloper.create!({:salary => 1})
|
User.create!({:salary => 1})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#page' do
|
describe '#page' do
|
||||||
|
|
||||||
context 'page 1' do
|
context 'page 1' do
|
||||||
subject { MongoidExtensionDeveloper.page 1 }
|
subject { User.page 1 }
|
||||||
it { should be_a Mongoid::Criteria }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:limit_value) { should == 25 }
|
its(:limit_value) { should == 25 }
|
||||||
|
@ -43,7 +20,7 @@ if defined? Mongoid
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page 2' do
|
context 'page 2' do
|
||||||
subject { MongoidExtensionDeveloper.page 2 }
|
subject { User.page 2 }
|
||||||
it { should be_a Mongoid::Criteria }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 25 }
|
its(:limit_value) { should == 25 }
|
||||||
|
@ -52,7 +29,7 @@ if defined? Mongoid
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'page "foobar"' do
|
context 'page "foobar"' do
|
||||||
subject { MongoidExtensionDeveloper.page 'foobar' }
|
subject { User.page 'foobar' }
|
||||||
it { should be_a Mongoid::Criteria }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:current_page) { should == 1 }
|
its(:current_page) { should == 1 }
|
||||||
its(:limit_value) { should == 25 }
|
its(:limit_value) { should == 25 }
|
||||||
|
@ -73,18 +50,18 @@ if defined? Mongoid
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria before' do
|
context 'with criteria before' do
|
||||||
subject { MongoidExtensionDeveloper.where(:salary => 1).page 2 }
|
subject { User.where(:salary => 1).page 2 }
|
||||||
it_should_behave_like 'complete valid pagination'
|
it_should_behave_like 'complete valid pagination'
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with criteria after' do
|
context 'with criteria after' do
|
||||||
subject { MongoidExtensionDeveloper.page(2).where(:salary => 1) }
|
subject { User.page(2).where(:salary => 1) }
|
||||||
it_should_behave_like 'complete valid pagination'
|
it_should_behave_like 'complete valid pagination'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#per' do
|
describe '#per' do
|
||||||
subject { MongoidExtensionDeveloper.page(2).per(10) }
|
subject { User.page(2).per(10) }
|
||||||
it { should be_a Mongoid::Criteria }
|
it { should be_a Mongoid::Criteria }
|
||||||
its(:current_page) { should == 2 }
|
its(:current_page) { should == 2 }
|
||||||
its(:limit_value) { should == 10 }
|
its(:limit_value) { should == 10 }
|
||||||
|
@ -93,22 +70,7 @@ if defined? Mongoid
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#page in embedded documents' do
|
describe '#page in embedded documents' do
|
||||||
before :all do
|
before 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
|
|
||||||
|
|
||||||
before :all do
|
|
||||||
@mongo_developer = MongoMongoidExtensionDeveloper.new
|
@mongo_developer = MongoMongoidExtensionDeveloper.new
|
||||||
@mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
|
@mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
|
||||||
@mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
|
@mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
|
||||||
|
|
|
@ -1,24 +1,16 @@
|
||||||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||||
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
||||||
|
|
||||||
|
require 'rails'
|
||||||
require 'bundler/setup'
|
require 'bundler/setup'
|
||||||
Bundler.require
|
Bundler.require
|
||||||
|
|
||||||
require 'rails'
|
|
||||||
require 'mongoid'
|
|
||||||
require 'dm-core'
|
|
||||||
require 'kaminari'
|
|
||||||
require 'database_cleaner'
|
require 'database_cleaner'
|
||||||
# Ensure we use 'syck' instead of 'psych' in 1.9.2
|
|
||||||
# RubyGems >= 1.5.0 uses 'psych' on 1.9.2, but
|
# Simulate a gem providing a subclass of ActiveRecord::Base before the Railtie is loaded.
|
||||||
# Psych does not yet support YAML 1.1 merge keys.
|
require 'fake_gem' if defined? ActiveRecord
|
||||||
# Merge keys is often used in mongoid.yml
|
|
||||||
# See: http://redmine.ruby-lang.org/issues/show/4300
|
require 'fake_app/fake_app'
|
||||||
if RUBY_VERSION >= '1.9.2'
|
|
||||||
YAML::ENGINE.yamler = 'syck'
|
|
||||||
end
|
|
||||||
require 'fake_gem'
|
|
||||||
require 'fake_app'
|
|
||||||
|
|
||||||
require 'rspec/rails'
|
require 'rspec/rails'
|
||||||
# Requires supporting files with custom matchers and macros, etc,
|
# Requires supporting files with custom matchers and macros, etc,
|
||||||
|
@ -27,8 +19,4 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
config.mock_with :rr
|
config.mock_with :rr
|
||||||
config.before :all do
|
|
||||||
# ActiveRecord::Base.connection.execute 'CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255))' unless ActiveRecord::Base.connection.table_exists? 'users'
|
|
||||||
CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'users'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue