"""Unit tests for request util.""" import pytest from abacus_contract.constants.error import ERROR_INVALID_LIMIT_OFFSET from abacus_contract.utils.request import ( escape_like_pattern, validate_pagination_params, ) def test_validate_pagination_params_success(): """Test validate_pagination_params when all parameters are valid.""" params = {'limit': 1, 'offset': 0} expected_response = {'limit': 1, 'offset': 0} res = validate_pagination_params(**params) assert res == expected_response def test_validate_pagination_params_error(): """Test validate_pagination_params for an invalid params.""" with pytest.raises(Exception, match=ERROR_INVALID_LIMIT_OFFSET): params = {'limit': 'test', 'offset': 0} validate_pagination_params(**params) @pytest.mark.parametrize( ('raw', 'expected'), [ ('foo', 'foo'), ('', ''), ('100%', r'100\%'), ('foo_bar', r'foo\_bar'), ('back\\slash', r'back\\slash'), ('%_\\', r'\%\_\\'), ('mix 50% on_track', r'mix 50\% on\_track'), # Wildcards from other systems (e.g. `*`, `?`) are NOT SQL LIKE # wildcards and must pass through unchanged. ('*?', '*?'), ], ) def test_escape_like_pattern_escapes_only_sql_wildcards(raw, expected): """Backslash, percent, and underscore are escaped; everything else passes through.""" assert escape_like_pattern(raw) == expected def test_escape_like_pattern_coerces_non_string_input(): """Non-string input (e.g. an int from a caller) is stringified before escaping.""" assert escape_like_pattern(42) == '42'