Skip to content

Commit

Permalink
Refactor Test Code for Efficiency and Quality (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
freddiewanah committed Apr 20, 2024
1 parent 1f77254 commit 08cb018
Show file tree
Hide file tree
Showing 29 changed files with 188 additions and 218 deletions.
43 changes: 12 additions & 31 deletions test/test_apprise_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_apprise_config(tmpdir):
assert isinstance(ac[0].url(), str)

# pop an entry from our list
assert isinstance(ac.pop(0), ConfigBase) is True
assert isinstance(ac.pop(0), ConfigBase)

# Determine we have no more configuration entries loaded
assert len(ac) == 0
Expand Down Expand Up @@ -271,19 +271,13 @@ def url(self, **kwargs):
assert ac.add(configs=object()) is False

# Try to pop an element out of range
try:
with pytest.raises(IndexError):
ac.server_pop(len(ac.servers()))
# We should have thrown an exception here
assert False

except IndexError:
# We expect to be here
assert True

# Pop our elements
while len(ac.servers()) > 0:
assert isinstance(
ac.server_pop(len(ac.servers()) - 1), NotifyBase) is True
ac.server_pop(len(ac.servers()) - 1), NotifyBase)


def test_apprise_add_config():
Expand Down Expand Up @@ -623,7 +617,7 @@ def url(self, **kwargs):

# reference index 0 of our list
ref = a[0]
assert isinstance(ref, NotifyBase) is True
assert isinstance(ref, NotifyBase)

# Our length is unchanged
assert len(a) == 5
Expand All @@ -632,7 +626,7 @@ def url(self, **kwargs):
ref_popped = a.pop(0)

# Verify our response
assert isinstance(ref_popped, NotifyBase) is True
assert isinstance(ref_popped, NotifyBase)

# Our length drops by 1
assert len(a) == 4
Expand All @@ -642,34 +636,21 @@ def url(self, **kwargs):
assert ref == ref_popped

# pop an index out of range
try:
with pytest.raises(IndexError):
a.pop(len(a))
# We'll thrown an IndexError and not make it this far
assert False

except IndexError:
# As expected
assert True

# Our length remains unchanged
assert len(a) == 4

# Reference content out of range
try:
with pytest.raises(IndexError):
a[len(a)]

# We'll thrown an IndexError and not make it this far
assert False

except IndexError:
# As expected
assert True

# reference index at the end of our list
ref = a[len(a) - 1]

# Verify our response
assert isinstance(ref, NotifyBase) is True
assert isinstance(ref, NotifyBase)

# Our length stays the same
assert len(a) == 4
Expand All @@ -678,7 +659,7 @@ def url(self, **kwargs):
ref_popped = a.pop(len(a) - 1)

# Verify our response
assert isinstance(ref_popped, NotifyBase) is True
assert isinstance(ref_popped, NotifyBase)

# Content popped is the same as one referenced by index
# earlier
Expand All @@ -703,13 +684,13 @@ def url(self, **kwargs):
ref = a[len(a) - 1]

# Verify our response
assert isinstance(ref, NotifyBase) is True
assert isinstance(ref, NotifyBase)

# We can pop from the back of the list without a problem too
ref_popped = a.pop(len(a) - 1)

# Verify our response
assert isinstance(ref_popped, NotifyBase) is True
assert isinstance(ref_popped, NotifyBase)

# Content popped is the same as one referenced by index
# earlier
Expand All @@ -720,7 +701,7 @@ def url(self, **kwargs):

# pop our list
while len(a) > 0:
assert isinstance(a.pop(len(a) - 1), NotifyBase) is True
assert isinstance(a.pop(len(a) - 1), NotifyBase)


def test_recursive_config_inclusion(tmpdir):
Expand Down
14 changes: 7 additions & 7 deletions test/test_apprise_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_parse_qsd():
"utils: parse_qsd() testing """

result = utils.parse_qsd('a=1&b=&c&d=abcd')
assert isinstance(result, dict) is True
assert isinstance(result, dict)
assert len(result) == 4
assert 'qsd' in result
assert 'qsd+' in result
Expand Down Expand Up @@ -2607,39 +2607,39 @@ def test_apply_templating():

result = utils.apply_template(
template, **{'fname': 'Chris', 'whence': 'this morning'})
assert isinstance(result, str) is True
assert isinstance(result, str)
assert result == "Hello Chris, How are you this morning?"

# In this example 'whence' isn't provided, so it isn't swapped
result = utils.apply_template(
template, **{'fname': 'Chris'})
assert isinstance(result, str) is True
assert isinstance(result, str)
assert result == "Hello Chris, How are you {{whence}}?"

# white space won't cause any ill affects:
template = "Hello {{ fname }}, How are you {{ whence}}?"
result = utils.apply_template(
template, **{'fname': 'Chris', 'whence': 'this morning'})
assert isinstance(result, str) is True
assert isinstance(result, str)
assert result == "Hello Chris, How are you this morning?"

# No arguments won't cause any problems
template = "Hello {{fname}}, How are you {{whence}}?"
result = utils.apply_template(template)
assert isinstance(result, str) is True
assert isinstance(result, str)
assert result == template

# Wrong elements are simply ignored
result = utils.apply_template(
template,
**{'fname': 'l2g', 'whence': 'this evening', 'ignore': 'me'})
assert isinstance(result, str) is True
assert isinstance(result, str)
assert result == "Hello l2g, How are you this evening?"

# Empty template makes things easy
result = utils.apply_template(
"", **{'fname': 'l2g', 'whence': 'this evening'})
assert isinstance(result, str) is True
assert isinstance(result, str)
assert result == ""

# Regular expressions are safely escapped and act as normal
Expand Down
4 changes: 2 additions & 2 deletions test/test_config_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,9 @@ def test_config_base_config_parse_yaml():
assert asset.theme == AppriseAsset().theme

# Empty string assignment
assert isinstance(asset.image_url_mask, str) is True
assert isinstance(asset.image_url_mask, str)
assert asset.image_url_mask == ""
assert isinstance(asset.image_url_logo, str) is True
assert isinstance(asset.image_url_logo, str)
assert asset.image_url_logo == ""

# For on-lookers looking through this file; here is a perfectly formatted
Expand Down
8 changes: 4 additions & 4 deletions test/test_config_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def test_config_memory():
assert len(cm) == 1

# Test general functions
assert isinstance(cm.url(), str) is True
assert isinstance(cm.read(), str) is True
assert isinstance(cm.url(), str)
assert isinstance(cm.read(), str)

# Test situation where an auto-detect is required:
cm = ConfigMemory(content="json://localhost")
Expand All @@ -58,8 +58,8 @@ def test_config_memory():
assert len(cm) == 1

# Test general functions
assert isinstance(cm.url(), str) is True
assert isinstance(cm.read(), str) is True
assert isinstance(cm.url(), str)
assert isinstance(cm.read(), str)

# Test situation where we can not detect the data
assert len(ConfigMemory(content="garbage")) == 0
2 changes: 1 addition & 1 deletion test/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def test_apprise_secure_logging(mock_post):
assert a.add("json://user:pass1$-3!@localhost") is True

# Our servers should carry this flag
a[0].asset.secure_logging is True
assert a[0].asset.secure_logging is True

logs = re.split(r'\r*\n', stream.getvalue().rstrip())
assert len(logs) == 1
Expand Down
2 changes: 1 addition & 1 deletion test/test_notification_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_notification_manager_general():
N_MGR.unload_modules()
assert bool(N_MGR) is False
assert len([x for x in iter(N_MGR)]) > 0
assert bool(N_MGR) is True
assert bool(N_MGR)

N_MGR.unload_modules()
assert isinstance(N_MGR.plugins(), types.GeneratorType)
Expand Down
18 changes: 7 additions & 11 deletions test/test_notify_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,11 @@ def test_notify_base():
assert isinstance(nb.url(), str)
assert str(nb) == nb.url()

try:
nb.send('test message')
assert False

except NotImplementedError:
with pytest.raises(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
nb.send('test message')

# Throttle overrides..
nb = NotifyBase()
Expand Down Expand Up @@ -208,13 +204,13 @@ def test_notify_base():

result = NotifyBase.parse_list(
',path,?name=Dr%20Disrespect', unquote=False)
assert isinstance(result, list) is True
assert isinstance(result, list)
assert len(result) == 2
assert 'path' in result
assert '?name=Dr%20Disrespect' in result

result = NotifyBase.parse_list(',path,?name=Dr%20Disrespect', unquote=True)
assert isinstance(result, list) is True
assert isinstance(result, list)
assert len(result) == 2
assert 'path' in result
assert '?name=Dr Disrespect' in result
Expand All @@ -225,7 +221,7 @@ def test_notify_base():
# eliminates duplicates in addition to unquoting content by default
result = NotifyBase.parse_list(
',%2F,%2F%2F, , , ,%2F%2F%2F, %2F', unquote=True)
assert isinstance(result, list) is True
assert isinstance(result, list)
assert len(result) == 3
assert '/' in result
assert '//' in result
Expand All @@ -238,15 +234,15 @@ def test_notify_base():

result = NotifyBase.parse_phone_no(
'+1-800-123-1234,(800) 123-4567', unquote=False)
assert isinstance(result, list) is True
assert isinstance(result, list)
assert len(result) == 2
assert '+1-800-123-1234' in result
assert '(800) 123-4567' in result

# %2B == +
result = NotifyBase.parse_phone_no(
'%2B1-800-123-1234,%2B1%20800%20123%204567', unquote=True)
assert isinstance(result, list) is True
assert isinstance(result, list)
assert len(result) == 2
assert '+1-800-123-1234' in result
assert '+1 800 123 4567' in result
Expand Down
4 changes: 2 additions & 2 deletions test/test_plugin_custom_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_plugin_custom_json_edge_cases(mock_get, mock_post):
assert results['query'] == 'command'
assert results['schema'] == 'json'
assert results['url'] == 'json://localhost:8080/command'
assert isinstance(results['qsd:'], dict) is True
assert isinstance(results['qsd:'], dict)
assert results['qsd:']['message'] == 'msg'
# empty special mapping
assert results['qsd:']['type'] == ''
Expand Down Expand Up @@ -321,7 +321,7 @@ def test_plugin_custom_form_for_synology(mock_post):
assert results['query'] == 'entry.cgi'
assert results['schema'] == 'jsons'
assert results['url'] == 'jsons://localhost:8081/webapi/entry.cgi'
assert isinstance(results['qsd:'], dict) is True
assert isinstance(results['qsd:'], dict)
# Header Entries
assert results['qsd-']['api'] == 'SYNO.Chat.External'
assert results['qsd-']['method'] == 'incoming'
Expand Down
6 changes: 3 additions & 3 deletions test/test_plugin_custom_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def test_plugin_custom_xml_edge_cases(mock_get, mock_post):
assert results['query'] == 'command'
assert results['schema'] == 'xml'
assert results['url'] == 'xml://localhost:8080/command'
assert isinstance(results['qsd:'], dict) is True
assert isinstance(results['qsd:'], dict)
assert results['qsd:']['Message'] == 'Body'
assert results['qsd:']['Key'] == 'value'
assert results['qsd:'][','] == 'invalid'
Expand Down Expand Up @@ -316,7 +316,7 @@ def test_plugin_custom_xml_edge_cases(mock_get, mock_post):
assert results['query'] == 'command'
assert results['schema'] == 'xml'
assert results['url'] == 'xml://localhost:8081/command'
assert isinstance(results['qsd:'], dict) is True
assert isinstance(results['qsd:'], dict)
assert results['qsd:']['New'] == 'Value'

instance = NotifyXML(**results)
Expand Down Expand Up @@ -364,7 +364,7 @@ def test_plugin_custom_xml_edge_cases(mock_get, mock_post):
assert results['query'] is None
assert results['schema'] == 'xmls'
assert results['url'] == 'xmls://localhost'
assert isinstance(results['qsd:'], dict) is True
assert isinstance(results['qsd:'], dict)
assert results['qsd:']['Version'] == ''
assert results['qsd:']['Message'] == 'Body'
assert results['qsd:']['Subject'] == 'Title'
Expand Down
Loading

0 comments on commit 08cb018

Please sign in to comment.