-
Notifications
You must be signed in to change notification settings - Fork 0
feat: prevent duplicate inserts for canonical/reference rows (#978) #1058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dgee2
merged 5 commits into
main
from
dgee2/issue-978-prevent-duplicate-inserts-for-canonical-2d3383
May 22, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2feaeca
feat: add lookup-before-insert for Ingredient canonical rows (#978)
dgee2 c4b7169
fix: extract /api/ingredient URL into constant (S1192)
dgee2 0121b66
refactor: address Copilot PR review comments
dgee2 b8d881a
test: lock in get-or-create contract for unknown UnitIds on existing …
dgee2 428c81d
Merge branch 'main' into dgee2/issue-978-prevent-duplicate-inserts-fo…
dgee2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
114 changes: 114 additions & 0 deletions
114
backend/MenuApi.Tests/Repositories/IngredientRepositoryTests.cs
This file contains hidden or 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,114 @@ | ||
| using AwesomeAssertions; | ||
| using MenuDB; | ||
| using MenuDB.Data; | ||
| using MenuApi.Repositories; | ||
| using MenuApi.ValueObjects; | ||
| using MenuApi.ViewModel; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Xunit; | ||
|
|
||
| namespace MenuApi.Tests.Repositories; | ||
|
|
||
| public class IngredientRepositoryTests | ||
| { | ||
| [Fact] | ||
| public async Task CreateIngredientAsync_Creates_New_Ingredient_When_Name_Does_Not_Exist() | ||
| { | ||
| var cancellationToken = TestContext.Current.CancellationToken; | ||
| await using var db = CreateDbContext(); | ||
| await SeedUnitsAsync(db, cancellationToken); | ||
| var sut = new IngredientRepository(db); | ||
|
|
||
| var result = await sut.CreateIngredientAsync(new NewIngredient | ||
| { | ||
| Name = IngredientName.From("Sugar"), | ||
| UnitIds = [4], | ||
| }); | ||
|
|
||
| result.Id.Value.Should().BeGreaterThan(0); | ||
| result.Name.Should().Be(IngredientName.From("Sugar")); | ||
| result.Units.Should().ContainSingle(u => u.Name == IngredientUnitName.From("Grams")); | ||
|
|
||
| var count = await db.Ingredients.CountAsync(cancellationToken); | ||
| count.Should().Be(1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CreateIngredientAsync_Returns_Existing_Ingredient_When_Name_Already_Exists() | ||
| { | ||
| var cancellationToken = TestContext.Current.CancellationToken; | ||
| await using var db = CreateDbContext(); | ||
| await SeedUnitsAsync(db, cancellationToken); | ||
|
|
||
| var unitType = new UnitTypeEntity { Id = 99, Name = "Other" }; | ||
| var otherUnit = new UnitEntity { Id = 99, Name = "Cup", UnitTypeId = 99, UnitType = unitType }; | ||
| db.UnitTypes.Add(unitType); | ||
| db.Units.Add(otherUnit); | ||
| await db.SaveChangesAsync(cancellationToken); | ||
|
|
||
| var sut = new IngredientRepository(db); | ||
| var first = await sut.CreateIngredientAsync(new NewIngredient | ||
| { | ||
| Name = IngredientName.From("Sugar"), | ||
| UnitIds = [4], | ||
| }); | ||
|
|
||
| var second = await sut.CreateIngredientAsync(new NewIngredient | ||
| { | ||
| Name = IngredientName.From("Sugar"), | ||
| UnitIds = [99], | ||
| }); | ||
|
|
||
| second.Id.Should().Be(first.Id); | ||
| second.Units.Should().ContainSingle(u => u.Name == IngredientUnitName.From("Grams")); | ||
|
|
||
| var count = await db.Ingredients.CountAsync(i => i.Name == "Sugar", cancellationToken); | ||
| count.Should().Be(1); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CreateIngredientAsync_Returns_Existing_Ingredient_When_Name_Already_Exists_Even_With_Unknown_UnitId() | ||
| { | ||
| // UnitIds are only used when inserting a new row. When the canonical row already exists, | ||
| // the provided UnitIds are intentionally ignored — the existing ingredient is returned as-is. | ||
| var cancellationToken = TestContext.Current.CancellationToken; | ||
| await using var db = CreateDbContext(); | ||
| await SeedUnitsAsync(db, cancellationToken); | ||
|
|
||
| var sut = new IngredientRepository(db); | ||
| var first = await sut.CreateIngredientAsync(new NewIngredient | ||
| { | ||
| Name = IngredientName.From("Sugar"), | ||
| UnitIds = [4], | ||
| }); | ||
|
|
||
| var second = await sut.CreateIngredientAsync(new NewIngredient | ||
| { | ||
| Name = IngredientName.From("Sugar"), | ||
| UnitIds = [9999], // non-existent unit ID — ignored because ingredient already exists | ||
| }); | ||
|
|
||
| second.Id.Should().Be(first.Id); | ||
| second.Units.Should().ContainSingle(u => u.Name == IngredientUnitName.From("Grams")); | ||
| } | ||
|
|
||
| private static MenuDbContext CreateDbContext() | ||
| { | ||
| var options = new DbContextOptionsBuilder<MenuDbContext>() | ||
| .UseInMemoryDatabase(Guid.NewGuid().ToString()) | ||
| .Options; | ||
|
|
||
| return new MenuDbContext(options); | ||
| } | ||
|
|
||
| private static async Task SeedUnitsAsync(MenuDbContext db, CancellationToken cancellationToken) | ||
| { | ||
| var unitType = new UnitTypeEntity { Id = 3, Name = "Weight" }; | ||
| var unit = new UnitEntity { Id = 4, Name = "Grams", UnitTypeId = 3, UnitType = unitType }; | ||
|
|
||
| db.UnitTypes.Add(unitType); | ||
| db.Units.Add(unit); | ||
|
|
||
| await db.SaveChangesAsync(cancellationToken); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.