Commit 3c36ec18 authored by Hamaker, Alec's avatar Hamaker, Alec
Browse files

Merge branch 'develop' into 'main'

Merge develop into main

See merge request !16
parents 0c553582 b52ea080
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -4,3 +4,8 @@
build/
__pycache__/
*.egg-info/

.DS_Store
src/.DS_Store
# ignore backup files
*.bak

COMMON_VERSION

0 → 100644
+1 −0
Original line number Diff line number Diff line
0.1.15
 No newline at end of file
+1 −6
Original line number Diff line number Diff line
FROM python:3.9-slim
FROM --platform=linux/amd64 python:3.9-slim

RUN apt-get -yqq update && apt-get -yqq upgrade && \
      apt-get -yqq install vim && apt-get -yqq install postgresql-client
@@ -9,9 +9,4 @@ COPY src/ tmp/src
RUN set -eux && pip install --upgrade pip && \
      cd /tmp/src/ && pip install -e . && \
      pip install pytest pytest-cov


COPY ./test/test.py /test/test.py


CMD ['sh', 'tail', '-f', '/dev/null']
+80 −3
Original line number Diff line number Diff line
@@ -7,10 +7,9 @@ Everything is configurable with environmental variables.

### Installation via pip

Run this command to install version 0.1.11 (latest) via pip:

`python3 -m pip --no-cache-dir install common==0.1.11 --index-url https://code.ornl.gov/api/v4/projects/10568/packages/pypi/simple  --trusted-host code.ornl.gov`
Run this command to install version 0.1.15 (latest) via pip:

`python3 -m pip --no-cache-dir install common==0.1.15 --index-url https://code.ornl.gov/api/v4/projects/10568/packages/pypi/simple  --trusted-host code.ornl.gov`
This will install latest (not recommended):

`python3 -m pip --no-cache-dir install common --index-url https://code.ornl.gov/api/v4/projects/10568/packages/pypi/simple  --trusted-host code.ornl.gov`
@@ -21,6 +20,9 @@ A docker image and python package are provided for use as a base image in `docke

`common_package/sql/` contains the .sql files `create_tables.sql` and `fill_tables.sql` which serve as examples for how to initialize a postgres Database in your project (See below in the Creating an extensible Database Python Class section for details in how to create the Python Class.) The `volumes` statement in the docker-compose files runs these sql files once the container is created.

## Releases

To initiate a release, increment the value in `COMMON_VERSION` run `./release.sh release`. This will force checkout develop and run the release process to push to the package registry and container registry.

### Configuration via Environment Variables
Below is a list of environmental variables and what they do:
@@ -54,6 +56,81 @@ with PostgresDatabase() as db:
    db.query('SELECT NOW()')
```

### Creating a CRUD table with crud_table.py
```
from common.mixins.postgres import PostgresMixin
from common.database import Database
from common.crud_table import CRUDTable

# PLEASE NOTE: the name of class MUST be consistent with the name of the
# table in the database itself.
class users(CRUDTable):
    """Provides CRUD access to the users table"""
    def __init__(self, db):
    """__init__:

    :param db: the database that contains the 'users' table.
    """
    # be sure to define the columns as they appear in the database.
    # in this example, the users table has 6 columns.
    columns = {
        'id': int,
        'email': str,
        'password': str,
        'roles': str,
        'token': str,
        'is_active': bool
    }
    # initialize the super class with the column definition, the schema that
    # the table is in, and the database object.
    super().__init__(columns, schema='public', db)

    # TODO add additional functions for your class here, specific to your needs


class PostgresDatabase(Database, PostgresMixin):
    """The class that allows Database to interact with psycopg2."""
    def __init__(self):
        super().__init__()
        # here we are assigning the CRUD table to the database's 'users'
        # variable and passing the database object to the constructor
        self.users = users(self)
    # TODO add additional functions for your class here, specific to your needs

with PostgresDatabase() as db:
    db.query('SELECT NOW()')

    # this will create a user in the database, with the specified fields and
    # null for the columns not specified
    db.users.create(id=1337, email='admin@test.com', password='*****',
                    is_active=True)

    # this will read the entire user's table 'SELECT * FROM public.users;'
    users_table = db.users.read()

    # this will read only id, and email columns from the users table
    # 'SELECT id, email FROM public.users;'
    columns_users_table = db.users.read(columns=['id', 'email'])

    # data can be read from the database in 'json' format by specifying
    # json=True in your call. The 'json' format returned is a python dictionary
    # that is json serializable
    json_data = db.users.read(json=True)

    # where clauses can be passed as keyword arguments to the call
    # 'SELECT * FROM public.users WHERE id = 1337;
    filtered_users_table = db.users.read(id=1337)

    # to update entries, a where clause must be passed as follows
    # 'UPDATE public.users SET email='update@test.com' WHERE id=1337;'
    db.users.update(where={'id': 1337}, email='update@test.com')

    # to delete entries, a where clause must also be passed. The were clause
    # for a deletion should come in the form of keyword arguments
    # 'DELETE FROM public.users WHERE id=1337;'
    db.users.delete(id=1337)
```

## Testing

Testing for the `common_package` is contained within `/common_package/test/test.py`. This can be accessed by building the test docker compose file with:
+3 −3
Original line number Diff line number Diff line
@@ -7,10 +7,10 @@ services:
      - ./src/:/tmp/src
      - ./test/:/test/
    env_file:
      #- ./envfile_mssql
      - ./envfile
      - ./test_envfile
    #command: ["sh", "-c", "pytest --cov=common /test/test.py"]
    command: ["sh", "-c", "test-db-conn && pytest -v --cov=common /test/test.py --cov-report term-missing"]
    #command: ["sh", "-c", "test-db-conn && pytest -v --cov=common /test/test.py"]
    command: ["python3", "/test/test.py"]
    depends_on:
      - postgres

Loading