-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from dlcs/feature/humanReadableId
Change id's to be generated by sqids and an injected id generator
- Loading branch information
Showing
10 changed files
with
185 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/IIIFPresentation/API.Tests/Infrastructure/SqidsGeneratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using API.Infrastructure.IdGenerator; | ||
using FluentAssertions; | ||
using Sqids; | ||
|
||
namespace API.Tests.Infrastructure; | ||
|
||
public class SqidsGeneratorTests | ||
{ | ||
private readonly SqidsGenerator sqidsGenerator; | ||
private readonly SqidsEncoder<long> sqidsEncoder; | ||
|
||
public SqidsGeneratorTests() | ||
{ | ||
sqidsEncoder = new SqidsEncoder<long>(); | ||
sqidsGenerator = new SqidsGenerator(sqidsEncoder); | ||
} | ||
|
||
[Fact] | ||
public void Generate_GeneratesId_WithSeed() | ||
{ | ||
// Arrange | ||
var seed = new List<long>() | ||
{ | ||
1, | ||
2, | ||
3, | ||
4 | ||
}; | ||
|
||
// Act | ||
var id = sqidsGenerator.Generate(seed); | ||
var decoded = sqidsEncoder.Decode(id); | ||
|
||
// Assert | ||
id.Should().NotBeNullOrEmpty(); | ||
decoded.Should().Equal(seed); | ||
} | ||
|
||
[Fact] | ||
public void Generate_GeneratesId_WithoutSeed() | ||
{ | ||
// Act | ||
var id = sqidsGenerator.Generate(); | ||
var decoded = sqidsEncoder.Decode(id); | ||
|
||
// Assert | ||
id.Should().NotBeNullOrEmpty(); | ||
decoded.Count.Should().Be(5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/IIIFPresentation/API/Infrastructure/IdGenerator/IIdGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace API.Infrastructure.IdGenerator; | ||
|
||
public interface IIdGenerator | ||
{ | ||
string Generate(List<long>? seed = null); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/IIIFPresentation/API/Infrastructure/IdGenerator/SqidsGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Sqids; | ||
|
||
namespace API.Infrastructure.IdGenerator; | ||
|
||
public class SqidsGenerator(SqidsEncoder<long> sqids) : IIdGenerator | ||
{ | ||
private const int ListLength = 5; | ||
|
||
public string Generate(List<long>? seed = null) | ||
{ | ||
if (seed == null) | ||
{ | ||
Random rand = new Random(); | ||
seed = Enumerable.Range(0, ListLength) | ||
.Select(i => new Tuple<int, long>(rand.Next(int.MaxValue), i)) | ||
.OrderBy(i => i.Item1) | ||
.Select(i => i.Item2).ToList(); | ||
} | ||
|
||
return sqids.Encode(seed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters