Beispiel #1
0
async def test_forward():
    sub_both = Subscription(guild_id=1,
                            channel_id=2,
                            _filter="type:test1 or type:test2")
    sub_one = Subscription(guild_id=1, channel_id=1, _filter="type:test1")

    subscriber.add_subscription(sub_both)
    subscriber.add_subscription(sub_one)

    event1 = Event(_type="test1", time=datetime.utcnow())
    event2 = Event(_type="test2", time=datetime.utcnow())
    bot = MockBot()

    with patch("bot.subscriber.format_embed", return_value=None):
        with patch("bot.subscriber.send_event", new=bot.send_event):
            await subscriber.forward(event1, bot)
            await subscriber.forward(event2, bot)

    await asyncio.sleep(2)

    assert (sub_both, event1) in bot.event_sub_pairs
    assert (sub_both, event2) in bot.event_sub_pairs

    assert (sub_one, event1) in bot.event_sub_pairs
    assert (
        sub_one, event2
    ) not in bot.event_sub_pairs, "A subscription was forwarded an event it was supposed to filter."
Beispiel #2
0
async def test_unsub():
    subs = [
        Subscription(guild_id=2, channel_id=6, _filter="type:nominate"),
        Subscription(guild_id=2, channel_id=4, _filter="user:someone"),
        Subscription(guild_id=1, channel_id=6, _filter="type:nominate")
    ]
    database = Database(BOT_TEST_DB_NAME)
    for sub in subs:
        database.insert_subscription(sub)
    subscriber.load()

    mock_message = MockMessage(
        channel=MockChannel(_id=6, guild=MockGuild(_id=2)))
    mock_command = MockCommand("unsub", context=mock_message)

    assert all(sub in subscriber.cache for sub in subs)

    assert await receive_command(mock_command)
    assert mock_command.response.startswith("✓")
    assert "🔕" in mock_command.response_embed.fields[0].name.lower()
    assert "unsubscribed from" in mock_command.response_embed.fields[
        0].name.lower()
    assert "type:nominate" in mock_command.response_embed.fields[0].value
    assert "`type:nominate`" in mock_command.response_embed.fields[0].value
    assert subs[0] not in subscriber.cache
    assert subs[1] in subscriber.cache
    assert subs[2] in subscriber.cache
Beispiel #3
0
def test_add_subscription():
    sub1 = Subscription(guild_id=1, channel_id=1, _filter="type:nominate")
    sub2 = Subscription(guild_id=1, channel_id=2, _filter="type:ranked")
    sub3 = Subscription(guild_id=1, channel_id=2, _filter="type:qualify")

    subscriber.add_subscription(sub1)
    subscriber.add_subscription(sub2)
    subscriber.add_subscription(sub3)

    assert sub1 in subscriber.cache
    assert sub2 not in subscriber.cache
    assert sub3 in subscriber.cache
Beispiel #4
0
def test_insert_retrieve_channel_sub(bot_test_database):
    sub1 = Subscription(guild_id=3,
                        channel_id=1,
                        _filter="type:problem and state:qualified")
    sub2 = Subscription(guild_id=3, channel_id=2, _filter="type:ranked")

    bot_test_database.insert_subscription(sub1)
    bot_test_database.insert_subscription(sub2)

    retrieved_sub = bot_test_database.retrieve_subscription(
        "guild_id=%s AND channel_id=%s", (3, 1))
    assert retrieved_sub == sub1
Beispiel #5
0
def test_load():
    sub1 = Subscription(guild_id=1, channel_id=1, _filter="type:nominate")
    sub2 = Subscription(guild_id=1, channel_id=2, _filter="type:ranked")

    database = Database(BOT_TEST_DB_NAME)
    database.insert_subscription(sub1)
    database.insert_subscription(sub2)

    subscriber.load()

    assert sub1 in subscriber.cache
    assert sub2 in subscriber.cache
Beispiel #6
0
def test_insert_retrieve_channel_subs(bot_test_database):
    sub1 = Subscription(guild_id=3,
                        channel_id=1,
                        _filter="type:problem and state:qualified")
    sub2 = Subscription(guild_id=3, channel_id=2, _filter="type:ranked")

    bot_test_database.insert_subscription(sub1)
    bot_test_database.insert_subscription(sub2)

    retrieved_subs = bot_test_database.retrieve_subscriptions()
    assert next(retrieved_subs, None) == sub1
    assert next(retrieved_subs, None) == sub2
    assert next(retrieved_subs, None) is None
Beispiel #7
0
def test_add_subscription_dm_channel():
    sub = Subscription(guild_id=None, channel_id=1, _filter="type:nominate")

    with pytest.raises(ValueError) as err:
        subscriber.add_subscription(sub)

    assert "DM channel" in str(err)
Beispiel #8
0
def test_remove_subscription():
    sub1 = Subscription(guild_id=1, channel_id=1, _filter="type:nominate")

    subscriber.add_subscription(sub1)
    assert sub1 in subscriber.cache

    subscriber.remove_subscription(sub1)
    assert sub1 not in subscriber.cache
Beispiel #9
0
def unsubscribe(
        channel_or_subscription: Union[TextChannel, Subscription]) -> None:
    """Deletes a channel and its filter from the subscription table of the database and reloads the cache."""
    sub = channel_or_subscription
    if isinstance(channel_or_subscription, TextChannel):
        channel = channel_or_subscription
        sub = Subscription(guild_id_or_none(channel), channel.id, None)
    remove_subscription(sub)
Beispiel #10
0
def test_insert_retrieve_channel_sub_no_filter(bot_test_database):
    sub = Subscription(guild_id=1, channel_id=1, _filter=None)

    # A subscription should always have an explicit filter to prevent
    # the creation of an unfiltered subscription unintentionally.
    with pytest.raises(ValueError) as err:
        bot_test_database.insert_subscription(sub)

    assert "filter cannot be falsy" in str(err).lower()
Beispiel #11
0
 def retrieve_subscriptions(self, where: str=None, where_values: tuple=None) -> Generator[Subscription, None, None]:
     """Returns a generator of all subscriptions from the database matching the given WHERE clause."""
     fetched_rows = self.retrieve_table_data(
         table        = "subscriptions",
         where        = where,
         where_values = where_values,
         selection    = "guild_id, channel_id, filter"
     )
     for row in (fetched_rows or []):
         guild_id = row[0]
         channel_id = row[1]
         _filter = row[2]
         yield Subscription(guild_id, channel_id, _filter)
Beispiel #12
0
async def test_sub_no_arg():
    subscriber.add_subscription(
        Subscription(guild_id=2,
                     channel_id=6,
                     _filter="type:(nominate or qualify)"))

    mock_message = MockMessage(
        channel=MockChannel(_id=6, guild=MockGuild(_id=2)))
    mock_command = MockCommand("sub", context=mock_message)

    assert await receive_command(mock_command)
    assert f"`{DEFAULT_PREFIX}sub <filter>`" in mock_command.response  # Should suggest these for if the user intended something else.
    assert f"`{DEFAULT_PREFIX}unsub`" in mock_command.response
    assert "🔔\u2000Current Subscription" in mock_command.response_embed.fields[
        0].name
    assert "type:(nominate or qualify)" in mock_command.response_embed.fields[
        0].value
    assert "`type:nominate or type:qualify`" in mock_command.response_embed.fields[
        0].value
Beispiel #13
0
def test_sub_str():
    sub = Subscription(guild_id=1, channel_id=3, _filter="type:nominate")
    assert "\"type:nominate\"" in str(sub)
    assert "1" in str(sub)
    assert "3" in str(sub)
Beispiel #14
0
def test_sub_eq_type_mismatch():
    sub = Subscription(guild_id=1, channel_id=3, _filter="type:nominate")
    assert sub != "not a sub"
Beispiel #15
0
def subscribe(channel: TextChannel, _filter: str) -> None:
    """Inserts a channel and filter into the subscription table of the database and updates the cache.
    Causes any new events passing the filter to be sent to the channel."""
    sub = Subscription(guild_id_or_none(channel), channel.id, _filter)
    add_subscription(sub)
Beispiel #16
0
def test_sub_hash():
    sub = Subscription(guild_id=1, channel_id=3, _filter="type:nominate")
    assert sub.__hash__()
Beispiel #17
0
def test_sub_init_str_ids():
    sub = Subscription(guild_id="1", channel_id="3", _filter="type:nominate")
    assert sub.guild_id == 1
    assert sub.channel_id == 3
Beispiel #18
0
def test_sub_eq():
    sub1 = Subscription(guild_id=1, channel_id=3, _filter="type:nominate")
    sub2 = Subscription(guild_id=1, channel_id=3, _filter="type:nominate")
    sub3 = Subscription(guild_id=1, channel_id=4, _filter="type:nominate")
    assert sub1 == sub2
    assert sub1 != sub3