Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use setUpTestData in tests #1348

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions apps/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@

class AppsTest(TestCase):

def setUp(self):
self.client = Client()
self.course = Course.objects.create(name="Test", code="test", url="test")
self.instance = CourseInstance.objects.create(course=self.course,
@classmethod
def setUpTestData(cls):
cls.client = Client()
cls.course = Course.objects.create(name="Test", code="test", url="test")
cls.instance = CourseInstance.objects.create(course=cls.course,
instance_name="Ins",
starting_time="2000-01-01T12:00:00.000Z",
ending_time="2020-01-01T12:00:00.000Z")

self.html_plugin = HTMLPlugin.objects.create(container=self.instance,
cls.html_plugin = HTMLPlugin.objects.create(container=cls.instance,
title="HTML Plugin",
views="exercise,course_instance",
content=HTML_PLUGIN_CONTENT)

self.rss_plugin = RSSPlugin.objects.create(container=self.instance,
cls.rss_plugin = RSSPlugin.objects.create(container=cls.instance,
title="RSS Plugin",
views="course_instance",
feed_url=RSS_PLUGIN_ADDRESS)

self.iframe_plugin = ExternalIFramePlugin.objects.create(container=self.instance,
cls.iframe_plugin = ExternalIFramePlugin.objects.create(container=cls.instance,
title="Iframe Plugin",
views="exercise",
service_url=IFRAME_PLUGIN_ADDRESS,
Expand Down
10 changes: 4 additions & 6 deletions course/api/tests.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from django.test import TestCase, override_settings
from django.test import override_settings
from rest_framework.test import APIClient

from course.models import CourseInstance
from ..tests import CourseTestCase


class CourseInstanceAPITest(TestCase):
# use same setUp as for normal tests
from ..tests import CourseTest # pylint: disable=import-outside-toplevel
setUp = CourseTest.setUp
class CourseInstanceAPITest(CourseTestCase):
# uses the same setUpTestData as for normal tests

@override_settings(BASE_URL="http://testserver/")
def test_get_courselist(self):
Expand Down
125 changes: 63 additions & 62 deletions course/test_visibility_enroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,145 +17,146 @@ class CourseVisibilityTest(TestCase):
There are also some tests about enrollment.
"""

def setUp(self):
self.user = User(username="testUser") # not enrolled in the course
self.user.set_password("testUser")
self.user.save()
self.user.userprofile.student_id = '123456'
self.user.userprofile.organization = settings.LOCAL_ORGANIZATION
self.user.userprofile.save()

self.student = User(username="student") # enrolled in the course
self.student.set_password("student")
self.student.save()
self.student.userprofile.student_id = '654321'
self.student.userprofile.organization = settings.LOCAL_ORGANIZATION
self.student.userprofile.save()

self.course = Course.objects.create(
@classmethod
def setUpTestData(cls):
cls.user = User(username="testUser") # not enrolled in the course
cls.user.set_password("testUser")
cls.user.save()
cls.user.userprofile.student_id = '123456'
cls.user.userprofile.organization = settings.LOCAL_ORGANIZATION
cls.user.userprofile.save()

cls.student = User(username="student") # enrolled in the course
cls.student.set_password("student")
cls.student.save()
cls.student.userprofile.student_id = '654321'
cls.student.userprofile.organization = settings.LOCAL_ORGANIZATION
cls.student.userprofile.save()

cls.course = Course.objects.create(
name="Test course",
code="123456",
url="Course-Url",
)

self.today = timezone.now()
self.tomorrow = self.today + timedelta(days=1)
self.two_days_from_now = self.tomorrow + timedelta(days=1)
self.yesterday = self.today - timedelta(days=1)
cls.today = timezone.now()
cls.tomorrow = cls.today + timedelta(days=1)
cls.two_days_from_now = cls.tomorrow + timedelta(days=1)
cls.yesterday = cls.today - timedelta(days=1)

# course instances with different view_access_to settings
self.public_course_instance = CourseInstance.objects.create(
cls.public_course_instance = CourseInstance.objects.create(
instance_name="Public",
starting_time=self.yesterday,
ending_time=self.tomorrow,
course=self.course,
starting_time=cls.yesterday,
ending_time=cls.tomorrow,
course=cls.course,
url="public",
view_content_to=CourseInstance.VIEW_ACCESS.PUBLIC,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
)

self.all_regist_course_instance = CourseInstance.objects.create(
cls.all_regist_course_instance = CourseInstance.objects.create(
instance_name="All registered users",
starting_time=self.yesterday,
ending_time=self.tomorrow,
course=self.course,
starting_time=cls.yesterday,
ending_time=cls.tomorrow,
course=cls.course,
url="allregistered",
view_content_to=CourseInstance.VIEW_ACCESS.ALL_REGISTERED,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
)

self.enroll_audience_course_instance = CourseInstance.objects.create(
cls.enroll_audience_course_instance = CourseInstance.objects.create(
instance_name="Enrollment audience",
starting_time=self.yesterday,
ending_time=self.two_days_from_now,
course=self.course,
starting_time=cls.yesterday,
ending_time=cls.two_days_from_now,
course=cls.course,
url="enrollmentaudience",
view_content_to=CourseInstance.VIEW_ACCESS.ENROLLMENT_AUDIENCE,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
)

self.enrolled_course_instance = CourseInstance.objects.create(
cls.enrolled_course_instance = CourseInstance.objects.create(
instance_name="Enrolled",
starting_time=self.yesterday,
ending_time=self.two_days_from_now,
course=self.course,
starting_time=cls.yesterday,
ending_time=cls.two_days_from_now,
course=cls.course,
url="enrolled",
view_content_to=CourseInstance.VIEW_ACCESS.ENROLLED,
enrollment_audience=CourseInstance.ENROLLMENT_AUDIENCE.INTERNAL_USERS,
)
self.course_instances = [self.public_course_instance, self.all_regist_course_instance,
self.enroll_audience_course_instance, self.enrolled_course_instance]
cls.course_instances = [cls.public_course_instance, cls.all_regist_course_instance,
cls.enroll_audience_course_instance, cls.enrolled_course_instance]

# enrollment
for instance in self.course_instances:
instance.enroll_student(self.student)
for instance in cls.course_instances:
instance.enroll_student(cls.student)

# module/exercise round for each course instance
self.course_modules = {}
for instance in self.course_instances:
self.course_modules[instance.id] = CourseModule.objects.create(
cls.course_modules = {}
for instance in cls.course_instances:
cls.course_modules[instance.id] = CourseModule.objects.create(
name="Test module",
url="test-module",
points_to_pass=10,
course_instance=instance,
opening_time=self.today,
closing_time=self.tomorrow,
opening_time=cls.today,
closing_time=cls.tomorrow,
)

# category
self.categories = {}
for instance in self.course_instances:
self.categories[instance.id] = LearningObjectCategory.objects.create(
cls.categories = {}
for instance in cls.course_instances:
cls.categories[instance.id] = LearningObjectCategory.objects.create(
name="Test category",
course_instance=instance,
points_to_pass=0,
)

# learning objects
self.learning_objects = {}
for instance in self.course_instances:
cls.learning_objects = {}
for instance in cls.course_instances:
lobjects = []
chapter = CourseChapter.objects.create(
name="Test chapter",
course_module=self.course_modules[instance.id],
category=self.categories[instance.id],
course_module=cls.course_modules[instance.id],
category=cls.categories[instance.id],
url='chapter1',
)
lobjects.append(chapter)
lobjects.append(BaseExercise.objects.create(
name="Embedded exercise",
parent=chapter,
status=LearningObject.STATUS.UNLISTED,
course_module=self.course_modules[instance.id],
category=self.categories[instance.id],
course_module=cls.course_modules[instance.id],
category=cls.categories[instance.id],
url='embedexercise',
max_submissions=10,
max_points=10,
points_to_pass=0,
))
lobjects.append(BaseExercise.objects.create(
name="Normal exercise",
course_module=self.course_modules[instance.id],
category=self.categories[instance.id],
course_module=cls.course_modules[instance.id],
category=cls.categories[instance.id],
url='normalexercise',
max_submissions=10,
max_points=10,
points_to_pass=0,
))
self.learning_objects[instance.id] = lobjects
cls.learning_objects[instance.id] = lobjects

# submissions
self.submissions = {}
for _course_instance_id, exercises in self.learning_objects.items():
cls.submissions = {}
for _course_instance_id, exercises in cls.learning_objects.items():
for exercise in exercises:
if not exercise.is_submittable:
continue
self.submissions[exercise.id] = []
cls.submissions[exercise.id] = []
submission = Submission.objects.create(
exercise=exercise,
)
submission.submitters.add(self.student.userprofile)
self.submissions[exercise.id].append(submission)
submission.submitters.add(cls.student.userprofile)
cls.submissions[exercise.id].append(submission)

# disable all logging
logging.disable(logging.CRITICAL)
Expand Down
Loading
Loading