the mongodb adapter returns the same results as the FS one for some page repository methods + rake spec runs all the specs except for the liquid and server specs

did committed Feb 24, 2015
commit 1be102113f6bc8cb175fea6a2b3a4d0c52f7883e
Showing 15 changed files with 170 additions and 65 deletions
Rakefile +5 -3
@@ @@ -15,15 +15,17 @@ require_relative 'lib/locomotive/steam'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new('spec') do |spec|
- spec.pattern = 'spec/unit/{services,core_ext,middlewares,decorators,adapters,entities,models,repositories}/**/*_spec.rb'
+ spec.exclude_pattern = 'spec/unit/liquid/**/*_spec.rb,spec/integration/server/**/*_spec.rb'
end
RSpec::Core::RakeTask.new('spec:integration') do |spec|
- spec.pattern = 'spec/integration/**/*_spec.rb'
+ # spec.pattern = 'spec/integration/**/*_spec.rb'
+ spec.pattern = 'spec/integration/{mongodb,repositories}/**/*_spec.rb'
end
RSpec::Core::RakeTask.new('spec:unit') do |spec|
- spec.pattern = 'spec/unit/**/*_spec.rb'
+ # spec.pattern = 'spec/unit/**/*_spec.rb'
+ spec.pattern = 'spec/unit/{services,core_ext,middlewares,decorators,adapters,entities,models,repositories}/**/*_spec.rb'
end
locomotive/steam/adapters/mongodb.rb b/lib/locomotive/steam/adapters/mongodb.rb +5 -5
@@ @@ -9,13 +9,13 @@ module Locomotive::Steam
class MongoDBAdapter < Struct.new(:database, :hosts)
- def all(mapper, selector = nil, options)
- dataset(mapper, selector, options)
+ def all(mapper, query)
+ dataset(mapper, query)
end
def query(mapper, scope, &block)
query = query_klass.new(scope, mapper.localized_attributes, &block)
- all(mapper, query.selector, query.options)
+ all(mapper, query)
end
private
@@ @@ -24,9 +24,9 @@ module Locomotive::Steam
Locomotive::Steam::Adapters::MongoDB::Query
end
- def dataset(mapper, selector = nil, options = {})
+ def dataset(mapper, query)
Locomotive::Steam::Adapters::MongoDB::Dataset.new do
- collection(mapper).find(selector).sort(options[:sort]).map do |attributes|
+ query.against(collection(mapper)).map do |attributes|
entity = mapper.to_entity(attributes)
end
end
locomotive/steam/adapters/mongodb/dataset.rb b/lib/locomotive/steam/adapters/mongodb/dataset.rb +20 -0
@@ @@ -0,0 +1,20 @@
+ module Locomotive::Steam
+ module Adapters
+ module MongoDB
+
+ class Dataset < SimpleDelegator
+
+ def initialize(records = [], &block)
+ @records = block_given? ? yield : records
+ super(@records)
+ end
+
+ def all
+ @records
+ end
+
+ end
+
+ end
+ end
+ end
locomotive/steam/adapters/mongodb/query.rb b/lib/locomotive/steam/adapters/mongodb/query.rb +31 -11
@@ @@ -4,10 +4,11 @@ module Locomotive::Steam
class Query
+ attr_reader :criteria, :sort
+
def initialize(scope, localized_attributes, &block)
- @query = ::Origin::Query.new
- @scope = scope
- @localized_attributes = localized_attributes
+ @criteria, @sort = {}, nil
+ @scope, @localized_attributes = scope, localized_attributes
apply_default_scope
@@ @@ -15,25 +16,44 @@ module Locomotive::Steam
end
def where(criterion = nil)
- @query = @query.where(criterion)
- self
+ self.tap do
+ @criteria.merge!(criterion) unless criterion.nil?
+ end
end
def order_by(*args)
- @query = @query.order_by(*args)
- self
+ @sort = [*args]
end
- def selector
- @query.selector
+ def against(collection)
+ _query = to_origin
+ selector, sort = _query.selector, _query.options[:sort]
+
+ if sort
+ collection.find(selector).sort(sort)
+ else
+ collection.find(selector)
+ end
end
- def options
- @query.options
+ def to_origin
+ build_origin_query.where(@criteria).order_by(*@sort)
end
private
+ def build_origin_query
+ ::Origin::Query.new(build_aliases(@localized_attributes, @scope.locale))
+ end
+
+ def build_aliases(localized_attributes, locale)
+ localized_attributes.inject({}) do |aliases, name|
+ aliases.tap do
+ aliases[name.to_s] = "#{name}.#{locale}"
+ end
+ end
+ end
+
def apply_default_scope
where(site_id: @scope.site._id) if @scope.site
end
locomotive/steam/models/repository.rb b/lib/locomotive/steam/models/repository.rb +1 -16
@@ @@ -52,23 +52,8 @@ module Locomotive::Steam
@mapper ||= Mapper.new(name, options, self, &block)
end
- # def scope
- # @scope ||= Scope.new(current_site, current_locale)
- # end
-
- # def scope=(scope)
- # @scope = scope
- # @current_locale = scope.locale
- # @current_site = scope.site
- # end
-
- # def current_locale; scope.locale; end
- # def current_locale=(locale); scope.locale = locale; end
-
- # def current_site=(site); scope.site; end
- # @current_site = scope.site = site
- # end
+ # TODO: not sure about that. could it be used further in the dev
# def collection_name
# mapper.name
# end
spec/fixtures/default/app/views/pages/archives.liquid.haml +5 -0
@@ @@ -0,0 +1,5 @@
+ ---
+ title: Archives
+ listed: false
+ published: false
+ ---
spec/fixtures/default/app/views/pages/songs.liquid +5 -0
@@ @@ -0,0 +1,5 @@
+ ---
+ title: Songs
+ listed: false
+ published: false
+ ---
spec/fixtures/default/app/views/pages/tags.liquid +5 -0
@@ @@ -0,0 +1,5 @@
+ ---
+ title: Tags
+ listed: false
+ published: false
+ ---
connection_spec.rb b/spec/integration/mongodb/connection_spec.rb +0 -7
@@ @@ -1,7 +0,0 @@
- require File.join(File.dirname(__FILE__), 'mongodb_helper')
-
- describe 'Connection to MongoDB' do
-
- it { expect(true).to eq(true) }
-
- end
mongodb_helper.rb b/spec/integration/mongodb/mongodb_helper.rb +0 -5
@@ @@ -1,5 +0,0 @@
- require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
-
- RSpec.configure do |config|
- config.before(:all) { restore_mongodb }
- end
spec/integration/mongodb_helper.rb +5 -0
@@ @@ -0,0 +1,5 @@
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+ RSpec.configure do |config|
+ config.before(:all) { restore_mongodb }
+ end
spec/integration/repositories/page_repository_spec.rb +16 -16
@@ @@ -1,4 +1,4 @@
- require 'spec_helper'
+ require File.join(File.dirname(__FILE__), '..', 'mongodb_helper')
require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb'
require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb'
@@ @@ -21,15 +21,15 @@ describe Locomotive::Steam::PageRepository do
it { expect(subject.title[:en]).to eq 'Home page' }
end
- # describe '#by_handle' do
- # subject { repository.by_handle('our-music') }
- # it { expect(subject.title[:en]).to eq 'Music' }
- # end
+ describe '#by_handle' do
+ subject { repository.by_handle('our-music') }
+ it { expect(subject.title[:en]).to eq 'Music' }
+ end
- # describe '#by_fullpath' do
- # subject { repository.by_fullpath('archives/news') }
- # it { expect(subject.title[:en]).to eq 'News archive' }
- # end
+ describe '#by_fullpath' do
+ subject { repository.by_fullpath('archives/news') }
+ it { expect(subject.title[:en]).to eq 'News archive' }
+ end
end
@@ @@ -44,17 +44,17 @@ describe Locomotive::Steam::PageRepository do
end
- # context 'Filesystem' do
+ context 'Filesystem' do
- # it_should_behave_like 'page repository' do
+ it_should_behave_like 'page repository' do
- # let(:site_id) { 1 }
- # let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) }
+ let(:site_id) { 1 }
+ let(:adapter) { Locomotive::Steam::FilesystemAdapter.new(default_fixture_site_path) }
- # after(:all) { Locomotive::Steam::Adapters::Filesystem::SimpleCacheStore.new.clear }
+ after(:all) { Locomotive::Steam::Adapters::Filesystem::SimpleCacheStore.new.clear }
- # end
+ end
- # end
+ end
end
spec/integration/repositories/site_repository_spec.rb +1 -1
@@ @@ -1,4 +1,4 @@
- require 'spec_helper'
+ require File.join(File.dirname(__FILE__), '..', 'mongodb_helper')
require_relative '../../../lib/locomotive/steam/adapters/filesystem.rb'
require_relative '../../../lib/locomotive/steam/adapters/mongodb.rb'
spec/unit/adapters/filesystem/yaml_loaders/page_spec.rb +1 -1
@@ @@ -15,7 +15,7 @@ describe Locomotive::Steam::Adapters::Filesystem::YAMLLoaders::Page do
subject { loader.load(scope).sort { |a, b| a[:_fullpath] <=> b[:_fullpath] } }
it 'tests various stuff' do
- expect(subject.size).to eq 21
+ expect(subject.size).to eq 24
expect(subject.first[:title]).to eq({ en: 'Page not found' })
end
query_spec.rb b/spec/unit/adapters/mongodb/query_spec.rb +70 -0
@@ @@ -0,0 +1,70 @@
+ require 'spec_helper'
+
+ require 'origin'
+
+ require_relative '../../../../lib/locomotive/steam/adapters/mongodb/origin.rb'
+ require_relative '../../../../lib/locomotive/steam/adapters/mongodb/query.rb'
+
+ # require_relative '../../../../lib/locomotive/steam/adapters/memory/order.rb'
+ # require_relative '../../../../lib/locomotive/steam/adapters/memory/query.rb'
+
+ describe Locomotive::Steam::Adapters::MongoDB::Query do
+
+ let(:site) { instance_double('Site', _id: 42) }
+ let(:scope) { instance_double('Scope', locale: :en, site: site) }
+ let(:localized_attributes) { [:title] }
+ let(:block) { nil }
+
+ let(:query) { Locomotive::Steam::Adapters::MongoDB::Query.new(scope, localized_attributes, &block) }
+
+ describe '#where' do
+
+ let(:criterion) { { fullpath: 'index' } }
+
+ context 'simple call' do
+
+ before { query.where(criterion) }
+
+ it { expect(query.criteria).to eq({ site_id: 42, fullpath: 'index' }) }
+
+ end
+
+ context 'chained' do
+
+ let(:another_criterion) { { published: true } }
+
+ before { query.where(criterion).where(another_criterion) }
+
+ it { expect(query.criteria).to eq({ site_id: 42, fullpath: 'index', published: true }) }
+
+ end
+
+ end
+
+ describe '#order_by' do
+
+ it { expect(query.order_by(title: :asc, published: :desc).sort).to eq [{title: :asc, published: :desc}] }
+
+ end
+
+ describe 'chaining where and order_by' do
+
+ before { query.where(published: true).order_by(title: :asc) }
+
+ it { expect(query.criteria).to eq({ site_id: 42, published: true }) }
+ it { expect(query.sort).to eq([{ title: :asc }]) }
+
+ end
+
+ describe '#to_origin' do
+
+ before { query.where(title: 'index').order_by(title: :asc) }
+
+ subject { query.to_origin }
+
+ it { expect(subject.selector).to eq({ 'site_id' => 42, 'title.en' => 'index' }) }
+ it { expect(subject.options[:sort]).to eq({ 'title.en' => 1 }) }
+
+ end
+
+ end