Skip to content

Commit

Permalink
Add a class representing paused trigger groups along with EF configur…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
SeanFarrow committed Oct 16, 2023
1 parent d14516c commit 8ec9215
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/QuartzNet.EntityFrameworkCore/DbContexts/QuartzContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using QuartzNet.EntityFrameworkCore.Entities;

namespace QuartzNet.EntityFrameworkCore.DbContexts
Expand All @@ -14,6 +15,11 @@ public class QuartzContext : DbContext
/// </summary>
public DbSet<CronTrigger> CronTriggers => Set<CronTrigger>();

/// <summary>
/// Gets or sets the <see cref="PausedTriggerGroup"/> instances.
/// </summary>
public DbSet<PausedTriggerGroup> PausedTriggerGroups => Set<PausedTriggerGroup>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace QuartzNet.EntityFrameworkCore.Entities.Configurations;

public class PausedTriggerGroupEntityConfiguration : IEntityTypeConfiguration<PausedTriggerGroup>
{
public void Configure(EntityTypeBuilder<PausedTriggerGroup> builder)
{
builder.ToTable("PausedTriggerGroups", "Quartz");
builder.HasKey("SchedulerName", "TriggerGroup");
builder.Property(x => x.SchedulerName).IsRequired().HasMaxLength(120);
builder.Property(x => x.TriggerGroup).IsRequired().HasMaxLength(150);
}
}
32 changes: 32 additions & 0 deletions src/QuartzNet.EntityFrameworkCore/Entities/PausedTriggerGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace QuartzNet.EntityFrameworkCore.Entities;

public class PausedTriggerGroup
{
public string SchedulerName { get; }
public string TriggerGroup { get; }

public PausedTriggerGroup(string schedulerName, string triggerGroup)
{
if (schedulerName is null)
{
throw new ArgumentNullException(nameof(schedulerName));
}

if (schedulerName.Length is 0)
{
throw new ArgumentException("The schedulerName parameter cannot be an empty string.", nameof(schedulerName));
}
SchedulerName = schedulerName;

if (triggerGroup is null)
{
throw new ArgumentNullException(nameof(triggerGroup));
}

if (triggerGroup.Length is 0)
{
throw new ArgumentException("The triggerGroup parameter cannot be an empty string.", nameof(triggerGroup));
}
TriggerGroup = triggerGroup;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.EntityFrameworkCore;
using QuartzNet.EntityFrameworkCore.Entities;
using QuartzNet.EntityFrameworkCore.Entities.Configurations;

namespace QuartzNet.EntityFrameworkCore.Tests.Unit.Entities.Configurations;

public class PausedTriggerGroupEntityConfigurationTests : EntityTypeConfigurationTestsBase<PausedTriggerGroupEntityConfiguration, PausedTriggerGroup>
{
[Fact]
public void Configure_ConfiguresThePausedTriggerGroupEntityAsExpected()
{
var sut = CreateSut();
var entityBuilder = GetEntityBuilder();

sut.Configure(entityBuilder);
var metadata = entityBuilder.Metadata;

using var scope = new AssertionScope();
metadata.GetSchema().Should().Be("Quartz");
metadata.GetTableName().Should().Be("PausedTriggerGroups");
var schedulerNameProperty = metadata.FindDeclaredProperty("SchedulerName");
ValidateModelProperty(schedulerNameProperty, x =>
{
x?.IsNullable.Should().BeFalse();
x?.GetMaxLength().Should().Be(120);
x?.GetContainingKeys().Should().HaveCount(1);
});

var triggerGroupProperty = metadata.FindDeclaredProperty("TriggerGroup");
ValidateModelProperty(triggerGroupProperty, x =>
{
x?.IsNullable.Should().BeFalse();
x?.GetMaxLength().Should().Be(150);
x?.GetContainingKeys().Should().HaveCount(1);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using FluentAssertions;
using QuartzNet.EntityFrameworkCore.Entities;

namespace QuartzNet.EntityFrameworkCore.Tests.Unit.Entities;

public class PausedTriggerGroupTests
{
#region Private members.
private string _schedulerName ="test scheduler";
private string _triggerGroup ="test group";

private PausedTriggerGroup CreateSut() => new(_schedulerName, _triggerGroup);
#endregion

#region Constructor tests.
[Fact]
public void AnArgumentNullExceptionShouldBeThrownWhenTheSchedulerNamePassedInIsNull()
{
_schedulerName = null!;

var act =CreateSut;

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void AnArgumentExceptionShouldBeThrownWhenTheSchedulerNamePassedInIsAnEmptyString()
{
_schedulerName = String.Empty;

var act = CreateSut;

act.Should().Throw<ArgumentException>();
}

[Fact]
public void AnArgumentNullExceptionShouldBeThrownWhenTheTriggerGroupPassedInIsNull()
{
_triggerGroup = null!;

var act = CreateSut;

act.Should().Throw<ArgumentNullException>();
}

[Fact]
public void AnArgumentExceptionShouldBeThrownWhenTheTriggerGroupPassedInIsAnEmptyString()
{
_triggerGroup = String.Empty;

var act = CreateSut;

act.Should().Throw<ArgumentException>();
}

[Fact]
public void APausedTriggerGroupInstanceShouldBeEnstantiatedWhenAllParametersAreValid()
{
var result = CreateSut();

result.Should().NotBeNull();
}
#endregion
}

0 comments on commit 8ec9215

Please sign in to comment.