Skip to content

Commit

Permalink
add some tests for new qlc functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Th3-M4jor committed May 22, 2024
1 parent c305e39 commit 8bb06d6
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 44 deletions.
29 changes: 20 additions & 9 deletions lib/nostrum/cache/message_cache.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,22 @@ defmodule Nostrum.Cache.MessageCache do
@spec get_by_channel(Channel.id(), timestamp_like(), timestamp_like() | :infinity) :: [
Message.t()
]
def get_by_channel(channel_id, after_timestamp \\ 0, before_timestamp \\ :infinity) do
def get_by_channel(
channel_id,
after_timestamp \\ 0,
before_timestamp \\ :infinity,
cache \\ @configured_cache
) do
after_timestamp = timestamp_like_to_snowflake(after_timestamp)
before_timestamp = timestamp_like_to_snowflake(before_timestamp)

unsorted_result =
wrap_qlc(fn ->
wrap_qlc(cache, fn ->
:nostrum_message_cache_qlc.by_channel(
channel_id,
after_timestamp,
before_timestamp,
@configured_cache
cache
)
|> :qlc.e()
end)
Expand All @@ -177,17 +182,22 @@ defmodule Nostrum.Cache.MessageCache do
@spec get_by_author(User.id(), timestamp_like(), timestamp_like() | :infinity) :: [
Message.t()
]
def get_by_author(author_id, after_timestamp \\ 0, before_timestamp \\ :infinity) do
def get_by_author(
author_id,
after_timestamp \\ 0,
before_timestamp \\ :infinity,
cache \\ @configured_cache
) do
after_timestamp = timestamp_like_to_snowflake(after_timestamp)
before_timestamp = timestamp_like_to_snowflake(before_timestamp)

unsorted_result =
wrap_qlc(fn ->
wrap_qlc(cache, fn ->
:nostrum_message_cache_qlc.by_author(
author_id,
after_timestamp,
before_timestamp,
@configured_cache
cache
)
|> :qlc.e()
end)
Expand All @@ -211,19 +221,20 @@ defmodule Nostrum.Cache.MessageCache do
channel_id,
author_id,
after_timestamp \\ 0,
before_timestamp \\ :infinity
before_timestamp \\ :infinity,
cache \\ @configured_cache
) do
after_timestamp = timestamp_like_to_snowflake(after_timestamp)
before_timestamp = timestamp_like_to_snowflake(before_timestamp)

unsorted_result =
wrap_qlc(fn ->
wrap_qlc(cache, fn ->
:nostrum_message_cache_qlc.by_channel_and_author(
channel_id,
author_id,
after_timestamp,
before_timestamp,
@configured_cache
cache
)
|> :qlc.e()
end)
Expand Down
10 changes: 5 additions & 5 deletions src/nostrum_message_cache_qlc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,21 @@ by_channel_and_author(RequestedChannelId, RequestedUserId, After, Before, Cache)
% with a message id greater than After and less than Before.
-spec by_author(
'Elixir.Nostrum.Struct.User':id(),
Before :: non_neg_integer(),
After :: non_neg_integer(),
Before :: non_neg_integer(),
module()
) -> qlc:query_handle().
by_author(RequestedUserId, Before, After, ?MNESIA_CACHE) ->
by_author(RequestedUserId, After, Before, ?MNESIA_CACHE) ->
qlc:q([
{MessageId, Message}
Message
|| {_Tag, MessageId, _ChannelId, AuthorId, Message} <- ?MNESIA_CACHE:query_handle(),
AuthorId =:= RequestedUserId,
MessageId =< Before,
MessageId >= After
]);
by_author(RequestedUserId, Before, After, Cache) ->
by_author(RequestedUserId, After, Before, Cache) ->
qlc:q([
{MessageId, Message}
Message
|| {MessageId, #{author := #{id := AuthorId}} = Message} <- Cache:query_handle(),
AuthorId =:= RequestedUserId,
MessageId =< Before,
Expand Down
Loading

0 comments on commit 8bb06d6

Please sign in to comment.