Skip to content

Commit

Permalink
refactor: Updated code to follow new lint rules for the CI/CD (#37)
Browse files Browse the repository at this point in the history
- Updated `requirements.txt`
- Fixed minor linting issues
  • Loading branch information
miguelcsx authored Apr 28, 2024
2 parents 159c9b6 + be28bf9 commit 52274c7
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 64 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
7 changes: 1 addition & 6 deletions app/server/commands/wikipedia_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,4 @@ def create_embed(self, title: str, content: str,
return embed

def get_help(self):
return {
"brief": "Access Wikipedia information",
"help": "This command allows you to search or get a summary of a topic from Wikipedia. You can use it as follows:\n"
"!wikipedia search <query> - Search for a topic\n"
"!wikipedia summary <topic> - Get a summary of a topic"
}
return self.parameter_error
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pluggy==1.4.0
pycodestyle==2.11.1
pydantic==2.7.1
pydantic_core==2.18.2
pylint==3.1.0
pyflakes==3.2.0
pytest==8.1.1
python-dotenv==1.0.1
PyYAML==6.0.1
Expand Down
3 changes: 3 additions & 0 deletions tests/app/business/models/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from app.business.models.topic import Topic


@pytest.fixture
def sample_topic():
return Topic(
Expand All @@ -12,12 +13,14 @@ def sample_topic():
subject_id=1
)


def test_topic_init(sample_topic):
assert sample_topic.id == 1
assert sample_topic.title == "Math"
assert sample_topic.content == "Mathematics"
assert sample_topic.subject_id == 1


def test_topic_repr(sample_topic):
expected_repr = "<Topic(id=1, title=Math, content=Mathematics, subject_id=1)>"
assert repr(sample_topic) == expected_repr
10 changes: 9 additions & 1 deletion tests/app/business/models/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from app.business.models.user import User


@pytest.fixture
def sample_user():
return User(
Expand All @@ -14,13 +15,20 @@ def sample_user():
subjects=["Math", "Science"]
)


def test_user_init(sample_user):
assert sample_user.id == 1
assert sample_user.discord_id == "123456789"
assert sample_user.username == "test_user"
assert sample_user.created_at == datetime(2024, 4, 10)
assert sample_user.subjects == ["Math", "Science"]


def test_user_repr(sample_user):
expected_repr = f"User(id={sample_user.id}, discord_id={sample_user.discord_id}, username={sample_user.username}, created_at={sample_user.created_at})"
expected_repr = (
f"User(id={sample_user.id}, "
f"discord_id={sample_user.discord_id}, "
f"username={sample_user.username}, "
f"created_at={sample_user.created_at})"
)
assert repr(sample_user) == expected_repr
7 changes: 6 additions & 1 deletion tests/app/database/models/test_subject_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ def test_subject_model_creation(sample_subject_model):
def test_subject_model_representation(sample_subject_model):
subject = sample_subject_model

expected_repr = f"Subject(id={subject.id}, name={subject.name}, description={subject.description}, user_id={subject.user_id})"
expected_repr = (
f"Subject(id={subject.id}, "
f"name={subject.name}, "
f"description={subject.description}, "
f"user_id={subject.user_id})"
)
assert repr(subject) == expected_repr


Expand Down
3 changes: 3 additions & 0 deletions tests/app/database/models/test_topic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from app.database.models.topic_model import TopicModel


@pytest.fixture
def sample_topic_model():
return TopicModel(
Expand All @@ -11,13 +12,15 @@ def sample_topic_model():
subject_id=1
)


def test_topic_model_creation(sample_topic_model):
topic = sample_topic_model

assert topic.title == "Math"
assert topic.content == "Mathematics"
assert topic.subject_id == 1


def test_topic_model_representation(sample_topic_model):
topic = sample_topic_model

Expand Down
11 changes: 10 additions & 1 deletion tests/app/database/models/test_user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from app.database.models.user_model import UserModel
from app.database.models.subject_model import SubjectModel


@pytest.fixture
def sample_user_model():
discord_id = "123456789"
Expand All @@ -13,6 +14,7 @@ def sample_user_model():
subjects = [SubjectModel(name="Math"), SubjectModel(name="Science")]
return UserModel(discord_id=discord_id, username=username, created_at=created_at, subjects=subjects)


def test_user_model_creation(sample_user_model):
user = sample_user_model

Expand All @@ -22,12 +24,19 @@ def test_user_model_creation(sample_user_model):
assert user.subjects[0].name == "Math"
assert user.subjects[1].name == "Science"


def test_user_model_repr(sample_user_model):
user = sample_user_model

expected_repr = f"<User(id={user.id}, discord_id={user.discord_id}, username={user.username}, created_at={user.created_at})>"
expected_repr = (
f"<User(id={user.id}, "
f"discord_id={user.discord_id}, "
f"username={user.username}, "
f"created_at={user.created_at})>"
)
assert repr(user) == expected_repr


def test_user_model_relationships(sample_user_model):
user = sample_user_model

Expand Down
7 changes: 3 additions & 4 deletions tests/app/database/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import pytest
from app.database.db import create_tables, engine


@pytest.fixture
def mock_inspector():
with patch("app.database.db.inspect") as mock_inspector:
yield mock_inspector


@pytest.fixture
def mock_create_all():
with patch("app.database.db.Base.metadata.create_all") as mock_create_all:
Expand All @@ -18,21 +20,18 @@ def mock_create_all():
def test_create_tables_when_no_tables_exist(mock_inspector, mock_create_all):
# Arrange
mock_inspector.return_value.get_table_names.return_value = []

# Act
create_tables()

# Assert
mock_inspector.assert_called_once_with(engine)
mock_create_all.assert_called_once_with(bind=engine)


def test_create_tables_when_tables_exist(mock_inspector, mock_create_all):
# Arrange
mock_inspector.return_value.get_table_names.return_value = ["users", "subjects"]

# Act
create_tables()

# Assert
mock_inspector.assert_called_once_with(engine)
mock_create_all.assert_not_called()
50 changes: 0 additions & 50 deletions tests/app/sources/wiki/test_scraper.py

This file was deleted.

0 comments on commit 52274c7

Please sign in to comment.