-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for 'wait_for_db' command handling database availability
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
Test custom Django management commands. | ||
""" | ||
from unittest.mock import patch | ||
|
||
from psycopg2 import OperationalError as Psycopg2Error | ||
|
||
from django.core.management import call_command | ||
from django.db.utils import OperationalError | ||
from django.test import SimpleTestCase | ||
|
||
|
||
@patch('core.management.commands.wait_for_db.Command.check') | ||
class CommandTests(SimpleTestCase): | ||
"""Test commands.""" | ||
|
||
def test_wait_for_db_ready(self, patched_check): | ||
"""Test waiting for database if database ready.""" | ||
patched_check.return_value = True | ||
|
||
call_command('wait_for_db') | ||
|
||
patched_check.assert_called_once_with(database=['default']) | ||
|
||
@patch('time.sleep') | ||
def test_wait_for_db_delay(self, patched_sleep, patched_check): | ||
"""Test waiting for database when getting OperationalError""" | ||
patched_check.side_effect = [Psycopg2Error] * 2 + \ | ||
[OperationalError] * 3 + [True] | ||
|
||
call_command('wait_for_db') | ||
|
||
self.assertEqual(patched_check.call_count, 6) | ||
patched_check.assert_called_with(database=['default']) |