From 0ec15d1d4da380fc59c4231cde97036a8e67f555 Mon Sep 17 00:00:00 2001 From: navjot Date: Sat, 7 Nov 2020 16:11:29 +0530 Subject: [PATCH] Test case for insert method default values --- src/helper_sent.py | 44 ++++++++++++++++++++++---------------------- src/tests/core.py | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/helper_sent.py b/src/helper_sent.py index 15e5c506..33cc770c 100644 --- a/src/helper_sent.py +++ b/src/helper_sent.py @@ -12,36 +12,36 @@ from helper_sql import sqlExecute def insert(t, is_testcase=False): """Perform an insert into the `sent` table""" - all_fields_availabe = all([True if i else False for i in t]) - if not all_fields_availabe: + # pylint: disable=unused-variable + + if '' in t or 0 in t: temp = list(t) - if not temp[0]: - temp[0] = uuid.uuid4().bytes # if msgid is empty the put uuid + temp_dict = { + 0: uuid.uuid4().bytes, # if msgid is empty the put uuid + 7: int(time.time()), # sentTime + 8: int(time.time()), # lastActionTime + 9: 0, # sleeptill + 10: 'msgqueued', + 11: 0, # retryNumber + 12: 'sent', # folder + 13: 2, # encoding + 14: BMConfigParser().getint('bitmessagesettings', 'ttl') + } + if not temp[2] or not temp[6]: status, addressVersionNumber, streamNumber, ripe = decodeAddress(temp[1]) if not temp[2]: - temp[2] = ripe + temp_dict[2] = ripe + if not temp[6]: stealthLevel = BMConfigParser().safeGetInt( 'bitmessagesettings', 'ackstealthlevel') ackdata = genAckPayload(streamNumber, stealthLevel) - temp[6] = ackdata - if not temp[7]: # sentTime - temp[7] = int(time.time()) - if not temp[8]: # lastActionTime - temp[8] = int(time.time()) - if not temp[9] and temp[9] != 0: # sleeptill - temp[9] = 0 - if not temp[10]: - temp[10] = 'msgqueued' - if not temp[11] and temp[11] != 0: - temp[11] = 0 - if not temp[12]: - temp[12] = 'sent' - if not temp[13]: - temp[13] = 2 # checking encoding - if not temp[14]: - temp[14] = BMConfigParser().getint('bitmessagesettings', 'ttl') + temp_dict[6] = ackdata + + for i in dict(enumerate(temp)): + if not temp[i]: + temp[i] = temp_dict[i] t = tuple(temp) sqlExecute('''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', *t) return t if is_testcase else None diff --git a/src/tests/core.py b/src/tests/core.py index da0673d2..345bab64 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -265,6 +265,31 @@ class TestCore(unittest.TestCase): result = helper_sent.insert(t, True) self.assertNotEqual(result[0], '') + def test_defalut_insert_values(self): + """Test missing values in insert method for message sending""" + fromAddress = 'BM-2cTrmD22fLRrumi3pPLg1ELJ6PdAaTRTdfg' + toAddress = 'BM-2cVWtdUzPwF7UNGDrZftWuHWgjdfkj89fdf' + message = 'test message' + subject = 'test subject' + t = ('', # msgid + toAddress, + '', # ripe + fromAddress, + subject, + message, + '', # ackdata + '', # sentTime + '', # lastActionTime + '', # sleeptill + '', # status + '', # retryNumber + '', # folder + '', # encoding + '') # ttl + result = helper_sent.insert(t, True) + for index in [0, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14]: + self.assertNotEqual(result[index], '') + def run(): """Starts all tests defined in this module"""