public async void UpdateTopic() { var theme = new Theme { Title = "New theme", }; var topic = new Topic { Title = "Old title", Slug = "old-title", Theme = new Theme { Title = "Old theme" } }; var contextId = Guid.NewGuid().ToString(); await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { context.Add(theme); context.Add(topic); await context.SaveChangesAsync(); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { var service = SetupTopicService(context); var result = await service.UpdateTopic( topic.Id, new SaveTopicViewModel { Title = "New title", ThemeId = theme.Id } ); Assert.True(result.IsRight); Assert.Equal(topic.Id, result.Right.Id); Assert.Equal("New title", result.Right.Title); Assert.Equal("new-title", result.Right.Slug); Assert.Equal(theme.Id, result.Right.ThemeId); var savedTopic = await context.Topics.FindAsync(result.Right.Id); Assert.Equal("New title", savedTopic.Title); Assert.Equal("new-title", savedTopic.Slug); Assert.Equal(theme.Id, savedTopic.ThemeId); } }
public async void UpdateTopic_FailsNonUniqueSlug() { var theme = new Theme { Title = "Test theme", }; var topic = new Topic { Title = "Old title", Slug = "old-title", Theme = theme }; var contextId = Guid.NewGuid().ToString(); await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { context.Add(topic); context.Add( new Topic { Title = "Other topic", Slug = "other-topic", Theme = theme } ); await context.SaveChangesAsync(); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { var service = SetupTopicService(context); var result = await service.UpdateTopic( topic.Id, new SaveTopicViewModel { Title = "Other topic", ThemeId = topic.ThemeId } ); Assert.True(result.IsLeft); var badRequestResult = Assert.IsType <BadRequestObjectResult>(result.Left); var details = Assert.IsType <ValidationProblemDetails>(badRequestResult.Value); Assert.Equal("SLUG_NOT_UNIQUE", details.Errors[""].First()); } }
public async void CreateTopic() { var theme = new Theme { Title = "Test theme", }; var contextId = Guid.NewGuid().ToString(); await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { context.Add(theme); await context.SaveChangesAsync(); } await using (var context = DbUtils.InMemoryApplicationDbContext(contextId)) { var service = SetupTopicService(context); var result = await service.CreateTopic( new SaveTopicViewModel { Title = "Test topic", ThemeId = theme.Id } ); Assert.True(result.IsRight); Assert.Equal("Test topic", result.Right.Title); Assert.Equal("test-topic", result.Right.Slug); Assert.Equal(theme.Id, result.Right.ThemeId); var savedTopic = await context.Topics.FindAsync(result.Right.Id); Assert.Equal("Test topic", savedTopic.Title); Assert.Equal("test-topic", savedTopic.Slug); Assert.Equal(theme.Id, savedTopic.ThemeId); } }