require 'spec_helper' describe Stackster do it "should create a new entry object" do @simple_db_mock = mock 'simple db' config = Stackster::Config.new(:config => { 'access_key' => 'the-key', 'secret_key' => 'secret', 'region' => 'us-west-1' }) Stackster::AWS::SimpleDB.should_receive(:new).and_return @simple_db_mock @simple_db_mock.should_receive(:create_domain). with("stacks"). and_return true entry = Stackster::Entry.new :config => config, :name => 'test-stack' entry.class.should == Stackster::Entry end it "should find the requested stack in simple db" do @simple_db_mock = mock 'simple db' config = Stackster::Config.new(:config => { 'access_key' => 'the-key', 'secret_key' => 'secret', 'region' => 'us-west-1' }) Stackster::AWS::SimpleDB.should_receive(:new).and_return @simple_db_mock @simple_db_mock.should_receive(:create_domain). with("stacks"). and_return true Stackster::Entry.find :name => 'stack-to-find', :config => config end context "with stack object" do before do @config = Stackster::Config.new(:config => { 'access_key' => 'the-key', 'secret_key' => 'secret', 'region' => 'us-west-1' }) @simple_db_mock = mock 'simple db' Stackster::AWS::SimpleDB.should_receive(:new).and_return @simple_db_mock @simple_db_mock.should_receive(:create_domain). with("stacks"). and_return true @entry = Stackster::Entry.new :config => @config, :name => 'test-stack' end it "should set the name to region-name for the stack" do @entry.name.should == 'test-stack-us-west-1' end it "should set the attributes in simple db including default attributes" do Timecop.travel Time.utc(2012, 10, 22, 13, 30) @simple_db_mock.should_receive(:select). with("select * from stacks where itemName() = 'test-stack-us-west-1'"). and_return('test-stack-us-west-1' => { 'key1' => ['value1'] }) @simple_db_mock.should_receive(:put_attributes). with("stacks", "test-stack-us-west-1", { "key" => "value", "key1" => "value1", "Name" => "test-stack-us-west-1", "CreatedAt" => "2012-10-22 13:30:00 UTC" }, { :replace => ["key1", "key", "Name", "CreatedAt"] } ) @entry.set_attributes(['key' => 'value']) @entry.save end it "should merge custom attributes" do @simple_db_mock.should_receive(:select). with("select * from stacks where itemName() = 'test-stack-us-west-1'"). and_return('test-stack' => { 'key1' => ['value1'] }) @entry.set_attributes(['key1' => 'value2']) @entry.attributes.should == {'key1' => 'value2' } end end end