From eee826ca4bda233ef3cbb0202cb27725a9088a1e Mon Sep 17 00:00:00 2001 From: Awais Qureshi Date: Wed, 9 Oct 2019 15:43:18 +0500 Subject: [PATCH] BOM-814 String comparsion within in raw html were causing tests failures in python3. The function parses the string and converts into dictionary. --- .../xmodule/xmodule/tests/test_sequence.py | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/common/lib/xmodule/xmodule/tests/test_sequence.py b/common/lib/xmodule/xmodule/tests/test_sequence.py index c7ee64376b..3dc8eda361 100644 --- a/common/lib/xmodule/xmodule/tests/test_sequence.py +++ b/common/lib/xmodule/xmodule/tests/test_sequence.py @@ -4,6 +4,7 @@ Tests for sequence module. # pylint: disable=no-member from __future__ import absolute_import +import ast import json from datetime import timedelta @@ -195,10 +196,10 @@ class SequenceBlockTestCase(XModuleXmlImportTest): extra_context=dict(specific_masquerade=True), ) self.assertIn("seq_module.html", html) - self.assertIn( - "'banner_text': u'Because the due date has passed, " - "this assignment is hidden from the learner.'", - html + html = self.get_context_dict_from_string(html) + self.assertEqual( + 'Because the due date has passed, this assignment is hidden from the learner.', + html['banner_text'] ) def test_hidden_content_self_paced_past_due_before_end(self): @@ -223,31 +224,35 @@ class SequenceBlockTestCase(XModuleXmlImportTest): Assert sequence content is gated """ self.assertIn("seq_module.html", html) - self.assertIn("'banner_text': None", html) - self.assertIn("'items': []", html) - self.assertIn("'gated': True", html) - self.assertIn("'prereq_url': 'PrereqUrl'", html) - self.assertIn("'prereq_section_name': 'PrereqSectionName'", html) - self.assertIn("'gated_section_name': u'{}'".format(six.text_type(sequence.display_name)), html) - self.assertIn("'next_url': 'NextSequential'", html) - self.assertIn("'prev_url': 'PrevSequential'", html) + html = self.get_context_dict_from_string(html) + self.assertIsNone(html['banner_text']) + self.assertEqual([], html['items']) + self.assertTrue(html['gated_content']['gated']) + self.assertEqual('PrereqUrl', html['gated_content']['prereq_url']) + self.assertEqual('PrereqSectionName', html['gated_content']['prereq_section_name']) + self.assertIn( + six.text_type(sequence.display_name), + html['gated_content']['gated_section_name'] + ) + self.assertEqual('NextSequential', html['next_url']) + self.assertEqual('PrevSequential', html['prev_url']) def _assert_prereq(self, html, sequence): """ Assert sequence is a prerequisite with unfulfilled gates """ self.assertIn("seq_module.html", html) - self.assertIn( - "'banner_text': u'This section is a prerequisite. " - "You must complete this section in order to unlock additional content.'", - html + html = self.get_context_dict_from_string(html) + self.assertEqual( + "This section is a prerequisite. You must complete this section in order to unlock additional content.", + html['banner_text'] ) - self.assertIn("'gated': False", html) - self.assertIn(six.text_type(sequence.location), html) - self.assertIn("'prereq_url': None", html) - self.assertIn("'prereq_section_name': None", html) - self.assertIn("'next_url': 'NextSequential'", html) - self.assertIn("'prev_url': 'PrevSequential'", html) + self.assertFalse(html['gated_content']['gated']) + self.assertEqual(six.text_type(sequence.location), html['item_id']) + self.assertIsNone(html['gated_content']['prereq_url']) + self.assertIsNone(html['gated_content']['prereq_section_name']) + self.assertEqual('NextSequential', html['next_url']) + self.assertEqual('PrevSequential', html['prev_url']) def _assert_ungated(self, html, sequence): """ @@ -295,7 +300,6 @@ class SequenceBlockTestCase(XModuleXmlImportTest): self.sequence_1_2, extra_context=dict(next_url='NextSequential', prev_url='PrevSequential'), ) - # assert that content and preq banner is shown self._assert_prereq(html, self.sequence_1_2) @@ -338,3 +342,11 @@ class SequenceBlockTestCase(XModuleXmlImportTest): {'usage_key': usage_key} ) self.assertIs(completion_return, None) + + def get_context_dict_from_string(self, data): + """ + Retrieve dictionary from string. + """ + # Replace tuple and un-necessary info from inside string and get the dictionary. + cleaned_data = data.replace("(('seq_module.html',\n", '').replace("),\n {})", '').strip() + return ast.literal_eval(cleaned_data)