Skip to content

Commit

Permalink
Added tests for base helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
nuwang committed Feb 17, 2019
1 parent abeb31d commit 8154a3d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cloudbridge/cloud/base/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def get_env(varname, default_value=None):
return value


# Alias deprication decorator, following:
# Alias deprecation decorator, following:
# https://stackoverflow.com/questions/49802412/
# how-to-implement-deprecation-in-python-with-argument-alias
def deprecated_alias(**aliases):
Expand Down
102 changes: 102 additions & 0 deletions test/test_base_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import unittest

from cloudbridge.cloud.base import helpers as cb_helpers
from cloudbridge.cloud.interfaces.exceptions import InvalidParamException


class BaseHelpersTestCase(unittest.TestCase):

_multiprocess_can_split_ = True

def test_cleanup_action_body_has_no_exception(self):
invoke_order = [""]

def cleanup_func():
invoke_order[0] += "cleanup"

with cb_helpers.cleanup_action(lambda: cleanup_func()):
invoke_order[0] += "body_"
self.assertEqual(invoke_order[0], "body_cleanup")

def test_cleanup_action_body_has_exception(self):
invoke_order = [""]

def cleanup_func():
invoke_order[0] += "cleanup"

class CustomException(Exception):
pass

with self.assertRaises(CustomException):
with cb_helpers.cleanup_action(lambda: cleanup_func()):
invoke_order[0] += "body_"
raise CustomException()
self.assertEqual(invoke_order[0], "body_cleanup")

def test_cleanup_action_cleanup_has_exception(self):
invoke_order = [""]

def cleanup_func():
invoke_order[0] += "cleanup"
raise Exception("test")

with cb_helpers.cleanup_action(lambda: cleanup_func()):
invoke_order[0] += "body_"
self.assertEqual(invoke_order[0], "body_cleanup")

def test_cleanup_action_body_and_cleanup_has_exception(self):
invoke_order = [""]

def cleanup_func():
invoke_order[0] += "cleanup"
raise Exception("test")

class CustomException(Exception):
pass

with self.assertRaises(CustomException):
with cb_helpers.cleanup_action(lambda: cleanup_func()):
invoke_order[0] += "body_"
raise CustomException()
self.assertEqual(invoke_order[0], "body_cleanup")

def test_deprecated_alias_no_rename(self):
param_values = {}

@cb_helpers.deprecated_alias(old_param='new_param')
def custom_func(new_param=None, old_param=None):
param_values['new_param'] = new_param
param_values['old_param'] = old_param

custom_func(new_param="hello")
self.assertDictEqual(param_values,
{
'new_param': "hello",
'old_param': None
})

def test_deprecated_alias_force_rename(self):
param_values = {}

@cb_helpers.deprecated_alias(old_param='new_param')
def custom_func(new_param=None, old_param=None):
param_values['new_param'] = new_param
param_values['old_param'] = old_param

custom_func(old_param="hello")
self.assertDictEqual(param_values,
{
'new_param': "hello",
'old_param': None
})

def test_deprecated_alias_force_conflict(self):
param_values = {}

@cb_helpers.deprecated_alias(old_param='new_param')
def custom_func(new_param=None, old_param=None):
param_values['new_param'] = new_param
param_values['old_param'] = old_param

with self.assertRaises(InvalidParamException):
custom_func(new_param="world", old_param="hello")

0 comments on commit 8154a3d

Please sign in to comment.