def test_safe_cache_functions_with_good_and_bad_keys(self) -> None: bad_items = { "SafeFunctionsTest:\nbadkey1": 1, "SafeFunctionsTest:\nbadkey2": 2 } good_items = { "SafeFunctionsTest:goodkey1": 3, "SafeFunctionsTest:goodkey2": 4 } items = {**good_items, **bad_items} with patch('zerver.lib.cache.logger.warning') as mock_warn: safe_cache_set_many(items) mock_warn.assert_called_once() self.assertEqual( mock_warn.call_args[0][1], ['SafeFunctionsTest:\nbadkey1', 'SafeFunctionsTest:\nbadkey2'], ) with patch('zerver.lib.cache.logger.warning') as mock_warn: result = safe_cache_get_many(list(items.keys())) mock_warn.assert_called_once() self.assertEqual( mock_warn.call_args[0][1], ['SafeFunctionsTest:\nbadkey1', 'SafeFunctionsTest:\nbadkey2'], ) self.assertEqual(result, good_items)
def test_safe_cache_functions_with_good_and_bad_keys(self) -> None: bad_items = { "SafeFunctionsTest:\nbadkey1": 1, "SafeFunctionsTest:\nbadkey2": 2 } good_items = { "SafeFunctionsTest:goodkey1": 3, "SafeFunctionsTest:goodkey2": 4 } items = {**good_items, **bad_items} with self.assertLogs(level="WARNING") as m: safe_cache_set_many(items) self.assertIn( "WARNING:root:Invalid cache key used: ['SafeFunctionsTest:\\nbadkey1', 'SafeFunctionsTest:\\nbadkey2']", m.output[0], ) self.assert_length(m.output, 1) with self.assertLogs(level="WARNING") as m: result = safe_cache_get_many(list(items.keys())) self.assertEqual(result, good_items) self.assertIn( "WARNING:root:Invalid cache key used: ['SafeFunctionsTest:\\nbadkey1', 'SafeFunctionsTest:\\nbadkey2']", m.output[0], ) self.assert_length(m.output, 1)
def test_safe_cache_functions_with_all_good_keys(self) -> None: items = {"SafeFunctionsTest:key1": 1, "SafeFunctionsTest:key2": 2, "SafeFunctionsTest:key3": 3} safe_cache_set_many(items) result = safe_cache_get_many(list(items.keys())) for key, value in result.items(): self.assertEqual(value, items[key])
def test_safe_cache_functions_with_all_bad_keys(self) -> None: items = {"SafeFunctionsTest:\nbadkey1": 1, "SafeFunctionsTest:\nbadkey2": 2} with patch('zerver.lib.cache.logger.warning') as mock_warn: safe_cache_set_many(items) mock_warn.assert_called_once() warning_string = mock_warn.call_args[0][0] self.assertIn("badkey1", warning_string) self.assertIn("badkey2", warning_string) with patch('zerver.lib.cache.logger.warning') as mock_warn: result = safe_cache_get_many(list(items.keys())) mock_warn.assert_called_once() warning_string = mock_warn.call_args[0][0] self.assertIn("badkey1", warning_string) self.assertIn("badkey2", warning_string) self.assertEqual(result, {})
def test_safe_cache_functions_with_all_bad_keys(self) -> None: items = { "SafeFunctionsTest:\nbadkey1": 1, "SafeFunctionsTest:\nbadkey2": 2 } with self.assertLogs(level="WARNING") as m: safe_cache_set_many(items) self.assertIn( 'WARNING:root:Invalid cache key used: [\'SafeFunctionsTest:\\nbadkey1\', \'SafeFunctionsTest:\\nbadkey2\']', m.output[0]) self.assertEqual(len(m.output), 1) with self.assertLogs(level="WARNING") as m: result = safe_cache_get_many(list(items.keys())) self.assertEqual(result, {}) self.assertIn( 'WARNING:root:Invalid cache key used: [\'SafeFunctionsTest:\\nbadkey1\', \'SafeFunctionsTest:\\nbadkey2\']', m.output[0]) self.assertEqual(len(m.output), 1)