From 0b1fe21f63ae1fbac8961df708b1269e35c1f29d Mon Sep 17 00:00:00 2001 From: Ananta Shrestha Date: Wed, 20 Nov 2024 08:22:29 -0700 Subject: [PATCH] Remove mock.patch, and use direct function calls instead. mock.patch adds unnecessary layer of complication over direct function calling. Both TimeComponentQuery and LocalDate calls inside get_time_query are straight forward, and would not need mocking. --- viz_scripts/tests/test_scaffolding.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/viz_scripts/tests/test_scaffolding.py b/viz_scripts/tests/test_scaffolding.py index 2af1f15..80bb818 100644 --- a/viz_scripts/tests/test_scaffolding.py +++ b/viz_scripts/tests/test_scaffolding.py @@ -5,21 +5,29 @@ import pandas as pd import numpy as np import collections as colls +import pytest # Dynamically import saved-notebooks.plots scaffolding = importlib.import_module('saved-notebooks.scaffolding') -@mock.patch('emission.core.wrapper.localdate.LocalDate') -@mock.patch('emission.storage.timeseries.tcquery.TimeComponentQuery') -def test_get_time_query(mock_tcquery, mock_localdate): - mock_localdate.return_value = ecwl.LocalDate({"year": 2022, "month": 6}) - mock_tcquery.return_value = esttc.TimeComponentQuery("data.start_local_dt", mock_localdate.return_value, mock_localdate.return_value) - +def test_get_time_query(): + # Test with both year and month result = scaffolding.get_time_query(2022, 6) + assert result is not None + assert isinstance(result, esttc.TimeComponentQuery) + + # Test with year and no month + result = scaffolding.get_time_query(2023, None) + assert result is not None + assert isinstance(result, esttc.TimeComponentQuery) + + # Test with month and no year + with pytest.raises(Exception) as e_info: + result = scaffolding.get_time_query(None, 12) - assert result == mock_tcquery.return_value - mock_localdate.assert_called_with({"year": 2022, "month": 6}) - mock_tcquery.assert_called_with("data.start_local_dt", mock_localdate.return_value, mock_localdate.return_value) + # Test with no year or month + result = scaffolding.get_time_query(None, None) + assert result is None def test_mapping_labels(): dynamic_labels = {