Skip to content

Commit

Permalink
testy release 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kozlov committed Dec 1, 2022
1 parent c2f546e commit b0889f6
Show file tree
Hide file tree
Showing 172 changed files with 9,241 additions and 189 deletions.
15 changes: 15 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.pyc
__pycache__/
/venv/
/static/
/media/
/pg-data/
.env
db.sqlite3
.DS_Store
.idea
.coverage
.vscode
node_modules/
testy_static/dist/*.html
package-lock.json
11 changes: 11 additions & 0 deletions .env.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
POSTGRES_NAME=postgres
POSTGRES_USER=postgres
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_SERVICE_HOST=127.0.0.1
POSTGRES_SERVICE_PORT=5435

COMPANY_DOMAIN=example.com

SUPERUSER_USERNAME=admin
SUPERUSER_PASSWORD=password
15 changes: 15 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
POSTGRES_NAME=postgres
POSTGRES_USER=postgres
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_SERVICE_HOST=db
POSTGRES_SERVICE_PORT=5432

COMPANY_DOMAIN=example.com

SUPERUSER_USERNAME=admin
SUPERUSER_PASSWORD=password

SECRET_KEY=12345

ALLOWED_HOSTS='["127.0.0.1"]'
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.pyc
__pycache__/
/venv/
/static/
/media/
/pg-data/
.env
.env.local
db.sqlite3
.DS_Store
.idea
.coverage
.vscode
node_modules/
testy_static/dist/*.html
package-lock.json
378 changes: 189 additions & 189 deletions LICENSE

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docker-compose-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3.9'
services:
testy-ci:
build:
context: .
dockerfile: testy/scripts/ci/Dockerfile
43 changes: 43 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: '3.9'
services:
testy:
build:
context: testy
dockerfile: scripts/dev/Dockerfile
volumes:
- './testy:/testy'
- './static:/testy/static'
ports:
- "8001:8000"
environment:
- DJANGO_SETTINGS_MODULE=testy.settings.development
- POSTGRES_NAME=${POSTGRES_NAME}
- POSTGRES_USER=${POSTGRES_USERNAME}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_SERVICE_HOST=${POSTGRES_SERVICE_HOST}
- POSTGRES_SERVICE_PORT=${POSTGRES_SERVICE_PORT}
- COMPANY_DOMAIN=${COMPANY_DOMAIN}
- SUPERUSER_USERNAME=${SUPERUSER_USERNAME}
- SUPERUSER_PASSWORD=${SUPERUSER_PASSWORD}
- ALLOWED_HOSTS=${ALLOWED_HOSTS}
- SECRET_KEY=${SECRET_KEY}
depends_on:
db:
condition: service_healthy
db:
image: postgres:14.5-alpine
volumes:
- ./pg-data:/var/lib/postgresql/data
ports:
- "5435:5432"
environment:
- POSTGRES_USER=${POSTGRES_USERNAME}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_NAME}
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 5s
timeout: 5s
retries: 5
volumes:
react_apps:
43 changes: 43 additions & 0 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: '3.9'
services:
testy:
build:
context: testy
dockerfile: scripts/prod/Dockerfile
volumes:
- './testy:/testy'
- './static:/testy/static'
ports:
- "8001:8000"
environment:
- DJANGO_SETTINGS_MODULE=testy.settings.production
- POSTGRES_NAME=${POSTGRES_NAME}
- POSTGRES_USER=${POSTGRES_USERNAME}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_SERVICE_HOST=${POSTGRES_SERVICE_HOST}
- POSTGRES_SERVICE_PORT=${POSTGRES_SERVICE_PORT}
- COMPANY_DOMAIN=${COMPANY_DOMAIN}
- SUPERUSER_USERNAME=${SUPERUSER_USERNAME}
- SUPERUSER_PASSWORD=${SUPERUSER_PASSWORD}
- ALLOWED_HOSTS=${ALLOWED_HOSTS}
- SECRET_KEY=${SECRET_KEY}
depends_on:
db:
condition: service_healthy
db:
image: postgres:14.5-alpine
volumes:
- ./pg-data:/var/lib/postgresql/data
ports:
- "5435:5432"
environment:
- POSTGRES_USER=${POSTGRES_USERNAME}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_NAME}
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 5s
timeout: 5s
retries: 5
volumes:
react_apps:
31 changes: 31 additions & 0 deletions linter-ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash


fails=""

inspect() {
# Usage: inspect $? lint
# * param1: job result code
# * param2: job name
if [ $1 -ne 0 ]; then
fails="${fails} $2"
fi
}

lint() {
docker-compose -f docker-compose-ci.yml up -d --build
docker-compose -f docker-compose-ci.yml run --rm testy-ci flake8 .
inspect $? lint
docker-compose -f docker-compose-ci.yml down -v
}

echo "Running linter!"
lint

if [ -n "${fails}" ]; then
echo "Linter failed: ${fails}"
exit 1
else
echo "Linter passed!"
exit 0
fi
60 changes: 60 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[tool:pytest]
DJANGO_SETTINGS_MODULE = testy.settings.development
testpaths = tests
pythonpath = testy
filterwarnings =
ignore::DeprecationWarning
ignore::pytest.PytestCollectionWarning

[flake8]
show-source = True
max-line-length = 120
exclude =
.git
__pycache__
venv
migrations
node_modules

# settings for flake8_copyright_validator
lines-to-exclude =
'# Generated by Django'
'#!'

copyright-text =
'# TestY TMS - Test Management System'
'# Copyright (C) 2022 KNS Group LLC (YADRO)'
'#'
'# This program is free software: you can redistribute it and/or modify'
'# it under the terms of the GNU Affero General Public License as published'
'# by the Free Software Foundation, either version 3 of the License, or'
'# (at your option) any later version.'
'#'
'# This program is distributed in the hope that it will be useful,'
'# but WITHOUT ANY WARRANTY; without even the implied warranty of'
'# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the'
'# GNU Affero General Public License for more details.'
'#'
'# You should have received a copy of the GNU Affero General Public License'
'# along with this program. If not, see <http://www.gnu.org/licenses/>.'
'#'
'# Also add information on how to contact you by electronic and paper mail.'
'#'
'# If your software can interact with users remotely through a computer'
'# network, you should also make sure that it provides a way for users to'
'# get its source. For example, if your program is a web application, its'
'# interface could display a "Source" link that leads users to an archive'
'# of the code. There are many ways you could offer source, and different'
'# solutions will be better for different programs; see section 13 for the'
'# specific requirements.'
'#'
'# You should also get your employer (if you work as a programmer) or school,'
'# if any, to sign a "copyright disclaimer" for the program, if necessary.'
'# For more information on this, and how to apply and follow the GNU AGPL, see'
'# <http://www.gnu.org/licenses/>.'

[isort]
# https://github.com/timothycrosley/isort#multi-line-output-modes
multi_line_output = 3
include_trailing_comma = true
line_length = 120
30 changes: 30 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# TestY TMS - Test Management System
# Copyright (C) 2022 KNS Group LLC (YADRO)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Also add information on how to contact you by electronic and paper mail.
#
# If your software can interact with users remotely through a computer
# network, you should also make sure that it provides a way for users to
# get its source. For example, if your program is a web application, its
# interface could display a "Source" link that leads users to an archive
# of the code. There are many ways you could offer source, and different
# solutions will be better for different programs; see section 13 for the
# specific requirements.
#
# You should also get your employer (if you work as a programmer) or school,
# if any, to sign a "copyright disclaimer" for the program, if necessary.
# For more information on this, and how to apply and follow the GNU AGPL, see
# <http://www.gnu.org/licenses/>.
93 changes: 93 additions & 0 deletions tests/commons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# TestY TMS - Test Management System
# Copyright (C) 2022 KNS Group LLC (YADRO)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Also add information on how to contact you by electronic and paper mail.
#
# If your software can interact with users remotely through a computer
# network, you should also make sure that it provides a way for users to
# get its source. For example, if your program is a web application, its
# interface could display a "Source" link that leads users to an archive
# of the code. There are many ways you could offer source, and different
# solutions will be better for different programs; see section 13 for the
# specific requirements.
#
# You should also get your employer (if you work as a programmer) or school,
# if any, to sign a "copyright disclaimer" for the program, if necessary.
# For more information on this, and how to apply and follow the GNU AGPL, see
# <http://www.gnu.org/licenses/>.

from enum import Enum
from http import HTTPStatus
from typing import Any, Dict, List, Union

from django.db.models import QuerySet
from django.forms import model_to_dict
from django.test.client import RequestFactory
from rest_framework.reverse import reverse
from rest_framework.test import APIClient


class RequestType(Enum):
POST = 'post'
GET = 'get'
PUT = 'put'
PATCH = 'patch'
DELETE = 'delete'


class RequestMock(RequestFactory):
GET = {}

@staticmethod
def build_absolute_uri(url):
return f'http://testserver{url}'


class CustomAPIClient(APIClient):
def send_request(
self,
view_name: str,
data: Dict[str, Any] = None,
expected_status: HTTPStatus = HTTPStatus.OK,
request_type: RequestType = RequestType.GET,
reverse_kwargs: Dict[str, Any] = None,
format='json',
query_params: Dict[str, Any] = None
):
url = reverse(view_name, kwargs=reverse_kwargs)
if query_params:
url = f'{url}?{"&".join([f"{field}={field_value}" for field, field_value in query_params.items()])}'
http_request = getattr(self, request_type.value, None)
if not http_request:
raise TypeError('Request type is not known')
response = http_request(url, data=data, format=format)

assert response.status_code == expected_status, f'Expected response code "{expected_status}", ' \
f'actual: "{response.status_code}"' \
f'Response content: {response.content}'
return response


def model_with_base_to_dict(instance) -> Dict[str, Any]:
instance_dict = model_to_dict(instance)
instance_dict['created_at'] = instance.created_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
instance_dict['updated_at'] = instance.updated_at.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
return instance_dict


def model_to_dict_via_serializer(instances: Union[QuerySet, Any], serializer_class, many=False) -> List[dict]:
serializer = serializer_class(instances if many else instances[0], many=many, context={'request': RequestMock()})
return [dict(elem) for elem in serializer.data] if many else dict(serializer.data)
Loading

0 comments on commit b0889f6

Please sign in to comment.