Skip to content

Commit

Permalink
tests: add unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Mar 26, 2024
1 parent e39ae06 commit 50cc856
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 13 deletions.
9 changes: 5 additions & 4 deletions taxonomy/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import permissions
from rest_framework.filters import OrderingFilter
from rest_framework.generics import RetrieveAPIView, ListAPIView
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from rest_framework.response import Response
from rest_framework.views import APIView
Expand Down Expand Up @@ -35,9 +35,9 @@
SkillCategory,
SkillsQuiz,
SkillSubCategory,
SkillValidationConfiguration,
XBlockSkillData,
XBlockSkills,
SkillValidationConfiguration,
)


Expand Down Expand Up @@ -305,8 +305,9 @@ def get_queryset(self):
"""
Get all the xblocks skills with prefetch_related objects.
"""
course_run_key = self.request.query_params.get('course_key')
skill_validation_disabled = SkillValidationConfiguration.is_disabled(course_run_key)
skill_validation_disabled = SkillValidationConfiguration.is_disabled(
self.request.query_params.get('course_key')
)
if skill_validation_disabled:
return XBlockSkills.objects.none()

Expand Down
6 changes: 3 additions & 3 deletions taxonomy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,15 +1270,15 @@ def is_disabled(cls, course_run_key) -> bool:
Returns:
bool: True if skill validation is disabled for the given course run key.
"""
is_valid_course_run_key, course_run_key = cls.is_valid_course_run_key(course_run_key)
is_valid_course_run_key, course_run_locator = cls.is_valid_course_run_key(course_run_key)
if not is_valid_course_run_key:
return False

if cls.objects.filter(organization=course_run_key.org).exists():
if cls.objects.filter(organization=course_run_locator.org).exists():
return True

course_key = get_course_metadata_provider().get_course_key(course_run_key)
if cls.objects.filter(course_key=course_key).exists():
if course_key and cls.objects.filter(course_key=course_key).exists():
return True

return False

Check warning on line 1284 in taxonomy/models.py

View check run for this annotation

Codecov / codecov/patch

taxonomy/models.py#L1284

Added line #L1284 was not covered by tests
9 changes: 8 additions & 1 deletion test_utils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from taxonomy.choices import UserGoal
from taxonomy.models import (
B2CJobAllowList,
CourseRunXBlockSkillsTracker,
CourseSkills,
Industry,
Expand All @@ -27,10 +28,10 @@
SkillCategory,
SkillsQuiz,
SkillSubCategory,
SkillValidationConfiguration,
Translation,
XBlockSkillData,
XBlockSkills,
B2CJobAllowList,
)

FAKER = FakerFactory.create()
Expand Down Expand Up @@ -411,3 +412,9 @@ class Meta:
model = B2CJobAllowList

job = factory.SubFactory(JobFactory)


class SkillValidationConfigurationFactory(factory.django.DjangoModelFactory):

class Meta:
model = SkillValidationConfiguration
17 changes: 13 additions & 4 deletions test_utils/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ def get_course_key(self, course_run_key):
str: course key
"""
try:
return True, CourseKey.from_string(course_run_key)
locator = CourseKey.from_string(course_run_key)
return '{org}+{course}'.format(org=locator.org, course=locator.course)
except InvalidKeyError:
return False, None
return None

def is_valid_course(self, course_key):
"""
Expand All @@ -123,7 +124,11 @@ def is_valid_course(self, course_key):
Returns:
bool: True if course is valid, False otherwise
"""
return bool(course_key)
courses = self.mock_courses
for course in courses:
if course.key == course_key:
return True
return False

def is_valid_organization(self, organization_key):
"""
Expand All @@ -135,7 +140,11 @@ def is_valid_organization(self, organization_key):
Returns:
bool: True if organization is valid, False otherwise
"""
return bool(organization_key)
courses = self.mock_courses
for course in courses:
if course.key.startswith(f'{organization_key}+'):
return True
return False


class DiscoveryProgramMetadataProvider(ProgramMetadataProvider):
Expand Down
72 changes: 71 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
"""
from unittest.mock import patch

import mock
import pytest
from pytest import mark

from django.conf import settings
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from django.test import TestCase

from taxonomy.models import Industry, Job, JobPostings, B2CJobAllowList, SkillValidationConfiguration
from taxonomy.models import B2CJobAllowList, Industry, Job, JobPostings, SkillValidationConfiguration
from taxonomy.signals.handlers import generate_job_description
from taxonomy.utils import generate_and_store_job_description
from test_utils import factories
from test_utils.mocks import MockCourse, MockCourseRun
from test_utils.providers import DiscoveryCourseMetadataProvider


@mark.django_db
Expand Down Expand Up @@ -738,3 +742,69 @@ class SkillValidationConfigurationTests(TestCase):
"""
Tests for the ``SkillValidationConfiguration`` model.
"""
def setUp(self):
super().setUp()

self.mock_course_run = MockCourseRun(course_key='RichX+GoldX', course_run_key='course-v1:RichX+GoldX+1T2024')
self.courses = [MockCourse() for _ in range(3)]
self.courses[0].key = self.mock_course_run.course_key
self.provider_patcher = patch('taxonomy.models.get_course_metadata_provider')
self.mock_provider = self.provider_patcher.start()
self.mock_provider.return_value = DiscoveryCourseMetadataProvider(self.courses)

def tearDown(self):
self.provider_patcher.stop()

def test_model_obj_creation_with_course_and_org(self):
"""
Verify that an `IntegrityError` is raised if user try to create a
SkillValidationConfiguration with both course and organization.
"""
with pytest.raises(IntegrityError) as raised_exception:
factories.SkillValidationConfigurationFactory(
course_key=self.courses[0].key,
organization=self.courses[0].key.split('+')[0]
)

assert raised_exception.value.args[0] == 'CHECK constraint failed: either_course_or_org'

def test_model_obj_creation_with_wrong_course_key(self):
"""
Verify that a `ValidationError` is raised if user try to create a
SkillValidationConfiguration with incorrect course key.
"""
with pytest.raises(ValidationError) as raised_exception:
factories.SkillValidationConfigurationFactory(course_key='MAx+WoWx')

assert raised_exception.value.message_dict == {'course_key': ['Course with key MAx+WoWx does not exist.']}

def test_model_obj_creation_with_wrong_org_key(self):
"""
Verify that a `ValidationError` is raised if user try to create a
SkillValidationConfiguration with incorrect organization key.
"""
with pytest.raises(ValidationError) as raised_exception:
factories.SkillValidationConfigurationFactory(organization='MAx')

assert raised_exception.value.message_dict == {'organization': ['Organization with key MAx does not exist.']}

def test_model_is_disabled_with_course_key(self):
"""
Verify that a `is_disabled` work as expected if a skill validation is disabled for a course.
"""
factories.SkillValidationConfigurationFactory(course_key=self.mock_course_run.course_key)
assert SkillValidationConfiguration.is_disabled(course_run_key=self.mock_course_run.course_run_key)

def test_model_is_disabled_with_org_key(self):
"""
Verify that a `is_disabled` work as expected if a skill validation is disabled for an organization.
"""
organization = self.courses[0].key.split('+')[0]
factories.SkillValidationConfigurationFactory(organization=organization)
assert SkillValidationConfiguration.is_disabled(course_run_key=self.mock_course_run.course_run_key)

def test_model_is_disabled_with_wrong_course_run_key_format(self):
"""
Verify that a `is_disabled` work as expected if a course_run_key is in wrong format.
"""
assert SkillValidationConfiguration.is_disabled(course_run_key='blah blah blah') is False

0 comments on commit 50cc856

Please sign in to comment.