From d7468d874765457218448987df3b103a98aa224d Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Thu, 14 Feb 2013 10:59:25 -0600 Subject: [PATCH] [rackspace|storage] rebased with dir_metadata branch - https://github.com/fog/fog/pull/1563 --- lib/fog/rackspace/models/storage/directory.rb | 2 +- .../models/storage/metadata_tests.rb | 141 +++++++++++++----- 2 files changed, 101 insertions(+), 42 deletions(-) diff --git a/lib/fog/rackspace/models/storage/directory.rb b/lib/fog/rackspace/models/storage/directory.rb index b64a2ca35..7ea255d9b 100644 --- a/lib/fog/rackspace/models/storage/directory.rb +++ b/lib/fog/rackspace/models/storage/directory.rb @@ -76,7 +76,7 @@ module Fog def save requires :key - headers = @metadata.nil? ? {} : metadata.to_headers + headers = attributes[:metadata].nil? ? {} : metadata.to_headers service.put_container(key, headers) if service.cdn && public? diff --git a/tests/rackspace/models/storage/metadata_tests.rb b/tests/rackspace/models/storage/metadata_tests.rb index 7978a64fd..3f1764b52 100644 --- a/tests/rackspace/models/storage/metadata_tests.rb +++ b/tests/rackspace/models/storage/metadata_tests.rb @@ -1,67 +1,126 @@ require 'fog/rackspace/models/storage/metadata' +require 'fog/rackspace/models/storage/directory' +require 'fog/rackspace/models/storage/file' Shindo.tests('Fog::Rackspace::Storage | metadata', ['rackspace']) do - - tests('#to_key') do - tests('valid key').returns(:image_size) do - Fog::Storage::Rackspace::Metadata.send(:to_key, "X-Container-Meta-Image-Size") + tests('Directory') do + @directory = Fog::Storage::Rackspace::Directory.new + tests('#to_key') do + tests('valid key').returns(:image_size) do + metadata = Fog::Storage::Rackspace::Metadata.new @directory + metadata.send(:to_key, "X-Container-Meta-Image-Size") + end + tests('invalid key').returns(nil) do + metadata = Fog::Storage::Rackspace::Metadata.new @directory + metadata.send(:to_key, "bad-key") + end end - tests('invalid key').returns(nil) do - Fog::Storage::Rackspace::Metadata.send(:to_key, "bad-key") + + tests('#to_header_key') do + metadata = Fog::Storage::Rackspace::Metadata.new @directory + + tests('key to add').returns("X-Container-Meta-Thumbnail-Image") do + metadata.send(:to_header_key, :thumbnail_image, true) + end + + tests('key to remove').returns("X-Remove-Container-Meta-Thumbnail-Image") do + metadata.send(:to_header_key, :thumbnail_image, nil) + end end - end - tests('#to_header_key') do - metadata = Fog::Storage::Rackspace::Metadata.new + tests('#to_headers').returns({"X-Container-Meta-Preview"=>true, "X-Remove-Container-Meta-Delete-Me"=>1}) do + metadata = Fog::Storage::Rackspace::Metadata.new @directory + metadata[:preview] = true + metadata[:delete_me] = nil - tests('key to add').returns("X-Container-Meta-Thumbnail-Image") do - metadata.send(:to_header_key, :thumbnail_image, true) + metadata.to_headers end - - tests('key to remove').returns("X-Remove-Container-Meta-Thumbnail-Image") do - metadata.send(:to_header_key, :thumbnail_image, nil) - end - end - tests('#to_headers').returns({"X-Container-Meta-Preview"=>true, "X-Remove-Container-Meta-Delete-Me"=>1}) do - metadata = Fog::Storage::Rackspace::Metadata.new - metadata[:preview] = true - metadata[:delete_me] = nil - - metadata.to_headers - end - - tests("#from_headers").returns({:my_boolean=>true, :my_integer=>42, :my_string=>"I am a string"}) do - headers = { - "X-Container-Meta-My-Integer"=> "42", - "X-Container-Meta-My-Boolean"=> "true", - "X-Container-Meta-My-String"=> "\"I am a string\"" - } + tests("#from_headers").returns({:my_boolean=>true, :my_integer=>42, :my_string=>"I am a string"}) do + headers = { + "X-Container-Meta-My-Integer"=> "42", + "X-Container-Meta-My-Boolean"=> "true", + "X-Container-Meta-My-String"=> "\"I am a string\"" + } - metadata = Fog::Storage::Rackspace::Metadata.from_headers headers - metadata.data - end + metadata = Fog::Storage::Rackspace::Metadata.from_headers @directory, headers + metadata.data + end - tests("#delete").returns({"X-Remove-Container-Meta-Delete-Me"=>1}) do - metadata = Fog::Storage::Rackspace::Metadata.new - metadata.delete(:delete_me) + tests("#delete").returns({"X-Remove-Container-Meta-Delete-Me"=>1}) do + metadata = Fog::Storage::Rackspace::Metadata.new @directory + metadata.delete(:delete_me) - metadata.to_headers + metadata.to_headers + end end - + + + tests('File') do + @file = Fog::Storage::Rackspace::File.new + tests('#to_key') do + tests('valid key').returns(:image_size) do + metadata = Fog::Storage::Rackspace::Metadata.new @file + metadata.send(:to_key, "X-Object-Meta-Image-Size") + end + tests('invalid key').returns(nil) do + metadata = Fog::Storage::Rackspace::Metadata.new @file + metadata.send(:to_key, "bad-key") + end + end + + tests('#to_header_key') do + metadata = Fog::Storage::Rackspace::Metadata.new @file + + tests('key to add').returns("X-Object-Meta-Thumbnail-Image") do + metadata.send(:to_header_key, :thumbnail_image, true) + end + + tests('key to remove').returns("X-Remove-Object-Meta-Thumbnail-Image") do + metadata.send(:to_header_key, :thumbnail_image, nil) + end + end + + tests('#to_headers').returns({"X-Object-Meta-Preview"=>true, "X-Remove-Object-Meta-Delete-Me"=>1}) do + metadata = Fog::Storage::Rackspace::Metadata.new @file + metadata[:preview] = true + metadata[:delete_me] = nil + + metadata.to_headers + end + + tests("#from_headers").returns({:my_boolean=>true, :my_integer=>42, :my_string=>"I am a string"}) do + headers = { + "X-Object-Meta-My-Integer"=> "42", + "X-Object-Meta-My-Boolean"=> "true", + "X-Object-Meta-My-String"=> "\"I am a string\"" + } + + metadata = Fog::Storage::Rackspace::Metadata.from_headers @file, headers + metadata.data + end + + tests("#delete").returns({"X-Remove-Object-Meta-Delete-Me"=>1}) do + metadata = Fog::Storage::Rackspace::Metadata.new @file + metadata.delete(:delete_me) + + metadata.to_headers + end + end + tests("#respond_to?") do tests('Should respond to all of the methods in Hash class').returns(true) do - metadata = Fog::Storage::Rackspace::Metadata.new + metadata = Fog::Storage::Rackspace::Metadata.new @file Hash.instance_methods.all? {|method| metadata.respond_to?(method)} end tests('Should respond to all of the methods in the Metadata class').returns(true) do - metadata = Fog::Storage::Rackspace::Metadata.new + metadata = Fog::Storage::Rackspace::Metadata.new @file metadata.methods.all? {|method| metadata.respond_to?(method)} end end tests("#method_missing").returns(true) do - metadata = Fog::Storage::Rackspace::Metadata.new + metadata = Fog::Storage::Rackspace::Metadata.new @file metadata[:test] = true metadata[:test] end