- StoryServiceTests.cs: 536 lines, comprehensive tests for StoryService - StoryGenerationServiceTests.cs: 469 lines, tests for AI story generation - StoryUnlockServiceTests.cs: 292 lines, tests for story unlock logic - StoryRepositoryTests.cs: 120 lines, tests for EF Core repository - StoryProgressRepositoryTests.cs: 74 lines, tests for progress repository - AppDbContext.cs: Made DbSet properties virtual for Moq compatibility - Fixed return types in StoryRepository and StoryProgressRepository - Updated docs/features/story-integration.md for Phase 3 completion - All 324 unit tests pass Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using GermanApp.Domain.Entities;
|
|
using GermanApp.Infrastructure.Data.DbContext;
|
|
using GermanApp.Infrastructure.Data.Repositories;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using Moq;
|
|
|
|
namespace GermanApp.Tests.Unit.Infrastructure.Data.Repositories;
|
|
|
|
[TestClass]
|
|
public class StoryProgressRepositoryTests
|
|
{
|
|
private StoryProgress CreateProgress(int id, int userId, int levelId, int storySegmentId, bool isCompleted = false)
|
|
{
|
|
var progress = StoryProgress.Create(userId, levelId, storySegmentId);
|
|
typeof(StoryProgress).GetProperty("Id")?.SetValue(progress, id);
|
|
if (isCompleted)
|
|
progress.MarkAsCompleted();
|
|
return progress;
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AddAsync_WithNewProgress_AddsToDbSet()
|
|
{
|
|
// Arrange
|
|
var progress = CreateProgress(0, 1, 1, 1);
|
|
var mockDbSet = new Mock<DbSet<StoryProgress>>();
|
|
var options = new DbContextOptions<AppDbContext>();
|
|
var mockContext = new Mock<AppDbContext>(options);
|
|
|
|
mockDbSet.Setup(d => d.AddAsync(progress, It.IsAny<CancellationToken>()))
|
|
.Returns(ValueTask.FromResult((EntityEntry<StoryProgress>?)null));
|
|
mockContext.Setup(c => c.StoryProgress).Returns(mockDbSet.Object);
|
|
mockContext.Setup(c => c.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1);
|
|
|
|
var repository = new StoryProgressRepository(mockContext.Object);
|
|
|
|
// Act
|
|
var result = await repository.AddAsync(progress);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(result);
|
|
mockDbSet.Verify(d => d.AddAsync(progress, It.IsAny<CancellationToken>()), Times.Once);
|
|
mockContext.Verify(c => c.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task UpdateAsync_WithExistingProgress_UpdatesDbSet()
|
|
{
|
|
// Arrange
|
|
var progress = CreateProgress(1, 1, 1, 1);
|
|
var mockDbSet = new Mock<DbSet<StoryProgress>>();
|
|
var options = new DbContextOptions<AppDbContext>();
|
|
var mockContext = new Mock<AppDbContext>(options);
|
|
|
|
mockContext.Setup(c => c.StoryProgress).Returns(mockDbSet.Object);
|
|
mockContext.Setup(c => c.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1);
|
|
|
|
var repository = new StoryProgressRepository(mockContext.Object);
|
|
|
|
// Act
|
|
await repository.UpdateAsync(progress);
|
|
|
|
// Assert
|
|
mockDbSet.Verify(d => d.Update(progress), Times.Once);
|
|
mockContext.Verify(c => c.SaveChangesAsync(It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
}
|