Skip to content

Commit

Permalink
test: use pytest
Browse files Browse the repository at this point in the history
setup.py test is deprecated, switch to pytest.

Also fix a bunch of warnings it flagged.
  • Loading branch information
pefoley2 committed Jun 11, 2021
1 parent 711ac75 commit 18287f1
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Ion.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ deploy
fabfile.py
manage.py
pyproject.toml
pytest.ini
requirements.txt
setup.py
.dependabot/config.yml
Expand Down
4 changes: 2 additions & 2 deletions ci/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ env:
pip install -U pip setuptools
pip install -U \
flake8 pylint pylint-django pylint-plugin-utils isort black==20.8b1 autopep8 \
coverage coveralls pyyaml
coveralls pyyaml pytest-django
pip install -U -r requirements.txt
echo "PATH=$PATH" >> $GITHUB_ENV
Expand Down Expand Up @@ -230,7 +230,7 @@ jobs:

# Tests
- name: Run tests
run: coverage run ./setup.py test
run: coverage run -m pytest
- name: Migrate database
run: coverage run -a ./manage.py migrate
- name: Collect static files
Expand Down
4 changes: 2 additions & 2 deletions intranet/apps/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def test_api_eighth_signup_list(self):

def test_api_bus_list(self):
self.make_token()
route = Route.objects.create(route_name="JT-001", bus_number="JT-001")
route = Route.objects.create(route_name="JT-01", bus_number="JT-01")
response = self.client.get(reverse("api_bus_list"), HTTP_AUTHORIZATION=self.auth)

self.assertEqual(response.status_code, 200)
Expand All @@ -517,7 +517,7 @@ def test_api_bus_list(self):

def test_api_bus_detail(self):
self.make_token()
route_1 = Route.objects.create(route_name="JT-001", bus_number="JT-001")
route_1 = Route.objects.create(route_name="JT-01", bus_number="JT-01")
response = self.client.get(reverse("api_bus_detail", args=[route_1.pk]), HTTP_AUTHORIZATION=self.auth)

self.assertEqual(response.status_code, 200)
Expand Down
6 changes: 2 additions & 4 deletions intranet/apps/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from django.views.generic.base import View

from ...utils.date import get_senior_graduation_date, get_senior_graduation_year
from ...utils.helpers import dark_mode_enabled, get_ap_week_warning
from ...utils.helpers import awaredate, dark_mode_enabled, get_ap_week_warning
from ..dashboard.views import dashboard_view, get_fcps_emerg
from ..eighth.models import EighthBlock
from ..events.models import Event
Expand Down Expand Up @@ -92,9 +92,7 @@ def get_week_sports_school_events() -> Tuple[Container[Event], Container[Event]]
"""
cache_result = cache.get("sports_school_events")
if not isinstance(cache_result, tuple):
events = Event.objects.filter(
time__gte=timezone.localtime(), time__lte=(timezone.localdate() + relativedelta(weeks=1)), public=True
).this_year()
events = Event.objects.filter(time__gte=timezone.localtime(), time__lte=(awaredate() + relativedelta(weeks=1)), public=True).this_year()
sports_events = list(events.filter(approved=True, category="sports").order_by("time")[:3])
school_events = list(events.filter(approved=True, category="school").order_by("time")[:3])

Expand Down
Empty file.
3 changes: 3 additions & 0 deletions intranet/apps/bus/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ def reset_status(self):

def __str__(self):
return self.route_name

class Meta:
ordering = ["route_name"]
4 changes: 2 additions & 2 deletions intranet/apps/bus/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_bus(self):
m.assert_called()

def test_routes(self):
route = Route.objects.get_or_create(route_name="JT-101", bus_number="JT-101")[0]
route = Route.objects.get_or_create(route_name="JT-01", bus_number="JT-01")[0]
route.status = "a"
route.space = "_1"
route.save()
Expand All @@ -47,7 +47,7 @@ def test_routes(self):
self.assertEqual(route.space, "")

def test_route_representation(self):
route = Route.objects.get_or_create(route_name="JT-101", bus_number="JT-101")[0]
route = Route.objects.get_or_create(route_name="JT-01", bus_number="JT-01")[0]
route_str = str(route)

self.assertEqual(route.route_name, route_str)
3 changes: 2 additions & 1 deletion intranet/apps/eighth/tests/admin/test_admin_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from intranet.apps.groups.models import Group

from .....utils.helpers import awaredate
from ...models import EighthActivity, EighthBlock, EighthRoom, EighthSponsor
from ..eighth_test import EighthAbstractTest

Expand Down Expand Up @@ -33,7 +34,7 @@ def test_eighth_admin_dashboard_view(self):
response = self.client.get(reverse("eighth_admin_dashboard"))
self.assertTemplateUsed(response, "eighth/admin/dashboard.html")

self.assertEqual(response.context["start_date"], timezone.localdate())
self.assertEqual(response.context["start_date"], awaredate())
self.assertQuerysetEqual(response.context["all_activities"], [repr(activity) for activity in EighthActivity.objects.all().order_by("name")])
self.assertQuerysetEqual(response.context["blocks_after_start_date"], [repr(block) for block in EighthBlock.objects.all()])
self.assertQuerysetEqual(response.context["groups"], [repr(group) for group in Group.objects.all().order_by("name")])
Expand Down
10 changes: 5 additions & 5 deletions intranet/apps/eighth/tests/admin/test_admin_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_download_group_csv_view(self):
group = Group.objects.get_or_create(name="test group 5")[0]
user1 = get_user_model().objects.get_or_create(username="2021ttest", first_name="Tommy", last_name="Test", student_id=1234568)[0]
user2 = get_user_model().objects.get_or_create(username="2021ttest1", first_name="Thomas", last_name="Test", student_id=1234567)[0]
user3 = get_user_model().objects.get_or_create(username="2021awilliam", first_name="A", last_name="William", student_id=12345679)[0]
user3 = get_user_model().objects.get_or_create(username="2021awilliam", first_name="A", last_name="William", student_id=1234569)[0]
for member in [user1, user2, user3]:
member.groups.add(group)
member.save()
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_eighth_admin_signup_group(self):
username="2021awilliam",
first_name="A",
last_name="William",
student_id=12345679,
student_id=1234569,
user_type="student",
graduation_year=get_senior_graduation_year(),
)[0]
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_eighth_admin_distribute_group(self):
username="2021awilliam",
first_name="A",
last_name="William",
student_id=12345679,
student_id=1234569,
user_type="student",
graduation_year=get_senior_graduation_year(),
)[0]
Expand Down Expand Up @@ -352,7 +352,7 @@ def test_eighth_admin_distribute_unsigned(self):
username="2021awilliam",
first_name="A",
last_name="William",
student_id=12345679,
student_id=1234569,
user_type="student",
graduation_year=get_senior_graduation_year(),
)[0]
Expand Down Expand Up @@ -417,7 +417,7 @@ def test_eighth_admin_distribute_action(self):
username="2021awilliam",
first_name="A",
last_name="William",
student_id=12345679,
student_id=1234569,
user_type="student",
graduation_year=get_senior_graduation_year(),
)[0]
Expand Down
6 changes: 4 additions & 2 deletions intranet/apps/eighth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

from django.utils import timezone

from ...utils.helpers import awaredate

DATE_FORMAT = "%m-%d-%Y"


def get_start_date(request):
if "start_date" in request.session and request.session.get("start_date_set_date") == timezone.localdate().strftime(DATE_FORMAT):
date = request.session["start_date"]
return datetime.strptime(date, DATE_FORMAT).date()
return timezone.make_aware(datetime.strptime(date, DATE_FORMAT))
else:
now = timezone.localdate()
now = awaredate()
set_start_date(request, now)
return now

Expand Down
5 changes: 3 additions & 2 deletions intranet/apps/eighth/views/admin/attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.contrib.auth import get_user_model
from django.db.models import Count, Q
from django.shortcuts import redirect, render
from django.utils import timezone

from .....utils.helpers import is_entirely_digit
from ....auth.decorators import eighth_admin_required
Expand Down Expand Up @@ -198,12 +199,12 @@ def after_deadline_signup_view(request):
end_date = request.GET.get("end", "")

try:
start_date = datetime.strptime(start_date, "%Y-%m-%d")
start_date = timezone.make_aware(datetime.strptime(start_date, "%Y-%m-%d"))
except ValueError:
start_date = get_start_date(request)

try:
end_date = datetime.strptime(end_date, "%Y-%m-%d")
end_date = timezone.make_aware(datetime.strptime(end_date, "%Y-%m-%d"))
except ValueError:
end_date = start_date + timedelta(days=7)

Expand Down
1 change: 1 addition & 0 deletions intranet/apps/eighth/views/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def metrics_view(request):
)
.filter(unique_signups__lt=F("total_signups"))
.values_list("id", F("total_signups") - F("unique_signups"))
.order_by("date", "block_letter")
.nocache()
):
metrics['intranet_eighth_duplicate_signups{{block_id="{}"}}'.format(block_id)] = num_duplicates
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions intranet/apps/events/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_add_event(self):
"location": "Location",
"scheduled_activity": "",
"announcement": "",
"groups": 1,
"groups": [],
"show_attending": "on",
"show_on_dashboard": "on",
"category": "sports",
Expand Down Expand Up @@ -228,7 +228,7 @@ def test_modify_event(self):
"location": "New location",
"scheduled_activity": "",
"announcement": "",
"groups": 1,
"groups": [],
"show_attending": "on",
"show_on_dashboard": "on",
"category": "sports",
Expand Down
4 changes: 2 additions & 2 deletions intranet/apps/events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone

from ...utils.helpers import get_id
from ...utils.helpers import awaredate, get_id
from ...utils.html import safe_html
from ..auth.decorators import deny_restricted
from .forms import AdminEventForm, EventForm
Expand Down Expand Up @@ -60,7 +60,7 @@ def events_view(request):
viewable_events = Event.objects.visible_to_user(request.user).this_year().prefetch_related("groups")

# get date objects for week and month
today = timezone.localtime().date()
today = awaredate()
delta = today - timezone.timedelta(days=today.weekday())
this_week = (delta, delta + timezone.timedelta(days=7))
this_month = (this_week[1], this_week[1] + timezone.timedelta(days=31))
Expand Down
3 changes: 2 additions & 1 deletion intranet/apps/users/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def retrieve(self, request, *args, **kwargs):
binary = user.default_photo
if binary is None:
default_image_path = os.path.join(settings.PROJECT_ROOT, "static/img/default_profile_pic.png")
binary = io.open(default_image_path, mode="rb").read()
with io.open(default_image_path, mode="rb") as f:
binary = f.read()

return Response(binary, content_type="image/jpeg")

Expand Down
3 changes: 2 additions & 1 deletion intranet/apps/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ def test_get_profile_picture_api(self):
response = self.client.get(reverse("api_user_profile_picture_default", args=[user.pk]), HTTP_AUTHORIZATION=self.auth)
self.assertEqual(response.content_type, "image/jpeg")
image_path = os.path.join(settings.PROJECT_ROOT, "static/img/default_profile_pic.png")
self.assertEqual(response.content, io.open(image_path, mode="rb").read())
with io.open(image_path, mode="rb") as f:
self.assertEqual(response.content, f.read())
response_with_username = self.client.get(
reverse("api_user_profile_picture_default_by_username", args=[user.username]), HTTP_AUTHORIZATION=self.auth
)
Expand Down
18 changes: 13 additions & 5 deletions intranet/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
PRODUCTION = os.getenv("PRODUCTION", "").upper() == "TRUE"
IN_CI = any(os.getenv(key, "").upper() == "TRUE" for key in ["TRAVIS", "GITHUB_ACTIONS"])
# FIXME: figure out a less-hacky way to do this.
TESTING = "test" in sys.argv
TESTING = any("test" in arg for arg in sys.argv)
LOGGING_VERBOSE = PRODUCTION

# Whether to report master password attempts
Expand Down Expand Up @@ -240,9 +240,18 @@
"CSS_COMPRESSOR": None,
"COMPILERS": ["pipeline.compilers.sass.SASSCompiler"],
"STYLESHEETS": {
"base": {"source_filenames": ["css/base.scss", "css/themes.scss", "css/responsive.scss"], "output_filename": "css/base.css"},
"eighth.admin": {"source_filenames": ["css/eighth.common.scss", "css/eighth.admin.scss"], "output_filename": "css/eighth.admin.css"},
"eighth.signup": {"source_filenames": ["css/eighth.common.scss", "css/eighth.signup.scss"], "output_filename": "css/eighth.signup.css"},
"base": {
"source_filenames": ["css/base.scss", "css/themes.scss", "css/responsive.scss"],
"output_filename": "css/base.css",
},
"eighth.admin": {
"source_filenames": ["css/eighth.common.scss", "css/eighth.admin.scss"],
"output_filename": "css/eighth.admin.css",
},
"eighth.signup": {
"source_filenames": ["css/eighth.common.scss", "css/eighth.signup.scss"],
"output_filename": "css/eighth.signup.css",
},
},
} # type: Dict[str,Any]

Expand Down Expand Up @@ -666,7 +675,6 @@ def get_log(name): # pylint: disable=redefined-outer-name; 'name' is used as th
},
}


# The debug toolbar is always loaded, unless you manually override SHOW_DEBUG_TOOLBAR
SHOW_DEBUG_TOOLBAR = os.getenv("SHOW_DEBUG_TOOLBAR", "YES") == "YES"

Expand Down
8 changes: 5 additions & 3 deletions intranet/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
from urllib import parse

from django.conf import settings
from django.utils import timezone

from ..apps.auth.helpers import get_login_theme_name
from ..apps.emerg.views import get_emerg

# from django.template.loader import get_template
# from django.utils import timezone
logger = logging.getLogger("intranet.settings")


logger = logging.getLogger("intranet.settings")
def awaredate():
# Note that date objects are always naive, so we have to use datetime for proper timezone support.
return timezone.localtime().replace(hour=0, minute=0, second=0, microsecond=0)


def get_id(obj):
Expand Down
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
DJANGO_SETTINGS_MODULE = intranet.settings
python_files = tests.py test_*.py
filterwarnings = error
2 changes: 1 addition & 1 deletion scripts/format.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
cd "$(dirname -- "$(dirname -- "$(readlink -f "$0")")")"

black intranet && autopep8 --in-place --recursive intranet && isort intranet
black . && autopep8 --in-place --recursive . && isort .
Loading

0 comments on commit 18287f1

Please sign in to comment.