-
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.
added wait_for_db command and updated docker files
- Loading branch information
1 parent
f52d9e2
commit d02f7f4
Showing
17 changed files
with
146 additions
and
7 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
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
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,12 @@ | ||
""" | ||
Calculator functions | ||
""" | ||
|
||
|
||
def add(x, y): | ||
"""Add x and y and return result.""" | ||
return x + y | ||
|
||
|
||
def subtract(x, y): | ||
return y - x |
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
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,22 @@ | ||
""" | ||
Sample tests | ||
""" | ||
from django.test import SimpleTestCase | ||
|
||
from app import calc | ||
|
||
|
||
class CalcTests(SimpleTestCase): | ||
"""Test the calc module""" | ||
|
||
def test_add_numbers(self): | ||
"""Test adding numbers together""" | ||
res = calc.add(5, 6) | ||
|
||
self.assertEqual(res, 11) | ||
|
||
def test_substract_numbers(self): | ||
"""Test subtracting numbers""" | ||
res = calc.subtract(10, 15) | ||
|
||
self.assertEqual(res, 5) |
Empty file.
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,3 @@ | ||
from django.contrib import admin # noqa | ||
|
||
# Register your models here. |
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CoreConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'core' |
Empty file.
Empty file.
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,27 @@ | ||
""" | ||
Django command to wait for the database to be available. | ||
""" | ||
import time | ||
|
||
from psycopg2 import OperationalError as Psycopg2Error | ||
|
||
from django.db.utils import OperationalError | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Django command to wait for database.""" | ||
|
||
def handle(self, *args, **options): | ||
"""Entrypoint for command""" | ||
self.stdout.write("Waiting for database...") | ||
db_up = False | ||
while db_up is False: | ||
try: | ||
self.check(databases=['default']) | ||
db_up = True | ||
except (Psycopg2Error, OperationalError): | ||
self.stdout.write('Database unavailable, waiting 1 second...') | ||
time.sleep(1) | ||
|
||
self.stdout.write(self.style.SUCCESS('Database available!')) |
Empty file.
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,3 @@ | ||
from django.db import models # noqa | ||
|
||
# Create your models here. |
Empty file.
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,33 @@ | ||
""" | ||
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 CommandsTestS(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(databases=['default']) | ||
|
||
@patch('time.sleep') | ||
def test_wait_for_db_delay(self, patched_sleep, patched_check): | ||
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(databases=['default']) |
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Django>=3.2.4,<3.3 | ||
djangorestframework>=3.12.4,<3.13 | ||
djangorestframework>=3.12.4,<3.13 | ||
psycopg2>=2.8.6,<2.11 |