Skip to content

Commit

Permalink
test: bypass deprecation warning in bot.search_url_callbacks() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgw committed Dec 9, 2023
1 parent 39ef1b2 commit 6b7f64d
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions test/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ def url_handler(*args, **kwargs):
return None

sopel.register_url_callback(r'https://example\.com', url_handler)
results = list(sopel.search_url_callbacks('https://example.com'))
results = list(sopel._search_url_callbacks_impl('https://example.com'))
assert len(results) == 1, 'Expected 1 handler; found %d' % len(results)
assert url_handler in results[0], 'Once registered, handler must be found'

Expand All @@ -1320,11 +1320,11 @@ def url_handler(*args, **kwargs):
return None

sopel.register_url_callback(r'https://(www\.)?example\.com', url_handler)
results = list(sopel.search_url_callbacks('https://example.com'))
results = list(sopel._search_url_callbacks_impl('https://example.com'))
assert len(results) == 1, 'Expected 1 handler; found %d' % len(results)
assert url_handler in results[0], 'Once registered, handler must be found'

results = list(sopel.search_url_callbacks('https://www.example.com'))
results = list(sopel._search_url_callbacks_impl('https://www.example.com'))
assert len(results) == 1, 'Regex pattern must match both URLs'
assert url_handler in results[0]

Expand All @@ -1338,27 +1338,27 @@ def url_handler(*args, **kwargs):
return None

sopel.register_url_callback(url_regex, url_handler)
results = list(sopel.search_url_callbacks('https://example.com'))
results = list(sopel._search_url_callbacks_impl('https://example.com'))
assert len(results) == 1, 'Expected 1 handler; found %d' % len(results)
assert url_handler in results[0], 'Once registered, handler must be found'

results = list(sopel.search_url_callbacks('https://www.example.com'))
results = list(sopel._search_url_callbacks_impl('https://www.example.com'))
assert len(results) == 1, 'Regex pattern must match both URLs'
assert url_handler in results[0]


def test_search_url_callbacks_not_found(tmpconfig):
"""Test search_url_callbacks when pattern does not match."""
sopel = bot.Sopel(tmpconfig, daemon=False)
results = sopel.search_url_callbacks('https://example.com')
results = sopel._search_url_callbacks_impl('https://example.com')
assert not list(results), 'No handler registered; must return an empty list'

def url_handler(*args, **kwargs):
return None

sopel.register_url_callback(r'https://(www\.)?example\.com', url_handler)

results = sopel.search_url_callbacks('https://not-example.com')
results = sopel._search_url_callbacks_impl('https://not-example.com')
assert not list(results), 'URL must not match any pattern'


Expand All @@ -1375,12 +1375,12 @@ def url_handler_replacement(*args, **kwargs):
sopel = bot.Sopel(tmpconfig, daemon=False)
sopel.register_url_callback(test_pattern, url_handler)

results = list(sopel.search_url_callbacks('https://www.example.com'))
results = list(sopel._search_url_callbacks_impl('https://www.example.com'))
assert url_handler in results[0]

sopel.register_url_callback(test_pattern, url_handler_replacement)

results = list(sopel.search_url_callbacks('https://www.example.com'))
results = list(sopel._search_url_callbacks_impl('https://www.example.com'))
assert len(results) == 1, 'There must be one and only one callback'
assert url_handler_replacement in results[0], (
'Handler must have been replaced')
Expand All @@ -1397,13 +1397,13 @@ def url_handler(*args, **kwargs):

# now register a pattern, make sure it still work
sopel.register_url_callback(test_pattern, url_handler)
assert list(sopel.search_url_callbacks('https://www.example.com'))
assert list(sopel._search_url_callbacks_impl('https://www.example.com'))

# unregister this pattern
sopel.unregister_url_callback(test_pattern, url_handler)

# now it is not possible to find a callback for this pattern
results = list(sopel.search_url_callbacks('https://www.example.com'))
results = list(sopel._search_url_callbacks_impl('https://www.example.com'))
assert not results, 'Unregistered URL callback must not work anymore'


Expand All @@ -1430,13 +1430,13 @@ def url_handler(*args, **kwargs):

# now register a pattern, make sure it still work
sopel.register_url_callback(test_pattern, url_handler)
assert list(sopel.search_url_callbacks('https://www.example.com'))
assert list(sopel._search_url_callbacks_impl('https://www.example.com'))

# unregister another pattern (that doesn't exist)
sopel.unregister_url_callback(r'http://localhost', url_handler)

# the existing pattern still work
assert list(sopel.search_url_callbacks('https://www.example.com'))
assert list(sopel._search_url_callbacks_impl('https://www.example.com'))


def test_unregister_url_callback_compiled_pattern(tmpconfig):
Expand All @@ -1451,12 +1451,12 @@ def url_handler(*args, **kwargs):

# now register a pattern, make sure it still work
sopel.register_url_callback(test_pattern, url_handler)
assert list(sopel.search_url_callbacks('https://www.example.com'))
assert list(sopel._search_url_callbacks_impl('https://www.example.com'))

# unregister using the compiled version
sopel.unregister_url_callback(url_regex, url_handler)

assert not list(sopel.search_url_callbacks('https://www.example.com'))
assert not list(sopel._search_url_callbacks_impl('https://www.example.com'))


def test_multiple_url_callback(tmpconfig):
Expand All @@ -1474,7 +1474,7 @@ def url_handler_global(*args, **kwargs):
sopel.register_url_callback(test_pattern_example, url_handler)
sopel.register_url_callback(test_pattern_global, url_handler_global)

results = list(sopel.search_url_callbacks('https://example.com'))
results = list(sopel._search_url_callbacks_impl('https://example.com'))
assert len(results) == 2
handlers = [result[0] for result in results]

Expand All @@ -1484,7 +1484,7 @@ def url_handler_global(*args, **kwargs):
# now unregister one of them: the other must still work
sopel.unregister_url_callback(test_pattern_example, url_handler)

results = list(sopel.search_url_callbacks('https://example.com'))
results = list(sopel._search_url_callbacks_impl('https://example.com'))
assert len(results) == 1, 'Exactly one handler must remain'
assert url_handler_global in results[0], 'Wrong remaining handler'

Expand All @@ -1504,7 +1504,7 @@ def url_handler(*args, **kwargs):

# register a callback manually
sopel.memory['url_callbacks'][re.compile(test_pattern)] = url_handler
results = list(sopel.search_url_callbacks("https://www.example.com"))
results = list(sopel._search_url_callbacks_impl("https://www.example.com"))
assert not results, "Manually registered callback must not be found"


Expand Down

0 comments on commit 6b7f64d

Please sign in to comment.