Skip to content

Commit

Permalink
tests for DB connect
Browse files Browse the repository at this point in the history
  • Loading branch information
rotem123456 committed Jun 30, 2024
1 parent 4cb9c42 commit 1766c32
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 4 deletions.
9 changes: 7 additions & 2 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -37,6 +38,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -75,8 +77,11 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ.get('DB_HOST'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS')
}
}

Expand Down
Empty file added app/core/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions app/core/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions app/core/apps.py
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 added app/core/management/__init__.py
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions app/core/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Django command for the database to be available.
"""

from typing import Any
from django.core.management import BaseCommand


class Command(BaseCommand):
"""Django command to wait for database"""

def handle(self, *args: Any, **options: Any):
pass
Empty file added app/core/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions app/core/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
Empty file added app/core/tests/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions app/core/tests/test_commands.py
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 Psycopg2OpError

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(databases=['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 = [Psycopg2OpError] * 2 + \
[OperationalError] * 3 + [True]

call_command('wait_for_db')

self.assertEqual(patched_check.call_count, 6)
patched_check.assert_called_with(databases=['default'])
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- DEV=true
ports:
- "8000:8000"
volumes:
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Django>=3.2.0,<3.3
djangorestframework>=3.12.4,<3.13
djangorestframework>=3.12.4,<3.13
psycopg2>=2.8.6,<2.9

0 comments on commit 1766c32

Please sign in to comment.