Skip to content

Commit

Permalink
fix: polish tests (#114)
Browse files Browse the repository at this point in the history
* fix: add test coverage

* use xml

* rename codecov yaml

* add yml path

* ignore codeforlife test helpers

* reset pipeline

* reset pipeline

* use updated workflow

* polish_test

* add todo

* Merge branch 'main' into polish_tests

* fix: test job

* use new test workflow

* add some small tests and don't cover some lines

* no cov when debugging tests

* test get queryset

* update config

* merge from main

* use main

* add test file

* Merge branch 'main' into polish_tests

* change

* echo repo owner id

* test if

* only assert project

* test user serializer and view

* fix remaining view tests

* test actions

* use correct test job

* remove unused imports

* Merge branch 'main' into polish_tests

* don't allow code coverage to drop

* fix: coverage settings
  • Loading branch information
SKairinos authored Apr 24, 2024
1 parent 7cf61ca commit be81148
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 821 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
with:
# Cannot be set with an env var. Value must match in the release job.
python-version: 3.8
codecov-slug: ocadotechnology/codeforlife-package-python

release:
concurrency: release
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"python.defaultInterpreterPath": ".venv/bin/python",
"python.testing.pytestArgs": [
"-n=auto",
"--cov",
"--cov=.",
"--cov-report=html",
"-c=pyproject.toml",
"."
Expand Down
10 changes: 3 additions & 7 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
coverage:
coverage: # https://docs.codecov.com/docs/codecov-yaml
precision: 2
round: down
range: 90...90
status:
patch:
default:
target: 90%
threshold: 1%
status: # https://docs.codecov.com/docs/commit-status
project:
default:
target: 90%
threshold: 1%
threshold: 0%

comment: false
114 changes: 3 additions & 111 deletions codeforlife/tests/model_view_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from ..serializers import BaseSerializer
from ..types import DataDict, JsonDict, KwArgs
from ..user.models import AnyUser as RequestUser
from ..user.models import Class, Student, User
from ..views import ModelViewSet
from .api import APIClient, APITestCase

Expand Down Expand Up @@ -742,13 +741,15 @@ def assert_get_serializer_class(
self.assertEqual(serializer_class, actual_serializer_class)

def assert_get_permissions(
self, permissions: t.List[Permission], *args, **kwargs
self, permissions: t.List[Permission], action: str, *args, **kwargs
):
"""Assert that the expected permissions are returned.
Args:
permissions: The expected permissions.
action: The model view set's action.
"""
kwargs["action"] = action
model_view_set = self.model_view_set_class(*args, **kwargs)
actual_permissions = model_view_set.get_permissions()
self.assertListEqual(permissions, actual_permissions)
Expand Down Expand Up @@ -794,112 +795,3 @@ def assert_get_serializer_context(
self.assertDictContainsSubset(
serializer_context, actual_serializer_context
)

def get_other_user(
self,
user: User,
other_users: QuerySet[User],
is_teacher: bool,
):
"""
Get a different user.
"""

other_user = other_users.first()
assert other_user
assert user != other_user
assert other_user.teacher if is_teacher else other_user.student
return other_user

def get_other_school_user(
self,
user: User,
other_users: QuerySet[User],
is_teacher: bool,
):
"""
Get a different user that is in a school.
- the provided user does not have to be in a school.
- the other user has to be in a school.
"""

other_user = self.get_other_user(user, other_users, is_teacher)
assert (
other_user.teacher.school
if is_teacher
else other_user.student.class_field.teacher.school
)
return other_user

def get_another_school_user(
self,
user: User,
other_users: QuerySet[User],
is_teacher: bool,
same_school: bool,
same_class: t.Optional[bool] = None,
):
"""
Get a different user that is also in a school.
- the provided user has to be in a school.
- the other user has to be in a school.
"""

other_user = self.get_other_school_user(user, other_users, is_teacher)

school = (
user.teacher.school
if user.teacher
else t.cast(
Class, t.cast(Student, user.student).class_field
).teacher.school
)
assert school

other_school = (
other_user.teacher.school
if is_teacher
else other_user.student.class_field.teacher.school
)
assert other_school

if same_school:
assert school == other_school

# Cannot assert that 2 teachers are in the same class since a class
# can only have 1 teacher.
if not (user.teacher and other_user.teacher):
# At this point, same_class needs to be set.
assert same_class is not None, "same_class must be set."

# If one of the users is a teacher.
if user.teacher or is_teacher:
# Get the teacher.
teacher = other_user if is_teacher else user

# Get the student's class' teacher.
class_teacher = (
user if is_teacher else other_user
).student.class_field.teacher.new_user

# Assert the teacher is the class' teacher.
assert (
teacher == class_teacher
if same_class
else teacher != class_teacher
)
# Else, both users are students.
else:
klass = t.cast(
Class, t.cast(Student, user.student).class_field
)

assert (
klass == other_user.student.class_field
if same_class
else klass != other_user.student.class_field
)
else:
assert school != other_school

return other_user
2 changes: 1 addition & 1 deletion codeforlife/user/filters/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# pylint: disable-next=missing-class-docstring
class UserFilterSet(filters.FilterSet):
students_in_class = filters.CharFilter(
"new_student__class_field",
"new_student__class_field__access_code",
"exact",
)

Expand Down
59 changes: 59 additions & 0 deletions codeforlife/user/serializers/user_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
© Ocado Group
Created on 18/04/2024 at 17:26:59(+01:00).
"""

from ...tests import ModelSerializerTestCase
from ..models import IndependentUser, StudentUser, TeacherUser, User
from .user import UserSerializer


# pylint: disable-next=missing-class-docstring
class TestUserSerializer(ModelSerializerTestCase[User, User]):
model_serializer_class = UserSerializer

# test: to representation

def test_to_representation__teacher(self):
"""Serialize teacher user to representation."""
user = TeacherUser.objects.first()
assert user

self.assert_to_representation(
user,
new_data={
"teacher": {
"id": user.teacher.id,
"school": user.teacher.school.id,
"is_admin": user.teacher.is_admin,
},
"student": None,
},
)

def test_to_representation__student(self):
"""Serialize student user to representation."""
user = StudentUser.objects.first()
assert user

self.assert_to_representation(
user,
new_data={
"teacher": None,
"student": {
"id": user.student.id,
"klass": user.student.class_field.access_code,
"school": user.student.class_field.teacher.school.id,
},
},
)

def test_to_representation__indy(self):
"""Serialize independent user to representation."""
user = IndependentUser.objects.first()
assert user

self.assert_to_representation(
user,
new_data={"teacher": None, "student": None},
)
Loading

0 comments on commit be81148

Please sign in to comment.