-  
 -   Notifications  
You must be signed in to change notification settings  - Fork 159
 
Refactor tests #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
   Merged  
     Merged  
 Refactor tests #941
Changes from 1 commit
 Commits 
  Show all changes 
  48 commits   Select commit Hold shift + click to select a range 
 355083c  Refactored tests for HttpReadOnly/NoHttpPost/Patch/Delete controller … 
  90274c6  Refactored tests for casing convention 
  4bdb3e8  Refactored tests for custom exception handler 
  cbe01c2  Refactored tests for nulls/default query string parameters; removed s… 
  0c5d5e8  Refactored tests for DisableQueryStringAttribute 
  7057c89  Refactored tests for resource injection 
  8756f25  Fixed assertions on DateTime/DateTimeOffset in tests 
  44653f8  Refactored tests for non-json:api controllers 
  22738b6  Refactored tests for ActionResult usage 
  e12d27f  Refactored tests for custom routes 
  ddcd4ac  Refactored tests for links rendering 
  8b443d8  Refactored tests for exception handling in serializer 
  64e09db  Refactored tests for serialization 
  8bf0989  Refactored tests for resource hooks 
  255ed36  General cleanup 
  c7000b8  Extracted test building blocks in shared project 
  db9778e  Auto-adjust namespaces 
  d9a2ac7  Refactored tests for service discovery 
  53f8c82  Refactored tests for no EF Core 
  86bac20  Renamed IntegrationTestContext to ExampleIntegrationTestContext becau… 
  d94235d  Adjusted test names for ModelStateValidationTests 
  c8a6012  Adjusted test names for pagination 
  cb983b9  Adjusted test names for meta 
  5ad5c76  Fixed broken tests; simpler bootstrap setup 
  838a158  Adjusted test names for hooks 
  d3ff09a  Enable concurrent testruns (makes running all tests 60% faster) 
  64c5b4a  Removed unused using 
  e2a2bb0  Merge branch 'master' into refactor-tests 
  bcb7320  Cleanup tests for hooks 
  60ac856  Cleanup example models 
  de40fb7  Revert "Enable concurrent testruns (makes running all tests 60% faste… 
  bd11b52  fixed failing testrunner 
  b0ac024  Enable running integration tests in parallel 
  f343d08  test 
  38c127a  Disable duplicate builds 
  9ee3fc5  Revert "Disable duplicate builds" 
  59fccd8  Addressed review feedback 
  21b94c5  Moved integration tests for filter/include/page/sort/fields into Quer… 
  a044cf0  Move types into separate files 
  78bc0c3  Moved unit tests for query string parameters into ExampleTests projec… 
  c2da909  Refactored query string integration tests to use private models. Chan… 
  e594391  Refactored query string unit tests to use private models. 
  f2fd733  Cleanup models in example project 
  f97b81f  Addressed review feedback 
  2516d2e  Rename 
  114dc07  Refactored tests for logging 
  37487cf  Refactored tests for meta 
  ea404bf  renamed ResourceHook tests and fixed a few wrong assertions 
  maurei File filter
Filter by extension
Conversations
 Failed to load comments.  
    Loading  
 Jump to
  Jump to file  
  Failed to load files.  
    Loading  
 Diff view
Diff view
 Refactored tests for custom exception handler 
 - Loading branch information
 
There are no files selected for viewing
   80 changes: 0 additions & 80 deletions  80   test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/CustomErrorHandlingTests.cs          
  This file was deleted.
  Oops, something went wrong.  
    37 changes: 37 additions & 0 deletions  37   ...ApiDotNetCoreExampleTests/IntegrationTests/ExceptionHandling/AlternateExceptionHandler.cs          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System; | ||
| using JsonApiDotNetCore.Configuration; | ||
| using JsonApiDotNetCore.Middleware; | ||
| using JsonApiDotNetCore.Serialization.Objects; | ||
| using Microsoft.Extensions.Logging; | ||
|   |  ||
| namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling | ||
| { | ||
| public sealed class AlternateExceptionHandler : ExceptionHandler | ||
| { | ||
| public AlternateExceptionHandler(ILoggerFactory loggerFactory, IJsonApiOptions options) | ||
| : base(loggerFactory, options) | ||
| { | ||
| } | ||
|   |  ||
| protected override LogLevel GetLogLevel(Exception exception) | ||
| { | ||
| if (exception is ConsumerArticleIsNoLongerAvailableException) | ||
| { | ||
| return LogLevel.Warning; | ||
| } | ||
|   |  ||
| return base.GetLogLevel(exception); | ||
| } | ||
|   |  ||
| protected override ErrorDocument CreateErrorDocument(Exception exception) | ||
| { | ||
| if (exception is ConsumerArticleIsNoLongerAvailableException articleException) | ||
| { | ||
| articleException.Errors[0].Meta.Data.Add("support", | ||
| $"Please contact us for info about similar articles at {articleException.SupportEmailAddress}."); | ||
| } | ||
|   |  ||
| return base.CreateErrorDocument(exception); | ||
| } | ||
| } | ||
| } | 
   11 changes: 11 additions & 0 deletions  11   test/JsonApiDotNetCoreExampleTests/IntegrationTests/ExceptionHandling/ConsumerArticle.cs          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using JsonApiDotNetCore.Resources; | ||
| using JsonApiDotNetCore.Resources.Annotations; | ||
|   |  ||
| namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling | ||
| { | ||
| public sealed class ConsumerArticle : Identifiable | ||
| { | ||
| [Attr] | ||
| public string Code { get; set; } | ||
| } | ||
| } | 
   23 changes: 23 additions & 0 deletions  23   ...leTests/IntegrationTests/ExceptionHandling/ConsumerArticleIsNoLongerAvailableException.cs          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System.Net; | ||
| using JsonApiDotNetCore.Errors; | ||
| using JsonApiDotNetCore.Serialization.Objects; | ||
|   |  ||
| namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling | ||
| { | ||
| public sealed class ConsumerArticleIsNoLongerAvailableException : JsonApiException | ||
| { | ||
| public string ArticleCode { get; } | ||
| public string SupportEmailAddress { get; } | ||
|   |  ||
| public ConsumerArticleIsNoLongerAvailableException(string articleCode, string supportEmailAddress) | ||
| : base(new Error(HttpStatusCode.Gone) | ||
| { | ||
| Title = "The requested article is no longer available.", | ||
| Detail = $"Article with code '{articleCode}' is no longer available." | ||
| }) | ||
| { | ||
| ArticleCode = articleCode; | ||
| SupportEmailAddress = supportEmailAddress; | ||
| } | ||
| } | ||
| } | 
   39 changes: 39 additions & 0 deletions  39   ...sonApiDotNetCoreExampleTests/IntegrationTests/ExceptionHandling/ConsumerArticleService.cs          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using JsonApiDotNetCore.Configuration; | ||
| using JsonApiDotNetCore.Hooks; | ||
| using JsonApiDotNetCore.Middleware; | ||
| using JsonApiDotNetCore.Queries; | ||
| using JsonApiDotNetCore.Repositories; | ||
| using JsonApiDotNetCore.Resources; | ||
| using JsonApiDotNetCore.Services; | ||
| using Microsoft.Extensions.Logging; | ||
|   |  ||
| namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling | ||
| { | ||
| public sealed class ConsumerArticleService : JsonApiResourceService<ConsumerArticle> | ||
| { | ||
| private const string _supportEmailAddress = "company@email.com"; | ||
|   |  ||
| public ConsumerArticleService(IResourceRepositoryAccessor repositoryAccessor, IQueryLayerComposer queryLayerComposer, | ||
| IPaginationContext paginationContext, IJsonApiOptions options, ILoggerFactory loggerFactory, | ||
| IJsonApiRequest request, IResourceChangeTracker<ConsumerArticle> resourceChangeTracker, | ||
| IResourceHookExecutorFacade hookExecutor) | ||
| : base(repositoryAccessor, queryLayerComposer, paginationContext, options, loggerFactory, request, | ||
| resourceChangeTracker, hookExecutor) | ||
| { | ||
| } | ||
|   |  ||
| public override async Task<ConsumerArticle> GetAsync(int id, CancellationToken cancellationToken) | ||
| { | ||
| var consumerArticle = await base.GetAsync(id, cancellationToken); | ||
|   |  ||
| if (consumerArticle.Code.StartsWith("X")) | ||
| { | ||
| throw new ConsumerArticleIsNoLongerAvailableException(consumerArticle.Code, _supportEmailAddress); | ||
| } | ||
|   |  ||
| return consumerArticle; | ||
| } | ||
| } | ||
| } | 
   16 changes: 16 additions & 0 deletions  16   ...piDotNetCoreExampleTests/IntegrationTests/ExceptionHandling/ConsumerArticlesController.cs          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using JsonApiDotNetCore.Configuration; | ||
| using JsonApiDotNetCore.Controllers; | ||
| using JsonApiDotNetCore.Services; | ||
| using Microsoft.Extensions.Logging; | ||
|   |  ||
| namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling | ||
| { | ||
| public sealed class ConsumerArticlesController : JsonApiController<ConsumerArticle> | ||
| { | ||
| public ConsumerArticlesController(IJsonApiOptions options, ILoggerFactory loggerFactory, | ||
| IResourceService<ConsumerArticle> resourceService) | ||
| : base(options, loggerFactory, resourceService) | ||
| { | ||
| } | ||
| } | ||
| } | 
   14 changes: 14 additions & 0 deletions  14   test/JsonApiDotNetCoreExampleTests/IntegrationTests/ExceptionHandling/ErrorDbContext.cs          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
|   |  ||
| namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling | ||
| { | ||
| public sealed class ErrorDbContext : DbContext | ||
| { | ||
| public DbSet<ConsumerArticle> ConsumerArticles { get; set; } | ||
|   |  ||
| public ErrorDbContext(DbContextOptions<ErrorDbContext> options) | ||
| : base(options) | ||
| { | ||
| } | ||
| } | ||
| } | 
   85 changes: 85 additions & 0 deletions  85   ...JsonApiDotNetCoreExampleTests/IntegrationTests/ExceptionHandling/ExceptionHandlerTests.cs          
     This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using JsonApiDotNetCore.Configuration; | ||
| using JsonApiDotNetCore.Middleware; | ||
| using JsonApiDotNetCore.Serialization.Objects; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Xunit; | ||
|   |  ||
| namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ExceptionHandling | ||
| { | ||
| public sealed class ExceptionHandlerTests | ||
| : IClassFixture<IntegrationTestContext<TestableStartup<ErrorDbContext>, ErrorDbContext>> | ||
| { | ||
| private readonly IntegrationTestContext<TestableStartup<ErrorDbContext>, ErrorDbContext> _testContext; | ||
|   |  ||
| public ExceptionHandlerTests(IntegrationTestContext<TestableStartup<ErrorDbContext>, ErrorDbContext> testContext) | ||
| { | ||
| _testContext = testContext; | ||
|   |  ||
| FakeLoggerFactory loggerFactory = null; | ||
|   |  ||
| testContext.ConfigureLogging(options => | ||
| { | ||
| loggerFactory = new FakeLoggerFactory(); | ||
|   |  ||
| options.ClearProviders(); | ||
| options.AddProvider(loggerFactory); | ||
| options.SetMinimumLevel(LogLevel.Warning); | ||
| }); | ||
|   |  ||
| testContext.ConfigureServicesBeforeStartup(services => | ||
| { | ||
| if (loggerFactory != null) | ||
| { | ||
| services.AddSingleton(_ => loggerFactory); | ||
| } | ||
| }); | ||
|   |  ||
| testContext.ConfigureServicesAfterStartup(services => | ||
| { | ||
| services.AddResourceService<ConsumerArticleService>(); | ||
| services.AddScoped<IExceptionHandler, AlternateExceptionHandler>(); | ||
| }); | ||
| } | ||
|   |  ||
| [Fact] | ||
| public async Task Logs_and_produces_error_response_for_custom_exception() | ||
| { | ||
| // Arrange | ||
| var loggerFactory = _testContext.Factory.Services.GetRequiredService<FakeLoggerFactory>(); | ||
| loggerFactory.Logger.Clear(); | ||
|   |  ||
| var consumerArticle = new ConsumerArticle | ||
| { | ||
| Code = "X123" | ||
| }; | ||
|   |  ||
| await _testContext.RunOnDatabaseAsync(async dbContext => | ||
| { | ||
| dbContext.ConsumerArticles.Add(consumerArticle); | ||
| await dbContext.SaveChangesAsync(); | ||
| }); | ||
|   |  ||
| var route = "/consumerArticles/" + consumerArticle.StringId; | ||
|   |  ||
| // Act | ||
| var (httpResponse, responseDocument) = await _testContext.ExecuteGetAsync<ErrorDocument>(route); | ||
|   |  ||
| // Assert | ||
| httpResponse.Should().HaveStatusCode(HttpStatusCode.Gone); | ||
|   |  ||
| responseDocument.Errors.Should().HaveCount(1); | ||
| responseDocument.Errors[0].StatusCode.Should().Be(HttpStatusCode.Gone); | ||
| responseDocument.Errors[0].Title.Should().Be("The requested article is no longer available."); | ||
| responseDocument.Errors[0].Detail.Should().Be("Article with code 'X123' is no longer available."); | ||
| responseDocument.Errors[0].Meta.Data["support"].Should().Be("Please contact us for info about similar articles at company@email.com."); | ||
|   |  ||
| loggerFactory.Logger.Messages.Should().HaveCount(1); | ||
| loggerFactory.Logger.Messages.Single().Text.Should().Contain("Article with code 'X123' is no longer available."); | ||
| } | ||
| } | ||
| } | ||
 Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.    
 
Uh oh!
There was an error while loading. Please reload this page.