engine/spec/lib/locomotive/export_spec.rb

130 lines
4.9 KiB
Ruby
Raw Normal View History

2011-07-06 23:34:11 +00:00
require 'spec_helper'
describe Locomotive::Export do
2011-07-08 21:06:07 +00:00
context '#content_type' do
before(:each) do
2011-08-25 21:28:56 +00:00
site = FactoryGirl.build('another site')
2011-11-26 05:22:48 +00:00
Locomotive::Site.stubs(:find).returns(site)
2011-07-08 21:06:07 +00:00
project_type = build_project_type(site)
project_type.entries.build(:title => 'Project #1', :description => 'Lorem ipsum', :active => true)
project_type.entries.build(:title => 'Project #2', :description => 'More Lorem ipsum', :active => false)
2011-07-08 21:06:07 +00:00
team_type = build_team_type(site, project_type)
team_type.entries.build(:name => 'Ben', :projects => project_type.entries, :current_project => project_type.entries.first)
team_type.entries.build(:name => 'Zach', :current_project => project_type.entries.last)
2011-07-08 21:06:07 +00:00
@project_data = ::Locomotive::Export.new(site).send(:extract_contents, project_type)
@team_data = ::Locomotive::Export.new(site).send(:extract_contents, team_type)
end
it 'includes the exact number of entries' do
2011-07-08 21:06:07 +00:00
@project_data.size.should == 2
@project_data.collect { |n| n.keys.first }.should == ['Project #1', 'Project #2']
end
it 'deals with real booleans' do
@project_data.first.values.first['active'].should === true
@project_data.last.values.first['active'].should === false
2011-07-08 21:06:07 +00:00
end
it 'stores the list of highlighted values in a has_many relationship' do
@team_data.first.values.first['projects'].size.should == 2
@team_data.first.values.first['projects'].should == ['Project #1', 'Project #2']
@team_data.last.values.first['projects'].should be_nil
2011-07-08 21:06:07 +00:00
end
it 'stores a highlighted value in a has_one relationship' do
@team_data.collect { |n| n.values.first['current_project'] }.should == ['Project #1', 'Project #2']
end
def build_project_type(site)
2011-08-25 21:28:56 +00:00
FactoryGirl.build(:content_type, :site => site, :highlighted_field_name => 'custom_field_1').tap do |content_type|
content_type.entries_custom_fields.build :label => 'Title', :name => 'title', :type => 'string'
content_type.entries_custom_fields.build :label => 'My Description', :name => 'description', :type => 'text'
content_type.entries_custom_fields.build :label => 'Active', :type => 'boolean'
2011-07-08 21:06:07 +00:00
end
end
def build_team_type(site, project_type)
Object.send(:remove_const, 'TestProject') rescue nil
klass = Object.const_set('TestProject', Class.new { def self.embedded?; false; end })
2011-08-25 21:28:56 +00:00
content_type = FactoryGirl.build(:content_type, :site => site, :name => 'team', :highlighted_field_name => 'custom_field_1')
content_type.entries_custom_fields.build :label => 'Name', :name => 'name', :type => 'string'
content_type.entries_custom_fields.build :label => 'Projects', :type => 'has_many', :name => 'projects', :target => 'TestProject'
content_type.entries_custom_fields.build :label => 'Bio', :name => 'bio', :type => 'text'
content_type.entries_custom_fields.build :label => 'Current Project', :type => 'has_one', :name => 'current_project', :target => 'TestProject'
2011-07-08 21:06:07 +00:00
content_type
end
end
context '#zipfile' do
2011-07-06 23:34:11 +00:00
before(:all) do
2011-08-25 21:28:56 +00:00
@site = FactoryGirl.create('another site')
2011-07-06 23:34:11 +00:00
# first import a brand new site
self.import_it
# then export it of course
@zip_file = self.export_it
end
it 'generates a zipfile' do
@zip_file.should_not be_nil
File.exists?(@zip_file).should be_true
end
it 'has the exact number of pages from the original site' do
self.unzip
2011-07-14 13:18:19 +00:00
Dir[File.join(self.zip_folder, 'app', 'views', 'pages', '**/*.liquid')].size.should == 11
2011-07-06 23:34:11 +00:00
end
2011-07-07 14:59:50 +00:00
it 'includes snippets' do
self.unzip
Dir[File.join(self.zip_folder, 'app', 'views', 'snippets', '*.liquid')].size.should == 1
end
it 'has yaml files describing the content types as well as their data' do
self.unzip
Dir[File.join(self.zip_folder, 'app', 'content_types', '*.yml')].size.should == 4
Dir[File.join(self.zip_folder, 'data', '*.yml')].size.should == 4
end
2011-07-06 23:34:11 +00:00
def import_it
job = Locomotive::Import::Job.new(FixturedTheme.duplicate_and_open('default.zip'), @site, { :samples => true, :reset => true })
job.perform
job.success nil
end
def export_it
::Locomotive::Export.run!(@site, @site.name.parameterize)
end
def zip_folder
2011-07-07 14:59:50 +00:00
File.join(File.dirname(@zip_file), 'locomotive-test-website-2')
2011-07-06 23:34:11 +00:00
end
def unzip
return if @zip_file || File.exists?(self.zip_folder)
Zip::ZipFile.open(@zip_file) do |zipfile|
destination_path = File.dirname(@zip_file)
zipfile.each do |entry|
FileUtils.mkdir_p(File.dirname(File.join(destination_path, entry.name)))
zipfile.extract(entry, File.join(destination_path, entry.name))
end
end
end
after(:all) do
2011-07-07 14:59:50 +00:00
FileUtils.rm_rf(self.zip_folder) if File.exists?(self.zip_folder)
2011-11-26 05:22:48 +00:00
Locomotive::Site.destroy_all
2011-07-06 23:34:11 +00:00
end
end
2011-11-26 05:22:48 +00:00
end