Skip to content

Commit

Permalink
chore: Add test cases for word cloud xblock
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan committed Feb 21, 2025
1 parent 264ae5f commit e7f3e0d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion xblocks_contrib/word_cloud/static/js/src/word_cloud.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Javascript for WordCloudXBlock. */

const blockIdentifier = '.word_cloud';
const blockIdentifier = '._word_cloud_extracted';
// Dimensions of the box where the word cloud will be drawn.
const width = 635;
const height = 635;
Expand Down
Empty file.
63 changes: 60 additions & 3 deletions xblocks_contrib/word_cloud/tests/test_word_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
class TestWordCloudBlock(TestCase):
"""Tests for WordCloudBlock"""

def setUp(self):
super().setUp()
scope_ids = ScopeIds("1", "2", "3", "4")
self.block = WordCloudBlock(ToyRuntime(), scope_ids=scope_ids)

def test_my_student_view(self):
"""Test the basic view loads."""
scope_ids = ScopeIds("1", "2", "3", "4")
block = WordCloudBlock(ToyRuntime(), scope_ids=scope_ids)
frag = block.student_view()
frag = self.block.student_view()
as_dict = frag.to_dict()
content = as_dict["content"]
self.assertIn(
Expand All @@ -29,3 +32,57 @@ def test_my_student_view(self):
content,
"XBlock did not render correct student view",
)

def test_good_word(self):
self.assertEqual(self.block.good_word(" Test "), "test")
self.assertEqual(self.block.good_word("Hello"), "hello")
self.assertEqual(self.block.good_word(" WORLD "), "world")

def test_top_dict(self):
words = {"hello": 3, "world": 5, "python": 2}
top_words = self.block.top_dict(words, 2)
self.assertEqual(top_words, {"world": 5, "hello": 3})

def test_get_state_not_submitted(self):
self.block.submitted = False
state = self.block.get_state()
self.assertFalse(state["submitted"])
self.assertEqual(state["top_words"], {})

def test_get_state_submitted(self):
self.block.submitted = True
self.block.student_words = ["Mango", "Strawberry", "Banana"]
self.block.all_words = {"Mango": 11, "Apple": 13, "Banana": 21, "Strawberry": 28}
self.block.top_words = {"Strawberry": 28, "Banana": 21}
state = self.block.get_state()
self.assertTrue(state["submitted"])
self.assertEqual(state["top_words"], [{'text': 'Banana', 'size': 21, 'percent': 29}, {'text': 'Strawberry', 'size': 28, 'percent': 71}])
self.assertEqual(state['total_count'], 73)

def test_submit_state_first_time(self):
self.block.submitted = False
data = {"student_words": ["hello", "world", "hello"]}
response = self.block.submit_state(data)
self.assertTrue(response['status'] == 'success')
self.assertTrue(self.block.submitted)
self.assertEqual(self.block.student_words, ["hello", "world", "hello"])
self.assertEqual(self.block.all_words["hello"], 2)
self.assertEqual(self.block.all_words["world"], 1)

def test_submit_state_already_submitted(self):
self.block.submitted = True
data = {"student_words": ["new"]}
response = self.block.submit_state(data)
self.assertEqual(response["status"], "fail")
self.assertEqual(response["error"], "You have already posted your data.")

def test_prepare_words(self):
top_words = {"hello": 3, "world": 2}
result = self.block.prepare_words(top_words, 5)
self.assertEqual(len(result), 2)
self.assertEqual(result[0]["text"], "hello")
self.assertEqual(result[0]["size"], 3)
self.assertEqual(result[0]["percent"], 60)
self.assertEqual(result[1]["text"], "world")
self.assertEqual(result[1]["size"], 2)
self.assertEqual(result[1]["percent"], 40)

0 comments on commit e7f3e0d

Please sign in to comment.