From 8e816fc874ba81ea5492239e80c530a72a05c1c3 Mon Sep 17 00:00:00 2001 From: aarif Date: Thu, 3 Oct 2019 14:25:33 +0500 Subject: [PATCH] Updated the list comparison to work irrespective of order of items Updated the dictionary iteration to make it work with both python versions Updated the order of expected items for list comparison --- common/lib/xmodule/xmodule/tests/test_word_cloud.py | 11 +++++------ common/lib/xmodule/xmodule/word_cloud_module.py | 5 +++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/common/lib/xmodule/xmodule/tests/test_word_cloud.py b/common/lib/xmodule/xmodule/tests/test_word_cloud.py index e0f59941cb..36656492a6 100644 --- a/common/lib/xmodule/xmodule/tests/test_word_cloud.py +++ b/common/lib/xmodule/xmodule/tests/test_word_cloud.py @@ -38,15 +38,14 @@ class WordCloudModuleTest(LogicTest): response['student_words'], {'sun': 1, 'dog': 6, 'cat': 12} ) + self.assertListEqual( response['top_words'], - [{'text': 'dad', 'size': 2, 'percent': 9.0}, - {'text': 'sun', 'size': 1, 'percent': 5.0}, + [{'text': 'cat', 'size': 12, 'percent': 55.0}, + {'text': 'dad', 'size': 2, 'percent': 9.0}, {'text': 'dog', 'size': 6, 'percent': 27.0}, {'text': 'mom', 'size': 1, 'percent': 5.0}, - {'text': 'cat', 'size': 12, 'percent': 54.0}] + {'text': 'sun', 'size': 1, 'percent': 4.0}] ) - self.assertEqual( - 100.0, - sum(i['percent'] for i in response['top_words'])) + self.assertEqual(100.0, sum(i['percent'] for i in response['top_words'])) diff --git a/common/lib/xmodule/xmodule/word_cloud_module.py b/common/lib/xmodule/xmodule/word_cloud_module.py index f3a8011062..e6ca4f53a4 100644 --- a/common/lib/xmodule/xmodule/word_cloud_module.py +++ b/common/lib/xmodule/xmodule/word_cloud_module.py @@ -148,11 +148,12 @@ class WordCloudModule(WordCloudFields, XModule): """ list_to_return = [] percents = 0 - for num, word_tuple in enumerate(six.iteritems(top_words)): + sorted_top_words = sorted(top_words.items(), key=lambda x: x[0].lower()) + for num, word_tuple in enumerate(sorted_top_words): if num == len(top_words) - 1: percent = 100 - percents else: - percent = round(100.0 * word_tuple[1] / total_count) + percent = round((100.0 * word_tuple[1]) / total_count) percents += percent list_to_return.append( {