def test_notify_base(): """ API: NotifyBase() object """ # invalid types throw exceptions try: nb = NotifyBase(**{'format': 'invalid'}) # We should never reach here as an exception should be thrown assert(False) except TypeError: assert(True) # Bad port information nb = NotifyBase(port='invalid') assert nb.port is None nb = NotifyBase(port=10) assert nb.port == 10 # Throttle overrides.. nb = NotifyBase() nb.throttle_attempt = 0.0 start_time = default_timer() nb.throttle() elapsed = default_timer() - start_time # Should be a very fast response time since we set it to zero but we'll # check for less then 500 to be fair as some testing systems may be slower # then other assert elapsed < 0.5 start_time = default_timer() nb.throttle(1.0) elapsed = default_timer() - start_time # Should be a very fast response time since we set it to zero but we'll # check for less then 500 to be fair as some testing systems may be slower # then other assert elapsed < 1.5 # our NotifyBase wasn't initialized with an ImageSize so this will fail assert nb.image_url(notify_type=NotifyType.INFO) is None assert nb.image_path(notify_type=NotifyType.INFO) is None assert nb.image_raw(notify_type=NotifyType.INFO) is None # Color handling assert nb.color(notify_type='invalid') is None assert compat_is_basestring( nb.color(notify_type=NotifyType.INFO, color_type=None)) assert isinstance( nb.color(notify_type=NotifyType.INFO, color_type=int), int) assert isinstance( nb.color(notify_type=NotifyType.INFO, color_type=tuple), tuple) # Create an object nb = NotifyBase() # Force an image size since the default doesn't have one nb.image_size = NotifyImageSize.XY_256 # We'll get an object this time around assert nb.image_url(notify_type=NotifyType.INFO) is not None assert nb.image_path(notify_type=NotifyType.INFO) is not None assert nb.image_raw(notify_type=NotifyType.INFO) is not None # But we will not get a response with an invalid notification type assert nb.image_url(notify_type='invalid') is None assert nb.image_path(notify_type='invalid') is None assert nb.image_raw(notify_type='invalid') is None # Static function testing assert NotifyBase.escape_html("<content>'\t \n</content>") == \ '<content>'  \n</content>' assert NotifyBase.escape_html( "<content>'\t \n</content>", convert_new_lines=True) == \ '<content>'  <br/></content>' assert NotifyBase.split_path( '/path/?name=Dr%20Disrespect', unquote=False) == \ ['path', '?name=Dr%20Disrespect'] assert NotifyBase.split_path( '/path/?name=Dr%20Disrespect', unquote=True) == \ ['path', '?name=Dr', 'Disrespect'] # Test is_email assert NotifyBase.is_email('*****@*****.**') is True assert NotifyBase.is_email('invalid.com') is False # Test is_hostname assert NotifyBase.is_hostname('example.com') is True # Test quote assert NotifyBase.unquote('%20') == ' ' assert NotifyBase.quote(' ') == '%20' assert NotifyBase.unquote(None) == '' assert NotifyBase.quote(None) == ''
def test_notify_base(): """ API: NotifyBase() object """ # invalid types throw exceptions try: nb = NotifyBase(**{'format': 'invalid'}) # We should never reach here as an exception should be thrown assert (False) except TypeError: assert (True) # invalid types throw exceptions try: nb = NotifyBase(**{'overflow': 'invalid'}) # We should never reach here as an exception should be thrown assert (False) except TypeError: assert (True) # Bad port information nb = NotifyBase(port='invalid') assert nb.port is None nb = NotifyBase(port=10) assert nb.port == 10 try: nb.url() assert False except NotImplementedError: # Each sub-module is that inherits this as a parent is required to # over-ride this function. So direct calls to this throws a not # implemented error intentionally assert True try: nb.send('test message') assert False except NotImplementedError: # Each sub-module is that inherits this as a parent is required to # over-ride this function. So direct calls to this throws a not # implemented error intentionally assert True # Throttle overrides.. nb = NotifyBase() nb.request_rate_per_sec = 0.0 start_time = default_timer() nb.throttle() elapsed = default_timer() - start_time # Should be a very fast response time since we set it to zero but we'll # check for less then 500 to be fair as some testing systems may be slower # then other assert elapsed < 0.5 # Concurrent calls should achieve the same response start_time = default_timer() nb.throttle() elapsed = default_timer() - start_time assert elapsed < 0.5 nb = NotifyBase() nb.request_rate_per_sec = 1.0 # Set our time to now start_time = default_timer() nb.throttle() elapsed = default_timer() - start_time # A first call to throttle (Without telling it a time previously ran) does # not block for any length of time; it just merely sets us up for # concurrent calls to block assert elapsed < 0.5 # Concurrent calls could take up to the rate_per_sec though... start_time = default_timer() nb.throttle(last_io=datetime.now()) elapsed = default_timer() - start_time assert elapsed > 0.5 and elapsed < 1.5 nb = NotifyBase() nb.request_rate_per_sec = 1.0 # Set our time to now start_time = default_timer() nb.throttle(last_io=datetime.now()) elapsed = default_timer() - start_time # because we told it that we had already done a previous action (now) # the throttle holds out until the right time has passed assert elapsed > 0.5 and elapsed < 1.5 # Concurrent calls could take up to the rate_per_sec though... start_time = default_timer() nb.throttle(last_io=datetime.now()) elapsed = default_timer() - start_time assert elapsed > 0.5 and elapsed < 1.5 nb = NotifyBase() start_time = default_timer() nb.request_rate_per_sec = 1.0 # Force a time in the past nb.throttle(last_io=(datetime.now() - timedelta(seconds=20))) elapsed = default_timer() - start_time # Should be a very fast response time since we set it to zero but we'll # check for less then 500 to be fair as some testing systems may be slower # then other assert elapsed < 0.5 # our NotifyBase wasn't initialized with an ImageSize so this will fail assert nb.image_url(notify_type=NotifyType.INFO) is None assert nb.image_path(notify_type=NotifyType.INFO) is None assert nb.image_raw(notify_type=NotifyType.INFO) is None # Color handling assert nb.color(notify_type='invalid') is None assert compat_is_basestring( nb.color(notify_type=NotifyType.INFO, color_type=None)) assert isinstance(nb.color(notify_type=NotifyType.INFO, color_type=int), int) assert isinstance(nb.color(notify_type=NotifyType.INFO, color_type=tuple), tuple) # Create an object nb = NotifyBase() # Force an image size since the default doesn't have one nb.image_size = NotifyImageSize.XY_256 # We'll get an object this time around assert nb.image_url(notify_type=NotifyType.INFO) is not None assert nb.image_path(notify_type=NotifyType.INFO) is not None assert nb.image_raw(notify_type=NotifyType.INFO) is not None # But we will not get a response with an invalid notification type assert nb.image_url(notify_type='invalid') is None assert nb.image_path(notify_type='invalid') is None assert nb.image_raw(notify_type='invalid') is None # Static function testing assert NotifyBase.escape_html("<content>'\t \n</content>") == \ '<content>'  \n</content>' assert NotifyBase.escape_html( "<content>'\t \n</content>", convert_new_lines=True) == \ '<content>'  <br/></content>' assert NotifyBase.split_path( '/path/?name=Dr%20Disrespect', unquote=False) == \ ['path', '?name=Dr%20Disrespect'] assert NotifyBase.split_path( '/path/?name=Dr%20Disrespect', unquote=True) == \ ['path', '?name=Dr', 'Disrespect'] # Test is_email assert NotifyBase.is_email('*****@*****.**') is True assert NotifyBase.is_email('invalid.com') is False # Test is_hostname assert NotifyBase.is_hostname('example.com') is True # Test quote assert NotifyBase.unquote('%20') == ' ' assert NotifyBase.quote(' ') == '%20' assert NotifyBase.unquote(None) == '' assert NotifyBase.quote(None) == ''