Skip to content

Commit

Permalink
Add SnowflakeId Test process
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoogn committed Apr 14, 2024
1 parent a9f6ba4 commit 144eeff
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tests/SnowflakeId.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
</ItemGroup>

Expand Down
18 changes: 18 additions & 0 deletions tests/SnowflakeIdServiceExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Everyone is permitted to copy and distribute verbatim copies

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using SnowflakeId.Core;
using SnowflakeId.DependencyInjection;

Expand Down Expand Up @@ -40,6 +41,23 @@ public void Can_Add_SnowflakeId_To_Service_Collections_With_SnowflakOptions()
Assert.Equal(1, snowflakeIdOption.DataCenterId);
}

[Fact]
public void Can_Replace_SnowflakeId_Default_Registration_By_Creating_Object_That_Implement_ISnowflakeService()
{
var services = new ServiceCollection();
services.AddScoped(typeof(ISnowflakeService), sp => Mock.Of<ISnowflakeService>());

services.AddSnowflakeUniqueId(s => s.DataCenterId = 1);

var serviceProvider = services.BuildServiceProvider();

var snowflakeId = services.FirstOrDefault(desc => desc.ServiceType == typeof(ISnowflakeService));

Assert.NotNull(snowflakeId);
Assert.Equal(ServiceLifetime.Scoped, snowflakeId.Lifetime);
// Assert.IsType<SnowflakeIdService>(serviceProvider.GetRequiredService<ISnowflakeService>());
}

[Fact]
public void AddSnowflakeUniqueId_Allow_Chaining()
{
Expand Down
42 changes: 42 additions & 0 deletions tests/SnowflakeIdServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.Extensions.DependencyInjection;
using SnowflakeId.Core;
using SnowflakeId.DependencyInjection;

namespace SnowflakeId.Tests
{
public class SnowflakeIdServiceTest
{
[Fact]
public void Can_Genertate_UniqueId()
{
var services = new ServiceCollection();
services.AddSnowflakeUniqueId(s => s.DataCenterId = 1);

var serviceProvider = services.BuildServiceProvider();
var snowflakeService = serviceProvider.GetRequiredService<ISnowflakeService>();

var id = snowflakeService.GenerateSnowflakeId();
var idLength = id.ToString().Length == 19;

Assert.IsType<long>(id);
Assert.True(idLength);
}

[Fact]
public void Can_Find_Data_CenterId_From_Generated_UniqueId()
{
var services = new ServiceCollection();
services.AddSnowflakeUniqueId(s => s.DataCenterId = 17);

var serviceProvider = services.BuildServiceProvider();
var snowflakeService = serviceProvider.GetRequiredService<ISnowflakeService>();

var id = snowflakeService.GenerateSnowflakeId();
var dataCenterId = snowflakeService.GetDataCenterIdBySnowflakId(id);
Assert.IsType<int>(dataCenterId);
Assert.Equal(17, dataCenterId);

}

}
}

0 comments on commit 144eeff

Please sign in to comment.