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

test: refactors tests so shared data can be re-used #623

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
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,24 @@
"""
from unittest.mock import patch

from openedx_tagging.core.tagging.models import Tag
from organizations.models import Organization

from common.djangoapps.student.tests.factories import UserFactory
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, ModuleStoreTestCase
from xmodule.modulestore.tests.factories import BlockFactory, CourseFactory

from .... import api
from ....models import ContentObjectTag
from ....tests.test_api import TestGetAllObjectTagsMixin
from ..objecttag_export_helpers import TaggedContent, build_object_tree_with_objecttags, iterate_with_level


class TaggedCourseMixin(ModuleStoreTestCase):
class TaggedCourseMixin(TestGetAllObjectTagsMixin, ModuleStoreTestCase): # type: ignore[misc]
"""
Mixin with a course structure and taxonomies
"""
MODULESTORE = TEST_DATA_SPLIT_MODULESTORE

def setUp(self):
super().setUp()
# Create user
self.user = UserFactory.create()

self.orgA = Organization.objects.create(name="Organization A", short_name="orgA")
self.taxonomy_1 = api.create_taxonomy(name="Taxonomy 1")
api.set_taxonomy_orgs(self.taxonomy_1, all_orgs=True)
Tag.objects.create(
taxonomy=self.taxonomy_1,
value="Tag 1.1",
)
Tag.objects.create(
taxonomy=self.taxonomy_1,
value="Tag 1.2",
)

self.taxonomy_2 = api.create_taxonomy(name="Taxonomy 2")
api.set_taxonomy_orgs(self.taxonomy_2, all_orgs=True)

Tag.objects.create(
taxonomy=self.taxonomy_2,
value="Tag 2.1",
)
Tag.objects.create(
taxonomy=self.taxonomy_2,
value="Tag 2.2",
)

# Patch modulestore
self.patcher = patch("openedx.core.djangoapps.content_tagging.tasks.modulestore", return_value=self.store)
self.addCleanup(self.patcher.stop)
self.patcher.start()
Expand All @@ -61,19 +32,13 @@ def setUp(self):
run="test_run",
display_name="Test Course",
)
course_tags = api.tag_content_object(
object_key=self.course.id,
taxonomy=self.taxonomy_1,
tags=['Tag 1.1'],
)

self.expected_tagged_xblock = TaggedContent(
display_name="Test Course",
block_id="course-v1:orgA+test_course+test_run",
category="course",
children=[],
object_tags={
self.taxonomy_1.id: list(course_tags),
self.taxonomy_1.id: list(self.course_tags),
},
)

Expand All @@ -84,24 +49,14 @@ def setUp(self):
display_name="test sequential",
)
# Tag blocks
sequential_tags1 = api.tag_content_object(
object_key=self.sequential.location,
taxonomy=self.taxonomy_1,
tags=['Tag 1.1', 'Tag 1.2'],
)
sequential_tags2 = api.tag_content_object(
object_key=self.sequential.location,
taxonomy=self.taxonomy_2,
tags=['Tag 2.1'],
)
tagged_sequential = TaggedContent(
display_name="test sequential",
block_id="block-v1:orgA+test_course+test_run+type@sequential+block@test_sequential",
category="sequential",
children=[],
object_tags={
self.taxonomy_1.id: list(sequential_tags1),
self.taxonomy_2.id: list(sequential_tags2),
self.taxonomy_1.id: list(self.sequential_tags1),
self.taxonomy_2.id: list(self.sequential_tags2),
},
)

Expand Down Expand Up @@ -144,21 +99,15 @@ def setUp(self):
category="vertical",
display_name="test vertical1",
)
vertical_tags = api.tag_content_object(
object_key=vertical.location,
taxonomy=self.taxonomy_2,
tags=['Tag 2.2'],
)
tagged_vertical = TaggedContent(
display_name="test vertical1",
block_id="block-v1:orgA+test_course+test_run+type@vertical+block@test_vertical1",
category="vertical",
children=[],
object_tags={
self.taxonomy_2.id: list(vertical_tags),
self.taxonomy_2.id: list(self.vertical1_tags),
},
)

assert tagged_sequential.children is not None # type guard
tagged_sequential.children.append(tagged_vertical)

Expand All @@ -182,39 +131,18 @@ def setUp(self):
category="html",
display_name="test html",
)
html_tags = api.tag_content_object(
object_key=html.location,
taxonomy=self.taxonomy_2,
tags=['Tag 2.1'],
)
tagged_text = TaggedContent(
display_name="test html",
block_id="block-v1:orgA+test_course+test_run+type@html+block@test_html",
category="html",
children=[],
object_tags={
self.taxonomy_2.id: list(html_tags),
self.taxonomy_2.id: list(self.html_tags),
},
)

assert untagged_vertical2.children is not None # type guard
untagged_vertical2.children.append(tagged_text)

# Create "deleted" object tags, which will be omitted from the results.
for object_id in (
self.course.id,
self.sequential.location,
vertical.location,
html.location,
):
ContentObjectTag.objects.create(
object_id=str(object_id),
taxonomy=None,
tag=None,
_value="deleted tag",
_name="deleted taxonomy",
)

self.all_object_tags, _ = api.get_all_object_tags(self.course.id)
self.expected_tagged_content_list = [
(self.expected_tagged_xblock, 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

from __future__ import annotations

import abc
import json
from io import BytesIO
from urllib.parse import parse_qs, urlparse
from unittest.mock import MagicMock
from urllib.parse import parse_qs, urlparse

import abc
import ddt
from common.djangoapps.student.tests.factories import UserFactory
from django.contrib.auth import get_user_model
from django.core.files.uploadedfile import SimpleUploadedFile
from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator
Expand All @@ -29,13 +28,10 @@
OrgContentCreatorRole,
OrgInstructorRole,
OrgLibraryUserRole,
OrgStaffRole,
)
from openedx.core.djangoapps.content_libraries.api import (
AccessLevel,
create_library,
set_library_user_permissions,
OrgStaffRole
)
from common.djangoapps.student.tests.factories import UserFactory
from openedx.core.djangoapps.content_libraries.api import AccessLevel, create_library, set_library_user_permissions
from openedx.core.djangoapps.content_tagging import api as tagging_api
from openedx.core.djangoapps.content_tagging.models import TaxonomyOrg
from openedx.core.djangolib.testing.utils import skip_unless_cms
Expand Down Expand Up @@ -1637,6 +1633,7 @@ class TestContentObjectChildrenExportView(TaggedCourseMixin, APITestCase): # ty
"""
def setUp(self):
super().setUp()
self.user = UserFactory.create()
self.staff = UserFactory.create(
username="staff",
email="[email protected]",
Expand Down
47 changes: 35 additions & 12 deletions openedx/core/djangoapps/content_tagging/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from organizations.models import Organization

from .. import api
from ..models import ContentObjectTag


class TestTaxonomyMixin:
Expand Down Expand Up @@ -250,13 +251,14 @@ def test_cannot_tag_across_orgs(self):
)


class TestGetAllObjectTags(TestCase):
class TestGetAllObjectTagsMixin:
"""
Test the get_all_object_tags function
Set up data to test get_all_object_tags functions
"""

def setUp(self):
super().setUp()

self.orgA = Organization.objects.create(name="Organization A", short_name="orgA")
self.taxonomy_1 = api.create_taxonomy(name="Taxonomy 1")
api.set_taxonomy_orgs(self.taxonomy_1, all_orgs=True)
Expand All @@ -281,58 +283,79 @@ def setUp(self):
value="Tag 2.2",
)

course_tags = api.tag_content_object(
self.course_tags = api.tag_content_object(
object_key=CourseKey.from_string("course-v1:orgA+test_course+test_run"),
taxonomy=self.taxonomy_1,
tags=['Tag 1.1'],
)

# Tag blocks
sequential_tags = api.tag_content_object(
self.sequential_tags1 = api.tag_content_object(
object_key=UsageKey.from_string(
"block-v1:orgA+test_course+test_run+type@sequential+block@test_sequential"
),
taxonomy=self.taxonomy_1,
tags=['Tag 1.1', 'Tag 1.2'],
)
sequential_tags2 = api.tag_content_object(
self.sequential_tags2 = api.tag_content_object(
object_key=UsageKey.from_string(
"block-v1:orgA+test_course+test_run+type@sequential+block@test_sequential"
),
taxonomy=self.taxonomy_2,
tags=['Tag 2.1'],
)
vertical1_tags = api.tag_content_object(
self.vertical1_tags = api.tag_content_object(
object_key=UsageKey.from_string(
"block-v1:orgA+test_course+test_run+type@vertical+block@test_vertical1"
),
taxonomy=self.taxonomy_2,
tags=['Tag 2.2'],
)
html_tags = api.tag_content_object(
self.html_tags = api.tag_content_object(
object_key=UsageKey.from_string(
"block-v1:orgA+test_course+test_run+type@html+block@test_html"
),
taxonomy=self.taxonomy_2,
tags=['Tag 2.1'],
)

# Create "deleted" object tags, which will be omitted from the results.
for object_id in (
"course-v1:orgA+test_course+test_run",
"block-v1:orgA+test_course+test_run+type@sequential+block@test_sequential",
"block-v1:orgA+test_course+test_run+type@vertical+block@test_vertical1",
"block-v1:orgA+test_course+test_run+type@html+block@test_html",
):
ContentObjectTag.objects.create(
object_id=str(object_id),
taxonomy=None,
tag=None,
_value="deleted tag",
_name="deleted taxonomy",
)

self.expected_objecttags = {
"course-v1:orgA+test_course+test_run": {
self.taxonomy_1.id: list(course_tags),
self.taxonomy_1.id: list(self.course_tags),
},
"block-v1:orgA+test_course+test_run+type@sequential+block@test_sequential": {
self.taxonomy_1.id: list(sequential_tags),
self.taxonomy_2.id: list(sequential_tags2),
self.taxonomy_1.id: list(self.sequential_tags1),
self.taxonomy_2.id: list(self.sequential_tags2),
},
"block-v1:orgA+test_course+test_run+type@vertical+block@test_vertical1": {
self.taxonomy_2.id: list(vertical1_tags),
self.taxonomy_2.id: list(self.vertical1_tags),
},
"block-v1:orgA+test_course+test_run+type@html+block@test_html": {
self.taxonomy_2.id: list(html_tags),
self.taxonomy_2.id: list(self.html_tags),
},
}


class TestGetAllObjectTags(TestGetAllObjectTagsMixin, TestCase):
"""
Test get_all_object_tags api function
"""

def test_get_all_object_tags(self):
"""
Test the get_all_object_tags function
Expand Down
Loading