-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test for SystemTextJson serialization and projections handling …
…with records
- Loading branch information
1 parent
f133de1
commit d3c0649
Showing
3 changed files
with
130 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/EventSourcingTests/Json/SystemTextJsonRecordSerializationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Marten; | ||
using Marten.Events.Aggregation; | ||
using Marten.Events.Projections; | ||
using Marten.Linq; | ||
using Marten.Services; | ||
using Marten.Storage; | ||
using Marten.Testing.Harness; | ||
using Weasel.Core; | ||
using Xunit; | ||
using Shouldly; | ||
|
||
namespace EventSourcingTests.Json; | ||
|
||
public class SystemTextJsonRecordSerializationTests: IntegrationContext | ||
{ | ||
public SystemTextJsonRecordSerializationTests(DefaultStoreFixture fixture): base(fixture) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task ForSystemTextJson_ProjectionShouldBeUpdated() | ||
{ | ||
StoreOptions(opts => | ||
{ | ||
// Optionally configure the serializer directly | ||
opts.Serializer(new SystemTextJsonSerializer | ||
{ | ||
// Optionally override the enum storage | ||
EnumStorage = EnumStorage.AsString, | ||
|
||
// Optionally override the member casing | ||
Casing = Casing.CamelCase, | ||
}); | ||
|
||
opts.Policies.AllDocumentsAreMultiTenanted(); | ||
opts.Events.TenancyStyle = TenancyStyle.Conjoined; | ||
opts.Events.MetadataConfig.EnableAll(); | ||
opts.Schema.For<Resource>().DatabaseSchemaName("resources"); | ||
|
||
opts.Projections.Add<ResourceProjection>(ProjectionLifecycle.Inline); | ||
|
||
opts.AutoCreateSchemaObjects = AutoCreate.All; | ||
|
||
opts.DatabaseSchemaName = "fleetmonitor"; | ||
opts.Events.DatabaseSchemaName = "events"; | ||
}); | ||
|
||
var resourceId = Guid.NewGuid(); | ||
var resourceName = "Test"; | ||
|
||
theSession.Events.Append(resourceId, new ResourceCreatedEvent(resourceName)); | ||
await theSession.SaveChangesAsync(); | ||
|
||
var resource = await theSession.Query<Resource>().SingleOrDefaultAsync(r => r.Id == resourceId); | ||
|
||
resource.ShouldNotBeNull(); | ||
resource.Id.ShouldBe(resourceId); | ||
resource.Name.ShouldBe(resourceName); | ||
resource.State.ShouldBe(ResourceState.Enabled); | ||
|
||
theSession.Events.Append(resourceId, new ResourceDisabledEvent()); | ||
await theSession.SaveChangesAsync(); | ||
|
||
resource = await theSession.Query<Resource>().SingleOrDefaultAsync(r => r.Id == resourceId); | ||
|
||
resource.ShouldNotBeNull(); | ||
resource.Id.ShouldBe(resourceId); | ||
resource.Name.ShouldBe(resourceName); | ||
resource.State.ShouldBe(ResourceState.Disabled); | ||
} | ||
} | ||
|
||
public record Event; | ||
|
||
public record ResourceCreatedEvent(string Name): Event; | ||
|
||
public record ResourceRemovedEvent(): Event; | ||
|
||
public record ResourceEnabledEvent(): Event; | ||
|
||
public record ResourceDisabledEvent(): Event; | ||
|
||
public class ResourceProjection: SingleStreamAggregation<Resource> | ||
{ | ||
public ResourceProjection() | ||
{ | ||
DeleteEvent<ResourceRemovedEvent>(); | ||
|
||
Lifecycle = ProjectionLifecycle.Inline; | ||
} | ||
|
||
public void Apply(ResourceDisabledEvent e, Resource resource) => resource.State = ResourceState.Disabled; | ||
|
||
public void Apply(ResourceEnabledEvent e, Resource resource) | ||
{ | ||
resource.State = ResourceState.Enabled; | ||
} | ||
|
||
public Resource Create(ResourceCreatedEvent create) | ||
{ | ||
return new Resource { Name = create.Name, State = ResourceState.Enabled }; | ||
} | ||
} | ||
|
||
public record Resource | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } = null!; | ||
public ResourceState State { get; set; } | ||
} | ||
|
||
public enum ResourceState | ||
{ | ||
Disabled, | ||
Enabled | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters