Commit 439acf2d by Victor Shnayder

Fix docstring and test for get_cohort

Returns None if course isn't cohorted, even if cohorts exist.  Shouldn't happen on prod, but could if cohorting is turned off mid-course.
parent 6847779f
...@@ -70,15 +70,19 @@ def get_cohort(user, course_id): ...@@ -70,15 +70,19 @@ def get_cohort(user, course_id):
def _get_cohort(user, course_id): def _get_cohort(user, course_id):
""" """
Given a django User and a course_id, return the user's cohort. In classes with Given a django User and a course_id, return the user's cohort in that
auto-cohorting, put the user in a cohort if they aren't in one already. cohort.
TODO: In classes with auto-cohorting, put the user in a cohort if they
aren't in one already.
Arguments: Arguments:
user: a Django User object. user: a Django User object.
course_id: string in the format 'org/course/run' course_id: string in the format 'org/course/run'
Returns: Returns:
A CourseUserGroup object if the User has a cohort, or None. A CourseUserGroup object if the course is cohorted and the User has a
cohort, else None.
Raises: Raises:
ValueError if the course_id doesn't exist. ValueError if the course_id doesn't exist.
......
from nose import SkipTest
import django.test import django.test
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.conf import settings from django.conf import settings
from override_settings import override_settings from override_settings import override_settings
from course_groups.models import CourseUserGroup from course_groups.models import CourseUserGroup
from course_groups.cohorts import get_cohort, get_course_cohorts from course_groups.cohorts import get_cohort, get_course_cohorts
from xmodule.tests.test_import import BaseCourseTestCase
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
def xml_store_config(data_dir):
return {
'default': {
'ENGINE': 'xmodule.modulestore.xml.XMLModuleStore',
'OPTIONS': {
'data_dir': data_dir,
'default_class': 'xmodule.hidden_module.HiddenDescriptor',
}
}
}
TEST_DATA_DIR = settings.COMMON_TEST_DATA_ROOT
TEST_DATA_XML_MODULESTORE = xml_store_config(TEST_DATA_DIR)
@override_settings(MODULESTORE=TEST_DATA_XML_MODULESTORE)
class TestCohorts(django.test.TestCase): class TestCohorts(django.test.TestCase):
def test_get_cohort(self): def test_get_cohort(self):
...@@ -36,22 +18,31 @@ class TestCohorts(django.test.TestCase): ...@@ -36,22 +18,31 @@ class TestCohorts(django.test.TestCase):
# Proper fix: give all tests a standard modulestore that uses the test # Proper fix: give all tests a standard modulestore that uses the test
# dir. # dir.
raise SkipTest()
course = modulestore().get_course("edX/toy/2012_Fall") course = modulestore().get_course("edX/toy/2012_Fall")
cohort = CourseUserGroup.objects.create(name="TestCohort", self.assertEqual(course.id, "edX/toy/2012_Fall")
course_id=course.id,
group_type=CourseUserGroup.COHORT)
user = User.objects.create(username="test", email="a@b.com") user = User.objects.create(username="test", email="a@b.com")
other_user = User.objects.create(username="test2", email="a2@b.com") other_user = User.objects.create(username="test2", email="a2@b.com")
self.assertIsNone(get_cohort(user, course.id), "No cohort created yet")
cohort = CourseUserGroup.objects.create(name="TestCohort",
course_id=course.id,
group_type=CourseUserGroup.COHORT)
cohort.users.add(user) cohort.users.add(user)
got = get_cohort(user, course.id) self.assertIsNone(get_cohort(user, course.id),
self.assertEquals(got.id, cohort.id, "Should find the right cohort") "Course isn't cohorted, so shouldn't have a cohort")
# Make the course cohorted...
course.metadata["cohort_config"] = {"cohorted": True}
self.assertEquals(get_cohort(user, course.id).id, cohort.id,
"Should find the right cohort")
got = get_cohort(other_user, course.id) self.assertEquals(get_cohort(other_user, course.id), None,
self.assertEquals(got, None, "other_user shouldn't have a cohort") "other_user shouldn't have a cohort")
def test_get_course_cohorts(self): def test_get_course_cohorts(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment