require 'spec_helper' require 'projectdx_pipeline/database_configuration_template' describe ProjectdxPipeline::DatabaseConfigurationTemplate do describe '#configuration' do it 'returns an ActiveRecord configuration for each of the specified environments' do template = ProjectdxPipeline::DatabaseConfigurationTemplate.new template.database_prefix = 'awesomeapp' template.client_id = 'thismachineshostname' template.environments = %w(development test) template.config_data['adapter'] = 'postgresql' template.config_data['encoding'] = 'unicode' template.config_data['pool'] = 5 template.config_data['min_messages'] = 'warning' template.config_data['host'] = 'database.example.com' template.config_data['username'] = 'chuckles' template.config_data['password'] = 'stinkyface' expected_data = { 'development' => { 'adapter' => 'postgresql', 'encoding' => 'unicode', 'pool' => 5, 'min_messages' => 'warning', 'host' => 'database.example.com', 'username' => 'chuckles', 'password' => 'stinkyface', 'database' => 'awesomeapp-development-thismachineshostname' }, 'test' => { 'adapter' => 'postgresql', 'encoding' => 'unicode', 'pool' => 5, 'min_messages' => 'warning', 'host' => 'database.example.com', 'username' => 'chuckles', 'password' => 'stinkyface', 'database' => 'awesomeapp-test-thismachineshostname' } } template.configuration.should == expected_data end end describe '#database_name' do it 'returns the database name as {database_prefix}-{environment}-{client_hostname}' do template = ProjectdxPipeline::DatabaseConfigurationTemplate.new template.database_prefix = 'awesomeapp' template.client_id = 'some_client' template.database_name('terrarium').should == 'awesomeapp-terrarium-some_client' end end describe '.new' do it 'sets attributes from specified YAML file' do data = { 'database_prefix' => 'awesomeapp', 'adapter' => 'postgresql', 'encoding' => 'unicode', 'pool' => 5, 'min_messages' => 'warning', 'host' => 'database.example.com', 'username' => 'chuckles', 'password' => 'stinkyface', 'environments' => %w(development test) } ProjectdxPipeline::DatabaseConfigurationTemplate.should_receive(:read_template) \ .with('/my/template/file.yml') \ .and_return(data) template = ProjectdxPipeline::DatabaseConfigurationTemplate.new('/my/template/file.yml') template.database_prefix.should == 'awesomeapp' template.environments.should == %w(development test) template.config_data['adapter'].should == 'postgresql' template.config_data['encoding'].should == 'unicode' template.config_data['pool'].should == 5 template.config_data['min_messages'].should == 'warning' template.config_data['host'].should == 'database.example.com' template.config_data['username'].should == 'chuckles' template.config_data['password'].should == 'stinkyface' end end describe '#client_id' do it 'defaults to the hostname of the machine it is running on' do Socket.stub!(:gethostname => 'foo.bar') template = ProjectdxPipeline::DatabaseConfigurationTemplate.new template.client_id.should == 'foo_bar' end end describe '#render_to_file' do it 'creates a YAML dump of the configuration at the specified path' do file_io = stub File.should_receive(:open).with('/destination/file.yml', 'w').and_yield(file_io) YAML.should_receive(:dump).with('some data', file_io) template = ProjectdxPipeline::DatabaseConfigurationTemplate.new template.stub!(:configuration => 'some data') template.render_to_file('/destination/file.yml') end end end