|
| 1 | +using Microsoft.Data.Sqlite; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using NSubstitute; |
| 4 | +using RustPlusBot.Domain.Connections; |
| 5 | +using RustPlusBot.Domain.Entities; |
| 6 | +using RustPlusBot.Domain.Events; |
| 7 | +using RustPlusBot.Domain.Guilds; |
| 8 | +using RustPlusBot.Domain.Servers; |
| 9 | +using RustPlusBot.Domain.Switches; |
| 10 | +using RustPlusBot.Features.Workspace.Teardown; |
| 11 | +using RustPlusBot.Persistence; |
| 12 | +using RustPlusBot.Persistence.Servers; |
| 13 | + |
| 14 | +namespace RustPlusBot.Features.Workspace.Tests.Teardown; |
| 15 | + |
| 16 | +public sealed class GuildPurgeServiceTests |
| 17 | +{ |
| 18 | + private static BotDbContext NewContext(SqliteConnection connection) |
| 19 | + { |
| 20 | + var options = new DbContextOptionsBuilder<BotDbContext>().UseSqlite(connection).Options; |
| 21 | + var context = new BotDbContext(options); |
| 22 | + context.Database.Migrate(); |
| 23 | + return context; |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task PurgeGuild_RemovesTargetGuildRows_AndLeavesOtherGuildIntact() |
| 28 | + { |
| 29 | + var connection = new SqliteConnection("DataSource=:memory:"); |
| 30 | + await connection.OpenAsync(); |
| 31 | + await using var _ = connection; |
| 32 | + await using var context = NewContext(connection); |
| 33 | + |
| 34 | + var serverA = new RustServer |
| 35 | + { |
| 36 | + GuildId = 1, Name = "A", Ip = "a", Port = 1 |
| 37 | + }; |
| 38 | + var serverB = new RustServer |
| 39 | + { |
| 40 | + GuildId = 2, Name = "B", Ip = "b", Port = 2 |
| 41 | + }; |
| 42 | + context.RustServers.AddRange(serverA, serverB); |
| 43 | + context.SmartSwitches.Add(new SmartSwitch |
| 44 | + { |
| 45 | + GuildId = 1, ServerId = serverA.Id, EntityId = 10, Name = "sw" |
| 46 | + }); |
| 47 | + context.ConnectionStates.Add(new ConnectionState |
| 48 | + { |
| 49 | + RustServerId = serverA.Id, GuildId = 1, Status = ConnectionStatus.Connected |
| 50 | + }); |
| 51 | + context.EventSubscriptions.Add(new EventSubscription |
| 52 | + { |
| 53 | + GuildId = 1, RustServerId = serverA.Id, EventKey = "cargo" |
| 54 | + }); |
| 55 | + context.EventSubscriptions.Add(new EventSubscription |
| 56 | + { |
| 57 | + GuildId = 2, RustServerId = serverB.Id, EventKey = "cargo" |
| 58 | + }); |
| 59 | + context.PairedEntities.Add(new PairedEntity |
| 60 | + { |
| 61 | + GuildId = 1, RustServerId = serverA.Id, EntityId = 5, Name = "dev" |
| 62 | + }); |
| 63 | + context.GuildSettings.Add(new GuildSettings |
| 64 | + { |
| 65 | + GuildId = 1, Culture = "en" |
| 66 | + }); |
| 67 | + context.GuildSettings.Add(new GuildSettings |
| 68 | + { |
| 69 | + GuildId = 2, Culture = "fr" |
| 70 | + }); |
| 71 | + await context.SaveChangesAsync(); |
| 72 | + |
| 73 | + var teardown = Substitute.For<IWorkspaceTeardownService>(); |
| 74 | + var service = new GuildPurgeService(context, new ServerService(context), teardown); |
| 75 | + |
| 76 | + await service.PurgeGuildAsync(1); |
| 77 | + |
| 78 | + await teardown.Received(1).ResetGuildAsync(1, Arg.Any<CancellationToken>()); |
| 79 | + Assert.Empty(await context.RustServers.Where(s => s.GuildId == 1).ToListAsync()); |
| 80 | + Assert.Empty(await context.SmartSwitches.ToListAsync()); |
| 81 | + Assert.Empty(await context.ConnectionStates.ToListAsync()); |
| 82 | + Assert.Empty(await context.EventSubscriptions.Where(e => e.GuildId == 1).ToListAsync()); |
| 83 | + Assert.Empty(await context.PairedEntities.Where(p => p.GuildId == 1).ToListAsync()); |
| 84 | + Assert.Empty(await context.GuildSettings.Where(g => g.GuildId == 1).ToListAsync()); |
| 85 | + |
| 86 | + // Guild 2 untouched. |
| 87 | + Assert.Single(await context.RustServers.Where(s => s.GuildId == 2).ToListAsync()); |
| 88 | + Assert.Single(await context.EventSubscriptions.Where(e => e.GuildId == 2).ToListAsync()); |
| 89 | + Assert.Single(await context.GuildSettings.Where(g => g.GuildId == 2).ToListAsync()); |
| 90 | + } |
| 91 | +} |
0 commit comments