""" Unit tests for data router helper functions. """ import pytest from monday_com_orca_backend.api.routers.data import helpers from monday_com_orca_backend.api.routers.data.typings import LabelId class TestArgsExtractLabelIds: """Test the args_extract_label_ids function.""" def test_valid_single_id(self): """Test extraction of a single valid label ID.""" result = helpers.args_extract_label_ids("123") assert result == [123] assert isinstance(result[0], int) def test_valid_multiple_ids(self): """Test extraction of multiple valid label IDs.""" result = helpers.args_extract_label_ids("123,456,789") assert result == [123, 456, 789] assert all(isinstance(id_, int) for id_ in result) def test_ids_with_spaces(self): """Test extraction with spaces around IDs.""" result = helpers.args_extract_label_ids("123, 456 , 789") assert result == [123, 456, 789] def test_duplicate_ids_removed_and_sorted(self): """Test that duplicate IDs are removed and result is sorted.""" result = helpers.args_extract_label_ids("789,123,456,123,456") assert result == [123, 456, 789] assert len(result) == 3 @pytest.mark.parametrize( "input_value", [ "", # empty string None, # None input " ", # whitespace only ], ) def test_empty_or_none_inputs_return_none(self, input_value): """Test that empty, None, or whitespace-only inputs return None.""" result = helpers.args_extract_label_ids(input_value) assert result is None @pytest.mark.parametrize( "input_str,expected", [ ("123,abc,456", [123, 456]), ("123, abc, 456, def, 789", [123, 456, 789]), ("123,,456,", [123, 456]), # empty segments ("123,-456,789", [123, 789]), # negative numbers ("123,45.6,789", [123, 789]), # floating point numbers ], ) def test_invalid_formats_filtered_out(self, input_str, expected): """Test that various invalid formats are filtered out correctly.""" result = helpers.args_extract_label_ids(input_str) assert result == expected @pytest.mark.parametrize( "invalid_input", [ "abc,def,xyz", "invalid,values,only", "-1,-2,-3", "1.1,2.2,3.3", ",,,,", ], ) def test_no_valid_ids_raises_error(self, invalid_input): """Test that providing only invalid IDs raises ValueError.""" with pytest.raises(ValueError): helpers.args_extract_label_ids(invalid_input) @pytest.mark.parametrize( "input_str,expected", [ ("999,1,500,25", [1, 25, 500, 999]), ("789,123,456", [123, 456, 789]), ("3,1,4,1,5,9,2,6", [1, 2, 3, 4, 5, 6, 9]), # with duplicates ], ) def test_sorted_output(self, input_str, expected): """Test that output is always sorted in ascending order.""" result = helpers.args_extract_label_ids(input_str) assert result == expected def test_return_type_annotation(self): """Test that return type matches the annotation.""" result = helpers.args_extract_label_ids("123,456") assert isinstance(result, list) assert all(isinstance(id_, LabelId) for id_ in result) class TestValidateDateYearmonth: """Test the validate_date_yearmonth function.""" @pytest.mark.parametrize( "valid_date", [ "2023-01", "2023-12", "2000-01", # Year 2000 "9999-12", # Far future year "1900-06", # Historical year "2023-1", # Single digit month (valid with %Y-%m) "2025-7", # Current context date format ], ) def test_valid_formats(self, valid_date): """Test validation with valid YYYY-MM and YYYY-M formats.""" # Should not raise any exception helpers.validate_date_yearmonth(valid_date) @pytest.mark.parametrize( "invalid_date", [ "2023/01", # Wrong separator "2023.01", # Wrong separator "23-01", # 2-digit year "abcd-01", # Non-numeric year "2023-ab", # Non-numeric month "2023-00", # Invalid month (0) "2023-13", # Invalid month (13) "2023-01-01", # Includes day "2023-01-extra", # Extra characters ], ) def test_invalid_format_raises_value_error(self, invalid_date): """Test validation with various invalid formats.""" with pytest.raises(ValueError, match="Invalid date_yearmonth format"): helpers.validate_date_yearmonth(invalid_date) def test_empty_string_raises_value_error(self): """Test validation with empty string.""" with pytest.raises(ValueError, match="Invalid date_yearmonth format"): helpers.validate_date_yearmonth("") def test_none_input_raises_type_error(self): """Test validation with None input raises TypeError.""" with pytest.raises(TypeError): helpers.validate_date_yearmonth(None) # type: ignore