-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Fix publishing in old mongo #1203
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
189 changes: 189 additions & 0 deletions
189
common/lib/xmodule/xmodule/modulestore/tests/test_publish.py
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,189 @@ | ||
| """ | ||
| Test the publish code (primary causing orphans) | ||
| """ | ||
| import uuid | ||
| import mock | ||
| import unittest | ||
| import datetime | ||
| import random | ||
|
|
||
| from xmodule.modulestore.inheritance import InheritanceMixin | ||
| from xmodule.modulestore.mongo import MongoModuleStore, DraftMongoModuleStore | ||
| from xmodule.modulestore import Location | ||
| from xmodule.fields import Date | ||
| from xmodule.modulestore.exceptions import ItemNotFoundError | ||
| from xmodule.modulestore.mongo.draft import DIRECT_ONLY_CATEGORIES | ||
|
|
||
|
|
||
| class TestPublish(unittest.TestCase): | ||
| """ | ||
| Test the publish code (primary causing orphans) | ||
| """ | ||
|
|
||
| # Snippet of what would be in the django settings envs file | ||
| db_config = { | ||
| 'host': 'localhost', | ||
| 'db': 'test_xmodule', | ||
| } | ||
|
|
||
| modulestore_options = dict({ | ||
| 'default_class': 'xmodule.raw_module.RawDescriptor', | ||
| 'fs_root': '', | ||
| 'render_template': mock.Mock(return_value=""), | ||
| 'xblock_mixins': (InheritanceMixin,) | ||
| }, **db_config) | ||
|
|
||
| def setUp(self): | ||
| self.modulestore_options['collection'] = 'modulestore{0}'.format(uuid.uuid4().hex) | ||
|
|
||
| self.old_mongo = MongoModuleStore(**self.modulestore_options) | ||
| self.draft_mongo = DraftMongoModuleStore(**self.modulestore_options) | ||
| self.addCleanup(self.tear_down_mongo) | ||
| self.course_location = None | ||
|
|
||
| def tear_down_mongo(self): | ||
| # old_mongo doesn't give a db attr, but all of the dbs are the same and draft and pub use same collection | ||
| dbref = self.old_mongo.collection.database | ||
| dbref.drop_collection(self.old_mongo.collection) | ||
| dbref.connection.close() | ||
|
|
||
| def _create_item(self, category, name, data, metadata, parent_category, parent_name, runtime): | ||
| """ | ||
| Create the item in either draft or direct based on category and attach to its parent. | ||
| """ | ||
| location = self.course_location.replace(category=category, name=name) | ||
| if category in DIRECT_ONLY_CATEGORIES: | ||
| mongo = self.old_mongo | ||
| else: | ||
| mongo = self.draft_mongo | ||
| mongo.create_and_save_xmodule(location, data, metadata, runtime) | ||
| if isinstance(data, basestring): | ||
| fields = {'data': data} | ||
| else: | ||
| fields = data.copy() | ||
| fields.update(metadata) | ||
| if parent_name: | ||
| # add child to parent in mongo | ||
| parent_location = self.course_location.replace(category=parent_category, name=parent_name) | ||
| parent = self.draft_mongo.get_item(parent_location) | ||
| parent.children.append(location.url()) | ||
| if parent_category in DIRECT_ONLY_CATEGORIES: | ||
| mongo = self.old_mongo | ||
| else: | ||
| mongo = self.draft_mongo | ||
| mongo.update_children(parent_location, parent.children) | ||
|
|
||
| def _create_course(self): | ||
| """ | ||
| Create the course, publish all verticals | ||
| * some detached items | ||
| """ | ||
| date_proxy = Date() | ||
| metadata = { | ||
| 'start': date_proxy.to_json(datetime.datetime(2000, 3, 13, 4)), | ||
| 'display_name': 'Migration test course', | ||
| } | ||
| data = { | ||
| 'wiki_slug': 'test_course_slug' | ||
| } | ||
| fields = metadata.copy() | ||
| fields.update(data) | ||
|
|
||
| self.course_location = Location('i4x', 'test_org', 'test_course', 'course', 'runid') | ||
| self.old_mongo.create_and_save_xmodule(self.course_location, data, metadata) | ||
| runtime = self.draft_mongo.get_item(self.course_location).runtime | ||
|
|
||
| self._create_item('chapter', 'Chapter1', {}, {'display_name': 'Chapter 1'}, 'course', 'runid', runtime) | ||
| self._create_item('chapter', 'Chapter2', {}, {'display_name': 'Chapter 2'}, 'course', 'runid', runtime) | ||
| self._create_item('vertical', 'Vert1', {}, {'display_name': 'Vertical 1'}, 'chapter', 'Chapter1', runtime) | ||
| self._create_item('vertical', 'Vert2', {}, {'display_name': 'Vertical 2'}, 'chapter', 'Chapter1', runtime) | ||
| self._create_item('html', 'Html1', "<p>Goodbye</p>", {'display_name': 'Parented Html'}, 'vertical', 'Vert1', runtime) | ||
| self._create_item( | ||
| 'discussion', 'Discussion1', | ||
| "discussion discussion_category=\"Lecture 1\" discussion_id=\"a08bfd89b2aa40fa81f2c650a9332846\" discussion_target=\"Lecture 1\"/>\n", | ||
| { | ||
| "discussion_category": "Lecture 1", | ||
| "discussion_target": "Lecture 1", | ||
| "display_name": "Lecture 1 Discussion", | ||
| "discussion_id": "a08bfd89b2aa40fa81f2c650a9332846" | ||
| }, | ||
| 'vertical', 'Vert1', runtime | ||
| ) | ||
| self._create_item('html', 'Html2', "<p>Hellow</p>", {'display_name': 'Hollow Html'}, 'vertical', 'Vert1', runtime) | ||
| self._create_item( | ||
| 'discussion', 'Discussion2', | ||
| "discussion discussion_category=\"Lecture 2\" discussion_id=\"b08bfd89b2aa40fa81f2c650a9332846\" discussion_target=\"Lecture 2\"/>\n", | ||
| { | ||
| "discussion_category": "Lecture 2", | ||
| "discussion_target": "Lecture 2", | ||
| "display_name": "Lecture 2 Discussion", | ||
| "discussion_id": "b08bfd89b2aa40fa81f2c650a9332846" | ||
| }, | ||
| 'vertical', 'Vert2', runtime | ||
| ) | ||
| self._create_item('static_tab', 'staticuno', "<p>tab</p>", {'display_name': 'Tab uno'}, None, None, runtime) | ||
| self._create_item('about', 'overview', "<p>overview</p>", {}, None, None, runtime) | ||
| self._create_item('course_info', 'updates', "<ol><li><h2>Sep 22</h2><p>test</p></li></ol>", {}, None, None, runtime) | ||
|
|
||
| def _xmodule_recurse(self, item, action): | ||
| """ | ||
| Applies action depth-first down tree and to item last. | ||
|
|
||
| A copy of cms.djangoapps.contentstore.views.requests._xmodule_recurse to reproduce its use and behavior | ||
| outside of django. | ||
| """ | ||
| for child in item.get_children(): | ||
| self._xmodule_recurse(child, action) | ||
|
|
||
| action(item) | ||
|
|
||
| def test_publish_draft_delete(self): | ||
| """ | ||
| To reproduce a bug (STUD-811) publish a vertical, convert to draft, delete a child, move a child, publish. | ||
| See if deleted and moved children still is connected or exists in db (bug was disconnected but existed) | ||
| """ | ||
| self._create_course() | ||
| userid = random.getrandbits(32) | ||
| location = self.course_location.replace(category='vertical', name='Vert1') | ||
| item = self.draft_mongo.get_item(location, 2) | ||
| self._xmodule_recurse( | ||
| item, | ||
| lambda i: self.draft_mongo.publish(i.location, userid) | ||
| ) | ||
| # verify status | ||
| item = self.draft_mongo.get_item(location, 0) | ||
| self.assertFalse(getattr(item, 'is_draft', False), "Item was published. Draft should not exist") | ||
| # however, children are still draft, but I'm not sure that's by design | ||
|
|
||
| # convert back to draft | ||
| self.draft_mongo.convert_to_draft(location) | ||
| # both draft and published should exist | ||
| draft_vert = self.draft_mongo.get_item(location, 0) | ||
| self.assertTrue(getattr(draft_vert, 'is_draft', False), "Item was converted to draft but doesn't say so") | ||
| item = self.old_mongo.get_item(location, 0) | ||
| self.assertFalse(getattr(item, 'is_draft', False), "Published item doesn't say so") | ||
|
|
||
| # delete the discussion (which oddly is not in draft mode) | ||
| location = self.course_location.replace(category='discussion', name='Discussion1') | ||
| self.draft_mongo.delete_item(location) | ||
| # remove pointer from draft vertical (verify presence first to ensure process is valid) | ||
| self.assertIn(location.url(), draft_vert.children) | ||
| draft_vert.children.remove(location.url()) | ||
| # move the other child | ||
| other_child_loc = self.course_location.replace(category='html', name='Html2') | ||
| draft_vert.children.remove(other_child_loc.url()) | ||
| other_vert = self.draft_mongo.get_item(self.course_location.replace(category='vertical', name='Vert2'), 0) | ||
| other_vert.children.append(other_child_loc.url()) | ||
| self.draft_mongo.update_children(draft_vert.location, draft_vert.children) | ||
| self.draft_mongo.update_children(other_vert.location, other_vert.children) | ||
| # publish | ||
| self._xmodule_recurse( | ||
| draft_vert, | ||
| lambda i: self.draft_mongo.publish(i.location, userid) | ||
| ) | ||
| item = self.old_mongo.get_item(draft_vert.location, 0) | ||
| self.assertNotIn(location.url(), item.children) | ||
| with self.assertRaises(ItemNotFoundError): | ||
| self.draft_mongo.get_item(location) | ||
| self.assertNotIn(other_child_loc.url(), item.children) | ||
| self.assertTrue(self.draft_mongo.has_item(None, other_child_loc), "Oops, lost moved item") | ||
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.
Have you explored the CourseFactory to see if that would make this easier? I'm still not very skilled with the CourseFactory myself.
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.
The course factory depends upon django and doesn't handle draft v published.