Skip to content

Commit

Permalink
Storage action compliance tests are all green for Marten!
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jan 3, 2025
1 parent 3e90adf commit b7be46b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using IntegrationTests;
using Marten;
using Wolverine;
using Wolverine.ComplianceTests;
using Wolverine.Marten;

namespace MartenTests;

public class using_storage_return_types_and_entity_attributes : StorageActionCompliance
{
protected override void configureWolverine(WolverineOptions opts)
{
opts.Services.AddMarten(m =>
{
m.Connection(Servers.PostgresConnectionString);
m.DatabaseSchemaName = "todos";
m.DisableNpgsqlLogging = true;
}).IntegrateWithWolverine();

opts.Policies.AutoApplyTransactions();
}

public override async Task<Todo?> Load(Guid id)
{
var store = Host.DocumentStore();
await using var session = store.QuerySession();
return await session.LoadAsync<Todo>(id);
}

public override async Task Persist(Todo todo)
{
var store = Host.DocumentStore();
await using var session = store.LightweightSession();
session.Store(todo);
await session.SaveChangesAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,39 @@ public Frame DetermineDeleteFrame(Variable variable, IServiceContainer container

public Frame DetermineStorageActionFrame(Type entityType, Variable action)
{
throw new NotImplementedException();
var method = typeof(MartenStorageActionApplier).GetMethod("ApplyAction")
.MakeGenericMethod(entityType);

var call = new MethodCall(typeof(MartenStorageActionApplier), method);
call.Arguments[1] = action;

return call;
}

}

public static class MartenStorageActionApplier
{
public static void ApplyAction<T>(IDocumentSession session, IStorageAction<T> action)
{
if (action.Entity == null) return;

switch (action.Action)
{
case StorageAction.Delete:
session.Delete(action.Entity!);
break;
case StorageAction.Insert:
session.Insert(action.Entity);
break;
case StorageAction.Store:
session.Store(action.Entity);
break;
case StorageAction.Update:
session.Update(action.Entity);
break;

}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Wolverine/ISideEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public void Apply(IReadOnlyList<IChain> chains, GenerationRules rules, IServiceC
{
if (effect.VariableType.CanBeCastTo(typeof(ISideEffectAware)))
{
if (!Storage.TryApply(effect, rules, container))
if (!Storage.TryApply(effect, rules, container, chain))
{
var applier = typeof(Applier<>).CloseAndBuildAs<IApplier>(effect.VariableType);

effect.UseReturnAction(v => applier.Apply(chain, effect, rules, container));
var frame = applier.Apply(chain, effect, rules, container);
effect.UseReturnAction(v => frame);
}
}
else
Expand Down
7 changes: 6 additions & 1 deletion src/Wolverine/Persistence/SideEffects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static class Storage
public static Delete<T> Delete<T>(T entity) => new Delete<T>(entity);
public static Nothing<T> Nothing<T>() => new();

internal static bool TryApply(Variable effect, GenerationRules rules, IServiceContainer container)
internal static bool TryApply(Variable effect, GenerationRules rules, IServiceContainer container, IChain chain)
{
if (effect.VariableType.Closes(typeof(IStorageAction<>)) &&
effect.VariableType.GetGenericTypeDefinition() == typeof(IStorageAction<>))
Expand All @@ -100,6 +100,7 @@ internal static bool TryApply(Variable effect, GenerationRules rules, IServiceCo
if (rules.TryFindPersistenceFrameProvider(container, entityType, out var provider))
{
effect.UseReturnAction(v => provider.DetermineStorageActionFrame(entityType, effect).WrapIfNotNull(effect));
provider.ApplyTransactionSupport(chain, container);
return true;
}

Expand Down Expand Up @@ -150,6 +151,7 @@ public static Frame BuildFrame(IChain chain, Variable variable, GenerationRules
{
if (rules.TryFindPersistenceFrameProvider(container, typeof(T), out var provider))
{
provider.ApplyTransactionSupport(chain, container);
var value = new EntityVariable(variable);
return provider.DetermineStoreFrame(value, container).WrapIfNotNull(variable);
}
Expand All @@ -167,6 +169,7 @@ public static Frame BuildFrame(IChain chain, Variable variable, GenerationRules
{
if (rules.TryFindPersistenceFrameProvider(container, typeof(T), out var provider))
{
provider.ApplyTransactionSupport(chain, container);
var value = new EntityVariable(variable);
return provider.DetermineDeleteFrame(value, container).WrapIfNotNull(variable);
}
Expand All @@ -185,6 +188,7 @@ public static Frame BuildFrame(IChain chain, Variable variable, GenerationRules
{
if (rules.TryFindPersistenceFrameProvider(container, typeof(T), out var provider))
{
provider.ApplyTransactionSupport(chain, container);
var value = new EntityVariable(variable);
return provider.DetermineInsertFrame(value, container).WrapIfNotNull(variable);
}
Expand All @@ -202,6 +206,7 @@ public static Frame BuildFrame(IChain chain, Variable variable, GenerationRules
{
if (rules.TryFindPersistenceFrameProvider(container, typeof(T), out var provider))
{
provider.ApplyTransactionSupport(chain, container);
var value = new EntityVariable(variable);
var frame = provider.DetermineUpdateFrame(value, container).WrapIfNotNull(variable);
return frame;
Expand Down

0 comments on commit b7be46b

Please sign in to comment.