From e0cf9fc3ab96bb09bc16072982ea7f9434a65bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 23 Jan 2021 16:16:10 +0300 Subject: [PATCH] Added initial tests. --- .../OrganizationAppServiceTests.cs | 64 +++++++++++++++++++ .../Samples/SampleAppServiceTests.cs | 33 ---------- .../Samples/SampleDomainTests.cs | 45 ------------- .../Samples/SampleRepositoryTests.cs | 44 ------------- .../EventHub.TestBase/EventHubTestBase.cs | 14 +++- .../EventHub.TestBase/EventHubTestData.cs | 16 +++++ .../EventHubTestDataSeedContributor.cs | 44 +++++++++++-- 7 files changed, 132 insertions(+), 128 deletions(-) create mode 100644 eventhub/test/EventHub.Application.Tests/Organizations/OrganizationAppServiceTests.cs delete mode 100644 eventhub/test/EventHub.Application.Tests/Samples/SampleAppServiceTests.cs delete mode 100644 eventhub/test/EventHub.Domain.Tests/Samples/SampleDomainTests.cs delete mode 100644 eventhub/test/EventHub.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepositoryTests.cs create mode 100644 eventhub/test/EventHub.TestBase/EventHubTestData.cs diff --git a/eventhub/test/EventHub.Application.Tests/Organizations/OrganizationAppServiceTests.cs b/eventhub/test/EventHub.Application.Tests/Organizations/OrganizationAppServiceTests.cs new file mode 100644 index 0000000..04820ae --- /dev/null +++ b/eventhub/test/EventHub.Application.Tests/Organizations/OrganizationAppServiceTests.cs @@ -0,0 +1,64 @@ +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp; +using Volo.Abp.Application.Dtos; +using Xunit; + +namespace EventHub.Organizations +{ + public class OrganizationAppServiceTests : EventHubApplicationTestBase + { + private readonly IOrganizationAppService _organizationAppService; + private readonly EventHubTestData _testData; + + public OrganizationAppServiceTests() + { + _organizationAppService = GetRequiredService(); + _testData = GetRequiredService(); + } + + [Fact] + public async Task Should_Create_A_Valid_Organization() + { + await _organizationAppService.CreateAsync( + new CreateOrganizationDto + { + Name = "test-org-8374", + DisplayName = "Test Display Name", + Description = "Test description text that is valid and long enough!" + } + ); + + var organization = await GetOrganizationOrNullAsync("test-org-8374"); + organization.Name.ShouldBe("test-org-8374"); + organization.DisplayName.ShouldBe("Test Display Name"); + organization.Description.ShouldBe("Test description text that is valid and long enough!"); + } + + [Fact] + public async Task Should_Not_Create_Organization_With_Existing_Name() + { + var exception = await Assert.ThrowsAsync(() => + _organizationAppService.CreateAsync( + new CreateOrganizationDto + { + Name = _testData.OrganizationVolosoftName, + DisplayName = "Test Display Name", + Description = "Test description text that is valid and long enough!" + } + ) + ); + + exception.Code.ShouldBe(EventHubErrorCodes.OrganizationNameAlreadyExists); + } + + [Fact] + public async Task Should_Get_List_Of_Organizations() + { + var result = await _organizationAppService.GetListAsync(new PagedResultRequestDto()); + result.TotalCount.ShouldBeGreaterThanOrEqualTo(2); + result.Items.ShouldContain(o => o.Name == _testData.OrganizationVolosoftName && o.Id == _testData.OrganizationVolosoftId); + result.Items.ShouldContain(o => o.Name == _testData.OrganizationDotnetEuropeName && o.Id == _testData.OrganizationDotnetEuropeId); + } + } +} diff --git a/eventhub/test/EventHub.Application.Tests/Samples/SampleAppServiceTests.cs b/eventhub/test/EventHub.Application.Tests/Samples/SampleAppServiceTests.cs deleted file mode 100644 index 95361e3..0000000 --- a/eventhub/test/EventHub.Application.Tests/Samples/SampleAppServiceTests.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Shouldly; -using System.Threading.Tasks; -using Volo.Abp.Identity; -using Xunit; - -namespace EventHub.Samples -{ - /* This is just an example test class. - * Normally, you don't test code of the modules you are using - * (like IIdentityUserAppService here). - * Only test your own application services. - */ - public class SampleAppServiceTests : EventHubApplicationTestBase - { - private readonly IIdentityUserAppService _userAppService; - - public SampleAppServiceTests() - { - _userAppService = GetRequiredService(); - } - - [Fact] - public async Task Initial_Data_Should_Contain_Admin_User() - { - //Act - var result = await _userAppService.GetListAsync(new GetIdentityUsersInput()); - - //Assert - result.TotalCount.ShouldBeGreaterThan(0); - result.Items.ShouldContain(u => u.UserName == "admin"); - } - } -} diff --git a/eventhub/test/EventHub.Domain.Tests/Samples/SampleDomainTests.cs b/eventhub/test/EventHub.Domain.Tests/Samples/SampleDomainTests.cs deleted file mode 100644 index 41a16cd..0000000 --- a/eventhub/test/EventHub.Domain.Tests/Samples/SampleDomainTests.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.Threading.Tasks; -using Shouldly; -using Volo.Abp.Identity; -using Xunit; - -namespace EventHub.Samples -{ - /* This is just an example test class. - * Normally, you don't test code of the modules you are using - * (like IdentityUserManager here). - * Only test your own domain services. - */ - public class SampleDomainTests : EventHubDomainTestBase - { - private readonly IIdentityUserRepository _identityUserRepository; - private readonly IdentityUserManager _identityUserManager; - - public SampleDomainTests() - { - _identityUserRepository = GetRequiredService(); - _identityUserManager = GetRequiredService(); - } - - [Fact] - public async Task Should_Set_Email_Of_A_User() - { - IdentityUser adminUser; - - /* Need to manually start Unit Of Work because - * FirstOrDefaultAsync should be executed while db connection / context is available. - */ - await WithUnitOfWorkAsync(async () => - { - adminUser = await _identityUserRepository - .FindByNormalizedUserNameAsync("ADMIN"); - - await _identityUserManager.SetEmailAsync(adminUser, "newemail@abp.io"); - await _identityUserRepository.UpdateAsync(adminUser); - }); - - adminUser = await _identityUserRepository.FindByNormalizedUserNameAsync("ADMIN"); - adminUser.Email.ShouldBe("newemail@abp.io"); - } - } -} diff --git a/eventhub/test/EventHub.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepositoryTests.cs b/eventhub/test/EventHub.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepositoryTests.cs deleted file mode 100644 index 416e0fa..0000000 --- a/eventhub/test/EventHub.EntityFrameworkCore.Tests/EntityFrameworkCore/Samples/SampleRepositoryTests.cs +++ /dev/null @@ -1,44 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using EventHub.Users; -using Shouldly; -using System; -using System.Linq; -using System.Threading.Tasks; -using Volo.Abp.Domain.Repositories; -using Xunit; - -namespace EventHub.EntityFrameworkCore.Samples -{ - /* This is just an example test class. - * Normally, you don't test ABP framework code - * (like default AppUser repository IRepository here). - * Only test your custom repository methods. - */ - public class SampleRepositoryTests : EventHubEntityFrameworkCoreTestBase - { - private readonly IRepository _appUserRepository; - - public SampleRepositoryTests() - { - _appUserRepository = GetRequiredService>(); - } - - [Fact] - public async Task Should_Query_AppUser() - { - /* Need to manually start Unit Of Work because - * FirstOrDefaultAsync should be executed while db connection / context is available. - */ - await WithUnitOfWorkAsync(async () => - { - //Act - var adminUser = await _appUserRepository - .Where(u => u.UserName == "admin") - .FirstOrDefaultAsync(); - - //Assert - adminUser.ShouldNotBeNull(); - }); - } - } -} diff --git a/eventhub/test/EventHub.TestBase/EventHubTestBase.cs b/eventhub/test/EventHub.TestBase/EventHubTestBase.cs index 6d964ab..022ad61 100644 --- a/eventhub/test/EventHub.TestBase/EventHubTestBase.cs +++ b/eventhub/test/EventHub.TestBase/EventHubTestBase.cs @@ -1,7 +1,9 @@ using System; using System.Threading.Tasks; +using EventHub.Organizations; using Microsoft.Extensions.DependencyInjection; using Volo.Abp; +using Volo.Abp.Domain.Repositories; using Volo.Abp.Modularity; using Volo.Abp.Uow; using Volo.Abp.Testing; @@ -10,7 +12,7 @@ namespace EventHub { /* All test classes are derived from this class, directly or indirectly. */ - public abstract class EventHubTestBase : AbpIntegratedTest + public abstract class EventHubTestBase : AbpIntegratedTest where TStartupModule : IAbpModule { protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) @@ -57,5 +59,15 @@ namespace EventHub } } } + + protected virtual async Task GetOrganizationOrNullAsync(string name) + { + var organizationRepository = GetRequiredService>(); + return await WithUnitOfWorkAsync(async () => + { + return await organizationRepository.FirstOrDefaultAsync(o => o.Name == name); + } + ); + } } } diff --git a/eventhub/test/EventHub.TestBase/EventHubTestData.cs b/eventhub/test/EventHub.TestBase/EventHubTestData.cs new file mode 100644 index 0000000..4bcfc0c --- /dev/null +++ b/eventhub/test/EventHub.TestBase/EventHubTestData.cs @@ -0,0 +1,16 @@ +using System; +using Volo.Abp.DependencyInjection; + +namespace EventHub +{ + public class EventHubTestData : ISingletonDependency + { + /* ORGANIZATIONS *****************************************************/ + + public Guid OrganizationVolosoftId { get; internal set; } + public string OrganizationVolosoftName { get; } = "volosoft"; + + public Guid OrganizationDotnetEuropeId { get; internal set; } + public string OrganizationDotnetEuropeName { get; } = "dotnet-europe"; + } +} diff --git a/eventhub/test/EventHub.TestBase/EventHubTestDataSeedContributor.cs b/eventhub/test/EventHub.TestBase/EventHubTestDataSeedContributor.cs index 17cb022..a911781 100644 --- a/eventhub/test/EventHub.TestBase/EventHubTestDataSeedContributor.cs +++ b/eventhub/test/EventHub.TestBase/EventHubTestDataSeedContributor.cs @@ -1,16 +1,50 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; +using EventHub.Organizations; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Repositories; namespace EventHub { public class EventHubTestDataSeedContributor : IDataSeedContributor, ITransientDependency { - public Task SeedAsync(DataSeedContext context) + private readonly IRepository _organizationRepository; + private readonly OrganizationManager _organizationManager; + private readonly EventHubTestData _eventHubTestData; + + public EventHubTestDataSeedContributor( + IRepository organizationRepository, + EventHubTestData eventHubTestData, + OrganizationManager organizationManager) + { + _organizationRepository = organizationRepository; + _eventHubTestData = eventHubTestData; + _organizationManager = organizationManager; + } + + public async Task SeedAsync(DataSeedContext context) + { + await CreateOrganizationsAsync(); + } + + private async Task CreateOrganizationsAsync() { - /* Seed additional test data... */ + var volosoft = await _organizationManager.CreateAsync( + Guid.NewGuid(), + _eventHubTestData.OrganizationVolosoftName, + "Volosoft", + "Volosoft is producing software development tools for developers. We are organizing events related to the ABP.IO platform and general software development topic." + ); + _eventHubTestData.OrganizationVolosoftId = volosoft.Id; - return Task.CompletedTask; + var dotnetEurope = await _organizationManager.CreateAsync( + Guid.NewGuid(), + _eventHubTestData.OrganizationDotnetEuropeName, + "Dotnet Europe", + "Organizing events on Microsoft's .NET Platform in European Countries." + ); + _eventHubTestData.OrganizationDotnetEuropeId = dotnetEurope.Id; } } -} \ No newline at end of file +}