1 # Copyright 2013 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 """Subclass of CloudBucket used for testing.""" 6 7 import os 8 import sys 9 10 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) 11 import cloud_bucket 12 13 14 class MockCloudBucket(cloud_bucket.BaseCloudBucket): 15 """Subclass of CloudBucket used for testing.""" 16 17 def __init__(self): 18 """Initializes the MockCloudBucket with its datastore. 19 20 Returns: 21 An instance of MockCloudBucket. 22 """ 23 self.datastore = {} 24 25 def Reset(self): 26 """Clears the MockCloudBucket's datastore.""" 27 self.datastore = {} 28 29 # override 30 def UploadFile(self, path, contents, content_type): 31 self.datastore[path] = contents 32 33 # override 34 def DownloadFile(self, path): 35 if self.datastore.has_key(path): 36 return self.datastore[path] 37 else: 38 raise cloud_bucket.FileNotFoundError 39 40 # override 41 def UpdateFile(self, path, contents): 42 if not self.FileExists(path): 43 raise cloud_bucket.FileNotFoundError 44 self.UploadFile(path, contents, '') 45 46 # override 47 def RemoveFile(self, path): 48 if self.datastore.has_key(path): 49 self.datastore.pop(path) 50 51 # override 52 def FileExists(self, path): 53 return self.datastore.has_key(path) 54 55 # override 56 def GetImageURL(self, path): 57 if self.datastore.has_key(path): 58 return path 59 else: 60 raise cloud_bucket.FileNotFoundError 61 62 # override 63 def GetAllPaths(self, prefix): 64 return (item[0] for item in self.datastore.items() 65 if item[0].startswith(prefix)) 66