Test for helper_sql #2196
|
@ -2,6 +2,7 @@ coverage
|
||||||
psutil
|
psutil
|
||||||
pycryptodome
|
pycryptodome
|
||||||
PyQt5;python_version>="3.7"
|
PyQt5;python_version>="3.7"
|
||||||
|
mock;python_version<="2.7"
|
||||||
python_prctl;platform_system=="Linux"
|
python_prctl;platform_system=="Linux"
|
||||||
six
|
six
|
||||||
xvfbwrapper;platform_system=="Linux"
|
xvfbwrapper;platform_system=="Linux"
|
||||||
|
|
131
src/tests/test_helper_sql.py
Normal file
|
@ -0,0 +1,131 @@
|
||||||
|
|||||||
|
"""Test cases for helper_sql"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
import unittest
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
try:
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
# Python 3
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
from unittest.mock import patch
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
except ImportError:
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
# Python 2
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
from mock import patch
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
import pybitmessage.helper_sql as helper_sql
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
class TestHelperSql(unittest.TestCase):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test class for helper_sql"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@classmethod
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def setUpClass(cls):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
helper_sql.sql_available = True
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlSubmitQueue.put")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlReturnQueue.get")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def test_sqlquery_no_args(self, mock_sqlreturnqueue_get, mock_sqlsubmitqueue_put):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test sqlQuery with no additional arguments"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
mock_sqlreturnqueue_get.return_value = ("dummy_result", None)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
result = helper_sql.sqlQuery(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"SELECT msgid FROM inbox where folder='inbox' ORDER BY received"
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(mock_sqlsubmitqueue_put.call_count, 2)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(result, "dummy_result")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlSubmitQueue.put")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlReturnQueue.get")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def test_sqlquery_with_args(self, mock_sqlreturnqueue_get, mock_sqlsubmitqueue_put):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test sqlQuery with additional arguments"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
mock_sqlreturnqueue_get.return_value = ("dummy_result", None)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
result = helper_sql.sqlQuery(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"SELECT address FROM addressbook WHERE address=?", "PB-5yfds868gbkj"
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(mock_sqlsubmitqueue_put.call_count, 2)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(result, "dummy_result")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlSubmitQueue.put")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlReturnQueue.get")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def test_sqlexecute(self, mock_sqlreturnqueue_get, mock_sqlsubmitqueue_put):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test sqlExecute with valid arguments"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
mock_sqlreturnqueue_get.return_value = (None, 1)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
rowcount = helper_sql.sqlExecute(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"UPDATE sent SET status = 'msgqueued'"
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"WHERE ackdata = ? AND folder = 'sent'",
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"1710652313",
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(mock_sqlsubmitqueue_put.call_count, 3)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(rowcount, 1)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.SqlBulkExecute.execute")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def test_sqlexecute_script(self, mock_execute):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test sqlExecuteScript with a SQL script"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
helper_sql.sqlExecuteScript(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"CREATE TABLE test (id INTEGER); INSERT INTO test VALUES (1);"
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertTrue(mock_execute.assert_called)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlSubmitQueue.put")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"pybitmessage.helper_sql.sqlReturnQueue.get",
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def test_sqlexecute_chunked(self, mock_sqlreturnqueue_get, mock_sqlsubmitqueue_put):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test sqlExecuteChunked with valid arguments"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
# side_effect is list of return value (_, rowcount)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
# of sqlReturnQueue.get for each chunk
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
CHUNK_COUNT = 6
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
CHUNK_SIZE = 999
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
ID_COUNT = CHUNK_COUNT * CHUNK_SIZE
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
CHUNKS_ROWCOUNT_LIST = [50, 29, 28, 18, 678, 900]
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
TOTAL_ROW_COUNT = sum(CHUNKS_ROWCOUNT_LIST)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
mock_sqlreturnqueue_get.side_effect = [(None, rowcount) for rowcount in CHUNKS_ROWCOUNT_LIST]
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
args = []
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
for i in range(0, ID_COUNT):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
args.append("arg{}".format(i))
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
total_row_count_return = helper_sql.sqlExecuteChunked(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"INSERT INTO table VALUES {}", ID_COUNT, *args
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(TOTAL_ROW_COUNT, total_row_count_return)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertTrue(mock_sqlsubmitqueue_put.called)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertTrue(mock_sqlreturnqueue_get.called)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlSubmitQueue.put")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlReturnQueue.get")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def test_sqlexecute_chunked_with_idcount_zero(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self, mock_sqlreturnqueue_get, mock_sqlsubmitqueue_put
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test sqlExecuteChunked with id count 0"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
ID_COUNT = 0
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
args = list()
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
for i in range(0, ID_COUNT):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
args.append("arg{}".format(i))
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
total_row_count = helper_sql.sqlExecuteChunked(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"INSERT INTO table VALUES {}", ID_COUNT, *args
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(total_row_count, 0)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertFalse(mock_sqlsubmitqueue_put.called)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertFalse(mock_sqlreturnqueue_get.called)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlSubmitQueue.put")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlReturnQueue.get")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def test_sqlexecute_chunked_with_args_less(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self, mock_sqlreturnqueue_get, mock_sqlsubmitqueue_put
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test sqlExecuteChunked with length of args less than idcount"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
ID_COUNT = 12
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
args = ["args0", "arg1"]
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
total_row_count = helper_sql.sqlExecuteChunked(
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"INSERT INTO table VALUES {}", ID_COUNT, *args
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertEqual(total_row_count, 0)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertFalse(mock_sqlsubmitqueue_put.called)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertFalse(mock_sqlreturnqueue_get.called)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlSubmitQueue.put")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@patch("pybitmessage.helper_sql.sqlSubmitQueue.task_done")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def test_sqlstored_procedure(self, mock_task_done, mock_sqlsubmitqueue_put):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
"""Test sqlStoredProcedure with a stored procedure name"""
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
helper_sql.sqlStoredProcedure("exit")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
self.assertTrue(mock_task_done.called_once)
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
mock_sqlsubmitqueue_put.assert_called_with("terminate")
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
@classmethod
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
def tearDownClass(cls):
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
|||||||
|
helper_sql.sql_available = False
|
||||||
No magic values (10, 20, 30) No magic values (10, 20, 30)
|
No magic values (10, 20, 30)
No magic values (10, 20, 30)