Skip to content

Commit

Permalink
feat: try adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanB committed Oct 26, 2024
1 parent af18494 commit 168e94d
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 6 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tests

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

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, '3.10']

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov
- name: Run tests
run: |
pytest --cov=./ --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
flags: unittests
name: codecov-umbrella
fail_ci_if_error: true
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file can be empty
10 changes: 5 additions & 5 deletions events/api/tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from rest_framework.test import APITestCase
from events.models import Event
from datetime import datetime
from django.utils import timezone
from rest_framework import status

from rest_framework.reverse import reverse as api_reverse
Expand All @@ -10,9 +10,9 @@ class EventAPITestCase(APITestCase):
def setUp(self):
event = Event.objects.create(
name="Event_Name",
start_time=datetime.now(),
end_time=datetime.now()
)
start_time=timezone.now(), # Use timezone-aware datetime
end_time=timezone.now() # Use timezone-aware datetime
)

def test_single_event(self):
event_count = Event.objects.count()
Expand All @@ -24,4 +24,4 @@ def test_single_event(self):
# data = {}
# url = api_reverse("api-events:event-create")
# response = self.client.get(url, data, format="json")
# self.assertEqual(response.status_code, status.HTTP_200_OK)
# self.assertEqual(response.status_code, status.HTTP_200_OK)
2 changes: 2 additions & 0 deletions home/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

class HomeConfig(AppConfig):
name = 'home'
default_auto_field = 'django.db.models.BigAutoField'

6 changes: 6 additions & 0 deletions home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Staff(models.Model):
github_url = models.URLField(null=True, blank=True)
email = models.EmailField()

class Meta:
app_label = 'home'

def __str__(self):
return self.name

Expand All @@ -23,5 +26,8 @@ class HistoricEvent(models.Model):
first_place = models.CharField(max_length=100)
second_place = models.CharField(max_length=100, null=True, blank=True)

class Meta:
app_label = 'home'

def __str__(self):
return self.name
102 changes: 101 additions & 1 deletion home/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,103 @@
import pytest
from django.test import TestCase
from django.core.exceptions import ValidationError
from datetime import date
from home.models import Staff, HistoricEvent

# Create your tests here.

@pytest.mark.django_db
class TestStaffModel:
@pytest.fixture
def staff_member(self):
return Staff.objects.create(
name="John Doe",
title="Senior Developer",
image_url="https://example.com/image.jpg",
bio="A talented developer with years of experience.",
email="[email protected]",
linkedin_url="https://linkedin.com/in/johndoe",
github_url="https://github.com/johndoe"
)

def test_staff_creation(self, staff_member):
"""Test that a staff member can be created with all fields"""
assert staff_member.name == "John Doe"
assert staff_member.title == "Senior Developer"
assert staff_member.image_url == "https://example.com/image.jpg"
assert staff_member.email == "[email protected]"

def test_staff_string_representation(self, staff_member):
"""Test the string representation of Staff model"""
assert str(staff_member) == "John Doe"

def test_optional_fields(self):
"""Test that linkedin_url and github_url are optional"""
staff_without_socials = Staff.objects.create(
name="Jane Doe",
title="Developer",
image_url="https://example.com/image2.jpg",
bio="Another talented developer.",
email="[email protected]"
)
assert staff_without_socials.linkedin_url is None
assert staff_without_socials.github_url is None

def test_invalid_email(self):
"""Test that invalid email raises validation error"""
with pytest.raises(ValidationError):
staff = Staff.objects.create(
name="Invalid Email",
title="Developer",
image_url="https://example.com/image.jpg",
bio="Test bio",
email="invalid-email"
)
staff.full_clean()


@pytest.mark.django_db
class TestHistoricEventModel:
@pytest.fixture
def event(self):
return HistoricEvent.objects.create(
name="Championship 2023",
date=date(2023, 12, 25),
youtube_url="https://youtube.com/watch?v=123",
first_place="Team Alpha",
second_place="Team Beta"
)

def test_event_creation(self, event):
"""Test that an event can be created with all fields"""
assert event.name == "Championship 2023"
assert event.date == date(2023, 12, 25)
assert event.first_place == "Team Alpha"
assert event.second_place == "Team Beta"

def test_event_string_representation(self, event):
"""Test the string representation of HistoricEvent model"""
assert str(event) == "Championship 2023"

def test_optional_fields(self):
"""Test that youtube_url and second_place are optional"""
event_minimal = HistoricEvent.objects.create(
name="Mini Tournament",
date=date(2023, 1, 1),
first_place="Winner Team"
)
assert event_minimal.youtube_url is None
assert event_minimal.second_place is None

@pytest.mark.parametrize("test_date,expected", [
(date(2024, 12, 25), 2024),
(date(2023, 1, 1), 2023),
(date(2025, 6, 15), 2025),
])
def test_date_validation(self, test_date, expected):
"""Test that the date field accepts valid dates"""
event = HistoricEvent.objects.create(
name="Test Championship",
date=test_date,
first_place="TBD"
)
assert event.date.year == expected
10 changes: 10 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[pytest]
DJANGO_SETTINGS_MODULE = SRCweb.settings
python_files = tests.py test_*.py *_tests.py
pythonpath = .
django_find_project = true
addopts = -v --cov=. --cov-report=html --cov-report=xml




3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ sqlparse==0.5.0
toml==0.10.2
urllib3==1.26.19
wheel==0.42.0
pytest==8.3.3
pytest-django==4.9.0
pytest-cov==5.0.0

0 comments on commit 168e94d

Please sign in to comment.