Skip to content

Commit

Permalink
Fix email and queue test extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofx committed Dec 4, 2023
1 parent ec2aa76 commit 0753a07
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
18 changes: 18 additions & 0 deletions src/Miru.Testing/MailingTestFixtureExtensions.cs
Expand Up @@ -11,6 +11,24 @@ namespace Miru.Testing;
[ShouldlyMethods]
public static class MailingTestFixtureExtensions
{
public static Email EnqueuedEmail(this ITestFixture fixture)
{
var mailingQueue = fixture.Get<MailingOptions>().QueueName;
var job = fixture.EnqueuedFor<EmailJob>(mailingQueue);

if (job == null)
throw new MiruException($"There is no EmailJob in the '{mailingQueue}' queue");

return job.Email;
}

public static IEnumerable<Email> EnqueuedEmails(
this ITestFixture fixture,
string queue = "default") =>
fixture
.AllEnqueuedFor<EmailJob>(queue: queue)
.Select(x => x.Email);

public static async Task SendEmailNowAsync<TMail>(this ITestFixture fixture, TMail mail)
where TMail : Mailable
{
Expand Down
31 changes: 7 additions & 24 deletions src/Miru.Testing/QueueingTestFixtureExtensions.cs
Expand Up @@ -23,34 +23,34 @@ namespace Miru.Testing
{
public static class QueueingTestFixtureExtensions
{
public static int EnqueuedCount(this ITestFixture fixture)
public static int EnqueuedCount(this ITestFixture fixture, string queue = "default")
{
return fixture.Get<JobStorage>()
.GetMonitoringApi()
.EnqueuedJobs("default", 0, 1000)
.EnqueuedJobs(queue, 0, 1000)
.Count;
}

public static bool AnyEnqueuedFor<TQueueable>(this ITestFixture fixture)
public static bool AnyEnqueuedFor<TQueueable>(this ITestFixture fixture, string queue = "default")
where TQueueable : IQueueable
{
return fixture.Get<JobStorage>()
.GetMonitoringApi()
.EnqueuedJobs("default", 0, 1000)
.EnqueuedJobs(queue, 0, 1000)
.Select(result => result.Value)
.Count(enqueueJob => enqueueJob.Job.Args[0].GetType() == typeof(TQueueable)) == 1;
}

public static TJob EnqueuedFor<TJob>(this ITestFixture fixture)
public static TJob EnqueuedFor<TJob>(this ITestFixture fixture, string queue = "default")
{
var entry = fixture.Get<JobStorage>()
.GetMonitoringApi()
.EnqueuedJobs("default", 0, 1000)
.EnqueuedJobs(queue, 0, 1000)
.Select(result => result.Value)
.FirstOrDefault(enqueueJob => enqueueJob.Job.Args[0].GetType() == typeof(TJob));

if (entry == null)
throw new ShouldAssertException($"No job queued found of type {typeof(TJob).FullName}");
throw new ShouldAssertException($"No job of type {typeof(TJob).FullName} found at queue '{queue}'");

return (TJob) entry.Job.Args[0];
}
Expand All @@ -65,22 +65,5 @@ public static TJob EnqueuedFor<TJob>(this ITestFixture fixture)
.Where(enqueueJob => enqueueJob.Job.Args[0].GetType() == typeof(TJob))
.Select(enqueueJob => (TJob) enqueueJob.Job.Args[0])
.ToList();

public static Email EnqueuedEmail(this ITestFixture fixture)
{
var job = fixture.EnqueuedFor<EmailJob>();

if (job == null)
throw new MiruException("There is no EmailJob queued");

return job.Email;
}

public static IEnumerable<Email> EnqueuedEmails(
this ITestFixture fixture,
string queue = "default") =>
fixture
.AllEnqueuedFor<EmailJob>(queue: queue)
.Select(x => x.Email);
}
}

0 comments on commit 0753a07

Please sign in to comment.