You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GuildTests.cs 4.4 kB

test: Split Unit and Integration tests into separate projects (#1290) * Squashed commit of test rewrite changes fix missing priority speaker flag rewrite the TestChannelPermissionModify test add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests copy the token utils tests make the mocked entities sealed classes copy the TypeReaderTests class properly dispose the CommandService in the TypeReaderTests start writing tests for EmbedBuilder and related classes test that properties throw ArgumentException when invalid add tests for the embed length property add withFooter tests finish adding tests to EmbedBuilder fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add some tests that show that guild can be modified await async assert add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests make the mocked entities sealed classes properly dispose the CommandService in the TypeReaderTests fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests update the azure CI build script separate execution of test projects so that if one fails the other will not pass one of the unit tests failed, but the analzyer tests passed fix test that would break in different timezones enable the integration tests (only on dev branch) * Squashed commit of test rewrite changes fix missing priority speaker flag rewrite the TestChannelPermissionModify test add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests copy the token utils tests make the mocked entities sealed classes copy the TypeReaderTests class properly dispose the CommandService in the TypeReaderTests start writing tests for EmbedBuilder and related classes test that properties throw ArgumentException when invalid add tests for the embed length property add withFooter tests finish adding tests to EmbedBuilder fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add some tests that show that guild can be modified await async assert add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests make the mocked entities sealed classes properly dispose the CommandService in the TypeReaderTests fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests update the azure CI build script separate execution of test projects so that if one fails the other will not pass one of the unit tests failed, but the analzyer tests passed fix test that would break in different timezones enable the integration tests (only on dev branch) * Update mocked channels for changed SendFileAsync signature * comment out the integration tests from the build script no bot token is provided to this script, and use of integration tests in CI is questionable here * force rebuild because Azure linux build broke
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. namespace Discord
  8. {
  9. [CollectionDefinition("GuildTests", DisableParallelization = true)]
  10. public class GuildTests : IClassFixture<RestGuildFixture>
  11. {
  12. private IDiscordClient client;
  13. private IGuild guild;
  14. private readonly ITestOutputHelper output;
  15. public GuildTests(RestGuildFixture guildFixture, ITestOutputHelper output)
  16. {
  17. client = guildFixture.Client;
  18. guild = guildFixture.Guild;
  19. this.output = output;
  20. output.WriteLine($"RestGuildFixture using guild: {guild.Id}");
  21. guildFixture.Client.Log += LogAsync;
  22. }
  23. private Task LogAsync(LogMessage message)
  24. {
  25. output.WriteLine(message.ToString());
  26. return Task.CompletedTask;
  27. }
  28. /// <summary>
  29. /// Ensures that the CurrentUser is the owner of the guild.
  30. /// </summary>
  31. [Fact]
  32. public void CheckOwner()
  33. {
  34. Assert.Equal(client.CurrentUser.Id, guild.OwnerId);
  35. }
  36. /// <summary>
  37. /// Checks that a Guild can be modified to non-default values.
  38. /// </summary>
  39. [Fact]
  40. public async Task ModifyGuild()
  41. {
  42. // set some initial properties of the guild that are not the defaults
  43. await guild.ModifyAsync(x =>
  44. {
  45. x.ExplicitContentFilter = ExplicitContentFilterLevel.AllMembers;
  46. x.Name = "updated";
  47. x.DefaultMessageNotifications = DefaultMessageNotifications.MentionsOnly;
  48. x.AfkTimeout = 900; // 15 minutes
  49. x.VerificationLevel = VerificationLevel.None;
  50. });
  51. // check that they were set
  52. Assert.Equal("updated", guild.Name);
  53. Assert.Equal(ExplicitContentFilterLevel.AllMembers, guild.ExplicitContentFilter);
  54. Assert.Equal(DefaultMessageNotifications.MentionsOnly, guild.DefaultMessageNotifications);
  55. Assert.Equal(VerificationLevel.None, guild.VerificationLevel);
  56. Assert.Equal(900, guild.AFKTimeout);
  57. }
  58. /// <summary>
  59. /// Checks that the SystemChannel property of a guild can be modified.
  60. /// </summary>
  61. [Fact]
  62. public async Task ModifySystemChannel()
  63. {
  64. var systemChannel = await guild.CreateTextChannelAsync("system");
  65. // set using the Id
  66. await guild.ModifyAsync(x => x.SystemChannelId = systemChannel.Id);
  67. Assert.Equal(systemChannel.Id, guild.SystemChannelId);
  68. // unset it
  69. await guild.ModifyAsync(x => x.SystemChannelId = null);
  70. Assert.Null(guild.SystemChannelId);
  71. Assert.Null(await guild.GetSystemChannelAsync());
  72. // set using the ITextChannel
  73. await guild.ModifyAsync(x => { x.SystemChannel = new Optional<ITextChannel>(systemChannel); });
  74. Assert.Equal(systemChannel.Id, guild.SystemChannelId);
  75. await Assert.ThrowsAsync<NullReferenceException>( async () =>
  76. {
  77. await guild.ModifyAsync(x => x.SystemChannel = null);
  78. });
  79. await systemChannel.DeleteAsync();
  80. }
  81. /// <summary>
  82. /// Checks that the AFK channel of a guild can be set.
  83. /// </summary>
  84. [Fact]
  85. public async Task ModifyAfkChannel()
  86. {
  87. var afkChannel = await guild.CreateVoiceChannelAsync("afk");
  88. // set using the Id
  89. await guild.ModifyAsync(x => x.AfkChannelId = afkChannel.Id);
  90. Assert.Equal(afkChannel.Id, guild.AFKChannelId);
  91. // unset using Id
  92. await guild.ModifyAsync(x => x.AfkChannelId = null);
  93. Assert.Null(guild.AFKChannelId);
  94. Assert.Null(await guild.GetAFKChannelAsync());
  95. // the same, but with the AfkChannel property
  96. await guild.ModifyAsync(x => x.AfkChannel = new Optional<IVoiceChannel>(afkChannel));
  97. Assert.Equal(afkChannel.Id, guild.AFKChannelId);
  98. await Assert.ThrowsAsync<NullReferenceException>( async () =>
  99. {
  100. await guild.ModifyAsync(x => x.AfkChannel = null);
  101. });
  102. await afkChannel.DeleteAsync();
  103. }
  104. }
  105. }