Skip to content

Commit

Permalink
Fix/add tests and behaviour for ancestor annotation and bitmask search.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Oct 12, 2021
1 parent 24b7f15 commit 4776c0f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
13 changes: 13 additions & 0 deletions kolibri/core/content/test/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
from kolibri.core.content.models import File
from kolibri.core.content.models import Language
from kolibri.core.content.models import LocalFile
from kolibri.core.content.test.test_channel_upgrade import ChannelBuilder
from kolibri.core.content.utils.annotation import calculate_included_languages
from kolibri.core.content.utils.annotation import calculate_published_size
from kolibri.core.content.utils.annotation import calculate_total_resource_count
from kolibri.core.content.utils.annotation import mark_local_files_as_available
from kolibri.core.content.utils.annotation import mark_local_files_as_unavailable
from kolibri.core.content.utils.annotation import recurse_annotation_up_tree
from kolibri.core.content.utils.annotation import set_channel_ancestors
from kolibri.core.content.utils.annotation import set_channel_metadata_fields
from kolibri.core.content.utils.annotation import (
set_leaf_node_availability_from_local_file_availability,
Expand Down Expand Up @@ -906,3 +908,14 @@ def test_public(self):

self.channel.refresh_from_db()
self.assertTrue(self.channel.public)


@patch("kolibri.core.content.utils.sqlalchemybridge.get_engine", new=get_engine)
class AncestorAnnotationTestCase(TransactionTestCase):
def test_ancestors(self):
builder = ChannelBuilder()
builder.insert_into_default_db()
channel_id = builder.channel["id"]
set_channel_ancestors(channel_id)
for n in ContentNode.objects.filter(channel_id=channel_id):
self.assertEqual(n.ancestors, list(n.get_ancestors().values("id", "title")))
3 changes: 2 additions & 1 deletion kolibri/core/content/test/test_channel_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ def test_destination_tree_ids(self, apps_mock, tree_id_mock, BridgeMock):


class MaliciousDatabaseTestCase(TestCase):
@patch("kolibri.core.content.utils.channel_import.set_channel_ancestors")
@patch("kolibri.core.content.utils.channel_import.initialize_import_manager")
def test_non_existent_root_node(self, initialize_manager_mock):
def test_non_existent_root_node(self, initialize_manager_mock, ancestor_mock):
import_mock = MagicMock()
initialize_manager_mock.return_value = import_mock
channel_id = "6199dde695db4ee4ab392222d5af1e5c"
Expand Down
7 changes: 5 additions & 2 deletions kolibri/core/content/test/test_channel_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from mock import patch
from sqlalchemy import create_engine

from kolibri.core.content.constants.kind_to_learningactivity import kind_activity_map
from kolibri.core.content.constants.schema_versions import CURRENT_SCHEMA_VERSION
from kolibri.core.content.models import ChannelMetadata
from kolibri.core.content.models import ContentNode
Expand Down Expand Up @@ -400,6 +401,8 @@ def file_data(
def contentnode_data(
self, node_id=None, content_id=None, parent_id=None, kind=None, root=False
):
# First kind in choices is Topic, so exclude it here.
kind = kind or random.choice(content_kinds.choices[1:])[0]
return {
"options": "{}",
"content_id": content_id or uuid4_hex(),
Expand All @@ -413,8 +416,8 @@ def contentnode_data(
"author": "",
"title": "Test",
"parent_id": None if root else parent_id or uuid4_hex(),
# First kind in choices is Topic, so exclude it here.
"kind": kind or random.choice(content_kinds.choices[1:])[0],
"kind": kind,
"learning_activities": kind_activity_map.get(kind),
"coach_content": False,
"available": False,
}
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/content/test/test_content_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def _assert_node(self, actual, expected):
"lft": expected.lft,
"rght": expected.rght,
"tree_id": expected.tree_id,
"ancestors": list(expected.get_ancestors().values("id", "title")),
"ancestors": [],
"tags": list(
expected.tags.all()
.order_by("tag_name")
Expand Down
19 changes: 19 additions & 0 deletions kolibri/core/content/test/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.test import TestCase
from le_utils.constants.labels import learning_activities

from kolibri.core.content.models import ContentNode
from kolibri.core.content.test.test_channel_upgrade import ChannelBuilder
from kolibri.core.content.utils.search import annotate_label_bitmasks


class BitMaskTestCase(TestCase):
def test_ancestors(self):
builder = ChannelBuilder()
builder.insert_into_default_db()
annotate_label_bitmasks(ContentNode.objects.all())
for la in learning_activities.LEARNINGACTIVITIESLIST:
self.assertEqual(
list(ContentNode.objects.filter(learning_activities__contains=la)),
list(ContentNode.objects.has_all_labels("learning_activities", [la])),
)
print(ContentNode.objects.has_all_labels("learning_activities", [la]))
2 changes: 1 addition & 1 deletion kolibri/core/content/utils/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ def set_channel_ancestors(channel_id):
)

# Go from the shallowest to deepest
for level in range(1, node_depth):
for level in range(1, node_depth + 1):

ancestors = select(
[
Expand Down
7 changes: 4 additions & 3 deletions kolibri/core/content/utils/channel_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,9 +1097,6 @@ def import_channel_from_local_db(channel_id, cancel_check=None):

import_manager.end()

annotate_label_bitmasks(ContentNode.objects.filter(channel_id=channel_id))
set_channel_ancestors(channel_id)

channel = ChannelMetadata.objects.get(id=channel_id)
channel.last_updated = local_now()
try:
Expand All @@ -1110,6 +1107,10 @@ def import_channel_from_local_db(channel_id, cancel_check=None):
ContentNode.objects.create(
id=node_id, title=channel.name, content_id=node_id, channel_id=channel_id
)

annotate_label_bitmasks(ContentNode.objects.filter(channel_id=channel_id))
set_channel_ancestors(channel_id)

channel.save()

logger.info("Channel {} successfully imported into the database".format(channel_id))
Expand Down

0 comments on commit 4776c0f

Please sign in to comment.