Skip to content
This repository was archived by the owner on Aug 8, 2020. It is now read-only.

Commit 3f12647

Browse files
author
Tonye Jack
committed
Added initial setup.
1 parent f8166b4 commit 3f12647

22 files changed

+602
-0
lines changed
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: django check constraint test.
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
services:
10+
postgres:
11+
image: postgres:12
12+
env:
13+
POSTGRES_USER: test_user
14+
POSTGRES_PASSWORD: ''
15+
POSTGRES_DB: test
16+
ports:
17+
- 5432:5432
18+
# needed because the postgres container does not provide a healthcheck
19+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
20+
mysql:
21+
image: mysql:8.0.18
22+
env:
23+
MYSQL_ROOT_PASSWORD: password
24+
ports:
25+
- 3306:3306
26+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
27+
28+
steps:
29+
- uses: actions/checkout@v1
30+
- name: Set up Python 3.7
31+
uses: actions/setup-python@v1
32+
with:
33+
python-version: 3.7
34+
- name: Test with tox
35+
run: |
36+
sudo apt-get install -y --no-install-recommends libpq-dev
37+
make nox
38+
env:
39+
# use postgres for the host here because we have specified a container for the job.
40+
# If we were running the job on the VM this would be localhost
41+
POSTGRES_HOST: postgres
42+
POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
43+
POSTGRES_USER: test_user
44+
POSTGRES_DB: test
45+
MYSQL_ROOT_PASSWORD: password
46+
MYSQL_PORT: ${{ job.services.mysql.ports[3306] }}
47+
48+

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
.idea/
2+
*.egg-info/
3+
build/
4+
dist/
5+
__pycache__/
6+
.nox/

Makefile

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Self-Documented Makefile see https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
2+
3+
.DEFAULT_GOAL := help
4+
5+
PYTHON := /usr/bin/env python
6+
PYTHON_VERSION := $(PYTHON) --version
7+
MANAGE_PY := $(PYTHON) manage.py
8+
PYTHON_PIP := /usr/bin/env pip
9+
PIP_COMPILE := /usr/bin/env pip-compile
10+
PART := patch
11+
PACKAGE_VERSION = $(shell $(PYTHON) setup.py --version)
12+
13+
# Put it first so that "make" without argument is like "make help".
14+
help:
15+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-32s-\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
16+
17+
.PHONY: help
18+
19+
guard-%: ## Checks that env var is set else exits with non 0 mainly used in CI;
20+
@if [ -z '${${*}}' ]; then echo 'Environment variable $* not set' && exit 1; fi
21+
22+
# --------------------------------------------------------
23+
# ------- Python package (pip) management commands -------
24+
# --------------------------------------------------------
25+
26+
clean-build: ## Clean project build artifacts.
27+
@echo "Removing build assets..."
28+
@$(PYTHON) setup.py clean
29+
@rm -rf build/
30+
@rm -rf dist/
31+
@rm -rf *.egg-info
32+
33+
test:
34+
@echo "Running `$(PYTHON_VERSION)` test..."
35+
@$(MANAGE_PY) test
36+
37+
install: clean-build ## Install project dependencies.
38+
@echo "Installing project in dependencies..."
39+
@$(PYTHON_PIP) install -r requirements.txt
40+
41+
install-lint: clean-build ## Install lint extra dependencies.
42+
@echo "Installing lint extra requirements..."
43+
@$(PYTHON_PIP) install -e .'[lint]'
44+
45+
install-test: clean-build clean-test-all ## Install test extra dependencies.
46+
@echo "Installing test extra requirements..."
47+
@$(PYTHON_PIP) install -e .'[test]'
48+
49+
install-dev: clean-build ## Install development extra dependencies.
50+
@echo "Installing development requirements..."
51+
@$(PYTHON_PIP) install -e .'[development]' -r requirements.txt
52+
53+
update-requirements: ## Updates the requirement.txt adding missing package dependencies
54+
@echo "Syncing the package requirements.txt..."
55+
@$(PIP_COMPILE)
56+
57+
tag-build:
58+
@git tag v$(PACKAGE_VERSION)
59+
60+
release-to-pypi: increase-version tag-build ## Release project to pypi
61+
@$(PYTHON_PIP) install -U twine
62+
@$(PYTHON) setup.py sdist bdist_wheel
63+
@twine upload dist/*
64+
65+
66+
# ----------------------------------------------------------
67+
# ---------- Upgrade project version (bumpversion) --------
68+
# ----------------------------------------------------------
69+
increase-version: clean-build guard-PART ## Bump the project version (using the $PART env: defaults to 'patch').
70+
@echo "Increasing project '$(PART)' version..."
71+
@$(PYTHON_PIP) install -q -e .'[deploy]'
72+
@bumpversion --verbose $(PART)
73+
@git-changelog . > CHANGELOG.md
74+
@git commit -am "Updated CHANGELOG.md."
75+
@git push --tags
76+
@git push
77+
78+
# ----------------------------------------------------------
79+
# --------- Run project Test -------------------------------
80+
# ----------------------------------------------------------
81+
nox: install-test ## Run tox test
82+
@nox --report status.json
83+
84+
clean-test-all: clean-build ## Clean build and test assets.
85+
@rm -rf .tox/
86+
@rm -rf test-results
87+
@rm -rf .pytest_cache/
88+
@rm -f test.db
89+
90+
91+
# -----------------------------------------------------------
92+
# --------- Run autopep8 ------------------------------------
93+
# -----------------------------------------------------------
94+
run-autopep8: ## Run autopep8 with inplace for check_constraint package.
95+
@autopep8 -ri check_constraint

check_constraint/__init__.py

Whitespace-only changes.

check_constraint/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CheckConstraintConfig(AppConfig):
5+
name = 'check_constraint'

check_constraint/models.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from django.db import models
2+
3+
from django.db.models.sql import Query
4+
5+
6+
class AnnotatedCheckConstraint(models.CheckConstraint):
7+
def __init__(self, *args, annotations=None, **kwargs):
8+
super().__init__(*args, **kwargs)
9+
self.annotations = annotations or {}
10+
11+
def _get_check_sql(self, model, schema_editor):
12+
query = Query(model=model)
13+
14+
# Add annotations
15+
for k, v in self.annotations.items():
16+
query.add_annotation(v, k)
17+
18+
where = query.build_where(self.check)
19+
20+
compiler = query.get_compiler(connection=schema_editor.connection)
21+
22+
sql, params = where.as_sql(compiler, schema_editor.connection)
23+
24+
return sql % tuple(schema_editor.quote_value(p) for p in params)
25+
26+
def deconstruct(self):
27+
path, args, kwargs = super(models.CheckConstraint, self).deconstruct()
28+
kwargs['check'] = self.check
29+
kwargs['annotations'] = self.annotations
30+
31+
return path, args, kwargs

check_constraint/tests.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
from django.test import TestCase
4+
5+
DATABASES = ['default']
6+
7+
8+
if 'ENV_DB' in os.environ:
9+
DATABASES += [os.environ['ENV_DB']]
10+
11+
class AnnotateCheckConstraintTestCase(TestCase):
12+
databases = DATABASES
13+
def test_dummy_setup(self):
14+
self.assertEqual(1, 1)

demo/__init__.py

Whitespace-only changes.

demo/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+

demo/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class DemoConfig(AppConfig):
5+
name = 'demo'

demo/migrations/__init__.py

Whitespace-only changes.

demo/models.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from django.contrib.auth import get_user_model
2+
from django.db import models
3+
4+
5+
class Books(models.Model):
6+
name = models.CharField(max_length=255)
7+
archived = models.BooleanField(default=False)
8+
created_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
9+
10+
11+
class Library(models.Model):
12+
name = models.CharField(max_length=255)
13+
books = models.ManyToManyField(Books, through='LibraryBooks')
14+
15+
16+
class LibraryBooks(models.Model):
17+
library = models.ForeignKey(
18+
Library,
19+
on_delete=models.CASCADE,
20+
related_name='library_books',
21+
)
22+
books = models.ForeignKey(Books, on_delete=models.PROTECT)

demo/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+

django_check_constraint/__init__.py

Whitespace-only changes.

django_check_constraint/settings.py

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"""
2+
Django settings for django_check_constraint project.
3+
4+
Generated by 'django-admin startproject' using Django 1.11.22.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.11/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/1.11/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '=#9#$u7g843$kr&)g8k_1&qmoa1hx549y^+43w!v0_uzh5stm9'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = [
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
]
41+
42+
MIDDLEWARE = [
43+
'django.middleware.security.SecurityMiddleware',
44+
'django.contrib.sessions.middleware.SessionMiddleware',
45+
'django.middleware.common.CommonMiddleware',
46+
'django.middleware.csrf.CsrfViewMiddleware',
47+
'django.contrib.auth.middleware.AuthenticationMiddleware',
48+
'django.contrib.messages.middleware.MessageMiddleware',
49+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
50+
]
51+
52+
ROOT_URLCONF = 'django_check_constraint.urls'
53+
54+
TEMPLATES = [
55+
{
56+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
57+
'DIRS': [],
58+
'APP_DIRS': True,
59+
'OPTIONS': {
60+
'context_processors': [
61+
'django.template.context_processors.debug',
62+
'django.template.context_processors.request',
63+
'django.contrib.auth.context_processors.auth',
64+
'django.contrib.messages.context_processors.messages',
65+
],
66+
},
67+
},
68+
]
69+
70+
WSGI_APPLICATION = 'django_check_constraint.wsgi.application'
71+
72+
73+
# Database
74+
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
75+
76+
DATABASES = {
77+
'default': {
78+
'ENGINE': 'django.db.backends.sqlite3',
79+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
80+
},
81+
}
82+
83+
84+
_ENV_DB = os.environ.get('ENV_DB')
85+
86+
if _ENV_DB == 'postgres':
87+
DATABASES['postgres'] = {
88+
'ENGINE': 'django.db.backends.postgresql',
89+
'NAME': os.environ.get('POSTGRES_DB', 'test_postgres'),
90+
'USER': os.environ.get('POSTGRES_USER', 'postgres'),
91+
'PASSWORD': os.environ.get('POSTGRES_PASSWORD', ''),
92+
'HOST': 'localhost',
93+
'PORT': '5432',
94+
}
95+
elif _ENV_DB == 'mysql':
96+
DATABASES['mysql'] = {
97+
'ENGINE': 'django.db.backends.mysql',
98+
'HOST': '127.0.0.1',
99+
'NAME': 'root',
100+
'USER': 'root',
101+
'PASSWORD': os.environ.get('MYSQL_ROOT_PASSWORD', ''),
102+
'PORT': '3306',
103+
}
104+
105+
106+
# Password validation
107+
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
108+
109+
AUTH_PASSWORD_VALIDATORS = [
110+
{
111+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
112+
},
113+
{
114+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
115+
},
116+
{
117+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
118+
},
119+
{
120+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
121+
},
122+
]
123+
124+
125+
# Internationalization
126+
# https://docs.djangoproject.com/en/1.11/topics/i18n/
127+
128+
LANGUAGE_CODE = 'en-us'
129+
130+
TIME_ZONE = 'UTC'
131+
132+
USE_I18N = True
133+
134+
USE_L10N = True
135+
136+
USE_TZ = True
137+
138+
139+
# Static files (CSS, JavaScript, Images)
140+
# https://docs.djangoproject.com/en/1.11/howto/static-files/
141+
142+
STATIC_URL = '/static/'

0 commit comments

Comments
 (0)