Skip to content

Commit

Permalink
Starting on cache unit tests + some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jun 6, 2024
1 parent 910e1e2 commit e9ae2fd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
9 changes: 2 additions & 7 deletions Sample/AnotherPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sample;


public partial class AnotherPage : ContentPage
{
public AnotherPage()
{
InitializeComponent();
this.InitializeComponent();
}
}
1 change: 0 additions & 1 deletion Sample/App.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Sample"
x:Class="Sample.App">
<Application.Resources>
<ResourceDictionary>
Expand Down
8 changes: 1 addition & 7 deletions Sample/EventPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sample;

public partial class EventPage : ContentPage
{
public EventPage()
{
InitializeComponent();
this.InitializeComponent();
}
}
67 changes: 66 additions & 1 deletion tests/Shiny.Mediator.Tests/CacheRequestMiddlewareTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,71 @@
using Microsoft.Extensions.Configuration;
using Shiny.Mediator.Middleware;

namespace Shiny.Mediator.Tests;

public class CacheRequestMiddlewareTests
{

[Fact]
public async Task EndToEnd()
{
var conn = new MockConnectivity();
var fs = new MockFileSystem();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
["Cache:Shiny.Mediator.Tests.CacheRequest"] = ""
})
.Build();

var middleware = new CacheRequestMiddleware<CacheRequest, CacheResponse>(configuration, conn, fs);

// TODO: test with ICacheItem
var request = new CacheRequest();

var result1 = await middleware.Process(
request,
() => Task.FromResult(
new CacheResponse(DateTimeOffset.UtcNow.Ticks)
),
CancellationToken.None
);
await Task.Delay(2000);
var result2 = await middleware.Process(
request,
() => Task.FromResult(
new CacheResponse(DateTimeOffset.UtcNow.Ticks)
),
CancellationToken.None
);

// if cached
result1.Ticks.Should().Be(result2.Ticks);
}
}


public record CacheRequest : IRequest<CacheResponse>;
public record CacheResponse(long Ticks);

public class MockConnectivity : IConnectivity
{
public IEnumerable<ConnectionProfile> ConnectionProfiles { get; set; }// = ConnectionProfile.WiFi;
public NetworkAccess NetworkAccess { get; set; }
public event EventHandler<ConnectivityChangedEventArgs>? ConnectivityChanged;
}

public class MockFileSystem : IFileSystem
{
public Task<Stream> OpenAppPackageFileAsync(string filename)
{
throw new NotImplementedException();
}

public Task<bool> AppPackageFileExistsAsync(string filename)
{
throw new NotImplementedException();
}

public string CacheDirectory { get; } = ".";
public string AppDataDirectory { get; } = ".";
}
2 changes: 2 additions & 0 deletions tests/Shiny.Mediator.Tests/Shiny.Mediator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.9.2" PrivateAssets="all" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
Expand All @@ -25,6 +26,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Shiny.Mediator.Maui\Shiny.Mediator.Maui.csproj" />
<ProjectReference Include="..\..\src\Shiny.Mediator\Shiny.Mediator.csproj" />
<ProjectReference Include="..\..\src\Shiny.Mediator.SourceGenerators\Shiny.Mediator.SourceGenerators.csproj"
ReferenceOutputAssembly="false"
Expand Down

0 comments on commit e9ae2fd

Please sign in to comment.