|
| 1 | +/* |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2007 - 2025 Microting A/S |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Threading.Tasks; |
| 27 | +using Microsoft.EntityFrameworkCore; |
| 28 | +using Microting.EformAngularFrontendBase.Infrastructure.Data; |
| 29 | +using Microting.EformAngularFrontendBase.Infrastructure.Data.Entities.Mailing; |
| 30 | +using NUnit.Framework; |
| 31 | + |
| 32 | +namespace Microting.EformAngularFrontendBase.Tests; |
| 33 | + |
| 34 | +[Parallelizable(ParallelScope.Fixtures)] |
| 35 | +[TestFixture] |
| 36 | +public class CasePostEmailRecipientTests : DbTestFixture |
| 37 | +{ |
| 38 | + [Test] |
| 39 | + public async Task CasePostEmailRecipient_Create_DoesCreate() |
| 40 | + { |
| 41 | + // Arrange - Create CasePost and EmailRecipient first |
| 42 | + var casePost = new CasePost |
| 43 | + { |
| 44 | + CaseId = 1, |
| 45 | + PostDate = DateTime.Now, |
| 46 | + Subject = "Test Subject", |
| 47 | + Text = "Test Text" |
| 48 | + }; |
| 49 | + DbContext.CasePosts.Add(casePost); |
| 50 | + await DbContext.SaveChangesAsync(); |
| 51 | + |
| 52 | + var emailRecipient = new EmailRecipient |
| 53 | + { |
| 54 | + Name = Guid.NewGuid().ToString(), |
| 55 | + Email = $"{Guid.NewGuid()}@test.com" |
| 56 | + }; |
| 57 | + DbContext.EmailRecipients.Add(emailRecipient); |
| 58 | + await DbContext.SaveChangesAsync(); |
| 59 | + |
| 60 | + var casePostEmailRecipient = new CasePostEmailRecipient |
| 61 | + { |
| 62 | + CasePostId = casePost.Id, |
| 63 | + EmailRecipientId = emailRecipient.Id |
| 64 | + }; |
| 65 | + |
| 66 | + // Act |
| 67 | + DbContext.CasePostEmailRecipients.Add(casePostEmailRecipient); |
| 68 | + await DbContext.SaveChangesAsync(); |
| 69 | + |
| 70 | + var dbCasePostEmailRecipient = await DbContext.CasePostEmailRecipients.AsNoTracking().FirstAsync(); |
| 71 | + |
| 72 | + // Assert |
| 73 | + Assert.That(dbCasePostEmailRecipient, Is.Not.Null); |
| 74 | + Assert.That(dbCasePostEmailRecipient.Id, Is.EqualTo(casePostEmailRecipient.Id)); |
| 75 | + Assert.That(dbCasePostEmailRecipient.CasePostId, Is.EqualTo(casePost.Id)); |
| 76 | + Assert.That(dbCasePostEmailRecipient.EmailRecipientId, Is.EqualTo(emailRecipient.Id)); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public async Task CasePostEmailRecipient_Update_DoesUpdate() |
| 81 | + { |
| 82 | + // Arrange - Create CasePost and EmailRecipient first |
| 83 | + var casePost = new CasePost |
| 84 | + { |
| 85 | + CaseId = 1, |
| 86 | + PostDate = DateTime.Now, |
| 87 | + Subject = "Test Subject", |
| 88 | + Text = "Test Text" |
| 89 | + }; |
| 90 | + DbContext.CasePosts.Add(casePost); |
| 91 | + await DbContext.SaveChangesAsync(); |
| 92 | + |
| 93 | + var emailRecipient1 = new EmailRecipient |
| 94 | + { |
| 95 | + Name = Guid.NewGuid().ToString(), |
| 96 | + Email = $"{Guid.NewGuid()}@test.com" |
| 97 | + }; |
| 98 | + var emailRecipient2 = new EmailRecipient |
| 99 | + { |
| 100 | + Name = Guid.NewGuid().ToString(), |
| 101 | + Email = $"{Guid.NewGuid()}@test.com" |
| 102 | + }; |
| 103 | + DbContext.EmailRecipients.AddRange(emailRecipient1, emailRecipient2); |
| 104 | + await DbContext.SaveChangesAsync(); |
| 105 | + |
| 106 | + var casePostEmailRecipient = new CasePostEmailRecipient |
| 107 | + { |
| 108 | + CasePostId = casePost.Id, |
| 109 | + EmailRecipientId = emailRecipient1.Id |
| 110 | + }; |
| 111 | + |
| 112 | + DbContext.CasePostEmailRecipients.Add(casePostEmailRecipient); |
| 113 | + await DbContext.SaveChangesAsync(); |
| 114 | + |
| 115 | + // Act |
| 116 | + casePostEmailRecipient.EmailRecipientId = emailRecipient2.Id; |
| 117 | + await DbContext.SaveChangesAsync(); |
| 118 | + |
| 119 | + var dbCasePostEmailRecipient = await DbContext.CasePostEmailRecipients.AsNoTracking().FirstAsync(); |
| 120 | + |
| 121 | + // Assert |
| 122 | + Assert.That(dbCasePostEmailRecipient, Is.Not.Null); |
| 123 | + Assert.That(dbCasePostEmailRecipient.Id, Is.EqualTo(casePostEmailRecipient.Id)); |
| 124 | + Assert.That(dbCasePostEmailRecipient.EmailRecipientId, Is.EqualTo(emailRecipient2.Id)); |
| 125 | + } |
| 126 | + |
| 127 | + [Test] |
| 128 | + public async Task CasePostEmailRecipient_Delete_DoesDelete() |
| 129 | + { |
| 130 | + // Arrange - Create CasePost and EmailRecipient first |
| 131 | + var casePost = new CasePost |
| 132 | + { |
| 133 | + CaseId = 1, |
| 134 | + PostDate = DateTime.Now, |
| 135 | + Subject = "Test Subject", |
| 136 | + Text = "Test Text" |
| 137 | + }; |
| 138 | + DbContext.CasePosts.Add(casePost); |
| 139 | + await DbContext.SaveChangesAsync(); |
| 140 | + |
| 141 | + var emailRecipient = new EmailRecipient |
| 142 | + { |
| 143 | + Name = Guid.NewGuid().ToString(), |
| 144 | + Email = $"{Guid.NewGuid()}@test.com" |
| 145 | + }; |
| 146 | + DbContext.EmailRecipients.Add(emailRecipient); |
| 147 | + await DbContext.SaveChangesAsync(); |
| 148 | + |
| 149 | + var casePostEmailRecipient = new CasePostEmailRecipient |
| 150 | + { |
| 151 | + CasePostId = casePost.Id, |
| 152 | + EmailRecipientId = emailRecipient.Id |
| 153 | + }; |
| 154 | + |
| 155 | + DbContext.CasePostEmailRecipients.Add(casePostEmailRecipient); |
| 156 | + await DbContext.SaveChangesAsync(); |
| 157 | + |
| 158 | + // Act |
| 159 | + DbContext.CasePostEmailRecipients.Remove(casePostEmailRecipient); |
| 160 | + await DbContext.SaveChangesAsync(); |
| 161 | + |
| 162 | + var dbCasePostEmailRecipients = await DbContext.CasePostEmailRecipients.AsNoTracking().ToListAsync(); |
| 163 | + |
| 164 | + // Assert |
| 165 | + Assert.That(dbCasePostEmailRecipients.Count, Is.EqualTo(0)); |
| 166 | + } |
| 167 | + |
| 168 | + [Test] |
| 169 | + public async Task CasePostEmailRecipient_AsNoTracking_ReturnsCorrectValues() |
| 170 | + { |
| 171 | + // Arrange - Create CasePost and EmailRecipient first |
| 172 | + var casePost = new CasePost |
| 173 | + { |
| 174 | + CaseId = 1, |
| 175 | + PostDate = DateTime.Now, |
| 176 | + Subject = "Test Subject", |
| 177 | + Text = "Test Text" |
| 178 | + }; |
| 179 | + DbContext.CasePosts.Add(casePost); |
| 180 | + await DbContext.SaveChangesAsync(); |
| 181 | + |
| 182 | + var emailRecipient = new EmailRecipient |
| 183 | + { |
| 184 | + Name = Guid.NewGuid().ToString(), |
| 185 | + Email = $"{Guid.NewGuid()}@test.com" |
| 186 | + }; |
| 187 | + DbContext.EmailRecipients.Add(emailRecipient); |
| 188 | + await DbContext.SaveChangesAsync(); |
| 189 | + |
| 190 | + var casePostEmailRecipient = new CasePostEmailRecipient |
| 191 | + { |
| 192 | + CasePostId = casePost.Id, |
| 193 | + EmailRecipientId = emailRecipient.Id |
| 194 | + }; |
| 195 | + |
| 196 | + DbContext.CasePostEmailRecipients.Add(casePostEmailRecipient); |
| 197 | + await DbContext.SaveChangesAsync(); |
| 198 | + |
| 199 | + var expectedId = casePostEmailRecipient.Id; |
| 200 | + |
| 201 | + // Act |
| 202 | + await using var newContext = GetContext(ConnectionString); |
| 203 | + var dbCasePostEmailRecipient = await newContext.CasePostEmailRecipients.AsNoTracking() |
| 204 | + .FirstOrDefaultAsync(cper => cper.CasePostId == casePost.Id && cper.EmailRecipientId == emailRecipient.Id); |
| 205 | + |
| 206 | + // Assert |
| 207 | + Assert.That(dbCasePostEmailRecipient, Is.Not.Null); |
| 208 | + Assert.That(dbCasePostEmailRecipient.Id, Is.EqualTo(expectedId)); |
| 209 | + Assert.That(dbCasePostEmailRecipient.CasePostId, Is.EqualTo(casePost.Id)); |
| 210 | + Assert.That(dbCasePostEmailRecipient.EmailRecipientId, Is.EqualTo(emailRecipient.Id)); |
| 211 | + } |
| 212 | + |
| 213 | + private BaseDbContext GetContext(string connectionStr) |
| 214 | + { |
| 215 | + var optionsBuilder = new DbContextOptionsBuilder<BaseDbContext>(); |
| 216 | + optionsBuilder.UseMySql(connectionStr, new MariaDbServerVersion( |
| 217 | + ServerVersion.AutoDetect(connectionStr))); |
| 218 | + return new BaseDbContext(optionsBuilder.Options); |
| 219 | + } |
| 220 | +} |
0 commit comments