Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
ToggleAutocompleteCommand,
ToggleVerboseCommand,
)
from tests.util import fake_cursor
from tests.util import fake_connect


class ReadFileCommandTest(TestCase):
Expand Down Expand Up @@ -130,6 +130,7 @@ def test_toggle_output(self, fake_cmd):
self.assertEqual(fake_cmd.reconnect.call_count, 1)


@patch('crate.crash.command.connect', fake_connect())
class ShowTablesCommandTest(TestCase):

def test_post_2_0(self):
Expand Down Expand Up @@ -267,7 +268,7 @@ def test_check_command_with_node_check(self, cmd):
cmd.logger.info.assert_called_with('NODE CHECK OK')


@patch('crate.client.connection.Cursor', fake_cursor())
@patch('crate.crash.command.connect', fake_connect())
class CommentsTest(TestCase):

def test_sql_comments(self):
Expand Down Expand Up @@ -364,7 +365,7 @@ def test_exec_type_with_line_comment(self):
self.assertIn("SELECT 1 row in set", cmd.logger.info.call_args[0][0])


@patch('crate.client.connection.Cursor', fake_cursor())
@patch('crate.crash.command.connect', fake_connect())
class MultipleStatementsTest(TestCase):

def test_single_line_multiple_sql_statements(self):
Expand Down
9 changes: 7 additions & 2 deletions tests/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import re
from unittest import TestCase
from unittest.mock import patch

from prompt_toolkit.buffer import Buffer
from prompt_toolkit.document import Document
Expand All @@ -31,12 +32,14 @@
_get_toolbar_tokens,
create_buffer,
)
from tests.util import fake_connect


class SQLCompleterTest(TestCase):

def setUp(self):
cmd = CrateShell()
with patch('crate.crash.command.connect', fake_connect()):
cmd = CrateShell()
self.completer = SQLCompleter(cmd)

def test_get_builtin_command_completions(self):
Expand All @@ -48,6 +51,7 @@ def test_get_command_completions_format(self):
self.assertEqual(result, ['dynamic'])


@patch('crate.crash.command.connect', fake_connect())
class CrashBufferTest(TestCase):

def test_create_buffer(self):
Expand All @@ -59,7 +63,8 @@ def test_create_buffer(self):
class AutoCapitalizeTest(TestCase):

def setUp(self):
cmd = CrateShell()
with patch('crate.crash.command.connect', fake_connect()):
cmd = CrateShell()
self.capitalizer = Capitalizer(cmd, SQLCompleter(cmd))

def test_capitalize(self):
Expand Down
26 changes: 26 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import Mock

from verlib2 import Version

from crate.client.cursor import Cursor
from crate.client.exceptions import ConnectionError


def mocked_cursor(description, records, duration=0.1):
Expand All @@ -21,3 +24,26 @@ def fake_cursor():
that just works if you do not care about results.
"""
return mocked_cursor(description=[('undef',)], records=[('undef', None)])


def fake_connect():
"""
Provide a mocked replacement for `crate.crash.command.connect`, so that
instantiating a `CrateShell` in unit tests never opens real network
connections to `localhost:4200`.

The mock behaves like a connection to an unreachable server: the reported
server version 0.0.0 makes `CrateShell.is_conn_available()` return False,
and `cursor.execute()` raises `ConnectionError`. Tests that need a
specific version set `cmd.connection.lowest_server_version` themselves;
each `connect()` call yields a fresh connection, so such changes do not
leak between tests.
"""
def make_connection(*args, **kwargs):
cursor = fake_cursor()()
cursor.execute.side_effect = ConnectionError('mocked connection: no server available')
connection = Mock(name='fake_connection')
connection.lowest_server_version = Version('0.0.0')
connection.cursor.return_value = cursor
return connection
return Mock(name='fake_connect', side_effect=make_connection)
Loading