-
Notifications
You must be signed in to change notification settings - Fork 59
Introduce Answer.course_key and deprecate course_id. #131
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import unicode_literals | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('problem_builder', '0002_auto_20160121_1525'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='answer', | ||
| name='course_key', | ||
| field=models.CharField(default=None, max_length=255, null=True, db_index=True), | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name='answer', | ||
| unique_together=set([('student_id', 'course_key', 'name'), ('student_id', 'course_id', 'name')]), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """ | ||
| Tests temporary AnswerMixin code that helps migrate course_id column to course_key. | ||
| """ | ||
| import unittest | ||
| from collections import namedtuple | ||
| from django.utils.crypto import get_random_string | ||
| from mock import patch | ||
|
|
||
| from problem_builder.answer import AnswerMixin | ||
| from problem_builder.models import Answer | ||
|
|
||
|
|
||
| class TestAnswerMixin(unittest.TestCase): | ||
| """ Unit tests for AnswerMixin. """ | ||
|
|
||
| FakeRuntime = namedtuple('FakeRuntime', ['course_id', 'anonymous_student_id']) | ||
|
|
||
| def setUp(self): | ||
| self.course_id = 'course-v1:edX+DemoX+Demo_Course' | ||
| self.anonymous_student_id = '12345678987654321' | ||
|
|
||
| def make_answer_mixin(self, name=None, course_id=None, student_id=None): | ||
| if name is None: | ||
| name = get_random_string() | ||
| if course_id is None: | ||
| course_id = self.course_id | ||
| if student_id is None: | ||
| student_id = self.anonymous_student_id | ||
| answer_mixin = AnswerMixin() | ||
| answer_mixin.name = name | ||
| answer_mixin.runtime = self.FakeRuntime(course_id, student_id) | ||
| return answer_mixin | ||
|
|
||
| def test_creates_model_instance(self): | ||
| name = 'test-model-creation' | ||
| answer_mixin = self.make_answer_mixin(name=name) | ||
| model = answer_mixin.get_model_object() | ||
| self.assertEqual(model.name, name) | ||
| self.assertEqual(model.student_id, self.anonymous_student_id) | ||
| self.assertEqual(model.course_id, self.course_id) | ||
| self.assertEqual(model.course_key, self.course_id) | ||
| self.assertEqual(Answer.objects.get(pk=model.pk), model) | ||
|
|
||
| def test_finds_instance_by_course_key(self): | ||
| name = 'test-course-key' | ||
| existing_model = Answer( | ||
| name=name, | ||
| student_id=self.anonymous_student_id, | ||
| course_key=self.course_id, | ||
| course_id='ignored' | ||
| ) | ||
| existing_model.save() | ||
| answer_mixin = self.make_answer_mixin(name=name) | ||
| model = answer_mixin.get_model_object() | ||
| self.assertEqual(model, existing_model) | ||
|
|
||
| def test_finds_instance_by_course_id(self): | ||
| name = 'test-course-id' | ||
| existing_model = Answer( | ||
| name=name, | ||
| student_id=self.anonymous_student_id, | ||
| course_id=self.course_id, | ||
| course_key=None | ||
| ) | ||
| # Temporarily patch full_clean to allow saving object with blank course_key to the database. | ||
| with patch.object(Answer, 'full_clean', return_value=None): | ||
| existing_model.save() | ||
| answer_mixin = self.make_answer_mixin(name=name) | ||
| model = answer_mixin.get_model_object() | ||
| self.assertEqual(model, existing_model) | ||
| self.assertEqual(model.course_key, self.course_id) | ||
|
|
||
| def test_works_with_long_course_keys(self): | ||
| course_id = 'course-v1:VeryLongOrganizationName+VeryLongCourseNumber+VeryLongCourseRun' | ||
| self.assertTrue(len(course_id) > 50) # precondition check | ||
| answer_mixin = self.make_answer_mixin(course_id=course_id) | ||
| model = answer_mixin.get_model_object() | ||
| self.assertEqual(model.course_key, course_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you writing raw SQL here rather than using the ORM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maxrothman Only because I was not familiar with django
Qobjects and didn't know django ORM supportsORqueries. Thanks for the hint, I opened #136 to convert the raw SQL query toQobjects - can you please review?If it looks good, I'll update the version number at https://github.com/edx/edx-platform/pull/14044 and we can finally get that one merged.