-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/add upsert operation #124
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
Closed
kane-vo-codeleap
wants to merge
9
commits into
wemogy:main
from
jtl-software:feat/add-upsert-operation
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ea32ee
chore: added sync-fork workflow
SebastianKuesters 801df7d
Merge pull request #1 from wemogy/main
github-actions[bot] 2799b8c
Merge pull request #2 from wemogy/main
github-actions[bot] 1375b0a
feat: add upsert operation
kane-vo-codeleap 7d06c27
chore: remove sync folk
kane-vo-codeleap ce648ba
chore: remove sync folk
kane-vo-codeleap 501eb8e
fix: change tests separate from mongo
kane-vo-codeleap 5421fb2
fix: removed duplicated typeparam docs
SebastianKuesters e3f2896
fix: adjusted tests to assert all values
SebastianKuesters 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
152 changes: 152 additions & 0 deletions
152
...ogy.Infrastructure.Database.Core.UnitTests/Repositories/RepositoryTestBase.UpsertAsync.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,152 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using Wemogy.Infrastructure.Database.Core.UnitTests.Fakes.Entities; | ||
| using Xunit; | ||
|
|
||
| namespace Wemogy.Infrastructure.Database.Core.UnitTests.Repositories; | ||
|
|
||
| public partial class RepositoryTestBase | ||
| { | ||
| [Fact] | ||
| public async Task UpsertAsync_ShouldCreateIfNotExist() | ||
| { | ||
| // Arrange | ||
| await ResetAsync(); | ||
| var user = User.Faker.Generate(); | ||
|
|
||
| // Act | ||
| Exception? exception = null; | ||
| User? updatedUser = null; | ||
|
|
||
| try | ||
| { | ||
| updatedUser = await MicrosoftUserRepository.UpsertAsync( | ||
| user, | ||
| user.TenantId); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| exception = ex; | ||
| } | ||
|
|
||
| // Assert | ||
| if (exception is NotSupportedException) | ||
| { | ||
| // Expected outcome in certain implementations | ||
| return; | ||
| } | ||
|
|
||
| exception.Should().BeNull(); | ||
| updatedUser.Should().NotBeNull(); | ||
| updatedUser.Id.Should().Be(user.Id); | ||
| updatedUser.TenantId.Should().Be(user.TenantId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task UpsertAsync_ShouldReplaceIfExist() | ||
| { | ||
| // Arrange | ||
| await ResetAsync(); | ||
| var user = User.Faker.Generate(); | ||
| await MicrosoftUserRepository.CreateAsync(user); | ||
| user.Firstname = "Updated"; | ||
|
|
||
| // Act | ||
| Exception? exception = null; | ||
| User? updatedUser = null; | ||
|
|
||
| try | ||
| { | ||
| updatedUser = await MicrosoftUserRepository.UpsertAsync( | ||
| user, | ||
| user.TenantId); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| exception = ex; | ||
| } | ||
|
|
||
| // Assert | ||
| if (exception is NotSupportedException) | ||
| { | ||
| // Expected outcome in certain implementations | ||
| return; | ||
| } | ||
|
|
||
| exception.Should().BeNull(); | ||
| updatedUser.Should().NotBeNull(); | ||
| updatedUser.Firstname.Should().Be("Updated"); | ||
| updatedUser.Id.Should().Be(user.Id); | ||
| updatedUser.TenantId.Should().Be(user.TenantId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task UpsertAsyncWithoutPartitionKey_ShouldCreateIfNotExist() | ||
| { | ||
| // Arrange | ||
| await ResetAsync(); | ||
| var user = User.Faker.Generate(); | ||
|
|
||
| // Act | ||
| Exception? exception = null; | ||
| User? updatedUser = null; | ||
|
|
||
| try | ||
| { | ||
| updatedUser = await MicrosoftUserRepository.UpsertAsync(user); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| exception = ex; | ||
| } | ||
|
|
||
| // Assert | ||
| if (exception is NotSupportedException) | ||
| { | ||
| // Expected outcome in certain implementations | ||
| return; | ||
| } | ||
|
|
||
| exception.Should().BeNull(); | ||
| updatedUser.Should().NotBeNull(); | ||
| updatedUser.Id.Should().Be(user.Id); | ||
| updatedUser.TenantId.Should().Be(user.TenantId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task UpsertAsyncWithoutPartitionKey_ShouldReplaceIfExist() | ||
| { | ||
| // Arrange | ||
| await ResetAsync(); | ||
| var user = User.Faker.Generate(); | ||
| await MicrosoftUserRepository.CreateAsync(user); | ||
| user.Firstname = "Updated"; | ||
|
|
||
| // Act | ||
| Exception? exception = null; | ||
| User? updatedUser = null; | ||
|
|
||
| try | ||
| { | ||
| updatedUser = await MicrosoftUserRepository.UpsertAsync(user); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| exception = ex; | ||
| } | ||
|
|
||
| // Assert | ||
| if (exception is NotSupportedException) | ||
| { | ||
| // Expected outcome in certain implementations | ||
| return; | ||
| } | ||
|
|
||
| exception.Should().BeNull(); | ||
| updatedUser.Should().NotBeNull(); | ||
| updatedUser.Firstname.Should().Be("Updated"); | ||
| updatedUser.Id.Should().Be(user.Id); | ||
| updatedUser.TenantId.Should().Be(user.TenantId); | ||
| } | ||
| } |
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
24 changes: 24 additions & 0 deletions
24
src/core/Wemogy.Infrastructure.Database.Core/Abstractions/IDatabaseRepository`1.Upsert.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,24 @@ | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Wemogy.Infrastructure.Database.Core.Abstractions; | ||
|
|
||
| /// <summary> | ||
| /// Defines methods for upserting entities in a database repository. | ||
| /// </summary> | ||
| public partial interface IDatabaseRepository<TEntity> | ||
| { | ||
| /// <summary> | ||
| /// Upsert an entity in the database. | ||
| /// </summary> | ||
| /// <param name="entity">The entity to upsert</param> | ||
| /// <returns>The upserted entity as persisted</returns> | ||
| Task<TEntity> UpsertAsync(TEntity entity); | ||
|
|
||
| /// <summary> | ||
| /// Upsert an entity in the database, using the specified partition key. | ||
| /// </summary> | ||
| /// <param name="entity">The entity to upsert.</param> | ||
| /// <param name="partitionKey">The partition key to use for the upsert operation.</param> | ||
| /// <returns>The upserted entity as persisted</returns> | ||
| Task<TEntity> UpsertAsync(TEntity entity, string partitionKey); | ||
| } |
30 changes: 30 additions & 0 deletions
30
...e.Core/Plugins/MultiTenantDatabase/Repositories/MultiTenantDatabaseRepository`1.Upsert.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,30 @@ | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Wemogy.Infrastructure.Database.Core.Plugins.MultiTenantDatabase.Repositories; | ||
|
|
||
| /// <summary> | ||
| /// Repository for handling multi-tenant database operations for <typeparamref name="TEntity"/>. | ||
| /// </summary> | ||
| public partial class MultiTenantDatabaseRepository<TEntity> | ||
| { | ||
| /// <summary> | ||
| /// Inserts or updates the specified entity in the database. | ||
| /// </summary> | ||
| /// <param name="entity">The entity to upsert.</param> | ||
| /// <returns>The upserted entity.</returns> | ||
| public Task<TEntity> UpsertAsync(TEntity entity) | ||
| { | ||
| return _databaseRepository.UpsertAsync(entity); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Inserts or updates the specified entity in the database using the provided partition key. | ||
| /// </summary> | ||
| /// <param name="entity">The entity to upsert.</param> | ||
| /// <param name="partitionKey">The partition key to use for the operation.</param> | ||
| /// <returns>The upserted entity.</returns> | ||
| public Task<TEntity> UpsertAsync(TEntity entity, string partitionKey) | ||
| { | ||
| return _databaseRepository.UpsertAsync(entity, partitionKey); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Upsert.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,35 @@ | ||
| using System.Threading.Tasks; | ||
| using Wemogy.Infrastructure.Database.Core.Abstractions; | ||
|
|
||
| namespace Wemogy.Infrastructure.Database.Core.Repositories; | ||
|
|
||
| /// <summary> | ||
| /// Represents a repository for performing database operations on entities of type <typeparamref name="TEntity"/>. | ||
| /// </summary> | ||
| /// <typeparam name="TEntity">The type of the entity.</typeparam> | ||
| public partial class DatabaseRepository<TEntity> | ||
| where TEntity : class, IEntityBase | ||
| { | ||
| /// <summary> | ||
| /// Inserts or updates the specified entity in the database using the provided partition key. | ||
| /// </summary> | ||
| /// <param name="entity">The entity to upsert.</param> | ||
| /// <param name="partitionKey">The partition key for the entity.</param> | ||
| /// <returns>A task that represents the asynchronous operation. The task result contains the upserted entity.</returns> | ||
| public Task<TEntity> UpsertAsync(TEntity entity, string partitionKey) | ||
| { | ||
| return _database.UpsertAsync( | ||
| entity, | ||
| partitionKey); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Inserts or updates the specified entity in the database. | ||
| /// </summary> | ||
| /// <param name="entity">The entity to upsert.</param> | ||
| /// <returns>A task that represents the asynchronous operation. The task result contains the upserted entity.</returns> | ||
| public Task<TEntity> UpsertAsync(TEntity entity) | ||
| { | ||
| return _database.UpsertAsync(entity); | ||
| } | ||
| } |
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
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
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
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.