Skip to content

Commit

Permalink
Added test for SystemTextJson serialization and projections handling …
Browse files Browse the repository at this point in the history
…with records
  • Loading branch information
oskardudycz committed Feb 18, 2023
1 parent f133de1 commit d3c0649
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 20 deletions.
16 changes: 6 additions & 10 deletions docs/configuration/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,16 @@ var store = DocumentStore.For(opts =>
opts.Connection("some connection string");

// Opt into System.Text.Json serialization
opts.UseDefaultSerialization(serializerType: SerializerType.SystemTextJson);

// Optionally configure the serializer directly
opts.Serializer(new SystemTextJsonSerializer
{
opts.UseDefaultSerialization(
serializerType: SerializerType.SystemTextJson,
// Optionally override the enum storage
EnumStorage = EnumStorage.AsString,

enumStorage: EnumStorage.AsString,
// Optionally override the member casing
Casing = Casing.CamelCase
});
casing: Casing.CamelCase
);
});
```
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/UsingSystemTextJsonSerializer.cs#L11-L31' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_stj_serialization' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/marten/blob/master/src/Marten.Testing/Examples/UsingSystemTextJsonSerializer.cs#L11-L27' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_stj_serialization' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Serializing with Jil
Expand Down
118 changes: 118 additions & 0 deletions src/EventSourcingTests/Json/SystemTextJsonRecordSerializationTests.cs
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
}
16 changes: 6 additions & 10 deletions src/Marten.Testing/Examples/UsingSystemTextJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@ internal void using_stj()
opts.Connection("some connection string");

// Opt into System.Text.Json serialization
opts.UseDefaultSerialization(serializerType: SerializerType.SystemTextJson);

// Optionally configure the serializer directly
opts.Serializer(new SystemTextJsonSerializer
{
opts.UseDefaultSerialization(
serializerType: SerializerType.SystemTextJson,
// Optionally override the enum storage
EnumStorage = EnumStorage.AsString,

enumStorage: EnumStorage.AsString,
// Optionally override the member casing
Casing = Casing.CamelCase
});
casing: Casing.CamelCase
);
});

#endregion
}
}
}

0 comments on commit d3c0649

Please sign in to comment.