fix specs for the content_type and editable_element models
This commit is contained in:
parent
2fd0410b66
commit
73b559260e
@ -67,7 +67,8 @@ module Locomotive
|
|||||||
:slug => slug,
|
:slug => slug,
|
||||||
:title => ::I18n.t("attributes.defaults.pages.#{slug}.title"),
|
:title => ::I18n.t("attributes.defaults.pages.#{slug}.title"),
|
||||||
:raw_template => ::I18n.t("attributes.defaults.pages.#{slug}.body"),
|
:raw_template => ::I18n.t("attributes.defaults.pages.#{slug}.body"),
|
||||||
:published => true
|
:published => true,
|
||||||
|
:depth => 0
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -10,7 +10,7 @@ describe Locomotive::ContentType do
|
|||||||
|
|
||||||
it 'should have a valid factory' do
|
it 'should have a valid factory' do
|
||||||
content_type = FactoryGirl.build(:content_type)
|
content_type = FactoryGirl.build(:content_type)
|
||||||
content_type.entries_custom_fields.build :label => 'anything', :type => 'String'
|
content_type.entries_custom_fields.build :label => 'anything', :type => 'string'
|
||||||
content_type.should be_valid
|
content_type.should be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ describe Locomotive::ContentType do
|
|||||||
|
|
||||||
it 'is not valid if slug is not unique' do
|
it 'is not valid if slug is not unique' do
|
||||||
content_type = FactoryGirl.build(:content_type)
|
content_type = FactoryGirl.build(:content_type)
|
||||||
content_type.entries_custom_fields.build :label => 'anything', :type => 'String'
|
content_type.entries_custom_fields.build :label => 'anything', :type => 'string'
|
||||||
content_type.save
|
content_type.save
|
||||||
(content_type = FactoryGirl.build(:content_type, :site => content_type.site)).should_not be_valid
|
(content_type = FactoryGirl.build(:content_type, :site => content_type.site)).should_not be_valid
|
||||||
content_type.errors[:slug].should == ["is already taken"]
|
content_type.errors[:slug].should == ["is already taken"]
|
||||||
@ -41,13 +41,13 @@ describe Locomotive::ContentType do
|
|||||||
it 'is not valid if there is not at least one field' do
|
it 'is not valid if there is not at least one field' do
|
||||||
content_type = FactoryGirl.build(:content_type)
|
content_type = FactoryGirl.build(:content_type)
|
||||||
content_type.should_not be_valid
|
content_type.should_not be_valid
|
||||||
content_type.errors[:entries_custom_fields].should == ["is too small (minimum element number is 1)"]
|
content_type.errors[:entries_custom_fields].should == { :base => ['At least, one custom field is required'] }
|
||||||
end
|
end
|
||||||
|
|
||||||
%w(created_at updated_at).each do |name|
|
%w(created_at updated_at).each do |name|
|
||||||
it "does not allow #{name} as name" do
|
it "does not allow #{name} as name" do
|
||||||
content_type = FactoryGirl.build(:content_type)
|
content_type = FactoryGirl.build(:content_type)
|
||||||
field = content_type.entries_custom_fields.build :label => 'anything', :type => 'String', :name => name
|
field = content_type.entries_custom_fields.build :label => 'anything', :type => 'string', :name => name
|
||||||
field.valid?.should be_false
|
field.valid?.should be_false
|
||||||
field.errors[:name].should == ['is reserved']
|
field.errors[:name].should == ['is reserved']
|
||||||
end
|
end
|
||||||
@ -58,50 +58,46 @@ describe Locomotive::ContentType do
|
|||||||
context '#ordered_entries' do
|
context '#ordered_entries' do
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
@content_type = FactoryGirl.build(:content_type, :order_by => 'created_at')
|
(@content_type = build_content_type(:order_by => 'created_at')).save
|
||||||
@content_1 = stub('content_1', :name => 'Did', :_position_in_list => 2)
|
@content_2 = @content_type.entries.create :name => 'Sacha'
|
||||||
@content_2 = stub('content_2', :name => 'Sacha', :_position_in_list => 1)
|
@content_1 = @content_type.entries.create :name => 'Did'
|
||||||
@content_type.stubs(:entries).returns([@content_1, @content_2])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'orders with the ASC direction by default' do
|
it 'orders with the ASC direction by default' do
|
||||||
@content_type.asc_order?.should == true
|
@content_type.order_direction.should == 'asc'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'has a getter for manual order' do
|
it 'has a getter for manual order' do
|
||||||
@content_type.order_manually?.should == false
|
@content_type.order_manually?.should == false
|
||||||
@content_type.order_by = '_position_in_list'
|
@content_type.order_by = '_position'
|
||||||
@content_type.order_manually?.should == true
|
@content_type.order_manually?.should == true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a list of entries ordered manually' do
|
it 'returns a list of entries ordered manually' do
|
||||||
@content_type.order_by = '_position_in_list'
|
@content_type.order_by = '_position'
|
||||||
@content_type.ordered_entries.collect(&:name).should == %w(Sacha Did)
|
@content_type.ordered_entries.collect(&:name).should == %w(Sacha Did)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a list of entries ordered by a column specified by order_by (ASC)' do
|
it 'returns a list of entries ordered by a column specified by order_by (ASC)' do
|
||||||
@content_type.order_by = 'name'
|
@content_type.order_by = @content_type.entries_custom_fields.where(:name => 'name').first._id
|
||||||
@content_type.ordered_entries.collect(&:name).should == %w(Did Sacha)
|
@content_type.ordered_entries.collect(&:name).should == %w(Did Sacha)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a list of entries ordered by a column specified by order_by (DESC)' do
|
it 'returns a list of entries ordered by a column specified by order_by (DESC)' do
|
||||||
@content_type.order_by = 'name'
|
@content_type.order_by = @content_type.entries_custom_fields.where(:name => 'name').first._id
|
||||||
@content_type.order_direction = 'desc'
|
@content_type.order_direction = 'desc'
|
||||||
@content_type.ordered_entries.collect(&:name).should == %w(Sacha Did)
|
@content_type.ordered_entries.collect(&:name).should == %w(Sacha Did)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a list of entries ordered by a Date column when first instance is missing the value' do
|
it 'returns a list of entries ordered by a Date column when first instance is missing the value' do
|
||||||
@content_type = FactoryGirl.build(:content_type, :order_by => 'created_at')
|
@content_type.order_by = @content_type.entries_custom_fields.where(:name => 'active_at').first._id
|
||||||
@content_type.content_custom_fields.build :label => 'Active at', :name => 'active_at', :type => 'Date'
|
@content_2.update_attribute :active_at, Date.parse('01/01/2001')
|
||||||
e = Date.parse('01/01/2001')
|
content_3 = @content_type.entries.create :name => 'Mario', :active_at => Date.parse('02/02/2001')
|
||||||
l = Date.parse('02/02/2002')
|
|
||||||
[nil,e,l].each { |d| @content_type.entries << @content_type.content_klass.new(:active_at => d) }
|
@content_type.ordered_entries.map(&:active_at).should == [nil, Date.parse('01/01/2001'), Date.parse('02/02/2001')]
|
||||||
@content_type.order_by = 'active_at'
|
|
||||||
@content_type.order_direction = 'asc'
|
|
||||||
lambda { @content_type.ordered_entries }.should_not raise_error(ArgumentError)
|
|
||||||
@content_type.ordered_entries.map(&:active_at).should == [nil,e,l]
|
|
||||||
@content_type.order_direction = 'desc'
|
@content_type.order_direction = 'desc'
|
||||||
@content_type.ordered_entries.map(&:active_at).should == [l,e,nil]
|
@content_type.ordered_entries.map(&:active_at).should == [Date.parse('02/02/2001'), Date.parse('01/01/2001'), nil]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@ -111,42 +107,24 @@ describe Locomotive::ContentType do
|
|||||||
before(:each) do
|
before(:each) do
|
||||||
site = FactoryGirl.build(:site)
|
site = FactoryGirl.build(:site)
|
||||||
Locomotive::Site.stubs(:find).returns(site)
|
Locomotive::Site.stubs(:find).returns(site)
|
||||||
@content_type = FactoryGirl.build(:content_type, :site => site, :highlighted_field_name => 'custom_field_1')
|
|
||||||
@content_type.entries_custom_fields.build :label => 'My Description', :name => 'description', :type => 'text'
|
@content_type = build_content_type(:site => site)
|
||||||
@content_type.entries_custom_fields.build :label => 'Active', :type => 'boolean'
|
|
||||||
# Locomotive::ContentType.logger = Logger.new($stdout)
|
# Locomotive::ContentType.logger = Logger.new($stdout)
|
||||||
# Locomotive::ContentType.db.connection.instance_variable_set(:@logger, Logger.new($stdout))
|
# Locomotive::ContentType.db.connection.instance_variable_set(:@logger, Logger.new($stdout))
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'unit' do
|
|
||||||
|
|
||||||
before(:each) do
|
|
||||||
@field = CustomFields::Field.new(:type => 'String')
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should tell if it is a String' do
|
|
||||||
@field.string?.should be_true
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should tell if it is a Text' do
|
|
||||||
@field.type = 'Text'
|
|
||||||
@field.text?.should be_true
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'validation' do
|
context 'validation' do
|
||||||
|
|
||||||
%w{label type}.each do |key|
|
%w{label type}.each do |key|
|
||||||
it "should validate presence of #{key}" do
|
it "should validate presence of #{key}" do
|
||||||
field = @content_type.entries_custom_fields.build({ :label => 'Shortcut', :type => 'String' }.merge(key.to_sym => nil))
|
field = @content_type.entries_custom_fields.build({ :label => 'Shortcut', :type => 'string' }.merge(key.to_sym => nil))
|
||||||
field.should_not be_valid
|
field.should_not be_valid
|
||||||
field.errors[key.to_sym].should == ["can't be blank"]
|
field.errors[key.to_sym].should == ["can't be blank"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not have unique label' do
|
it 'should not have unique label' do
|
||||||
field = @content_type.entries_custom_fields.build :label => 'Active', :type => 'Boolean'
|
field = @content_type.entries_custom_fields.build :label => 'Active', :type => 'boolean'
|
||||||
field.should_not be_valid
|
field.should_not be_valid
|
||||||
field.errors[:label].should == ["is already taken"]
|
field.errors[:label].should == ["is already taken"]
|
||||||
end
|
end
|
||||||
@ -162,25 +140,25 @@ describe Locomotive::ContentType do
|
|||||||
context 'define core attributes' do
|
context 'define core attributes' do
|
||||||
|
|
||||||
it 'should have an unique name' do
|
it 'should have an unique name' do
|
||||||
@content_type.entries_custom_fields.first._name.should == "custom_field_1"
|
@content_type.valid?
|
||||||
@content_type.entries_custom_fields.last._name.should == "custom_field_2"
|
@content_type.entries_custom_fields.first.name.should == 'name'
|
||||||
end
|
@content_type.entries_custom_fields.last.name.should == 'active_at'
|
||||||
|
|
||||||
it 'should have an unique alias' do
|
|
||||||
@content_type.entries_custom_fields.first.name.should == "description"
|
|
||||||
@content_type.entries_custom_fields.last.name.should == "active"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'build and save' do
|
context 'build and save' do
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
@content_type.save
|
||||||
|
end
|
||||||
|
|
||||||
it 'should build asset' do
|
it 'should build asset' do
|
||||||
asset = @content_type.entries.build
|
asset = @content_type.entries.build
|
||||||
lambda {
|
lambda {
|
||||||
|
asset.name
|
||||||
asset.description
|
asset.description
|
||||||
asset.active
|
asset.active
|
||||||
asset.custom_fields.size.should == 2
|
|
||||||
}.should_not raise_error
|
}.should_not raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -198,11 +176,11 @@ describe Locomotive::ContentType do
|
|||||||
asset.active.should == true
|
asset.active.should == true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not modify entries from another collection' do
|
it 'should not modify entries from another content type' do
|
||||||
asset = build_content_entry(@content_type)
|
asset = build_content_entry(@content_type)
|
||||||
asset.save and @content_type.reload
|
asset.save and @content_type.reload
|
||||||
new_collection = Locomotive::ContentType.new
|
another_content_type = Locomotive::ContentType.new
|
||||||
lambda { new_collection.entries.build.description }.should raise_error
|
lambda { another_content_type.entries.build.description }.should raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@ -210,35 +188,28 @@ describe Locomotive::ContentType do
|
|||||||
context 'modifying fields' do
|
context 'modifying fields' do
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
|
@content_type.save
|
||||||
@asset = build_content_entry(@content_type).save
|
@asset = build_content_entry(@content_type).save
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should add new field' do
|
it 'adds new field' do
|
||||||
@content_type.entries_custom_fields.build :label => 'Active at', :name => 'active_at', :type => 'Date'
|
@content_type.entries_custom_fields.build :label => 'Author', :name => 'author', :type => 'string'
|
||||||
@content_type.upsert(:validate => false)
|
@content_type.save && @content_type.reload
|
||||||
@content_type.invalidate_content_klass
|
|
||||||
@content_type.reload
|
|
||||||
asset = @content_type.entries.first
|
asset = @content_type.entries.first
|
||||||
lambda { asset.active_at }.should_not raise_error
|
lambda { asset.author }.should_not raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should remove field' do
|
it 'removes a field' do
|
||||||
@content_type.entries_custom_fields.clear
|
@content_type.entries_custom_fields.destroy_all :conditions => { :name => 'active_at' }
|
||||||
@content_type.upsert(:validate => false)
|
@content_type.save && @content_type.reload
|
||||||
@content_type.invalidate_content_klass
|
|
||||||
@content_type.reload
|
|
||||||
asset = @content_type.entries.first
|
asset = @content_type.entries.first
|
||||||
lambda { asset.active_at }.should raise_error
|
lambda { asset.active_at }.should raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should rename field label' do
|
it 'renames field label' do
|
||||||
@content_type.entries_custom_fields.first.label = 'Simple description'
|
@content_type.entries_custom_fields[1].label = 'Simple description'
|
||||||
@content_type.entries_custom_fields.first.name = nil
|
@content_type.entries_custom_fields[1].name = nil
|
||||||
@content_type.upsert(:validate => false)
|
@content_type.save && @content_type.reload
|
||||||
|
|
||||||
@content_type.invalidate_content_klass
|
|
||||||
@content_type.reload
|
|
||||||
|
|
||||||
asset = @content_type.entries.first
|
asset = @content_type.entries.first
|
||||||
asset.simple_description.should == 'Lorem ipsum'
|
asset.simple_description.should == 'Lorem ipsum'
|
||||||
end
|
end
|
||||||
@ -250,30 +221,45 @@ describe Locomotive::ContentType do
|
|||||||
it 'adds new field' do
|
it 'adds new field' do
|
||||||
@content_type.entries_custom_fields.clear
|
@content_type.entries_custom_fields.clear
|
||||||
field = @content_type.entries_custom_fields.build :label => 'Title'
|
field = @content_type.entries_custom_fields.build :label => 'Title'
|
||||||
@content_type.entries_custom_fields_attributes = { 0 => { :id => field.id.to_s, 'label' => 'A title', 'type' => 'String' }, 1 => { 'label' => 'Tagline', 'type' => 'String' } }
|
@content_type.entries_custom_fields_attributes = { 0 => { :id => field.id.to_s, 'label' => 'A title', 'type' => 'string' }, 1 => { 'label' => 'Tagline', 'type' => 'sring' } }
|
||||||
@content_type.entries_custom_fields.size.should == 2
|
@content_type.entries_custom_fields.size.should == 2
|
||||||
@content_type.entries_custom_fields.first.label.should == 'A title'
|
@content_type.entries_custom_fields.first.label.should == 'A title'
|
||||||
@content_type.entries_custom_fields.last.label.should == 'Tagline'
|
@content_type.entries_custom_fields.last.label.should == 'Tagline'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'updates/removes fields' do
|
it 'updates/removes fields' do
|
||||||
field = @content_type.entries_custom_fields.build :label => 'Title', :type => 'String'
|
@content_type.save
|
||||||
@content_type.save; @content_type = Locomotive::ContentType.find(@content_type.id)
|
|
||||||
|
field = @content_type.entries_custom_fields.build :label => 'Title', :type => 'string'
|
||||||
|
@content_type.save
|
||||||
|
|
||||||
@content_type.update_attributes(:entries_custom_fields_attributes => {
|
@content_type.update_attributes(:entries_custom_fields_attributes => {
|
||||||
'0' => { 'id' => lookup_field_id(0), 'label' => 'My Description', 'type' => 'Text', '_destroy' => '1' },
|
'0' => { '_id' => lookup_field_id(1), 'label' => 'My Description', 'type' => 'text', '_destroy' => '1' },
|
||||||
'1' => { 'id' => lookup_field_id(1), 'label' => 'Active', 'type' => 'Boolean', '_destroy' => '1' },
|
'1' => { '_id' => lookup_field_id(2), 'label' => 'Active', 'type' => 'boolean', '_destroy' => '1' },
|
||||||
'2' => { 'id' => lookup_field_id(2), 'label' => 'My Title !', 'type' => 'String' },
|
'2' => { '_id' => field._id, 'label' => 'My Title !' },
|
||||||
'new_record' => { 'label' => 'Published at', 'type' => 'String' }
|
'3' => { 'label' => 'Published at', 'type' => 'string' }
|
||||||
})
|
})
|
||||||
|
|
||||||
@content_type = Locomotive::ContentType.find(@content_type.id)
|
@content_type = Locomotive::ContentType.find(@content_type.id)
|
||||||
@content_type.entries_custom_fields.size.should == 2
|
|
||||||
@content_type.entries_custom_fields.first.label.should == 'My Title !'
|
@content_type.entries_custom_fields.size.should == 4
|
||||||
|
@content_type.entries_custom_fields.map(&:name).should == %w(name active_at title published_at)
|
||||||
|
@content_type.entries_custom_fields[2].label.should == 'My Title !'
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def build_content_type(options = {})
|
||||||
|
FactoryGirl.build(:content_type, options).tap do |content_type|
|
||||||
|
content_type.entries_custom_fields.build :label => 'Name', :type => 'string'
|
||||||
|
content_type.entries_custom_fields.build :label => 'Description', :type => 'text'
|
||||||
|
content_type.entries_custom_fields.build :label => 'Active', :type => 'boolean'
|
||||||
|
content_type.entries_custom_fields.build :label => 'Active at', :type => 'date'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def build_content_entry(content_type)
|
def build_content_entry(content_type)
|
||||||
content_type.entries.build(:name => 'Asset on steroids', :description => 'Lorem ipsum', :active => true)
|
content_type.entries.build(:name => 'Asset on steroids', :description => 'Lorem ipsum', :active => true)
|
||||||
end
|
end
|
||||||
|
@ -5,6 +5,7 @@ describe Locomotive::EditableElement do
|
|||||||
before(:each) do
|
before(:each) do
|
||||||
@site = FactoryGirl.create(:site)
|
@site = FactoryGirl.create(:site)
|
||||||
@home = @site.pages.root.first
|
@home = @site.pages.root.first
|
||||||
|
|
||||||
@home.update_attributes :raw_template => "{% block body %}{% editable_short_text 'body' %}Lorem ipsum{% endeditable_short_text %}{% endblock %}"
|
@home.update_attributes :raw_template => "{% block body %}{% editable_short_text 'body' %}Lorem ipsum{% endeditable_short_text %}{% endblock %}"
|
||||||
|
|
||||||
@sub_page_1 = FactoryGirl.create(:page, :slug => 'sub_page_1', :parent => @home, :raw_template => "{% extends 'parent' %}")
|
@sub_page_1 = FactoryGirl.create(:page, :slug => 'sub_page_1', :parent => @home, :raw_template => "{% extends 'parent' %}")
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
ENV["RAILS_ENV"] ||= 'test'
|
ENV['RAILS_ENV'] ||= 'test'
|
||||||
|
|
||||||
require 'rails/mongoid'
|
require 'rails/mongoid'
|
||||||
require File.join(File.dirname(__FILE__), 'dummy', 'config', 'environment.rb')
|
require File.join(File.dirname(__FILE__), 'dummy', 'config', 'environment.rb')
|
||||||
require 'rspec/rails'
|
require 'rspec/rails'
|
||||||
require 'factory_girl'
|
require 'factory_girl'
|
||||||
|
require 'database_cleaner'
|
||||||
|
|
||||||
# Requires supporting ruby files with custom matchers and macros, etc,
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
||||||
# in spec/support/ and its subdirectories.
|
# in spec/support/ and its subdirectories.
|
||||||
@ -25,7 +26,6 @@ RSpec.configure do |config|
|
|||||||
Locomotive.config.heroku = false
|
Locomotive.config.heroku = false
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'database_cleaner'
|
|
||||||
config.before(:suite) do
|
config.before(:suite) do
|
||||||
DatabaseCleaner.strategy = :truncation
|
DatabaseCleaner.strategy = :truncation
|
||||||
DatabaseCleaner.orm = 'mongoid'
|
DatabaseCleaner.orm = 'mongoid'
|
||||||
@ -34,4 +34,8 @@ RSpec.configure do |config|
|
|||||||
config.before(:each) do
|
config.before(:each) do
|
||||||
DatabaseCleaner.clean
|
DatabaseCleaner.clean
|
||||||
end
|
end
|
||||||
|
|
||||||
|
config.after(:suite) do
|
||||||
|
DatabaseCleaner.clean
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user