def test__login__called_before_connect_fails(self): with self.assertRaises(ValueError) as context: self._imap.login(imapconfig.user, imapconfig.password) actual_errmsg = exception.get_message(context.exception) expected_errmsg = mail._ERRMSG_NOT_CONNECTED self.assertEqual(expected_errmsg, actual_errmsg)
def test__get__does_not_throw_if_no_error_message(self): """ Test to ensure that empty ErrorMessage is returned if none found. """ err = Exception() actual = exception.get_message(err) self.assertEqual("", actual)
def test__connect__empty_server(self): server = "" with self.assertRaises(ValueError) as context: self._imap.connect(server) actual_errmsg = exception.get_message(context.exception) expected_errmsg = mail._ERRMSG_EMPTY_SERVER self.assertEqual(expected_errmsg, actual_errmsg)
def test__login__invalid_credentials(self): self._imap.connect(imapconfig.server) with self.assertRaises(Exception) as context: self._imap.login("invaliduser", "invalidpassword") actual_errmsg = exception.get_message(context.exception) expected_errmsg = mail._ERRMSG_INVALID_CREDENTIALS self.assertEqual(expected_errmsg, actual_errmsg)
def test__connect__timeout(self): with self.assertRaises(IOError) as context: self._imap.connect(imapconfig.not_imap_server) actual_errmsg = exception.get_message(context.exception) expected_errmsg = mail._ERRMSG_TIMEOUT.format( imapconfig.not_imap_server) self.assertEqual(expected_errmsg, actual_errmsg)
def test__get_messages__called_before_login_fails(self): self._imap.connect(imapconfig.server) with self.assertRaises(ValueError) as context: self._imap.get_messages() actual_errmsg = exception.get_message(context.exception) expected_errmsg = mail._ERRMSG_NOT_LOGGED_IN self.assertEqual(expected_errmsg, actual_errmsg)
def test__connect__server_not_found(self): server = "non-existant server" with self.assertRaises(IOError) as context: self._imap.connect(server) actual_errmsg = exception.get_message(context.exception) expected_errmsg = mail._ERRMSG_SERVER_NOT_FOUND.format( server) self.assertEqual(expected_errmsg, actual_errmsg)
def test__login__empty_password(self): self._imap.connect(imapconfig.server) with self.assertRaises(ValueError) as context: self._imap.login(user="******", password="") actual_errmsg = exception.get_message(context.exception) expected_errmsg = mail._ERRMSG_EMPTY_PASSWORD self.assertEqual(expected_errmsg, actual_errmsg)
def test__add__sanity(self): """ Test that added message by string can be retrieved. """ error_message = "error message" err = Exception() exception.add_message(err, error_message) actual = exception.get_message(err) self.assertEqual(error_message, actual)
def test__parse__fat_percentage_is_not_a_number(self): """ Test for string where mass is not a number. """ string = "-m 200 -f 1i5.2" expected_message = "fat percentage (1i5.2) is not a number" with self.assertRaises(ValueError) as context: self.parser.parse(string) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__parse__mass_is_not_a_number(self): """ Test for string where mass is not a number. """ string = "200a 15.2" expected_message = "mass (200a) is not a number" with self.assertRaises(ValueError) as context: self.parser.parse(string) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__parse__missing_fat_percentage(self): """ Test for string missing fat percentage value. """ string = "-m 100" expected_message = "fat percentage was not parsed " "from ({0})".format(string) with self.assertRaises(ValueError) as context: self.parser.parse(string) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__parse__missing_memo(self): """ Test that the method throws if string is missing memo flag. """ string = "-d phone -c bank -a 5" expected_message = "memo is missing from ({0})".format(string) with self.assertRaises(ValueError) as context: self.parser.parse(string) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__parse__missing_three_values(self): """ Test for string missing one value. """ string = "5.25" expected_message = "not enough arguments from ({0})".format(string) with self.assertRaises(ValueError) as context: self.parser.parse(string) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__parse__unknown_action(self): """ Test with string for an unknown action. """ string = "write this message" expected_message = "action key (write) is unknown" with self.assertRaises(KeyError) as context: self.parser.parse(string) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__parse__missing_mass(self): """ Test for string missing mass value. """ string = "-f 10.1" expected_message = "mass was not parsed from ({0})".format(string) with self.assertRaises(ValueError) as context: self.parser.parse(string) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__format_date__overflow_error(self): """ Test for a date that cannot be converted to C long. """ date = (9999999999,0,0,0,0,0,0,0,0) expected_message = ("date ({0}) cannot be ".format(date) + "converted to MM/DD/YYYY ") with self.assertRaises(OverflowError) as context: self._converter._format_date(date) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__format_date__type_error(self): """ Test for a date that is invalid. """ date = "invalid date type" expected_message = ("date ({0}) cannot be ".format(date) + "converted to MM/DD/YYYY ") with self.assertRaises(TypeError) as context: self._converter._format_date(date) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__format_date__value_error(self): """ Test for a date that is out of range. """ date = (1000,0,0,0,0,0,0,0,0) expected_message = ("date ({0}) cannot be ".format(date) + "converted to MM/DD/YYYY ") with self.assertRaises(ValueError) as context: self._converter._format_date(date) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__format_amount__invalid_debit_account(self): """ Test for a debit account that is invalid and does not exist. """ debit = "non-existant account" amount = "10.00" expected_message = "account key ({0}) does not exist".format(debit) with self.assertRaises(KeyError) as context: self._converter._format_amount(debit, amount) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__format_amount__invalid_amount(self): """ Test for an amount that cannot be converted to float. """ debit = self._ASSETS.keys()[0] amount = "5.01a" expected_message = "amount ({0}) cannot be converted to float".format(amount) with self.assertRaises(ValueError) as context: self._converter._format_amount(debit, amount) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__login__invalid_mailbox(self): mailbox = "invaild_mailbox" imap = mail.ImapMail(mailbox, imapconfig.timeout) imap.connect(imapconfig.server) with self.assertRaises(ValueError) as context: imap.login(imapconfig.user, imapconfig.password) actual_errmsg = exception.get_message(context.exception) expected_errmsg = mail._ERRMSG_INVALID_MAILBOX.format( mailbox) self.assertEqual(expected_errmsg, actual_errmsg)
def test__parse__amount_is_not_a_number(self): """ Test that the method throws when amount is not a number. """ amount = "35b" string = "{0} phone bank phone bill".format(amount) expected_message = "amount ({0}) is not a number".format(amount) with self.assertRaises(ValueError) as context: self.parser.parse(string) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__init__empty_password(self): """ Test exception is thrown if given email password is empty. """ with self.assertRaises(ValueError) as context: actionretriever.MailRetriever("", # todo: replace with MockMail self._mock_server, self._mock_address, "") expected_message = actionretriever.MailRetriever._ERROR_EMPTY_PASSWORD actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__do_action__action_type_unknown(self): """ Test for an action_type that is unknown. """ action_type = "unknown action key" data = "sample data" date = time.localtime() expected_message = "action type ({0}) is unknown".format(action_type) with self.assertRaises(KeyError) as context: self._action_manager.do_action(action_type, data, date) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__get_transfer_account__account_key_not_in_accounts(self): """ Test for accounts with associated QIF files but with no associated account. """ debit = "invalid account key" credit = self._FILES.keys()[0] expected_message = ( "account key ({0}) ".format(debit) + "does not have has an associated QIF account") with self.assertRaises(KeyError) as context: self._converter._get_transfer_account(debit, credit) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__get_transfer_account__both_have_no_file(self): """ Test for a invalid debit key (that has no associated file) and an invalid credit key (that has no associated file). """ debit = self._ACCOUNTS_WITHOUT_FILE.keys()[0] credit = self._ACCOUNTS_WITHOUT_FILE.keys()[1] expected_message = ( "neither debit key ({0}) ".format(debit) + "or credit key ({0}) ".format(credit) + "has an associated QIF file") with self.assertRaises(KeyError) as context: self._converter._get_transfer_account(debit, credit) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)
def test__init__key_in_files_is_not_in_accounts(self): """ Test to ensure that all keys in files must exist in accounts. """ extra_key = "filekey" extra_file = filesystem.MockFile("./", "filekey.qif") files = dict( self._FILES.items() + [(extra_key, extra_file)]) expected_message = ( "account key ({0}) ".format(extra_key) + "for file ({0}) ".format(extra_file.filename) + "does not have has an associated QIF account") with self.assertRaises(ValueError) as context: transactionconverter.QifConverter( self._log, files, self._ACCOUNT_TYPES, self._ASSETS, self._LIABILITIES, self._REVENUES, self._EXPENSES) actual_message = exception.get_message(context.exception) self.assertEqual(expected_message, actual_message)