Skip to content

Commit

Permalink
Merge pull request #65 from TukiLaCosa/develop
Browse files Browse the repository at this point in the history
Merge final a produccion
  • Loading branch information
anelioalvarez authored Nov 14, 2023
2 parents 2baeab9 + 86e907f commit 94123f9
Show file tree
Hide file tree
Showing 65 changed files with 5,637 additions and 372 deletions.
5 changes: 5 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ omit =
app/routers/games/__init__.py
app/routers/players/__init__.py
app/tests/*
app/routers/websockets/*
app/utils.py
app/database/utils.py
app/database/initialize_data.py
app/routers/games/intention.py

[html]
directory = htmlcov
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Byte-compiled / optimized / DLL files
__pycache__
*.pyc
*.pyo
*.pyd

# Tests directory
./app/tests/

# Database file
./app/database/*.sqlite
38 changes: 38 additions & 0 deletions .github/workflows/testing-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Tuki Testing CI
on: [push]

jobs:
test:
runs-on: ubuntu-latest

env:
ENVIRONMENT: test
TEST_DIRECTORY: ${{ github.workspace }}/app/tests
TEST_DB_FILE: ${{ github.workspace }}/app/database/database_test.sqlite

steps:
- uses: actions/checkout@v3

- name: Install and caching poetry dependencies
run: pipx install poetry

- uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "poetry"
- run: poetry install

- name: Test Game module
run: |
poetry run coverage run -m pytest $TEST_DIRECTORY/game_tests
rm -f $TEST_DB_FILE
- name: Test Player module
run: |
poetry run coverage run -m pytest $TEST_DIRECTORY/player_tests
rm -f $TEST_DB_FILE
- name: Test Card module
run: |
poetry run coverage run -m pytest $TEST_DIRECTORY/card_tests
rm -f $TEST_DB_FILE
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand Down
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM python:3.10-slim as requirements-stage

WORKDIR /tmp

RUN pip install poetry

COPY ./pyproject.toml ./poetry.lock* ./

RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

FROM python:3.10-slim

WORKDIR /code

COPY --from=requirements-stage /tmp/requirements.txt ./requirements.txt

RUN pip install --no-cache-dir --upgrade -r ./requirements.txt

COPY ./app ./app

EXPOSE 8000

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
34 changes: 27 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,33 @@ COV_GAMES_FILE = .coverage.games
COV_CARDS_FILE = .coverage.cards

# Define the UVicorn command
UVICORN_CMD = uvicorn $(MAIN_FILE):$(APP_NAME) --port $(PORT) --reload

.PHONY: run delete-db coverage-report coverage-clean test-all test-players test-games
UVICORN_CMD = uvicorn $(MAIN_FILE):$(APP_NAME) --port $(PORT) --reload --ws websockets

.PHONY: run delete-db coverage-report coverage-clean test-all test-players test-games help

# Define the 'help' target to display Makefile usage information
help:
@echo "Usage: make [target]"
@echo "Targets:"
@echo " run - Run the UVicorn server"
@echo " create-seed-data - Seed to plant 6 players and 1 Game in the Database"
@echo " delete-db - Delete the database file"
@echo " coverage-report - Generate coverage reports"
@echo " coverage-clean - Remove coverage reports"
@echo " test-all - Run all tests sequentially"
@echo " test-players - Run player tests"
@echo " test-games - Run game tests"
@echo " test-cards - Run card tests"
@echo " autopep8 - Run autopep8 to format code"
@echo " install - Install dependencies and create virtual environment"

# Define the 'run' target to run the UVicorn server within the virtual environment
run: install
poetry run $(UVICORN_CMD)

create-seed-data: install
poetry run python -c "from app.database.utils import create_seed_data; create_seed_data()"

# Define the 'delete-db' target to delete the database file
delete-db:
@if [ -f $(DB_FILE) ]; then \
Expand All @@ -50,19 +69,19 @@ TEST_DIRECTORY = ./app/tests

# Define the 'test-games' target to run game tests in the test environment
test-games: install
ENVIRONMENT=test poetry run coverage run --data-file=$(COV_GAMES_FILE) -m pytest -vv $(TEST_DIRECTORY)/game_tests; true
ENVIRONMENT=test poetry run coverage run --data-file=$(COV_GAMES_FILE) -m pytest --tb=no -vv $(TEST_DIRECTORY)/game_tests; true
rm -f $(TEST_DB_FILE)
unset ENVIRONMENT

# Define the 'test-players' target to run player tests in the test environment
test-players: install
ENVIRONMENT=test poetry run coverage run --data-file=$(COV_PLAYERS_FILE) -m pytest -vv $(TEST_DIRECTORY)/player_tests; true
ENVIRONMENT=test poetry run coverage run --data-file=$(COV_PLAYERS_FILE) -m pytest --tb=no -vv $(TEST_DIRECTORY)/player_tests; true
rm -f $(TEST_DB_FILE)
unset ENVIRONMENT

# Define the 'test-cards' target to run cards tests in the test environment
test-cards: install
ENVIRONMENT=test poetry run coverage run --data-file=$(COV_CARDS_FILE) -m pytest -vv $(TEST_DIRECTORY)/card_tests; true
ENVIRONMENT=test poetry run coverage run --data-file=$(COV_CARDS_FILE) -m pytest --tb=no -vv $(TEST_DIRECTORY)/card_tests; true
rm -f $(TEST_DB_FILE)
unset ENVIRONMENT

Expand Down Expand Up @@ -90,7 +109,8 @@ coverage-clean:

# Define the 'autopep8' target for running autopep8
autopep8:
poetry run autopep8 --in-place --recursive .
@echo "Standarizing all files to pep8..."
@poetry run autopep8 --in-place --recursive .

# Define the 'install' target to install dependencies and create the virtual environment
install: pyproject.toml
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ In the Makefile, you have the following targets:

`make run` starts the Uvicorn server with the application. If it's not defined in the .env file, de default port is 8000.

`make create-seed-data` Populates the database with 6 players and 1 game.

`make delete-db` deletes the application's database file if it exists. It will request confirmation before deletion.

`make test-all` runs the application's tests using pytest and tracks code coverage.
Expand All @@ -56,3 +58,15 @@ In the Makefile, you have the following targets:

**Remember to create a .env file with the necessary environment variables before using the Makefile**
**To know the necessary environment variables you can see the `.env.example` file.**


## Running the Application in Containers - Docker

### Building the Docker Image
`docker build -t backend-tuki .`

### Run a container based on the built image
`docker run --name backend-tuki-container -p 8000:8000 backend-tuki`

## Running the Application in Containers - Docker Compose
`docker-compose up --build`
1 change: 1 addition & 0 deletions app/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Settings(BaseSettings):
environment: Literal["test", "production", "development"] = "development"
CORS_origins: List[str] = ["*"]
CARDS_CSV_FILE_PTAH: str = "app/resources/cartas.csv"

model_config = SettingsConfigDict(env_file=".env", extra='allow')

Expand Down
10 changes: 5 additions & 5 deletions app/database/initialize_data.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from pony.orm import db_session
from .models import Card
import csv
from app.config.config import settings


@db_session
def populate_card_table():
if Card.select().count() > 0:
print("Los datos de cartas ya estan en la base de datos. No se realizará la inicialización")
return
cartas = 'app/resources/cartas.csv'
with open(cartas, newline='') as csvfile:

with open(settings.CARDS_CSV_FILE_PTAH, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=';')
for row in csvreader:
number, card_type, name, description = row
Card(number=number, type=card_type,
number, card_type, card_subtype, name, description = row
Card(number=number, type=card_type, subtype=card_subtype,
name=name, description=description)
20 changes: 19 additions & 1 deletion app/database/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pony.orm import PrimaryKey, Required, Set, Optional
from pony.orm import PrimaryKey, Required, Set, Optional, Json
from app.database import db
from app.routers.games.schemas import GameStatus, RoundDirection

Expand All @@ -9,8 +9,13 @@ class Player(db.Entity):
game_hosting = Optional('Game', reverse='host', cascade_delete=True)
name = Required(str)
rol = Optional(str, nullable=True)
isQuarentined = Optional(bool, default=False)
position = Required(int, default="-1")
hand = Set('Card')
intention_creator = Optional('Intention', reverse='player')
intention_objective = Optional('Intention', reverse='objective_player')
game_last_infected = Optional(
'Game', reverse='last_infected', nullable=True)


class Game(db.Entity):
Expand All @@ -24,15 +29,28 @@ class Game(db.Entity):
status = Required(str, default=GameStatus.UNSTARTED)
discard_deck = Set('Card', reverse='games_discard_deck')
draw_deck = Set('Card', reverse='games_draw_deck')
draw_deck_order = Required(Json, default=[])
round_direction = Required(str, default=RoundDirection.CLOCKWISE)
intention = Optional('Intention', reverse='game')
last_infected = Optional(
Player, reverse='game_last_infected', nullable=True)


class Card(db.Entity):
id = PrimaryKey(int, auto=True)
number = Required(int)
type = Required(str)
subtype = Required(str)
name = Required(str)
description = Required(str)
games_discard_deck = Set(Game, reverse='discard_deck')
games_draw_deck = Set(Game, reverse='draw_deck')
players_hand = Set(Player)


class Intention(db.Entity):
game = Required(Game)
player = Required(Player)
objective_player = Required(Player)
action_type = Required(str)
exchange_payload = Optional(Json, default={})
23 changes: 23 additions & 0 deletions app/database/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pony.orm import db_session, commit
from .models import Player, Game


@db_session
def create_seed_data():
players = []
for i in range(1, 7):
player = Player(name=f"Player{i}")
players.append(player)
commit()

game = Game(name="TestGame",
min_players=4,
max_players=12,
password='secure',
host=players[0],
)
commit()
players[0].game = game.name
commit()
for i in range(1, 6):
game.players.add(players[i])
12 changes: 7 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from app.routers.players import players
from app.routers.games import games
from app.routers.cards import cards
from pony.orm import *
from app.routers.websockets import websockets
# from .utils import show_initial_image
# import threading

app = FastAPI()

Expand All @@ -21,8 +23,8 @@
app.include_router(players.router)
app.include_router(games.router)
app.include_router(cards.router)
app.include_router(websockets.router)


@app.get("/")
async def root():
return {"message": "Hello World"}
# This displays the initial image with the sound
# t = threading.Thread(target=show_initial_image)
# t.start()
Loading

0 comments on commit 94123f9

Please sign in to comment.