|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using CosmosDbExample; |
| 5 | +using CosmosDbExample.Data; |
| 6 | +using CosmosDbExample.Models; |
| 7 | +using JetBrains.Annotations; |
| 8 | +using JsonApiDotNetCore.Resources; |
| 9 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +#pragma warning disable AV1115 // Member or local function contains the word 'and', which suggests doing multiple things |
| 14 | + |
| 15 | +namespace CosmosDbTests |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Test fixture that deletes and recreates the TodoItemDB database and PeopleAndTodoItems container before all of the test methods are run. It does not |
| 19 | + /// delete the database nor the container at the end. |
| 20 | + /// </summary> |
| 21 | + [PublicAPI] |
| 22 | + public class CosmosDbFixture : WebApplicationFactory<Startup>, IAsyncLifetime |
| 23 | + { |
| 24 | + public Guid BonnieId { get; } = Guid.NewGuid(); |
| 25 | + |
| 26 | + public Guid ClydeId { get; } = Guid.NewGuid(); |
| 27 | + |
| 28 | + public Guid TodoItemOwnedByBonnieId { get; } = Guid.NewGuid(); |
| 29 | + |
| 30 | + public Guid TodoItemOwnedByClydeId { get; } = Guid.NewGuid(); |
| 31 | + |
| 32 | + public Guid TodoItemWithOwnerAndAssigneeId { get; } = Guid.NewGuid(); |
| 33 | + |
| 34 | + public async Task InitializeAsync() |
| 35 | + { |
| 36 | + await RunOnDatabaseAsync(async context => |
| 37 | + { |
| 38 | + await context.Database.EnsureDeletedAsync(); |
| 39 | + await context.Database.EnsureCreatedAsync(); |
| 40 | + |
| 41 | + Person bonnie = new() |
| 42 | + { |
| 43 | + Id = BonnieId, |
| 44 | + FirstName = "Bonnie", |
| 45 | + LastName = "Parker" |
| 46 | + }; |
| 47 | + |
| 48 | + Person clyde = new() |
| 49 | + { |
| 50 | + Id = ClydeId, |
| 51 | + FirstName = "Clyde", |
| 52 | + LastName = "Barrow" |
| 53 | + }; |
| 54 | + |
| 55 | + TodoItem todoItemOwnedByBonnie = new() |
| 56 | + { |
| 57 | + Id = TodoItemOwnedByBonnieId, |
| 58 | + Description = "Rob bank", |
| 59 | + Priority = TodoItemPriority.High, |
| 60 | + OwnerId = bonnie.Id, |
| 61 | + Tags = new HashSet<Tag> |
| 62 | + { |
| 63 | + new() |
| 64 | + { |
| 65 | + Name = "Job" |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + TodoItem todoItemOwnedByClyde = new() |
| 71 | + { |
| 72 | + Id = TodoItemOwnedByClydeId, |
| 73 | + Description = "Wash car", |
| 74 | + Priority = TodoItemPriority.Low, |
| 75 | + OwnerId = clyde.Id |
| 76 | + }; |
| 77 | + |
| 78 | + TodoItem todoItemWithOwnerAndAssignee = new() |
| 79 | + { |
| 80 | + Id = TodoItemWithOwnerAndAssigneeId, |
| 81 | + Description = "Go shopping", |
| 82 | + Priority = TodoItemPriority.Medium, |
| 83 | + OwnerId = clyde.Id, |
| 84 | + AssigneeId = bonnie.Id, |
| 85 | + Tags = new HashSet<Tag> |
| 86 | + { |
| 87 | + new() |
| 88 | + { |
| 89 | + Name = "Errands" |
| 90 | + }, |
| 91 | + new() |
| 92 | + { |
| 93 | + Name = "Groceries" |
| 94 | + } |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + // @formatter:off |
| 99 | + |
| 100 | + context.People.AddRange(bonnie, clyde); |
| 101 | + context.TodoItems.AddRange(todoItemOwnedByBonnie, todoItemOwnedByClyde, todoItemWithOwnerAndAssignee); |
| 102 | + |
| 103 | + // @formatter:on |
| 104 | + |
| 105 | + await context.SaveChangesAsync(); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + public Task DisposeAsync() |
| 110 | + { |
| 111 | + return Task.CompletedTask; |
| 112 | + } |
| 113 | + |
| 114 | + public async Task<TEntity> CreateEntityAsync<TEntity>(TEntity entity) |
| 115 | + where TEntity : IIdentifiable |
| 116 | + { |
| 117 | + await RunOnDatabaseAsync(async context => |
| 118 | + { |
| 119 | + // ReSharper disable once MethodHasAsyncOverload |
| 120 | + context.Add(entity); |
| 121 | + await context.SaveChangesAsync(); |
| 122 | + }); |
| 123 | + |
| 124 | + return entity; |
| 125 | + } |
| 126 | + |
| 127 | + public async Task RunOnDatabaseAsync(Func<AppDbContext, Task> asyncAction) |
| 128 | + { |
| 129 | + using IServiceScope scope = Services.CreateScope(); |
| 130 | + var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>(); |
| 131 | + |
| 132 | + await asyncAction(dbContext); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments