Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to verify handling of expected URL types #58

Merged
merged 3 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ source. Clone the repo first, then:
$ pip install -e .
```

#### Testing changes

To run tests, make sure you have the development dependencies as well:

```shell
$ pip install -r dev-requirements.txt
```

Run the test suite from the repo root directory:

```shell
$ pytest -v .
```

## Getting your API key
Go to the [Google Developers Console](https://console.developers.google.com/)
and create an application. When it's created, go to the APIs section, select
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest~=7.1.0
75 changes: 75 additions & 0 deletions tests/test_url_matching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Tests for Sopel's ``youtube`` plugin"""
from __future__ import annotations

import pytest

from sopel.trigger import PreTrigger


TMP_CONFIG = """
[core]
owner = Admin
nick = Sopel
enable =
youtube
host = irc.libera.chat

[youtube]
api_key = deadbeef
"""


@pytest.fixture
def bot(botfactory, configfactory):
settings = configfactory('default.ini', TMP_CONFIG)
return botfactory.preloaded(settings, ['youtube'])


@pytest.mark.parametrize('proto', ('http://', 'https://'))
@pytest.mark.parametrize('base', (
'youtube.com',
'www.youtube.com',
))
@pytest.mark.parametrize('path, expected_matches', (
('/watch?v=kbLrmesC9x0&list=PLzV2uljPvyAcwIad4RmPAZRgdQNBWHDPp&index=1&pp=iBQA', ('get_video_info', 'get_playlist_info')),
('/watch?v=dQw4w9WgXcQ', ('get_video_info',)),
('/shorts/dQw4w9WgXcQ/', ('get_video_info',)),
('/shorts/dQw4w9WgXcQ', ('get_video_info',)),
('/live/dQw4w9WgXcQ/', ('get_video_info',)),
('/live/dQw4w9WgXcQ', ('get_video_info',)),
('/playlist?list=PLzV2uljPvyAcwIad4RmPAZRgdQNBWHDPp', ('get_playlist_info',)),
))
def test_long_url_matching(proto, base, path, expected_matches, bot):
link = proto + base + path

line = PreTrigger(bot.nick, ':User!user@irc.libera.chat PRIVMSG #channel {}'.format(link))
matched_rules = [
# we can ignore matches that don't come from this plugin
match[0].get_rule_label()
for match in bot.rules.get_triggered_rules(bot, line)
if match[0].get_plugin_name() == 'youtube'
]

matched_set = set(matched_rules)
expected_set = set(expected_matches)

assert len(matched_set) == len(expected_matches)
assert matched_set == expected_set


@pytest.mark.parametrize('proto', ('http://', 'https://'))
@pytest.mark.parametrize('base', ('youtu.be',))
@pytest.mark.parametrize('trailing_slash', ('/', ''))
def test_short_url_matching(proto, base, trailing_slash, bot):
link = proto + base + '/dQw4w9WgXcQ' + trailing_slash

line = PreTrigger(bot.nick, ':User!user@irc.libera.chat PRIVMSG #channel {}'.format(link))
matched_rules = [
# we can ignore matches that don't come from this plugin
match[0].get_rule_label()
for match in bot.rules.get_triggered_rules(bot, line)
if match[0].get_plugin_name() == 'youtube'
]

assert len(matched_rules) == 1
assert matched_rules[0] == 'get_video_info'