Skip to content

Commit

Permalink
Merge pull request #19 from jlikhuva/integrity-error-freetextresponse
Browse files Browse the repository at this point in the history
Suppress IntegrityError exception
  • Loading branch information
caseylitton authored Jul 25, 2017
2 parents d1def65 + ec5a21e commit c915fba
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
20 changes: 12 additions & 8 deletions freetextresponse/freetextresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This is the core logic for the Free-text Response XBlock
"""
from enum import Enum
from django.db import IntegrityError
from django.template.context import Context
from django.template.loader import get_template
from django.utils.translation import ungettext
Expand Down Expand Up @@ -454,14 +455,17 @@ def _compute_score(self):
"""
credit = self._determine_credit()
self.score = credit.value
self.runtime.publish(
self,
'grade',
{
'value': self.score,
'max_value': Credit.full.value
}
)
try:
self.runtime.publish(
self,
'grade',
{
'value': self.score,
'max_value': Credit.full.value
}
)
except IntegrityError:
pass

def _determine_credit(self):
# Not a standard xlbock pylint disable.
Expand Down
19 changes: 19 additions & 0 deletions freetextresponse/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from xblock.field_data import DictFieldData
from xblock.validation import ValidationMessage

from django.db import IntegrityError
from django.template.loader import get_template

from .freetextresponse import Credit
Expand Down Expand Up @@ -260,6 +261,24 @@ def test_compute_score(self, credit):
{'value': credit.value, 'max_value': Credit.full.value},
)

def test_compute_score_integrity_error(self):
# pylint: disable=protected-access, invalid-name
"""
Tests that _compute_score gracefully handles IntegrityError exception.
Tests to ensure that if an IntegrityError exception
is thrown by any of the methods/functions called in the
process of saving the score, the program handles it gracefully.
We force runtime.publish to throw an IntegrityError exception,
and expect _compute_score to be graceful about it.
"""
self.xblock.runtime.publish = MagicMock(return_value=None)
self.xblock.runtime.publish.side_effect = IntegrityError(
"Unique Key Violation"
)
self.xblock._determine_credit = MagicMock(return_value=Credit.zero)
self.xblock._compute_score()

def test_is_at_least_one_phrase_present(self):
# pylint: disable=invalid-name, protected-access
"""
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "xblock-free-text-response",
"title": "FreeTextResponse XBlock",
"description": "Enables instructors to create questions with free-text responses.",
"version": "0.1.5",
"version": "0.1.6",
"homepage": "https://github.com/Stanford-Online/xblock-free-text-response",
"author": {
"name": "Azim Pradhan",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def run_tests(self):

setup(
name="xblock-free-text-response",
version="0.1.5",
version="0.1.6",
description="Enables instructors to create questions with free-text responses.",
license='AGPL-3.0',
packages=[
Expand Down

0 comments on commit c915fba

Please sign in to comment.