54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using NSubstitute;
|
|
using UniVerse.Application.DTOs.Lectures;
|
|
using UniVerse.Application.Interfaces;
|
|
using UniVerse.Domain.Entities;
|
|
using UniVerse.Infrastructure.Data;
|
|
using UniVerse.Infrastructure.Services;
|
|
using Xunit;
|
|
|
|
namespace UniVerse.Api.Tests.Lectures;
|
|
|
|
public class LectureServiceTests
|
|
{
|
|
[Fact]
|
|
public async Task GetAllAsync_MarksLecturesEnrolledByCurrentUser()
|
|
{
|
|
await using var db = CreateDbContext();
|
|
var service = new LectureService(db, Substitute.For<IGamificationService>());
|
|
var startsAt = DateTime.UtcNow.AddDays(1);
|
|
|
|
db.Users.Add(new User { Id = 1, Email = "student@test.local" });
|
|
db.Courses.Add(new Course { Id = 1, Name = "Course" });
|
|
db.Lectures.AddRange(
|
|
Lecture(1, startsAt),
|
|
Lecture(2, startsAt.AddDays(1)));
|
|
db.LectureEnrollments.Add(new LectureEnrollment { LectureId = 1, UserId = 1 });
|
|
await db.SaveChangesAsync();
|
|
|
|
var result = await service.GetAllAsync(new LectureFilterRequest(null, null, null, null, null, null, null, null), 1);
|
|
|
|
Assert.True(result.Items.Single(item => item.Id == 1).IsEnrolled);
|
|
Assert.False(result.Items.Single(item => item.Id == 2).IsEnrolled);
|
|
}
|
|
|
|
private static AppDbContext CreateDbContext()
|
|
{
|
|
var options = new DbContextOptionsBuilder<AppDbContext>()
|
|
.UseInMemoryDatabase($"LectureServiceTests_{Guid.NewGuid()}")
|
|
.Options;
|
|
return new AppDbContext(options);
|
|
}
|
|
|
|
private static Lecture Lecture(int id, DateTime startsAt) => new()
|
|
{
|
|
Id = id,
|
|
CourseId = 1,
|
|
Title = $"Lecture {id}",
|
|
StartsAt = startsAt,
|
|
EndsAt = startsAt.AddHours(2),
|
|
IsOpen = true,
|
|
MaxEnrollments = 30
|
|
};
|
|
}
|