Skip to content

Commit

Permalink
tests: Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackclaws authored and jeremydmiller committed Jan 15, 2024
1 parent 784b1c3 commit b0222ad
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 1 deletion.
69 changes: 69 additions & 0 deletions src/Http/Wolverine.Http.Tests/Marten/compiled_query_writer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Alba;
using Marten.Schema.Identity;
using Shouldly;
using WolverineWebApi.Marten;

namespace Wolverine.Http.Tests.Marten;

public class compiled_query_writer : IntegrationContext
{
public compiled_query_writer(AppFixture fixture) : base(fixture)
{
}

[Fact]
public async Task endpoint_returning_compiled_list_query_should_return_query_result()
{
await using var session = Store.LightweightSession();
int notApprovedInvoices = 5;
int approvedInvoices = 3;
for (int i = 0; i < notApprovedInvoices; i++)
{
var invoice =
new Invoice()
{
Approved = false
};
session.Store(invoice);
}

for (int i = 0; i < approvedInvoices; i++)
{
var invoice =
new Invoice()
{
Approved = true
};
session.Store(invoice);
}

await session.SaveChangesAsync();

var approvedInvoiceList = await Host.GetAsJson<List<Invoice>>("/invoices/approved");
approvedInvoiceList.ShouldNotBeNull();
approvedInvoiceList.Count.ShouldBe(approvedInvoices);
}

[Fact]
public async Task endpoint_returning_compiled_query_should_return_query_result()
{
var invoice = new Invoice()
{
Id = Guid.NewGuid()
};
using var session = Store.LightweightSession();
session.Store(invoice);
await session.SaveChangesAsync();


var invoiceCompiled = await Host.GetAsJson<Invoice>($"/invoices/compiled/{invoice.Id}");
invoiceCompiled.ShouldNotBeNull();
invoiceCompiled.Id.ShouldBe(invoice.Id);

await Host.Scenario(x =>
{
x.Get.Url($"/invoices/compiled/{Guid.NewGuid()}");
x.StatusCodeShouldBe(404);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// <auto-generated/>
#pragma warning disable
using Microsoft.AspNetCore.Routing;
using System;
using System.Linq;
using Wolverine.Http;
using Wolverine.Marten.Publishing;
using Wolverine.Runtime;

namespace Internal.Generated.WolverineHandlers
{
// START: GET_invoices_approved
public class GET_invoices_approved : Wolverine.Http.HttpHandler
{
private readonly Wolverine.Http.WolverineHttpOptions _wolverineHttpOptions;
private readonly Wolverine.Marten.Publishing.OutboxedSessionFactory _outboxedSessionFactory;
private readonly Wolverine.Runtime.IWolverineRuntime _wolverineRuntime;

public GET_invoices_approved(Wolverine.Http.WolverineHttpOptions wolverineHttpOptions, Wolverine.Marten.Publishing.OutboxedSessionFactory outboxedSessionFactory, Wolverine.Runtime.IWolverineRuntime wolverineRuntime) : base(wolverineHttpOptions)
{
_wolverineHttpOptions = wolverineHttpOptions;
_outboxedSessionFactory = outboxedSessionFactory;
_wolverineRuntime = wolverineRuntime;
}



public override async System.Threading.Tasks.Task Handle(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
var messageContext = new Wolverine.Runtime.MessageContext(_wolverineRuntime);
// Building the Marten session
await using var documentSession = _outboxedSessionFactory.OpenSession(messageContext);

// The actual HTTP request handler execution
var approvedInvoicedCompiledQuery = WolverineWebApi.Marten.InvoicesEndpoint.GetApproved();

// Run the compiled query and stream the response
await Marten.AspNetCore.QueryableExtensions.WriteArray(documentSession, approvedInvoicedCompiledQuery, httpContext);
}

}

// END: GET_invoices_approved


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// <auto-generated/>
#pragma warning disable
using Microsoft.AspNetCore.Routing;
using System;
using System.Linq;
using Wolverine.Http;
using Wolverine.Marten.Publishing;
using Wolverine.Runtime;

namespace Internal.Generated.WolverineHandlers
{
// START: GET_invoices_compiled_id
public class GET_invoices_compiled_id : Wolverine.Http.HttpHandler
{
private readonly Wolverine.Http.WolverineHttpOptions _wolverineHttpOptions;
private readonly Wolverine.Marten.Publishing.OutboxedSessionFactory _outboxedSessionFactory;
private readonly Wolverine.Runtime.IWolverineRuntime _wolverineRuntime;

public GET_invoices_compiled_id(Wolverine.Http.WolverineHttpOptions wolverineHttpOptions, Wolverine.Marten.Publishing.OutboxedSessionFactory outboxedSessionFactory, Wolverine.Runtime.IWolverineRuntime wolverineRuntime) : base(wolverineHttpOptions)
{
_wolverineHttpOptions = wolverineHttpOptions;
_outboxedSessionFactory = outboxedSessionFactory;
_wolverineRuntime = wolverineRuntime;
}



public override async System.Threading.Tasks.Task Handle(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
var messageContext = new Wolverine.Runtime.MessageContext(_wolverineRuntime);
// Building the Marten session
await using var documentSession = _outboxedSessionFactory.OpenSession(messageContext);
if (!System.Guid.TryParse((string)httpContext.GetRouteValue("id"), out var id))
{
httpContext.Response.StatusCode = 404;
return;
}



// The actual HTTP request handler execution
var byIdCompiled = WolverineWebApi.Marten.InvoicesEndpoint.GetCompiled(id);

// Run the compiled query and stream the response
await Marten.AspNetCore.QueryableExtensions.WriteOne(documentSession, byIdCompiled, httpContext);
}

}

// END: GET_invoices_compiled_id


}

37 changes: 37 additions & 0 deletions src/Http/WolverineWebApi/Marten/Documents.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq.Expressions;
using Marten;
using Marten.Linq;
using Microsoft.AspNetCore.Mvc;
using Wolverine.Http;
using Wolverine.Http.Marten;
Expand Down Expand Up @@ -54,11 +56,46 @@ public static IMartenOp Approve([Document("number")] Invoice invoice)
}

#endregion

[WolverineGet("/invoices/approved")]
public static ApprovedInvoicedCompiledQuery GetApproved()
{
return new ApprovedInvoicedCompiledQuery();
}

[WolverineGet("/invoices/compiled/{id}")]
public static ByIdCompiled GetCompiled(Guid id)
{
return new ByIdCompiled(id);
}
}

public class Invoice
{
public Guid Id { get; set; }
public bool Paid { get; set; }
public bool Approved { get; set; }
}

public class ApprovedInvoicedCompiledQuery : ICompiledListQuery<Invoice>
{
public Expression<Func<IMartenQueryable<Invoice>, IEnumerable<Invoice>>> QueryIs()
{
return q => q.Where(x => x.Approved);
}
}

public class ByIdCompiled : ICompiledQuery<Invoice, Invoice?>
{
public readonly Guid Id;

public ByIdCompiled(Guid id)
{
Id = id;
}

public Expression<Func<IMartenQueryable<Invoice>, Invoice?>> QueryIs()
{
return q => q.FirstOrDefault(x => x.Id == Id);
}
}
3 changes: 3 additions & 0 deletions src/Http/WolverineWebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Wolverine.FluentValidation;
using Wolverine.Http;
using Wolverine.Http.FluentValidation;
using Wolverine.Http.Marten;
using Wolverine.Http.Tests.DifferentAssembly.Validation;
using Wolverine.Marten;
using WolverineWebApi;
Expand Down Expand Up @@ -139,6 +140,8 @@
// Only want this middleware on endpoints on this one handler
opts.AddMiddleware(typeof(BeforeAndAfterMiddleware),
chain => chain.Method.HandlerType == typeof(MiddlewareEndpoints));

opts.UseMartenCompiledQueryResultPolicy();

#region sample_register_http_middleware_by_type
opts.AddMiddlewareByMessageType(typeof(FakeAuthenticationMiddleware));
Expand Down
2 changes: 1 addition & 1 deletion src/Http/WolverineWebApi/WolverineWebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.3.0" />
<PackageReference Include="Lamar.Diagnostics" Version="11.1.2" />
<PackageReference Include="Marten.AspNetCore" Version="6.1.0" />
<PackageReference Include="Marten.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />

Expand Down

0 comments on commit b0222ad

Please sign in to comment.