From 165505a569a80b2f35491511ac6d0c4a8da59dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Primo=C5=BE=20Godec?= Date: Mon, 13 Jan 2020 16:52:54 +0100 Subject: [PATCH] WordCloud: Updated tests and added new tests --- .../text/widgets/tests/test_owworldcloud.py | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/orangecontrib/text/widgets/tests/test_owworldcloud.py b/orangecontrib/text/widgets/tests/test_owworldcloud.py index 7bcfd9f67..17772a621 100644 --- a/orangecontrib/text/widgets/tests/test_owworldcloud.py +++ b/orangecontrib/text/widgets/tests/test_owworldcloud.py @@ -1,10 +1,14 @@ import unittest +from unittest.mock import Mock + import numpy as np from Orange.widgets.tests.base import WidgetTest +from Orange.data import StringVariable, Domain from scipy.sparse import csr_matrix from orangecontrib.text.corpus import Corpus +from orangecontrib.text.topics import Topic from orangecontrib.text.widgets.owwordcloud import OWWordCloud @@ -13,6 +17,20 @@ def setUp(self): self.widget = self.create_widget(OWWordCloud) self.corpus = Corpus.from_file('deerwester') + self.topic = self.create_topic() + + def create_topic(self): + words = [[f"a{i}"] for i in range(10)] + weights = list(range(10)) + return Topic.from_numpy( + Domain([], metas=[ + StringVariable("Topic 1") + ]), + X=np.empty((10, 0)), + metas=np.array(words), + W=weights #np.array(weights).reshape(-1, 1) + ) + def test_data(self): """ Just basic test. @@ -95,6 +113,62 @@ def test_bow_info(self): self.send_signal(self.widget.Inputs.corpus, None) self.assertFalse(self.widget.Info.bow_weights.is_shown()) + def test_topic(self): + self.send_signal(self.widget.Inputs.topic, self.topic) + + self.assertIsNotNone(self.widget.topic) + self.assertEqual("a0", self.widget.wordlist[0][0]) + self.assertEqual(10, self.widget.wordlist[0][1]) + self.assertEqual("a9", self.widget.wordlist[9][0]) + self.assertEqual(40, self.widget.wordlist[9][1]) + + self.assertListEqual( + self.topic.metas[:, 0].tolist(), self.widget.shown_words.tolist()) + np.testing.assert_array_almost_equal(self.topic.W, self.widget.shown_weights) + + def test_input_summary(self): + insum = self.widget.info.set_input_summary = Mock() + + self.send_signal(self.widget.Inputs.corpus, self.corpus) + insum.assert_called_with("42", "9 documents with 42 words\n") + + self.send_signal(self.widget.Inputs.topic, self.topic) + insum.assert_called_with( + "42 | 10", "9 documents with 42 words\n10 words in a topic.") + + self.send_signal(self.widget.Inputs.corpus, None) + insum.assert_called_with(f"10", "10 words in a topic.") + + self.send_signal(self.widget.Inputs.topic, None) + insum.assert_called_with(self.widget.info.NoInput) + + self.send_signal(self.widget.Inputs.topic, self.topic) + insum.assert_called_with(f"10", "10 words in a topic.") + + def test_output_summary(self): + outsum = self.widget.info.set_output_summary = Mock() + + self.send_signal(self.widget.Inputs.corpus, self.corpus) + outsum.assert_called_with( + "0 | 0 | 42", "0 documents\n0 selected words\n42 words with counts" + ) + + self.send_signal(self.widget.Inputs.topic, self.topic) + outsum.assert_called_with( + "0 | 0 | 42", "0 documents\n0 selected words\n42 words with counts" + ) + + self.send_signal(self.widget.Inputs.corpus, None) + outsum.assert_called_with(self.widget.info.NoOutput) + + self.send_signal(self.widget.Inputs.topic, None) + outsum.assert_called_with(self.widget.info.NoOutput) + + def test_send_report(self): + self.widget.send_report() + self.send_signal(self.widget.Inputs.corpus, self.corpus) + self.widget.send_report() + if __name__ == "__main__": unittest.main()