<s> from __future__ import absolute_import <EOL> from mox import IsA <EOL> from horizon import exceptions <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . test import helpers as test <EOL> class SwiftApiTests ( test . APITestCase ) : <EOL> def test_swift_get_containers ( self ) : <EOL> containers = self . containers . list ( ) <EOL> cont_data = [ c . _apidict for c in containers ] <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . get_account ( limit = <NUM_LIT> , <EOL> marker = None , <EOL> full_listing = True ) . AndReturn ( [ { } , cont_data ] ) <EOL> self . mox . ReplayAll ( ) <EOL> ( conts , more ) = api . swift . swift_get_containers ( self . request ) <EOL> self . assertEqual ( len ( containers ) , len ( conts ) ) <EOL> self . assertFalse ( more ) <EOL> def test_swift_get_container_with_data ( self ) : <EOL> container = self . containers . first ( ) <EOL> objects = self . objects . list ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . get_object ( container . name , "<STR_LIT>" ) . AndReturn ( ( container , objects ) ) <EOL> self . mox . ReplayAll ( ) <EOL> cont = api . swift . swift_get_container ( self . request , container . name ) <EOL> self . assertEqual ( container . name , cont . name ) <EOL> self . assertEqual ( len ( objects ) , len ( cont . data ) ) <EOL> def test_swift_get_container_without_data ( self ) : <EOL> container = self . containers . first ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . head_container ( container . name ) . AndReturn ( container ) <EOL> self . mox . ReplayAll ( ) <EOL> cont = api . swift . swift_get_container ( self . request , <EOL> container . name , <EOL> with_data = False ) <EOL> self . assertEqual ( cont . name , container . name ) <EOL> self . assertIsNone ( cont . data ) <EOL> def test_swift_create_duplicate_container ( self ) : <EOL> metadata = { '<STR_LIT>' : False } <EOL> container = self . containers . first ( ) <EOL> headers = api . swift . _metadata_to_header ( metadata = ( metadata ) ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> exc = self . exceptions . swift <EOL> swift_api . head_container ( container . name ) . AndRaise ( exc ) <EOL> swift_api . put_container ( container . name , headers = headers ) . AndReturn ( container ) <EOL> self . mox . ReplayAll ( ) <EOL> api . swift . swift_create_container ( self . request , <EOL> container . name , <EOL> metadata = ( metadata ) ) <EOL> def test_swift_create_container ( self ) : <EOL> metadata = { '<STR_LIT>' : True } <EOL> container = self . containers . first ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . head_container ( container . name ) . AndReturn ( container ) <EOL> self . mox . ReplayAll ( ) <EOL> with self . assertRaises ( exceptions . AlreadyExists ) : <EOL> api . swift . swift_create_container ( self . request , <EOL> container . name , <EOL> metadata = ( metadata ) ) <EOL> def test_swift_update_container ( self ) : <EOL> metadata = { '<STR_LIT>' : True } <EOL> container = self . containers . first ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> headers = api . swift . _metadata_to_header ( metadata = ( metadata ) ) <EOL> swift_api . post_container ( container . name , headers = headers ) . AndReturn ( container ) <EOL> self . mox . ReplayAll ( ) <EOL> api . swift . swift_update_container ( self . request , <EOL> container . name , <EOL> metadata = ( metadata ) ) <EOL> def test_swift_get_objects ( self ) : <EOL> container = self . containers . first ( ) <EOL> objects = self . objects . list ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . get_container ( container . name , <EOL> limit = <NUM_LIT> , <EOL> marker = None , <EOL> prefix = None , <EOL> delimiter = '<STR_LIT:/>' , <EOL> full_listing = True ) . AndReturn ( [ { } , objects ] ) <EOL> self . mox . ReplayAll ( ) <EOL> ( objs , more ) = api . swift . swift_get_objects ( self . request , <EOL> container . name ) <EOL> self . assertEqual ( len ( objects ) , len ( objs ) ) <EOL> self . assertFalse ( more ) <EOL> def test_swift_get_object_with_data ( self ) : <EOL> container = self . containers . first ( ) <EOL> object = self . objects . first ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . get_object ( container . name , object . name ) . AndReturn ( [ object , object . data ] ) <EOL> self . mox . ReplayAll ( ) <EOL> obj = api . swift . swift_get_object ( self . request , <EOL> container . name , <EOL> object . name ) <EOL> self . assertEqual ( object . name , obj . name ) <EOL> def test_swift_get_object_without_data ( self ) : <EOL> container = self . containers . first ( ) <EOL> object = self . objects . first ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . head_object ( container . name , object . name ) . AndReturn ( object ) <EOL> self . mox . ReplayAll ( ) <EOL> obj = api . swift . swift_get_object ( self . request , <EOL> container . name , <EOL> object . name , <EOL> with_data = False ) <EOL> self . assertEqual ( object . name , obj . name ) <EOL> self . assertIsNone ( obj . data ) <EOL> def test_swift_upload_object ( self ) : <EOL> container = self . containers . first ( ) <EOL> obj = self . objects . first ( ) <EOL> fake_name = '<STR_LIT>' <EOL> class FakeFile ( object ) : <EOL> def __init__ ( self ) : <EOL> self . name = fake_name <EOL> self . data = obj . data <EOL> self . size = len ( obj . data ) <EOL> headers = { '<STR_LIT>' : fake_name } <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . put_object ( container . name , <EOL> obj . name , <EOL> IsA ( FakeFile ) , <EOL> headers = headers ) <EOL> self . mox . ReplayAll ( ) <EOL> api . swift . swift_upload_object ( self . request , <EOL> container . name , <EOL> obj . name , <EOL> FakeFile ( ) ) <EOL> def test_swift_upload_object_without_file ( self ) : <EOL> container = self . containers . first ( ) <EOL> obj = self . objects . first ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . put_object ( container . name , <EOL> obj . name , <EOL> None , <EOL> headers = { } ) <EOL> self . mox . ReplayAll ( ) <EOL> response = api . swift . swift_upload_object ( self . request , <EOL> container . name , <EOL> obj . name , <EOL> None ) <EOL> self . assertEqual ( <NUM_LIT:0> , response [ '<STR_LIT>' ] ) <EOL> def test_swift_object_exists ( self ) : <EOL> container = self . containers . first ( ) <EOL> obj = self . objects . first ( ) <EOL> swift_api = self . stub_swiftclient ( ) <EOL> swift_api . head_object ( container . name , obj . name ) . AndReturn ( container ) <EOL> exc = self . exceptions . swift <EOL> swift_api . head_object ( container . name , obj . name ) . AndRaise ( exc ) <EOL> self . mox . ReplayAll ( ) <EOL> args = self . request , container . name , obj . name <EOL> self . assertTrue ( api . swift . swift_object_exists ( * args ) ) <EOL> self . assertFalse ( api . swift . swift_object_exists ( * args ) ) </s>
