Skip to content

Commit

Permalink
Sundry (#13)
Browse files Browse the repository at this point in the history
* make mypy config stricter

* Update mypy.ini

* Update uv.lock

* break down Makefile

* bump backend qa tool versions
  • Loading branch information
IndrajeetPatil authored Nov 19, 2024
1 parent 403308f commit 34741b3
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 211 deletions.
133 changes: 21 additions & 112 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Define paths (note that the paths are relative to the Makefile location)
SERVER_DIR=./server
FRONTEND_DIR=./frontend
# Main Makefile for the project

# Detect operating system and shell
ifeq ($(OS),Windows_NT)
Expand All @@ -21,135 +19,46 @@ else
COLOR_BLUE_BG=\033[44m
endif

# Backend tools
RUFF=ruff
MYPY=mypy
PYTEST=pytest chatgptserver --verbose
PYCOVERAGE=coverage run -m pytest chatgptserver && coverage report --fail-under=95 && coverage html
DJANGO_RUNSERVER=chatgptserver/manage.py runserver
OPENAPI_SCHEMA=chatgptserver/manage.py spectacular --color --validate --file schema.yml
LOCUST=locust -H http://127.0.0.1:8000/
# Include layer-specific makefiles
include frontend.mk
include backend.mk

# Frontend tools
ESLINT=npm run lint:fix
PRETTIER=npm run format
TSC=npm run check-types
AUDIT=npm audit --audit-level=moderate
BUILD=npm run build
JEST=npm run test
NEXT_START=npm run start
PLAYWRIGHT=npm run test:e2e

# Targets
.PHONY: all lint format type-check backend-lint backend-format backend-test \
backend-type-check backend-validate-api-schema backend-load-test \
frontend-type-check frontend-lint frontend-format frontend-test \
run-backend run-frontend run \
test docker-up docker-down e2e-test qa qa-frontend qa-backend

# Run linters for both backend and frontend
# Aggregate targets
lint: backend-lint frontend-lint

# Run formatters for both backend and frontend
format: backend-format frontend-format

# Run type checkers for both backend and frontend
type-check: backend-type-check frontend-type-check

# Run tests for both backend and frontend
test: backend-test frontend-test

# Linting, formatting, type-checking, and unit testing for backend
backend-lint:
@echo "$(COLOR_BLUE_BG)Running backend linting with ruff...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(RUFF) check --fix .

backend-format:
@echo "$(COLOR_BLUE_BG)Running backend formatting with ruff...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(RUFF) format .

backend-type-check:
@echo "$(COLOR_BLUE_BG)Running backend static type checking with mypy...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(MYPY) .

backend-validate-api-schema:
@echo "$(COLOR_BLUE_BG)Validating API schema...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(OPENAPI_SCHEMA)

backend-test:
@echo "$(COLOR_BLUE_BG)Running backend unit tests...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(PYTEST) && $(PYCOVERAGE)

backend-load-test:
@echo "$(COLOR_BLUE_BG)Running backend load tests...$(COLOR_RESET)"
@$(MAKE) run-backend
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(LOCUST)

# Linting, formatting, type-checking, and unit testing for frontend
frontend-lint:
@echo "$(COLOR_BLUE_BG)Running frontend linting...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(ESLINT) .

frontend-format:
@echo "$(COLOR_BLUE_BG)Running frontend formatting...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(PRETTIER) .

frontend-type-check:
@echo "$(COLOR_BLUE_BG)Running frontend static type checking with TypeScript...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(TSC)

frontend-test:
@echo "$(COLOR_BLUE_BG)Running frontend unit tests...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(JEST)

frontend-build:
@echo "$(COLOR_BLUE_BG)Building frontend...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(BUILD)

frontend-audit:
@echo "$(COLOR_BLUE_BG)Auditing frontend dependencies...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(AUDIT)

lint-markdown:
@echo "$(COLOR_BLUE_BG)Linting markdown files...$(COLOR_RESET)"
markdownlint README.md

# Run backend server
run-backend:
@echo "$(COLOR_BLUE_BG)Running backend server...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(DJANGO_RUNSERVER) & echo $$! > backend.pid

# Run frontend server
run-frontend:
@echo "$(COLOR_BLUE_BG)Running frontend server...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(NEXT_START) & echo $$! > frontend.pid

# Run QA checks
# Quality Assurance targets
qa-frontend: frontend-lint frontend-format frontend-type-check frontend-test frontend-build frontend-audit
qa-backend: backend-lint backend-format backend-type-check backend-test
qa: format lint type-check backend-validate-api-schema test

# End-to-end testing with backend and frontend running
e2e-test:
@echo "$(COLOR_BLUE_BG)Starting backend and frontend services...$(COLOR_RESET)"
@$(MAKE) run-backend
@$(MAKE) run-frontend
@sleep 10 # Wait for services to start (adjust this as necessary)
@echo "$(COLOR_BLUE_BG)Running end-to-end tests...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(PLAYWRIGHT)
# Run targets
run-backend:
@$(MAKE) _run-backend

run-frontend:
@$(MAKE) _run-frontend

run: run-backend run-frontend

# Build containers
# Docker targets
docker-build:
@echo "$(COLOR_BLUE_BG)Building containerized services...$(COLOR_RESET)"
docker-compose build --no-cache

# Run all services
docker-up: docker-build
@echo "$(COLOR_BLUE_BG)Running containerized services...$(COLOR_RESET)"
docker-compose up -d

# Stop all services
docker-down:
@echo "$(COLOR_BLUE_BG)Stopping containerized services...$(COLOR_RESET)"
docker-compose down

# End-to-end testing
e2e-test:
@$(MAKE) run-backend
@$(MAKE) run-frontend
@sleep 10 # Wait for services to start
@$(MAKE) _run-e2e-test
43 changes: 43 additions & 0 deletions backend.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SERVER_DIR=./server

# Backend tool commands
RUFF=ruff
MYPY=mypy
PYTEST=pytest chatgptserver --verbose
PYCOVERAGE=coverage run -m pytest chatgptserver && coverage report --fail-under=95 && coverage html
DJANGO_RUNSERVER=chatgptserver/manage.py runserver
OPENAPI_SCHEMA=chatgptserver/manage.py spectacular --color --validate --file schema.yml
LOCUST=locust -H http://127.0.0.1:8000/

# Backend Targets
backend-lint:
@echo "$(COLOR_BLUE_BG)Running backend linting with ruff...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(RUFF) check --fix .

backend-format:
@echo "$(COLOR_BLUE_BG)Running backend formatting with ruff...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(RUFF) format .

backend-type-check:
@echo "$(COLOR_BLUE_BG)Running backend static type checking with mypy...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(MYPY) .

backend-validate-api-schema:
@echo "$(COLOR_BLUE_BG)Validating API schema...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(OPENAPI_SCHEMA)

backend-test:
@echo "$(COLOR_BLUE_BG)Running backend unit tests...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(PYTEST) && $(PYCOVERAGE)

_run-backend:
@echo "$(COLOR_BLUE_BG)Running backend server...$(COLOR_RESET)"
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(DJANGO_RUNSERVER) & echo $$! > backend.pid

backend-load-test:
@echo "$(COLOR_BLUE_BG)Running backend load tests...$(COLOR_RESET)"
@$(MAKE) _run-backend
cd $(SERVER_DIR) && $(VENV_ACTIVATE) && $(LOCUST)

.PHONY: backend-lint backend-format backend-type-check \
backend-validate-api-schema backend-test _run-backend backend-load-test
47 changes: 47 additions & 0 deletions frontend.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
FRONTEND_DIR=./frontend

# Frontend tool commands
ESLINT=npm run lint:fix
PRETTIER=npm run format
TSC=npm run check-types
AUDIT=npm audit --audit-level=moderate
BUILD=npm run build
JEST=npm run test
NEXT_START=npm run start
PLAYWRIGHT=npm run test:e2e

# Frontend Targets
frontend-lint:
@echo "$(COLOR_BLUE_BG)Running frontend linting...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(ESLINT) .

frontend-format:
@echo "$(COLOR_BLUE_BG)Running frontend formatting...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(PRETTIER) .

frontend-type-check:
@echo "$(COLOR_BLUE_BG)Running frontend static type checking with TypeScript...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(TSC)

frontend-test:
@echo "$(COLOR_BLUE_BG)Running frontend unit tests...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(JEST)

frontend-build:
@echo "$(COLOR_BLUE_BG)Building frontend...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(BUILD)

frontend-audit:
@echo "$(COLOR_BLUE_BG)Auditing frontend dependencies...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(AUDIT)

_run-frontend:
@echo "$(COLOR_BLUE_BG)Running frontend server...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(NEXT_START) & echo $$! > frontend.pid

_run-e2e-test:
@echo "$(COLOR_BLUE_BG)Running end-to-end tests...$(COLOR_RESET)"
cd $(FRONTEND_DIR) && $(PLAYWRIGHT)

.PHONY: frontend-lint frontend-format frontend-type-check \
frontend-test frontend-build frontend-audit _run-frontend _run-e2e-test
6 changes: 4 additions & 2 deletions server/chatgptserver/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Any

from rest_framework import serializers


class ChatRequestSerializer(serializers.Serializer):
class ChatRequestSerializer(serializers.Serializer[dict[str, Any]]):
prompt = serializers.CharField()


class AssistantResponseSerializer(serializers.Serializer):
class AssistantResponseSerializer(serializers.Serializer[dict[str, Any]]):
response = serializers.CharField()
8 changes: 7 additions & 1 deletion server/mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[mypy]
implicit_optional = True
check_untyped_defs = True
disallow_any_generics = True
disallow_untyped_decorators = True
implicit_optional = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_return_any = True

[mypy-dotenv]
ignore_missing_imports = True
Expand Down
4 changes: 2 additions & 2 deletions server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ dependencies = [

[tool.uv]
dev-dependencies = [
"coverage>=7.6.4",
"coverage>=7.6.7",
"locust>=2.32.0",
"mypy>=1.13.0",
"pytest-django>=4.9.0",
"pytest-random-order>=1.1.1",
"pytest>=8.3.3",
"ruff>=0.7.1",
"ruff>=0.7.4",
]

[tool.ruff]
Expand Down
Loading

0 comments on commit 34741b3

Please sign in to comment.