-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2335 from lunkwill42/workflow/coverage-play
Increase test coverage for GammuDispatcher
- Loading branch information
Showing
2 changed files
with
65 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,83 @@ | ||
from unittest import TestCase | ||
|
||
# mock/patch/hack/workaround: gammudispatcher normally requires gammu to be | ||
# installed, but we're only testing functionality from the module that is not | ||
# dependent on this. If there is no gammu module, we fake it so we can import | ||
# gammudispather without failures. | ||
import pytest | ||
from mock import Mock, patch | ||
|
||
from nav.smsd.dispatcher import PermanentDispatcherError, DispatcherError | ||
|
||
try: | ||
import gammu | ||
except ImportError: | ||
import sys | ||
|
||
# Mock a minimum gammu module when one isn't present to patch | ||
gammu = sys.modules['gammu'] = type(sys)('gammu') | ||
from nav.smsd.gammudispatcher import decode_sms_to_unicode | ||
mock_statemachine = Mock(SendSMS=Mock(return_value=42)) | ||
gammu.StateMachine = Mock(return_value=mock_statemachine) | ||
gammu.GSMError = Exception | ||
|
||
from nav.smsd.gammudispatcher import decode_sms_to_unicode, GammuDispatcher | ||
|
||
DUMMY_SENDSMS_ARGS = ("999999", ["Message 1", "Message 2"]) | ||
|
||
class EncodingTests(TestCase): | ||
def test_ascii_string_should_decode_to_equal_object(self): | ||
|
||
class TestThatDecodeSmsToUnicode: | ||
def test_should_decode_ascii_bytes_to_comparable_string(self): | ||
sms = 'Hello' | ||
unicode_sms = decode_sms_to_unicode(sms) | ||
self.assertEqual(sms, unicode_sms) | ||
assert sms == unicode_sms | ||
|
||
def test_unicode_object_should_be_decoded_to_equal_object(self): | ||
def test_should_decode_string_to_comparable_string(self): | ||
sms = u'A m\xf8\xf8se once bit my sister' | ||
unicode_sms = decode_sms_to_unicode(sms) | ||
self.assertEqual(sms, unicode_sms) | ||
assert sms == unicode_sms | ||
|
||
def test_utf_8_string_should_properly_decode(self): | ||
def test_should_decode_utf8_string_properly(self): | ||
sms = b'A m\xc3\xb8\xc3\xb8se once bit my sister' | ||
unicode_sms = decode_sms_to_unicode(sms) | ||
expected = u'A m\xf8\xf8se once bit my sister' | ||
self.assertEqual(unicode_sms, expected) | ||
assert unicode_sms == expected | ||
|
||
|
||
class TestThatGammuDispatcher: | ||
def test_can_be_initialized(self): | ||
assert GammuDispatcher(None) | ||
|
||
@patch('gammu.StateMachine', new=Mock()) | ||
def test_should_send_sms_without_error(self): | ||
dispatcher = GammuDispatcher(None) | ||
result = dispatcher.sendsms(*DUMMY_SENDSMS_ARGS) | ||
assert result | ||
|
||
def test_should_raise_permanent_error_when_gammu_cannot_read_config(self): | ||
mocked_statemachine = Mock(ReadConfig=Mock(side_effect=IOError('Fake'))) | ||
with patch('gammu.StateMachine') as statemachine: | ||
statemachine.return_value = mocked_statemachine | ||
dispatcher = GammuDispatcher(None) | ||
with pytest.raises(PermanentDispatcherError): | ||
dispatcher.sendsms(*DUMMY_SENDSMS_ARGS) | ||
|
||
def test_should_raise_permanent_error_when_gammu_errors_during_init( | ||
self, mock_gsm_error | ||
): | ||
mocked_statemachine = Mock(Init=Mock(side_effect=mock_gsm_error)) | ||
with patch('gammu.StateMachine') as statemachine: | ||
statemachine.return_value = mocked_statemachine | ||
dispatcher = GammuDispatcher(None) | ||
with pytest.raises(PermanentDispatcherError): | ||
dispatcher.sendsms(*DUMMY_SENDSMS_ARGS) | ||
|
||
def test_should_raise_temporary_when_gammu_send_fails(self, mock_gsm_error): | ||
mocked_statemachine = Mock(SendSMS=Mock(side_effect=mock_gsm_error)) | ||
with patch('gammu.StateMachine') as statemachine: | ||
statemachine.return_value = mocked_statemachine | ||
dispatcher = GammuDispatcher(None) | ||
with pytest.raises(DispatcherError): | ||
dispatcher.sendsms(*DUMMY_SENDSMS_ARGS) | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def mock_gsm_error(): | ||
return gammu.GSMError({"Where": Mock(), 'Code': 666, 'Text': Mock()}) |