Skip to content
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
12 changes: 6 additions & 6 deletions openedx/tests/completion_integration/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Test signal handlers for completion.
"""


from datetime import datetime

import ddt
import pytest
import six
from completion import handlers
from completion.models import BlockCompletion
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_handler_submits_completion(self, score_deleted, expected_completion):
context_key=self.context_key,
block_key=self.block_key,
)
self.assertEqual(completion.completion, expected_completion)
assert completion.completion == expected_completion

@XBlock.register_temp_plugin(CustomScorableBlock, 'custom_scorable')
def test_handler_skips_custom_block(self):
Expand All @@ -96,7 +96,7 @@ def test_handler_skips_custom_block(self):
context_key=self.context_key,
block_key=custom_block_key,
)
self.assertFalse(completion.exists())
assert not completion.exists()

@XBlock.register_temp_plugin(ExcludedScorableBlock, 'excluded_scorable')
def test_handler_skips_excluded_block(self):
Expand All @@ -107,7 +107,7 @@ def test_handler_skips_excluded_block(self):
context_key=self.context_key,
block_key=excluded_block_key,
)
self.assertFalse(completion.exists())
assert not completion.exists()

def test_handler_skips_discussion_block(self):
discussion_block_key = self.context_key.make_usage_key(block_type='discussion', block_id='blue')
Expand All @@ -117,7 +117,7 @@ def test_handler_skips_discussion_block(self):
context_key=self.context_key,
block_key=discussion_block_key,
)
self.assertFalse(completion.exists())
assert not completion.exists()

def test_signal_calls_handler(self):
with patch('completion.handlers.BlockCompletion.objects.submit_completion') as mock_handler:
Expand Down Expand Up @@ -157,7 +157,7 @@ def test_disabled_handler_does_not_submit_completion(self):
modified=datetime.utcnow().replace(tzinfo=utc),
score_db_table='submissions',
)
with self.assertRaises(BlockCompletion.DoesNotExist):
with pytest.raises(BlockCompletion.DoesNotExist):
BlockCompletion.objects.get(
user=self.user,
context_key=self.context_key,
Expand Down
67 changes: 31 additions & 36 deletions openedx/tests/completion_integration/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Test models, managers, and validators.
"""


import pytest
import six
from completion import models
from completion.test_utils import CompletionWaffleTestMixin, submit_completions_for_testing
Expand All @@ -13,8 +13,8 @@
from opaque_keys.edx.keys import CourseKey, UsageKey
from six.moves import range, zip

from openedx.core.djangolib.testing.utils import skip_unless_lms
from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory
from openedx.core.djangolib.testing.utils import skip_unless_lms

SELECT = 1
UPDATE = 1
Expand Down Expand Up @@ -72,9 +72,9 @@ def test_changed_value(self):
completion=0.9,
)
completion.refresh_from_db()
self.assertEqual(completion.completion, 0.9)
self.assertFalse(isnew)
self.assertEqual(models.BlockCompletion.objects.count(), 1)
assert completion.completion == 0.9
assert not isnew
assert models.BlockCompletion.objects.count() == 1

def test_unchanged_value(self):
with self.assertNumQueries(SELECT + 2 * SAVEPOINT):
Expand All @@ -84,9 +84,9 @@ def test_unchanged_value(self):
completion=0.5,
)
completion.refresh_from_db()
self.assertEqual(completion.completion, 0.5)
self.assertFalse(isnew)
self.assertEqual(models.BlockCompletion.objects.count(), 1)
assert completion.completion == 0.5
assert not isnew
assert models.BlockCompletion.objects.count() == 1

def test_new_user(self):
newuser = UserFactory()
Expand All @@ -96,8 +96,8 @@ def test_new_user(self):
block_key=self.block_key,
completion=0.0,
)
self.assertTrue(isnew)
self.assertEqual(models.BlockCompletion.objects.count(), 2)
assert isnew
assert models.BlockCompletion.objects.count() == 2

def test_new_block(self):
newblock = UsageKey.from_string(u'block-v1:edx+test+run+type@video+block@puppers')
Expand All @@ -107,19 +107,19 @@ def test_new_block(self):
block_key=newblock,
completion=1.0,
)
self.assertTrue(isnew)
self.assertEqual(models.BlockCompletion.objects.count(), 2)
assert isnew
assert models.BlockCompletion.objects.count() == 2

def test_invalid_completion(self):
with self.assertRaises(ValidationError):
with pytest.raises(ValidationError):
models.BlockCompletion.objects.submit_completion(
user=self.user,
block_key=self.block_key,
completion=1.2
)
completion = models.BlockCompletion.objects.get(user=self.user, block_key=self.block_key)
self.assertEqual(completion.completion, 0.5)
self.assertEqual(models.BlockCompletion.objects.count(), 1)
assert completion.completion == 0.5
assert models.BlockCompletion.objects.count() == 1


@skip_unless_lms
Expand All @@ -135,14 +135,14 @@ def setUp(self):
self.override_waffle_switch(False)

def test_cannot_call_submit_completion(self):
self.assertEqual(models.BlockCompletion.objects.count(), 1)
with self.assertRaises(RuntimeError):
assert models.BlockCompletion.objects.count() == 1
with pytest.raises(RuntimeError):
models.BlockCompletion.objects.submit_completion(
user=self.user,
block_key=self.block_key,
completion=0.9,
)
self.assertEqual(models.BlockCompletion.objects.count(), 1)
assert models.BlockCompletion.objects.count() == 1


@skip_unless_lms
Expand All @@ -163,29 +163,29 @@ def setUp(self):
def test_submit_batch_completion(self):
blocks = [(self.block_key, 1.0)]
models.BlockCompletion.objects.submit_batch_completion(self.user, blocks)
self.assertEqual(models.BlockCompletion.objects.count(), 1)
self.assertEqual(models.BlockCompletion.objects.last().completion, 1.0)
assert models.BlockCompletion.objects.count() == 1
assert models.BlockCompletion.objects.last().completion == 1.0

def test_submit_batch_completion_without_waffle(self):
with override_waffle_switch(ENABLE_COMPLETION_TRACKING_SWITCH, False):
with self.assertRaises(RuntimeError):
with pytest.raises(RuntimeError):
blocks = [(self.block_key, 1.0)]
models.BlockCompletion.objects.submit_batch_completion(self.user, blocks)

def test_submit_batch_completion_with_same_block_new_completion_value(self):
blocks = [(self.block_key, 0.0)]
self.assertEqual(models.BlockCompletion.objects.count(), 0)
assert models.BlockCompletion.objects.count() == 0
models.BlockCompletion.objects.submit_batch_completion(self.user, blocks)
self.assertEqual(models.BlockCompletion.objects.count(), 1)
assert models.BlockCompletion.objects.count() == 1
model = models.BlockCompletion.objects.first()
self.assertEqual(model.completion, 0.0)
assert model.completion == 0.0
blocks = [
(UsageKey.from_string('block-v1:edx+test+run+type@video+block@doggos'), 1.0),
]
models.BlockCompletion.objects.submit_batch_completion(self.user, blocks)
self.assertEqual(models.BlockCompletion.objects.count(), 1)
assert models.BlockCompletion.objects.count() == 1
model = models.BlockCompletion.objects.first()
self.assertEqual(model.completion, 1.0)
assert model.completion == 1.0


@skip_unless_lms
Expand Down Expand Up @@ -216,19 +216,14 @@ def test_get_learning_context_completions_missing_runs(self):
actual_completions = models.BlockCompletion.get_learning_context_completions(self.user, self.course_key)
expected_block_keys = self.block_keys_with_runs[:3]
expected_completions = dict(list(zip(expected_block_keys, [1.0, 0.8, 0.6])))
self.assertEqual(expected_completions, actual_completions)
assert expected_completions == actual_completions

def test_get_learning_context_completions_empty_result_set(self):
self.assertEqual(
models.BlockCompletion.get_learning_context_completions(self.other_user, self.other_course_key),
{}
)
assert models.BlockCompletion.get_learning_context_completions(self.other_user, self.other_course_key) == {}

def test_get_latest_block_completed(self):
self.assertEqual(
models.BlockCompletion.get_latest_block_completed(self.user, self.course_key).block_key,
self.block_keys[2]
)
assert models.BlockCompletion.get_latest_block_completed(self.user, self.course_key).block_key == \
self.block_keys[2]

def test_get_latest_completed_none_exist(self):
self.assertIsNone(models.BlockCompletion.get_latest_block_completed(self.other_user, self.other_course_key))
assert models.BlockCompletion.get_latest_block_completed(self.other_user, self.other_course_key) is None
62 changes: 24 additions & 38 deletions openedx/tests/completion_integration/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,19 @@ def get_module(descriptor):
def test_completion_service(self):
# Only the completions for the user and course specified for the CompletionService
# are returned. Values are returned for all keys provided.
self.assertEqual(
self.completion_service.get_completions(self.block_keys),
{
self.block_keys[0]: 1.0,
self.block_keys[1]: 0.8,
self.block_keys[2]: 0.6,
self.block_keys[3]: 0.0,
self.block_keys[4]: 0.0
},
)
assert self.completion_service.get_completions(self.block_keys) == {
self.block_keys[0]: 1.0, self.block_keys[1]: 0.8,
self.block_keys[2]: 0.6, self.block_keys[3]: 0.0,
self.block_keys[4]: 0.0
}

@ddt.data(True, False)
def test_enabled_honors_waffle_switch(self, enabled):
self.override_waffle_switch(enabled)
self.assertEqual(self.completion_service.completion_tracking_enabled(), enabled)
assert self.completion_service.completion_tracking_enabled() == enabled

def test_vertical_completion(self):
self.assertEqual(
self.completion_service.vertical_is_complete(self.vertical),
False,
)
assert self.completion_service.vertical_is_complete(self.vertical) is False

for block_key in self.block_keys:
BlockCompletion.objects.submit_completion(
Expand All @@ -169,10 +161,7 @@ def test_vertical_completion(self):
completion=1.0
)

self.assertEqual(
self.completion_service.vertical_is_complete(self.vertical),
True,
)
assert self.completion_service.vertical_is_complete(self.vertical) is True

def test_vertical_partial_completion(self):
block_keys_count = len(self.block_keys)
Expand All @@ -184,19 +173,16 @@ def test_vertical_partial_completion(self):
completion=1.0
)

self.assertEqual(
self.completion_service.vertical_is_complete(self.vertical),
False,
)
assert self.completion_service.vertical_is_complete(self.vertical) is False

def test_can_mark_block_complete_on_view(self):

self.assertEqual(self.completion_service.can_mark_block_complete_on_view(self.course), False)
self.assertEqual(self.completion_service.can_mark_block_complete_on_view(self.chapter), False)
self.assertEqual(self.completion_service.can_mark_block_complete_on_view(self.sequence), False)
self.assertEqual(self.completion_service.can_mark_block_complete_on_view(self.vertical), False)
self.assertEqual(self.completion_service.can_mark_block_complete_on_view(self.html), True)
self.assertEqual(self.completion_service.can_mark_block_complete_on_view(self.problem), False)
assert self.completion_service.can_mark_block_complete_on_view(self.course) is False
assert self.completion_service.can_mark_block_complete_on_view(self.chapter) is False
assert self.completion_service.can_mark_block_complete_on_view(self.sequence) is False
assert self.completion_service.can_mark_block_complete_on_view(self.vertical) is False
assert self.completion_service.can_mark_block_complete_on_view(self.html) is True
assert self.completion_service.can_mark_block_complete_on_view(self.problem) is False

def test_vertical_completion_with_library_content(self):
library = LibraryFactory.create(modulestore=self.store)
Expand All @@ -223,13 +209,13 @@ def test_vertical_completion_with_library_content(self):
# This is needed so we can call get_child_descriptors
self._bind_course_module(library_content_block)
# Make sure the runtime knows that the block's children vary per-user:
self.assertTrue(library_content_block.has_dynamic_children())
self.assertEqual(len(library_content_block.children), 3)
assert library_content_block.has_dynamic_children()
assert len(library_content_block.children) == 3
# Check how many children each user will see:
self.assertEqual(len(library_content_block.get_child_descriptors()), 1)
assert len(library_content_block.get_child_descriptors()) == 1

# No problems are complete yet
self.assertFalse(self.completion_service.vertical_is_complete(lib_vertical))
assert not self.completion_service.vertical_is_complete(lib_vertical)

for block_key in self.block_keys:
BlockCompletion.objects.submit_completion(
Expand All @@ -238,15 +224,15 @@ def test_vertical_completion_with_library_content(self):
completion=1.0
)
# Library content problems aren't complete yet
self.assertFalse(self.completion_service.vertical_is_complete(lib_vertical))
assert not self.completion_service.vertical_is_complete(lib_vertical)

for child in library_content_block.get_child_descriptors():
BlockCompletion.objects.submit_completion(
user=self.user,
block_key=child.scope_ids.usage_id,
completion=1.0
)
self.assertTrue(self.completion_service.vertical_is_complete(lib_vertical))
assert self.completion_service.vertical_is_complete(lib_vertical)

def test_vertical_completion_with_nested_children(self):
parent_vertical = ItemFactory(parent=self.sequence, category='vertical')
Expand All @@ -255,7 +241,7 @@ def test_vertical_completion_with_nested_children(self):
parent_vertical = self.store.get_item(parent_vertical.location)

# Nothing is complete
self.assertFalse(self.completion_service.vertical_is_complete(parent_vertical))
assert not self.completion_service.vertical_is_complete(parent_vertical)

for block_key in self.block_keys:
BlockCompletion.objects.submit_completion(
Expand All @@ -264,11 +250,11 @@ def test_vertical_completion_with_nested_children(self):
completion=1.0
)
# The nested child isn't complete yet
self.assertFalse(self.completion_service.vertical_is_complete(parent_vertical))
assert not self.completion_service.vertical_is_complete(parent_vertical)

BlockCompletion.objects.submit_completion(
user=self.user,
block_key=problem.location,
completion=1.0
)
self.assertTrue(self.completion_service.vertical_is_complete(parent_vertical))
assert self.completion_service.vertical_is_complete(parent_vertical)
Loading