fix specs for the content_type and editable_element models

This commit is contained in:
Didier Lafforgue 2012-02-16 16:17:30 +01:00
parent 2fd0410b66
commit 73b559260e
4 changed files with 79 additions and 87 deletions

View File

@ -67,7 +67,8 @@ module Locomotive
:slug => slug,
:title => ::I18n.t("attributes.defaults.pages.#{slug}.title"),
:raw_template => ::I18n.t("attributes.defaults.pages.#{slug}.body"),
:published => true
:published => true,
:depth => 0
})
end
end

View File

@ -10,7 +10,7 @@ describe Locomotive::ContentType do
it 'should have a valid factory' do
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
end
@ -32,7 +32,7 @@ describe Locomotive::ContentType do
it 'is not valid if slug is not unique' do
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 = FactoryGirl.build(:content_type, :site => content_type.site)).should_not be_valid
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
content_type = FactoryGirl.build(:content_type)
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
%w(created_at updated_at).each do |name|
it "does not allow #{name} as name" do
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.errors[:name].should == ['is reserved']
end
@ -58,50 +58,46 @@ describe Locomotive::ContentType do
context '#ordered_entries' do
before(:each) do
@content_type = FactoryGirl.build(:content_type, :order_by => 'created_at')
@content_1 = stub('content_1', :name => 'Did', :_position_in_list => 2)
@content_2 = stub('content_2', :name => 'Sacha', :_position_in_list => 1)
@content_type.stubs(:entries).returns([@content_1, @content_2])
(@content_type = build_content_type(:order_by => 'created_at')).save
@content_2 = @content_type.entries.create :name => 'Sacha'
@content_1 = @content_type.entries.create :name => 'Did'
end
it 'orders with the ASC direction by default' do
@content_type.asc_order?.should == true
@content_type.order_direction.should == 'asc'
end
it 'has a getter for manual order' do
@content_type.order_manually?.should == false
@content_type.order_by = '_position_in_list'
@content_type.order_by = '_position'
@content_type.order_manually?.should == true
end
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)
end
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)
end
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.ordered_entries.collect(&:name).should == %w(Sacha Did)
end
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.content_custom_fields.build :label => 'Active at', :name => 'active_at', :type => 'Date'
e = Date.parse('01/01/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.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_by = @content_type.entries_custom_fields.where(:name => 'active_at').first._id
@content_2.update_attribute :active_at, Date.parse('01/01/2001')
content_3 = @content_type.entries.create :name => 'Mario', :active_at => Date.parse('02/02/2001')
@content_type.ordered_entries.map(&:active_at).should == [nil, Date.parse('01/01/2001'), Date.parse('02/02/2001')]
@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
@ -111,42 +107,24 @@ describe Locomotive::ContentType do
before(:each) do
site = FactoryGirl.build(: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.entries_custom_fields.build :label => 'Active', :type => 'boolean'
@content_type = build_content_type(:site => site)
# Locomotive::ContentType.logger = Logger.new($stdout)
# Locomotive::ContentType.db.connection.instance_variable_set(:@logger, Logger.new($stdout))
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
%w{label type}.each do |key|
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.errors[key.to_sym].should == ["can't be blank"]
end
end
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.errors[:label].should == ["is already taken"]
end
@ -162,25 +140,25 @@ describe Locomotive::ContentType do
context 'define core attributes' do
it 'should have an unique name' do
@content_type.entries_custom_fields.first._name.should == "custom_field_1"
@content_type.entries_custom_fields.last._name.should == "custom_field_2"
end
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"
@content_type.valid?
@content_type.entries_custom_fields.first.name.should == 'name'
@content_type.entries_custom_fields.last.name.should == 'active_at'
end
end
context 'build and save' do
before(:each) do
@content_type.save
end
it 'should build asset' do
asset = @content_type.entries.build
lambda {
asset.name
asset.description
asset.active
asset.custom_fields.size.should == 2
}.should_not raise_error
end
@ -198,11 +176,11 @@ describe Locomotive::ContentType do
asset.active.should == true
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.save and @content_type.reload
new_collection = Locomotive::ContentType.new
lambda { new_collection.entries.build.description }.should raise_error
another_content_type = Locomotive::ContentType.new
lambda { another_content_type.entries.build.description }.should raise_error
end
end
@ -210,35 +188,28 @@ describe Locomotive::ContentType do
context 'modifying fields' do
before(:each) do
@content_type.save
@asset = build_content_entry(@content_type).save
end
it 'should add new field' do
@content_type.entries_custom_fields.build :label => 'Active at', :name => 'active_at', :type => 'Date'
@content_type.upsert(:validate => false)
@content_type.invalidate_content_klass
@content_type.reload
it 'adds new field' do
@content_type.entries_custom_fields.build :label => 'Author', :name => 'author', :type => 'string'
@content_type.save && @content_type.reload
asset = @content_type.entries.first
lambda { asset.active_at }.should_not raise_error
lambda { asset.author }.should_not raise_error
end
it 'should remove field' do
@content_type.entries_custom_fields.clear
@content_type.upsert(:validate => false)
@content_type.invalidate_content_klass
@content_type.reload
it 'removes a field' do
@content_type.entries_custom_fields.destroy_all :conditions => { :name => 'active_at' }
@content_type.save && @content_type.reload
asset = @content_type.entries.first
lambda { asset.active_at }.should raise_error
end
it 'should rename field label' do
@content_type.entries_custom_fields.first.label = 'Simple description'
@content_type.entries_custom_fields.first.name = nil
@content_type.upsert(:validate => false)
@content_type.invalidate_content_klass
@content_type.reload
it 'renames field label' do
@content_type.entries_custom_fields[1].label = 'Simple description'
@content_type.entries_custom_fields[1].name = nil
@content_type.save && @content_type.reload
asset = @content_type.entries.first
asset.simple_description.should == 'Lorem ipsum'
end
@ -250,30 +221,45 @@ describe Locomotive::ContentType do
it 'adds new field' do
@content_type.entries_custom_fields.clear
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.first.label.should == 'A title'
@content_type.entries_custom_fields.last.label.should == 'Tagline'
end
it 'updates/removes fields' do
field = @content_type.entries_custom_fields.build :label => 'Title', :type => 'String'
@content_type.save; @content_type = Locomotive::ContentType.find(@content_type.id)
@content_type.save
field = @content_type.entries_custom_fields.build :label => 'Title', :type => 'string'
@content_type.save
@content_type.update_attributes(:entries_custom_fields_attributes => {
'0' => { 'id' => lookup_field_id(0), 'label' => 'My Description', 'type' => 'Text', '_destroy' => '1' },
'1' => { 'id' => lookup_field_id(1), 'label' => 'Active', 'type' => 'Boolean', '_destroy' => '1' },
'2' => { 'id' => lookup_field_id(2), 'label' => 'My Title !', 'type' => 'String' },
'new_record' => { 'label' => 'Published at', 'type' => 'String' }
'0' => { '_id' => lookup_field_id(1), 'label' => 'My Description', 'type' => 'text', '_destroy' => '1' },
'1' => { '_id' => lookup_field_id(2), 'label' => 'Active', 'type' => 'boolean', '_destroy' => '1' },
'2' => { '_id' => field._id, 'label' => 'My Title !' },
'3' => { 'label' => 'Published at', 'type' => 'string' }
})
@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
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)
content_type.entries.build(:name => 'Asset on steroids', :description => 'Lorem ipsum', :active => true)
end

View File

@ -5,6 +5,7 @@ describe Locomotive::EditableElement do
before(:each) do
@site = FactoryGirl.create(:site)
@home = @site.pages.root.first
@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' %}")

View File

@ -1,9 +1,10 @@
ENV["RAILS_ENV"] ||= 'test'
ENV['RAILS_ENV'] ||= 'test'
require 'rails/mongoid'
require File.join(File.dirname(__FILE__), 'dummy', 'config', 'environment.rb')
require 'rspec/rails'
require 'factory_girl'
require 'database_cleaner'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
@ -25,7 +26,6 @@ RSpec.configure do |config|
Locomotive.config.heroku = false
end
require 'database_cleaner'
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = 'mongoid'
@ -34,4 +34,8 @@ RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.clean
end
config.after(:suite) do
DatabaseCleaner.clean
end
end