Skip to content

Commit

Permalink
chore(set-up-postgres): Set up postgres database
Browse files Browse the repository at this point in the history
- stop tracking django migrations by adding all migration folders to git
- update requrements.txt with django-environ and psycopg2
- add .env file with local db configuration
- add database settings in settings.py
- add unit tests for database connection and vendor verification
- update .travis.yml
- update the README.md documentation file

[Starts #161967003]
  • Loading branch information
babbageLabs authored and EmmanuelChayu committed Dec 6, 2018
1 parent e9003b3 commit 9ff6fff
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ ENV/

# SQLite3
db.sqlite3

authors/apps/*/migrations/*
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ python:
- "3.5"
- "3.6"

services:
- postgresql

before_script:
- psql -c 'create database ah_jumanji;' -U postgres

# Define env variables
env:
- DJANGO_SETTINGS_MODULE="authors.settings"
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,18 @@ No additional parameters required
### Get Tags

`GET /api/tags`

## Setting up the application
```
git clone https://github.com/andela/ah-jumanji.git
cd ah-jumanji
git checkout develop
virtualenv venv
source ./bin/activate
pip install -r requirements.txt
touch .env
echo "DATABASE_URL=postgres://username:password@host/db_name" >> .env
echo "DEBUG=on" >> .env
python manage.py migrate
python manage.py runserver
```
Empty file.
20 changes: 20 additions & 0 deletions authors/apps/core/tests/test_db_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
from django.db import connection


@pytest.mark.django_db
class TestDatabaseConnection:
"""Test cases -for db connection"""
db_connection = connection

def test_db_is_connected(self):
"""
asserts that a database cursor can be opened
:return:
"""
cursor = self.db_connection.cursor()
assert cursor.closed is False

def test_db_vendor_is_postgres(self):
"""Verify the databases vendors name"""
assert self.db_connection.vendor == "postgresql"
16 changes: 12 additions & 4 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import os
import logging
import logging.config

import environ
from django.utils.log import DEFAULT_LOGGING

# Get an instance of a logger
Expand All @@ -18,6 +20,14 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# configure the external environment
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
# read the .env file
environ.Env.read_env(env_file=os.path.join(BASE_DIR, '.env'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

Expand Down Expand Up @@ -84,10 +94,8 @@
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
# read the database environ
'default': env.db()
}

# Password validation
Expand Down
1 change: 1 addition & 0 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys

if __name__ == "__main__":

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "authors.settings")
try:
from django.core.management import execute_from_command_line
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ coverage==4.5.2
coveralls==1.5.1
Django==2.1.3
django-cors-middleware==1.3.1
django-environ==0.4.5
django-extensions==2.1.4
django-pytest==0.2.0
django-rest-swagger==2.2.0
Expand All @@ -25,6 +26,7 @@ mccabe==0.6.1
more-itertools==4.3.0
openapi-codec==1.3.2
pluggy==0.8.0
psycopg2-binary==2.7.6.1
py==1.7.0
pycodestyle==2.4.0
pyflakes==2.0.0
Expand Down

0 comments on commit 9ff6fff

Please sign in to comment.