From 8022e023e05cbfc73f65c630b78cecda7dd65d9d Mon Sep 17 00:00:00 2001 From: extreme4all <40169115+extreme4all@users.noreply.github.com> Date: Sun, 18 Aug 2024 19:48:38 +0200 Subject: [PATCH] working mvp now --- Makefile | 44 ++--------------- .../docker-entrypoint-initdb.d/01_tables.sql | 47 ++++++++++++------- requirements-dev.txt | 5 ++ requirements.txt | 3 -- tests/conftest.py | 3 ++ pytest.ini => tests/pytest.ini | 0 tests/test_player_repo.py | 29 ++++++------ 7 files changed, 55 insertions(+), 76 deletions(-) create mode 100644 requirements-dev.txt rename pytest.ini => tests/pytest.ini (100%) diff --git a/Makefile b/Makefile index 5c79e01..106ca79 100644 --- a/Makefile +++ b/Makefile @@ -12,31 +12,9 @@ for line in sys.stdin: endef export PRINT_HELP_PYSCRIPT -export ENV=DEV -export DATABASE_URL=mysql+aiomysql://root:root_bot_buster@localhost/playerdata -export KAFKA_HOST=localhost:9094 -export POOL_RECYCLE=60 -export POOL_TIMEOUT=30 - help: @python3 -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) -clean-pyc: ## clean python cache files - find . -name '*.pyc' -exec rm -f {} + - find . -name '*.pyo' -exec rm -f {} + - find . -name '*~' -exec rm -f {} + - find . -name '__pycache__' -exec rm -fr {} + - find . -name '.pytest_cache' -exec rm -fr {} + - -clean-test: ## cleanup pytests leftovers - rm -f .coverage - rm -fr htmlcov/ - rm -fr test_results/ - rm -f *report.html - rm -f log.html - rm -f test-results.html - rm -f output.xml - docker-restart: ## restart containers docker compose down docker compose up --build -d @@ -47,30 +25,14 @@ docker-test: docker-restart ## restart containers & test docker-test-verbose: docker-restart ## restart containers & test pytest -s -pre-commit-setup: ## Install pre-commit - python3 -m pip install pre-commit - pre-commit --version - -test-setup: ## installs pytest singular package for local testing - python3 -m pip install pytest - python3 -m pip install requests - python3 -m pip install hypothesis - python3 -m pip install pytest-asyncio - requirements: ## installs all requirements python3 -m pip install -r requirements.txt - python3 -m pip install ruff - + python3 -m pip install -r requirements-dev.txt + create-env: ## create .env file echo "ENV=DEV" > .env echo "DATABASE_URL=mysql+aiomysql://root:root_bot_buster@localhost/playerdata" >> .env - echo "KAFKA_HOST=localhost:9094" >> .env echo "POOL_RECYCLE=60" >> .env echo "POOL_TIMEOUT=30" >> .env -setup: create-env pre-commit-setup test-setup requirements ## setup requirements - -docs: ## opens your browser to the webapps testing docs - open http://localhost:5000/docs - xdg-open http://localhost:5000/docs - . http://localhost:5000/docs \ No newline at end of file +setup: create-env requirements ## create env & install requirements \ No newline at end of file diff --git a/mysql/docker-entrypoint-initdb.d/01_tables.sql b/mysql/docker-entrypoint-initdb.d/01_tables.sql index e05b379..b4c6521 100644 --- a/mysql/docker-entrypoint-initdb.d/01_tables.sql +++ b/mysql/docker-entrypoint-initdb.d/01_tables.sql @@ -1,25 +1,36 @@ USE playerdata; -CREATE TABLE Players ( - id INT PRIMARY KEY AUTO_INCREMENT, - name TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP, - possible_ban BOOLEAN, - confirmed_ban BOOLEAN, - confirmed_player BOOLEAN, - label_id INTEGER, - label_jagex INTEGER, - ironman BOOLEAN, - hardcore_ironman BOOLEAN, - ultimate_ironman BOOLEAN, - normalized_name TEXT -); +-- playerdata.Players definition-- playerdata.Labels definition CREATE TABLE Labels ( id int NOT NULL AUTO_INCREMENT, label varchar(50) NOT NULL, PRIMARY KEY (id), - UNIQUE KEY Unique_label (label) USING BTREE -) -; \ No newline at end of file + UNIQUE KEY Unique_label (label) +); + +INSERT INTO `Labels` (id, label) VALUES (0, "UNKOWN"); + +CREATE TABLE Players ( + id int NOT NULL AUTO_INCREMENT, + name text NOT NULL, + created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at datetime DEFAULT NULL, + possible_ban tinyint(1) NOT NULL DEFAULT '0', + confirmed_ban tinyint(1) NOT NULL DEFAULT '0', + confirmed_player tinyint(1) NOT NULL DEFAULT '0', + label_id int NOT NULL DEFAULT '0', + label_jagex int NOT NULL DEFAULT '0', + ironman tinyint DEFAULT NULL, + hardcore_ironman tinyint DEFAULT NULL, + ultimate_ironman tinyint DEFAULT NULL, + normalized_name text, + PRIMARY KEY (id), + UNIQUE KEY Unique_name (name (50)), + KEY FK_label_id (label_id), + KEY confirmed_ban_idx (confirmed_ban), + KEY normal_name_index (normalized_name (50)), + KEY Players_label_jagex_IDX (label_jagex), + KEY Players_possible_ban_IDX (possible_ban, confirmed_ban), + CONSTRAINT FK_label_id FOREIGN KEY (label_id) REFERENCES Labels (id) ON DELETE RESTRICT ON UPDATE RESTRICT +); \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..b7059b5 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,5 @@ +pytest==8.3.2 +pytest-asyncio==0.23.8 +pytest-dependency==0.6.0 +ruff==0.6.1 +httpx==0.27.0 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index df68628..328db5b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,10 +12,7 @@ pydantic==2.8.2 pydantic-settings==2.4.0 pydantic_core==2.20.1 PyMySQL==1.1.1 -pytest==8.3.2 -pytest-asyncio==0.23.8 python-dotenv==1.0.1 -ruff==0.6.1 SQLAlchemy==2.0.32 tomli==2.0.1 typing_extensions==4.12.2 diff --git a/tests/conftest.py b/tests/conftest.py index ccc23d1..8e92f6b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + import pytest from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker @@ -27,6 +29,7 @@ async def setup_database(): @pytest.fixture(scope="function") +@asynccontextmanager async def session(setup_database): """ Provide a new session for each test function. diff --git a/pytest.ini b/tests/pytest.ini similarity index 100% rename from pytest.ini rename to tests/pytest.ini diff --git a/tests/test_player_repo.py b/tests/test_player_repo.py index fab7a93..036bee3 100644 --- a/tests/test_player_repo.py +++ b/tests/test_player_repo.py @@ -7,18 +7,19 @@ @pytest.mark.asyncio async def test_create_player(session: AsyncSession): - player_repository = PlayerRepository(db_session=session) - player_create = PlayerCreate( - name="Test_Player", - possible_ban=False, - confirmed_ban=False, - confirmed_player=True, - label_id=1, - label_jagex=2, - ironman=False, - hardcore_ironman=False, - ultimate_ironman=False, - normalized_name="test player", - ) + async with session as db_session: + player_repository = PlayerRepository(db_session=db_session) + player_create = PlayerCreate( + name="Test_Player", + possible_ban=False, + confirmed_ban=False, + confirmed_player=True, + label_id=0, + label_jagex=2, + ironman=False, + hardcore_ironman=False, + ultimate_ironman=False, + normalized_name="test player", + ) - await player_repository.create(player_create) + await player_repository.create(player_create)