Skip to content

Commit

Permalink
Add postgres database
Browse files Browse the repository at this point in the history
  • Loading branch information
ram8545 committed Oct 11, 2023
1 parent b490ebc commit 598a3e1
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Checkout
uses: actions/Checkout@v2
uses: actions/checkout@v2
- name: Test
run: docker-compose run --rm app sh -c "python manage.py test"
- name: Lint
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.9-alpine3.13
FROM python:3.11-alpine
LABEL maintainer="Ram Sevak Mishra"

ENV PYTHONUNBUFFERED 1
Expand All @@ -16,14 +16,14 @@ ARG DEV=false
RUN python -m venv /py && \
/py/bin/pip install --upgrade pip && \
apk add --update --no-cache postgresql-client && \
apk add --update --no-cache --virtual .tmp-build-dev \
apk add --update --no-cache --virtual .tmp-build-deps \
build-base postgresql-dev musl-dev && \
/py/bin/pip install -r /tmp/requirements.txt && \
if [ $DEV = "true" ]; \
then /py/bin/pip install -r /tmp/requirements.dev.txt ; \
fi && \
rm -rf /tmp && \
apk del .tmp-build-dev && \
apk del .tmp-build-deps && \
adduser \
--disabled-password \
--no-create-home \
Expand Down
4 changes: 3 additions & 1 deletion app/app/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"""


def add(x, y):
"Add x and y values and return result"
return x + y


def sub(x, y):
"Add x and y values and return result"
return x - y
return x - y
2 changes: 1 addition & 1 deletion app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core'
'core',
]

MIDDLEWARE = [
Expand Down
7 changes: 4 additions & 3 deletions app/app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

from . import calc


class calcTests(SimpleTestCase):

def test_add_numbers(self):
res = calc.add(5,6)
res = calc.add(5, 6)

self.assertEqual(res, 11)

def test_subtract_numbers(self):
res = calc.sub(15,10)
res = calc.sub(15, 10)

self.assertEqual(res, 5)
self.assertEqual(res, 5)
3 changes: 1 addition & 2 deletions app/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from django.contrib import admin

# Register your models here.
# Register your models here.test
10 changes: 6 additions & 4 deletions app/core/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"""
import time

from psycopg2 import OperationalError as Psycopg2Error
from psycopg2 import OperationalError as Psycopg2OpError

from django.db.utils import OperationalError

from django.core.management.base import BaseCommand


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

Expand All @@ -20,7 +21,8 @@ def handle(self, *args, **options):
try:
self.check(databases=['default'])
db_up = True
except (Psycopg2Error, OperationalError):
self.stdout.write("Database unavailable, waiting 1 second ......")
except (Psycopg2OpError, OperationalError):
self.stdout.write("Database unavailable, waiting 1 second....")
time.sleep(1)
self.stdout.write(self.style.SUCCESS("Database available!"))

self.stdout.write(self.style.SUCCESS("Database available!"))
1 change: 0 additions & 1 deletion app/core/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from django.db import models

# Create your models here.
7 changes: 2 additions & 5 deletions app/core/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,22 @@

from django.test import SimpleTestCase


@patch('core.management.commands.wait_for_db_command.check')
class CommandTests(SimpleTestCase):
"""Test Command"""

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 = [Psycopg2Error] * 2 + \
[OperationalError] * 3 + [True]

call_command('wait_for_db')

self.assertEqual(patched_check.call_count, 6)
patched_check.assert_called_with(databases=['default'])
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
@@ -1,4 +1,4 @@
version: "3.9"
version: "3.11"

services:
app:
Expand Down
2 changes: 1 addition & 1 deletion requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
flake8>=3.9.2,<3.10
flake8
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Django>=3.2.4,<3.3
djangorestframework>=3.12.4,<3.13
psycopg2>=2.8.6,<2.9
Django
djangorestframework
psycopg2

0 comments on commit 598a3e1

Please sign in to comment.