Skip to content

Commit

Permalink
local-test-env and a DB-graph save/load test (#32)
Browse files Browse the repository at this point in the history
* create docker-compose for continuous testing

* add functions for clearing database entries, useful for testing

* add test for graph saving
  • Loading branch information
kahlstrm authored Apr 17, 2023
1 parent 6ee1042 commit 088950c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 5 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ start = "python3 -m kipubot"
dev = "watchfiles 'python3 -m kipubot' kipubot"
lint = "pylint kipubot"
test = "pytest"
test_hot = "watchfiles 'pytest' kipubot tests"

[packages]
pandas = "*"
Expand Down
21 changes: 21 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: "3.8"
services:
test-database:
image: postgres
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
kipubot:
image: kipubot-test
build:
context: .
dockerfile: test.Dockerfile
volumes:
- ./kipubot:/bot/kipubot
- ./tests:/bot/tests
environment:
- MODE=TEST
- PYTHONPATH=${PYTHONPATH}:kipubot
- DATABASE_URL=postgresql://username:password@test-database:5432
depends_on:
- test-database
4 changes: 2 additions & 2 deletions kipubot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
BOT_TOKEN = os.getenv('BOT_TOKEN', default=None)
DATABASE_URL = os.getenv('DATABASE_URL', default=None)
DEVELOPER_CHAT_ID = os.getenv('DEVELOPER_CHAT_ID', default=None)

if BOT_TOKEN is None:
MODE = os.getenv('MODE', default=None)
if BOT_TOKEN is None and MODE != "TEST":
logging.error('Bot token is not set!')
sys.exit(1)

Expand Down
11 changes: 11 additions & 0 deletions kipubot/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def save_raffle_data(chat_id: int,
_CON.commit()


def delete_raffle_data(chat_id: int):
_CON.execute('''DELETE FROM raffle where chat_id=%s''', (chat_id,))
_CON.commit()


def save_user_or_ignore(user_id: int) -> None:
_CON.execute('''INSERT INTO chat_user
VALUES (%s)
Expand All @@ -144,6 +149,12 @@ def save_chat_or_ignore(chat_id: int, title: str, admin_ids: List[int]) -> None:
_CON.commit()


def delete_chat(chat_id: int):
_CON.execute('''DELETE FROM chat where chat_id=%s''', (chat_id,))
_CON.commit()



def register_user(chat_id: int, user_id: int) -> None:

save_user_or_ignore(user_id)
Expand Down
20 changes: 20 additions & 0 deletions test.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# syntax=docker/dockerfile:1
FROM python:3.8 as base

# Setup ENV variables here (if needed in the future)


FROM base as python-deps

# Install pipenv
RUN pip3 install pipenv

# Install python dependencies in /.venv
WORKDIR /bot
COPY Pipfile .
COPY Pipfile.lock .
RUN pipenv install --dev

COPY . .
# Run the app
CMD [ "pipenv", "run", "test_hot" ]
Binary file added tests/example_data/example_1.xlsx
Binary file not shown.
36 changes: 33 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/usr/bin/env python3

# import pytest

from kipubot.utils import int_price_to_str, remove_emojis
import pytest
from datetime import datetime
from db import delete_chat, delete_raffle_data, save_chat_or_ignore, _init_db
from kipubot import DATABASE_URL
from pandas.testing import assert_frame_equal
from kipubot.utils import get_raffle, int_price_to_str, remove_emojis, read_excel_to_df, save_raffle


class TestUtils:
Expand Down Expand Up @@ -33,3 +36,30 @@ def test_remove_emojis(self):
'text with emoji at the end💩') == 'text with emoji at the end '
assert remove_emojis(
'💩text with emoji💩at the start, middle and end💩') == ' text with emoji at the start, middle and end '

class TestGraphSave:

@pytest.fixture(autouse=True)
def create_chat(self):
_init_db(DATABASE_URL)
save_chat_or_ignore(1, "testing", [1])
yield 1
delete_chat(1)


def test_graph_save(self):
file_path = "tests/example_data/example_1.xlsx"
start_date = datetime.fromisoformat("2022-08-01 03:15:00")
end_date = datetime.fromisoformat("2022-08-12 03:15:00")
entry_fee = 1
df = read_excel_to_df(file_path, start_date, end_date)
save_raffle(1, start_date, end_date, entry_fee, df)
raffle_from_db = get_raffle(1, True)
delete_raffle_data(1)
assert (start_date == raffle_from_db.start_date)
assert (end_date == raffle_from_db.end_date)
assert (entry_fee == raffle_from_db.entry_fee)

# behavior that get_raffle returns without index and read returns with probably should be changed.
df.set_index('date',inplace=True)
assert_frame_equal(df,raffle_from_db.df)

0 comments on commit 088950c

Please sign in to comment.