From 03f47c035075d299ecc138ab46947223db22c351 Mon Sep 17 00:00:00 2001 From: Ameya Khot <90050951+ameyakhot@users.noreply.github.com> Date: Tue, 18 Jun 2024 22:05:09 -0500 Subject: [PATCH 1/4] Added 3 Tests. More to be added. --- tests/test_jupyter_viz.py | 49 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/tests/test_jupyter_viz.py b/tests/test_jupyter_viz.py index 76ee666ee8a..6880f476358 100644 --- a/tests/test_jupyter_viz.py +++ b/tests/test_jupyter_viz.py @@ -1,11 +1,11 @@ import unittest -from unittest.mock import Mock +from unittest.mock import Mock, patch import ipyvuetify as vw import solara import mesa -from mesa.visualization.jupyter_viz import JupyterViz, Slider, UserInputs +from mesa.visualization.jupyter_viz import JupyterViz, Slider, UserInputs, ModelController, split_model_params class TestMakeUserInput(unittest.TestCase): @@ -151,3 +151,48 @@ def test_slider(): assert not slider_int.is_float_slider slider_dtype_float = Slider("Homophily", 3, 0, 8, 1, dtype=float) assert slider_dtype_float.is_float_slider + +class TestJupyterViz(unittest.TestCase): + # testing for correct init + @patch('solara.use_reactive') + @patch('solara.use_state') + def test_initialization(self, mock_use_state, mock_use_reactive): + mock_use_reactive.side_effect = [Mock(), Mock()] + mock_use_state.return_value = ({}, Mock()) + + @solara.component + def Test(): + JupyterViz(model_class=Mock(), model_params = {}) + + solara.render(Test(), handle_error = False) + + # testing for solara.AppBar() condition + def test_name_parameter(self): + @solara.component + def Test(): + return JupyterViz(model_class = Mock(__name__ = 'TestModel'), model_params={}) + + with patch('solara.AppBarTitle') as mock_app_bar_title: + solara.render(Test(), handle_error=False) + mock_app_bar_title.assert_called_with('TestModel') + + # testing for make_model + def test_make_model(self): + model_class = Mock() + model_params={"mock_key" : {"mock_value" : 10}} + + @solara.component + def Test(): + return JupyterViz(model_class = model_class, model_params=model_params) + + component = Test() + + model_instance = component().make_model() + model_class.__new__.assert_called_with(model_class) + model_class.__init__.assert_called_with(mock_key = 10) + +if __name__ == '__main__': + unittest.main() + + + From 36463f33702e009b3b6a9dba5c2b361737629168 Mon Sep 17 00:00:00 2001 From: Ameya Khot <90050951+ameyakhot@users.noreply.github.com> Date: Tue, 18 Jun 2024 22:18:35 -0500 Subject: [PATCH 2/4] Created Sections. Tests created for Sections 2/5. --- tests/test_jupyter_viz.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_jupyter_viz.py b/tests/test_jupyter_viz.py index 6880f476358..13a74e95806 100644 --- a/tests/test_jupyter_viz.py +++ b/tests/test_jupyter_viz.py @@ -153,6 +153,7 @@ def test_slider(): assert slider_dtype_float.is_float_slider class TestJupyterViz(unittest.TestCase): + # test for section 1. # testing for correct init @patch('solara.use_reactive') @patch('solara.use_state') @@ -166,6 +167,7 @@ def Test(): solara.render(Test(), handle_error = False) + # test for section 2. # testing for solara.AppBar() condition def test_name_parameter(self): @solara.component @@ -188,9 +190,21 @@ def Test(): component = Test() model_instance = component().make_model() - model_class.__new__.assert_called_with(model_class) + model_class.__new__.assert_called_with(model_class, mock_key = 10, seed = 0) model_class.__init__.assert_called_with(mock_key = 10) + # testing for handle_change_model_params + def test_handle_change_model_params(self): + @solara.component + def Test(): + return JupyterViz(model_class = Mock(), model_params={"mock_key" : {"mock_value" : 10}}) + + component = Test() + component().handle_change_model_params("mock_key", 20) + self.assertEqual(component().model_parameters["mock_key"], 20) + + # test for section 3. + if __name__ == '__main__': unittest.main() From 020deff42e54998cb49239c44f1cab912d9cfc1a Mon Sep 17 00:00:00 2001 From: Ameya Khot <90050951+ameyakhot@users.noreply.github.com> Date: Wed, 19 Jun 2024 21:29:23 -0500 Subject: [PATCH 3/4] Unit Tests Written. Need to check if works. --- tests/__init__.py | 5 ++ tests/test_jupyter_viz.py | 101 +++++++++++++++++++++++++++++--------- 2 files changed, 83 insertions(+), 23 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index e69de29bb2d..486fb178689 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,5 @@ +import sys +import os + +# Add the parent directory of the project to PYTHONPATH +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) diff --git a/tests/test_jupyter_viz.py b/tests/test_jupyter_viz.py index 13a74e95806..cb0f65a53c9 100644 --- a/tests/test_jupyter_viz.py +++ b/tests/test_jupyter_viz.py @@ -4,8 +4,18 @@ import ipyvuetify as vw import solara +import sys +import os +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + import mesa -from mesa.visualization.jupyter_viz import JupyterViz, Slider, UserInputs, ModelController, split_model_params +from mesa.visualization.jupyter_viz import ( + JupyterViz, + Slider, + UserInputs, + ModelController, + split_model_params, +) class TestMakeUserInput(unittest.TestCase): @@ -152,61 +162,106 @@ def test_slider(): slider_dtype_float = Slider("Homophily", 3, 0, 8, 1, dtype=float) assert slider_dtype_float.is_float_slider + class TestJupyterViz(unittest.TestCase): # test for section 1. # testing for correct init - @patch('solara.use_reactive') - @patch('solara.use_state') + @patch("solara.use_reactive") + @patch("solara.use_state") def test_initialization(self, mock_use_state, mock_use_reactive): mock_use_reactive.side_effect = [Mock(), Mock()] mock_use_state.return_value = ({}, Mock()) @solara.component def Test(): - JupyterViz(model_class=Mock(), model_params = {}) - - solara.render(Test(), handle_error = False) - + JupyterViz(model_class=Mock(), model_params={}) + + solara.render(Test(), handle_error=False) + # test for section 2. # testing for solara.AppBar() condition def test_name_parameter(self): @solara.component def Test(): - return JupyterViz(model_class = Mock(__name__ = 'TestModel'), model_params={}) - - with patch('solara.AppBarTitle') as mock_app_bar_title: + return JupyterViz(model_class=Mock(__name__="TestModel"), model_params={}) + + with patch("solara.AppBarTitle") as mock_app_bar_title: solara.render(Test(), handle_error=False) - mock_app_bar_title.assert_called_with('TestModel') - + mock_app_bar_title.assert_called_with("TestModel") + # testing for make_model def test_make_model(self): model_class = Mock() - model_params={"mock_key" : {"mock_value" : 10}} + model_params = {"mock_key": {"mock_value": 10}} @solara.component def Test(): - return JupyterViz(model_class = model_class, model_params=model_params) - + return JupyterViz(model_class=model_class, model_params=model_params) + component = Test() model_instance = component().make_model() - model_class.__new__.assert_called_with(model_class, mock_key = 10, seed = 0) - model_class.__init__.assert_called_with(mock_key = 10) + model_class.__new__.assert_called_with(model_class, mock_key=10, seed=0) + model_class.__init__.assert_called_with(mock_key=10) # testing for handle_change_model_params def test_handle_change_model_params(self): @solara.component def Test(): - return JupyterViz(model_class = Mock(), model_params={"mock_key" : {"mock_value" : 10}}) - + return JupyterViz( + model_class=Mock(), model_params={"mock_key": {"mock_value": 10}} + ) + component = Test() component().handle_change_model_params("mock_key", 20) self.assertEqual(component().model_parameters["mock_key"], 20) # test for section 3. + @patch("solara.AppBar") + @patch("solara.AppBarTitle") + def test_ui_setup(self, mock_app_bar_title, mock_app_bar): + @solara.component + def Test(): + return JupyterViz( + model_class=Mock(), model_params={"mock_key": {"mock_value": 10}} + ) + + solara.render(Test(), handle_error=False) + + mock_app_bar.asser_called() + mock_app_bar_title.assert_called_with("Mock") + + @patch("solara.GridFixed") + @patch("soalra.Markdown") + def test_render_in_jupyter(self, mock_markdown, mock_grid_fixed): + @solara.component + def Test(): + return JupyterViz( + model_class=Mock(), model_params={"mock_key": {"mock_value": 10}} + ) + + mock_grid_fixed.assert_called() + mock_markdown.assert_called() + + @patch("solara.Sidebar") + @patch("solara.GridDraggable") + def test_render_in_browser(self, mock_grid_draggable, mock_sidebar): + @solara.component + def Test(): + return JupyterViz( + model_class=Mock(), model_params={"mock_key": {"mock_value": 10}} + ) + + with patch("sys.argv", ["browser"]): + solara.render(Test(), handle_error=False) + + mock_grid_draggable.assert_called() + mock_sidebar.assert_called() -if __name__ == '__main__': - unittest.main() - - +if __name__ == "__main__": + loader = unittest.TestLoader() + suite = loader.discover("../mesa") + runner = unittest.TextTestRunner() + runner.run(suite()) + # unittest.main() From c51e3bee4d7e468451cdc9ded6373eb5f3e6623a Mon Sep 17 00:00:00 2001 From: Ameya Khot <90050951+ameyakhot@users.noreply.github.com> Date: Wed, 19 Jun 2024 23:06:56 -0500 Subject: [PATCH 4/4] Test comment added --- tests/test_jupyter_viz.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_jupyter_viz.py b/tests/test_jupyter_viz.py index cb0f65a53c9..d5d4f82944d 100644 --- a/tests/test_jupyter_viz.py +++ b/tests/test_jupyter_viz.py @@ -260,6 +260,7 @@ def Test(): if __name__ == "__main__": + # Make sure to remove this codeblock before submitting loader = unittest.TestLoader() suite = loader.discover("../mesa") runner = unittest.TextTestRunner()