diff --git a/Chirp/src/Chirp.Core/AuthorService.cs b/Chirp/src/Chirp.Core/AuthorService.cs index c9bc4457..090f5e1b 100644 --- a/Chirp/src/Chirp.Core/AuthorService.cs +++ b/Chirp/src/Chirp.Core/AuthorService.cs @@ -2,7 +2,6 @@ namespace Chirp.Core; - public interface IAuthorService { public List GetFollows(string username); @@ -11,13 +10,14 @@ public interface IAuthorService public bool Unfollow(string currentUser, string userToUnFollow); public bool MakeFollowersUnfollow(string username); } + public class AuthorService(IAuthorRepository db) : IAuthorService { public List GetFollows(string username) { return db.GetAuthorFollows(username).Result; } - + public List GetFollowers(string username) { return db.GetAuthorFollowers(username).Result; @@ -27,12 +27,12 @@ public bool MakeFollowersUnfollow(string username) { return db.MakeFollowersUnfollow(username).Result; } - + public bool Follow(string currentUser, string userToFollow) { return db.Follow(currentUser, userToFollow).Result; } - + public bool Unfollow(string currentUser, string userToUnFollow) { return db.UnFollow(currentUser, userToUnFollow).Result; diff --git a/Chirp/src/Chirp.Core/CheepService.cs b/Chirp/src/Chirp.Core/CheepService.cs index c8c8375c..980d8090 100644 --- a/Chirp/src/Chirp.Core/CheepService.cs +++ b/Chirp/src/Chirp.Core/CheepService.cs @@ -23,8 +23,9 @@ public List GetCheepsByPage(int page, int pageSize) { ArgumentOutOfRangeException.ThrowIfNegative(pageSize); - var result = db.GetCheepsByPage(page, pageSize).Result ?? throw new ArgumentNullException(nameof(db.GetCheepsByPage)); - + var result = db.GetCheepsByPage(page, pageSize).Result ?? + throw new ArgumentNullException(nameof(db.GetCheepsByPage)); + return result.ToList(); } @@ -37,7 +38,7 @@ public List GetCheepsFromAuthorByPage(string author, int page, int pag { return db.GetCheepsFromAuthorByPage(author, page, pageSize).Result.ToList(); } - + public List GetCheepsFromAuthorsByPage(IEnumerable authors, int page, int pageSize) { return db.GetCheepsFromAuthorsByPage(authors, page, pageSize).Result.ToList(); diff --git a/Chirp/src/Chirp.Core/DTO/CommentDTO.cs b/Chirp/src/Chirp.Core/DTO/CommentDTO.cs index 5c7d1091..9f4b9296 100644 --- a/Chirp/src/Chirp.Core/DTO/CommentDTO.cs +++ b/Chirp/src/Chirp.Core/DTO/CommentDTO.cs @@ -1,6 +1,6 @@ namespace Chirp.Core.DTO; -public class CommentDTO (string author, int cheepId, string message, long unixTimestamp) +public class CommentDTO(string author, int cheepId, string message, long unixTimestamp) { public string Author { get; set; } = author; public int CheepId { get; set; } = cheepId; diff --git a/Chirp/src/Chirp.Core/IAuthorRepository.cs b/Chirp/src/Chirp.Core/IAuthorRepository.cs index 8765441b..4a33404c 100644 --- a/Chirp/src/Chirp.Core/IAuthorRepository.cs +++ b/Chirp/src/Chirp.Core/IAuthorRepository.cs @@ -8,7 +8,7 @@ public interface IAuthorRepository public Task AddAuthor(AuthorDTO author); public Task> GetAuthorFollows(string username); public Task> GetAuthorFollowers(string username); - public Task Follow (string currentUser, string userToFollow); - public Task UnFollow (string currentUser, string userToUnFollow); + public Task Follow(string currentUser, string userToFollow); + public Task UnFollow(string currentUser, string userToUnFollow); public Task MakeFollowersUnfollow(string user); } \ No newline at end of file diff --git a/Chirp/src/Chirp.Infrastructure/AuthorRepository.cs b/Chirp/src/Chirp.Infrastructure/AuthorRepository.cs index 410a3d00..7fbb741d 100644 --- a/Chirp/src/Chirp.Infrastructure/AuthorRepository.cs +++ b/Chirp/src/Chirp.Infrastructure/AuthorRepository.cs @@ -43,7 +43,7 @@ public async Task> GetAuthorFollowers(string username) .Where(a => a.NormalizedUserName == username.ToUpper()) .Select(a => a.Followers) .FirstOrDefaultAsync() ?? new List(); - + return followers.Select(a => new AuthorDTO(a.UserName!, a.Email!)).ToList(); } @@ -53,10 +53,10 @@ public async Task Follow(string currentUser, string userToFollow) if (!await UserExists(currentUser)) throw new UserDoesNotExist(); if (!await UserExists(userToFollow)) throw new UserDoesNotExist("User to follow does not exist"); if (await DoesFollow(currentUser, userToFollow)) return false; - + var authorToFollow = await GetAuthor(userToFollow); GetAuthor(currentUser).Result.Following.Add(authorToFollow); - + await context.SaveChangesAsync(); return true; } @@ -69,17 +69,17 @@ public async Task UnFollow(string currentUser, string userToUnfollow) if (!await UserExists(currentUser)) throw new UserDoesNotExist(); if (!await UserExists(userToUnfollow)) throw new UserDoesNotExist("User to unfollow does not exist"); if (!await DoesFollow(currentUser, userToUnfollow)) return false; - + var authorToUnfollow = await GetAuthor(userToUnfollow); context.Authors .Where(a => a.NormalizedUserName == currentUser) .Include(a => a.Following) .FirstOrDefault()!.Following.Remove(authorToUnfollow); - + await context.SaveChangesAsync(); return true; } - + private async Task GetAuthor(string username) { username = username.ToUpper(); @@ -98,11 +98,11 @@ private async Task DoesFollow(string currentUser, string userToFollow) { currentUser = currentUser.ToUpper(); userToFollow = userToFollow.ToUpper(); - var list = await context.Authors - .Where(a => a.NormalizedUserName == currentUser) - .Select(a => a.Following).FirstOrDefaultAsync(); - - return list != null && list.Any(a => a.NormalizedUserName == userToFollow); + var list = await context.Authors + .Where(a => a.NormalizedUserName == currentUser) + .Select(a => a.Following).FirstOrDefaultAsync(); + + return list != null && list.Any(a => a.NormalizedUserName == userToFollow); } private async Task> GetFollowing(string currentUser) diff --git a/Chirp/src/Chirp.Infrastructure/CheepRepository.cs b/Chirp/src/Chirp.Infrastructure/CheepRepository.cs index 0bedabab..a47390fe 100644 --- a/Chirp/src/Chirp.Infrastructure/CheepRepository.cs +++ b/Chirp/src/Chirp.Infrastructure/CheepRepository.cs @@ -12,9 +12,9 @@ public class CheepRepository(ChirpDBContext context) : ICheepRepository public async Task?> GetCheepsByPage(int page, int pageSize) { if (pageSize < 0) return null; - + var query = context.Cheeps - .Select(cheep => new {cheep.Id, cheep.Author.UserName, cheep.Message, cheep.TimeStamp }) + .Select(cheep => new { cheep.Id, cheep.Author.UserName, cheep.Message, cheep.TimeStamp }) .OrderByDescending(cheep => cheep.TimeStamp) .Skip((page - 1) * pageSize) .Take(pageSize); @@ -22,7 +22,8 @@ public class CheepRepository(ChirpDBContext context) : ICheepRepository var cheeps = await query.ToListAsync(); return cheeps.Select(cheep => - new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds())); + new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, + new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds())); } public async Task> GetCheepsFromAuthorByPage(string author, int page, int pageSize) @@ -40,10 +41,12 @@ public async Task> GetCheepsFromAuthor(string author) var cheeps = await query.ToListAsync(); return cheeps.Select(cheep => - new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds())); + new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, + new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds())); } - - public async Task> GetCheepsFromAuthorsByPage(IEnumerable authors, int page, int pageSize) + + public async Task> GetCheepsFromAuthorsByPage(IEnumerable authors, int page, + int pageSize) { authors = authors.Select(author => author.ToUpper()); var query = context.Cheeps @@ -56,7 +59,8 @@ public async Task> GetCheepsFromAuthorsByPage(IEnumerable< var cheeps = await query.ToListAsync(); return cheeps.Select(cheep => - new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds())); + new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, + new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds())); } public Task GetAmountOfCheeps() @@ -77,7 +81,11 @@ public async Task CreateCheep(CheepDTO cheep) .Where(a => a.UserName == cheep.Author) .FirstOrDefaultAsync(); if (author == null) return false; - var cheep2 = new Cheep {Author = author, Message = cheep.Message, TimeStamp = DateTimeOffset.FromUnixTimeSeconds(cheep.UnixTimestamp).DateTime}; + var cheep2 = new Cheep + { + Author = author, Message = cheep.Message, + TimeStamp = DateTimeOffset.FromUnixTimeSeconds(cheep.UnixTimestamp).DateTime + }; context.Cheeps.Add(cheep2); await context.SaveChangesAsync(); return true; @@ -94,15 +102,19 @@ public async Task AddCommentToCheep(CommentDTO comment) .Include(c => c.Comments) .FirstOrDefaultAsync(); if (cheep == null) return false; - var newComment = new Comment {Author = author, Cheep = cheep, Message = comment.Message, TimeStamp = DateTimeOffset.FromUnixTimeSeconds(comment.UnixTimestamp).DateTime}; + var newComment = new Comment + { + Author = author, Cheep = cheep, Message = comment.Message, + TimeStamp = DateTimeOffset.FromUnixTimeSeconds(comment.UnixTimestamp).DateTime + }; cheep.Comments.Add(newComment); await context.SaveChangesAsync(); - return true; + return true; } public async Task GetCommentAmountOnCheep(int? cheepId) { - if (cheepId == null) return 0; + if (cheepId == null) return 0; var query = await context.Cheeps .Include(c => c.Comments) .Where(c => c.Id == cheepId) @@ -117,8 +129,10 @@ public async Task GetCheepById(int cheepId) .Where(c => c.Id == cheepId) .Select(cheep => new { cheep.Id, cheep.Author.UserName, cheep.Message, cheep.TimeStamp }) .FirstOrDefaultAsync(); - - return new CheepDTO(cheep!.Id, cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds());; + + return new CheepDTO(cheep!.Id, cheep.UserName!, cheep.Message, + new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds()); + ; } public async Task> GetCommentsForCheep(int cheepId) @@ -130,6 +144,7 @@ public async Task> GetCommentsForCheep(int cheepId) .FirstOrDefaultAsync(); var comments = query!.Comments.OrderByDescending(c => c.TimeStamp); return comments.Select(comment => - new CommentDTO(comment.Author.UserName!, comment.Id, comment.Message, new DateTimeOffset(comment.TimeStamp).ToUnixTimeSeconds())); + new CommentDTO(comment.Author.UserName!, comment.Id, comment.Message, + new DateTimeOffset(comment.TimeStamp).ToUnixTimeSeconds())); } } \ No newline at end of file diff --git a/Chirp/src/Chirp.Infrastructure/Chirp.Infrastructure.csproj b/Chirp/src/Chirp.Infrastructure/Chirp.Infrastructure.csproj index 705e30cb..36b90add 100644 --- a/Chirp/src/Chirp.Infrastructure/Chirp.Infrastructure.csproj +++ b/Chirp/src/Chirp.Infrastructure/Chirp.Infrastructure.csproj @@ -7,12 +7,12 @@ - + - - + + diff --git a/Chirp/src/Chirp.Infrastructure/ChirpDbContext.cs b/Chirp/src/Chirp.Infrastructure/ChirpDbContext.cs index 4e461cc3..1915dca4 100644 --- a/Chirp/src/Chirp.Infrastructure/ChirpDbContext.cs +++ b/Chirp/src/Chirp.Infrastructure/ChirpDbContext.cs @@ -8,7 +8,6 @@ public class ChirpDBContext(DbContextOptions options) : Identity { public DbSet Cheeps { get; set; } public DbSet Authors { get; set; } - public DbSet Comments { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) @@ -52,14 +51,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) entity.HasMany(a => a.Following) .WithMany(a => a.Followers); }); - + //define comment entity constraints modelBuilder.Entity(entity => { entity.HasKey((c => c.Id)); entity.Property(c => c.Id) .ValueGeneratedOnAdd(); - + entity.Property(c => c.Message) .IsRequired() .HasMaxLength(160); @@ -69,12 +68,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .WithMany(a => a.Comments) .IsRequired() .OnDelete(DeleteBehavior.Cascade); - + entity.HasOne(c => c.Cheep) .WithMany(c => c.Comments) .IsRequired() .OnDelete(DeleteBehavior.Cascade); }); - } } \ No newline at end of file diff --git a/Chirp/src/Chirp.Infrastructure/Model/Author.cs b/Chirp/src/Chirp.Infrastructure/Model/Author.cs index e4a2203a..a7794260 100644 --- a/Chirp/src/Chirp.Infrastructure/Model/Author.cs +++ b/Chirp/src/Chirp.Infrastructure/Model/Author.cs @@ -8,7 +8,6 @@ public sealed class Author : IdentityUser public List Cheeps { get; set; } = []; public List Following { get; set; } = []; public List Followers { get; set; } = []; - public List Comments { get; set; } = []; public Author() diff --git a/Chirp/src/Chirp.Infrastructure/Model/Cheep.cs b/Chirp/src/Chirp.Infrastructure/Model/Cheep.cs index 9ac1dc85..a305ea57 100644 --- a/Chirp/src/Chirp.Infrastructure/Model/Cheep.cs +++ b/Chirp/src/Chirp.Infrastructure/Model/Cheep.cs @@ -4,15 +4,14 @@ namespace Chirp.Infrastructure.Model; public class Cheep { - public int Id { get; set; } - public required string Message { get; set; } - public DateTime TimeStamp { get; set; } + public int Id { get; set; } + public required string Message { get; set; } + public DateTime TimeStamp { get; set; } public required Author Author { get; set; } - public List Comments { get; set; } = []; + public Cheep() { - } public Cheep(int id, string message, DateTime timeStamp, Author author) diff --git a/Chirp/src/Chirp.Infrastructure/Model/Comment.cs b/Chirp/src/Chirp.Infrastructure/Model/Comment.cs index 1698daf3..65d98398 100644 --- a/Chirp/src/Chirp.Infrastructure/Model/Comment.cs +++ b/Chirp/src/Chirp.Infrastructure/Model/Comment.cs @@ -5,12 +5,11 @@ public class Comment public int Id { get; set; } public required Author Author { get; set; } public required Cheep Cheep { get; set; } - public DateTime TimeStamp { get; set; } + public DateTime TimeStamp { get; set; } public required string Message { get; set; } public Comment() { - } public Comment(int id, Author author, Cheep cheep, string message, DateTime timeStamp) diff --git a/Chirp/src/Chirp.Web/Pages/Shared/Components/CheepList/Default.cshtml b/Chirp/src/Chirp.Web/Pages/Shared/Components/CheepList/Default.cshtml index 59dc5f8c..bb676ce6 100644 --- a/Chirp/src/Chirp.Web/Pages/Shared/Components/CheepList/Default.cshtml +++ b/Chirp/src/Chirp.Web/Pages/Shared/Components/CheepList/Default.cshtml @@ -1,10 +1,9 @@ @using Chirp.Core @using Chirp.Core.DTO -@using Chirp.Web.Pages.BindingModels @inject IAuthorService service @inject ICheepService CheepService -private +private @{ var targetPage = ViewBag.TargetPage as string ?? ""; IEnumerable cheeps = ViewBag.Cheeps; @@ -23,11 +22,11 @@ private function cancelPopup() { document.getElementById('popup').style.display = 'none'; } - + function goToSpecificCheep(cheepId) { window.location.href = `/SpecificCheep?cheepId=${cheepId}`; } - +
    @@ -39,14 +38,14 @@ private @cheep.Author @if (User.Identity!.IsAuthenticated && User.Identity!.Name != cheep.Author) { - @if (Follows.Any(a => a.Name == cheep.Author)) - { - - } - else - { - - } + @if (Follows.Any(a => a.Name == cheep.Author)) + { + + } + else + { + + } }

    @@ -54,7 +53,8 @@ private
    — @DateTimeOffset.FromUnixTimeSeconds(cheep.UnixTimestamp).DateTime.ToString("dd/MM/yy H:mm:ss") - Comment Icon + Comment Icon @CheepService.GetCommentAmountOnCheep(cheep.Id)
    diff --git a/Chirp/src/Chirp.Web/Pages/Shared/TimeLinePageModel.cs b/Chirp/src/Chirp.Web/Pages/Shared/TimeLinePageModel.cs index e84e0fe0..24b22295 100644 --- a/Chirp/src/Chirp.Web/Pages/Shared/TimeLinePageModel.cs +++ b/Chirp/src/Chirp.Web/Pages/Shared/TimeLinePageModel.cs @@ -16,24 +16,25 @@ public abstract class TimeLinePageModel(ICheepService cheepService) : PageModel public int LastPageNumber = 1; protected const int PageSize = 32; - [BindProperty] - public MessageModel MessageModel { get; set; } = new MessageModel(); - + [BindProperty] public MessageModel MessageModel { get; set; } = new MessageModel(); + public ActionResult OnPost(string? author, [FromQuery] int page) { - if (string.IsNullOrWhiteSpace(MessageModel.Message)) { + if (string.IsNullOrWhiteSpace(MessageModel.Message)) + { ModelState.AddModelError("Message", "Message cannot be empty"); } - - if (!ModelState.IsValid) { + + if (!ModelState.IsValid) + { LoadCheeps(page); return Page(); } - + var dt = (DateTimeOffset)DateTime.UtcNow; if (User.Identity != null) { - var cheep = new CheepDTO ( + var cheep = new CheepDTO( null, User.Identity.Name ?? "no name", MessageModel.Message!, @@ -55,9 +56,9 @@ public IActionResult OnPostComment(string author, int cheepId, string comment) var dt = DateTimeOffset.UtcNow; var commentDTO = new CommentDTO ( - author, - cheepId, - comment, + author, + cheepId, + comment, dt.ToUnixTimeSeconds() ); @@ -65,7 +66,7 @@ public IActionResult OnPostComment(string author, int cheepId, string comment) { throw new ApplicationException("Failed to add comment"); } - + return RedirectToPage(null); } diff --git a/Chirp/src/Chirp.Web/Pages/SpecificCheep.cshtml b/Chirp/src/Chirp.Web/Pages/SpecificCheep.cshtml index b4c16f88..98f8b697 100644 --- a/Chirp/src/Chirp.Web/Pages/SpecificCheep.cshtml +++ b/Chirp/src/Chirp.Web/Pages/SpecificCheep.cshtml @@ -15,31 +15,33 @@

    @cheep.Message


    - Comment Icon + Comment Icon @Model.CommentCount

    @if (User.Identity!.IsAuthenticated) { -
    -
    - - - - - -
    -
    - +
    +
    + + + + + +
    +
    } else {

    Log in to comment on this Cheep

    } - +
      - @foreach(var comment in comments) + @foreach (var comment in comments) {
    • @@ -48,20 +50,11 @@ - @Model.TimeSinceComment(comment.UnixTimestamp) - +

      @comment.Message

    • }
    - - - - - - - - - diff --git a/Chirp/src/Chirp.Web/Pages/SpecificCheep.cshtml.cs b/Chirp/src/Chirp.Web/Pages/SpecificCheep.cshtml.cs index e732b488..8d0d116d 100644 --- a/Chirp/src/Chirp.Web/Pages/SpecificCheep.cshtml.cs +++ b/Chirp/src/Chirp.Web/Pages/SpecificCheep.cshtml.cs @@ -11,9 +11,9 @@ public class SpecificCheep(ICheepService cheepService) : PageModel public CheepDTO Cheep { get; set; } = null!; public int CommentCount { get; set; } public List Comments { get; set; } = []; - - [BindProperty] - public MessageModel MessageModel { get; set; } = new MessageModel(); + + [BindProperty] public MessageModel MessageModel { get; set; } = new MessageModel(); + public void OnGet(int cheepId) { Cheep = cheepService.GetCheepFromId(cheepId); @@ -23,11 +23,11 @@ public void OnGet(int cheepId) public string TimeSinceComment(long timeStamp) { - var utcThen = DateTimeOffset.FromUnixTimeSeconds(timeStamp); + var utcThen = DateTimeOffset.FromUnixTimeSeconds(timeStamp); var localTimeZone = TimeZoneInfo.Local; var then = TimeZoneInfo.ConvertTime(utcThen, localTimeZone); var now = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, localTimeZone); - + var timesince = (now - then) switch { { TotalMinutes: < 60 } ts => $"{ts.Minutes} minutes ago", @@ -38,7 +38,7 @@ public string TimeSinceComment(long timeStamp) }; return timesince; } - + public IActionResult OnPostComment(string author, int cheepId) { if (string.IsNullOrWhiteSpace(MessageModel.Message)) @@ -53,14 +53,14 @@ public IActionResult OnPostComment(string author, int cheepId) Reload(cheepId); return Page(); } - - + + var dt = DateTimeOffset.UtcNow; var commentDTO = new CommentDTO ( - author, - cheepId, - MessageModel.Message, + author, + cheepId, + MessageModel.Message, dt.ToUnixTimeSeconds() ); @@ -68,7 +68,7 @@ public IActionResult OnPostComment(string author, int cheepId) { throw new ApplicationException("Failed to add comment"); } - + return RedirectToPage("/SpecificCheep", new { cheepId = cheepId }); } diff --git a/Chirp/test/PlaywrightTests/UITests/CommentTests.cs b/Chirp/test/PlaywrightTests/UITests/CommentTests.cs index 144e95fc..16bd9406 100644 --- a/Chirp/test/PlaywrightTests/UITests/CommentTests.cs +++ b/Chirp/test/PlaywrightTests/UITests/CommentTests.cs @@ -1,15 +1,13 @@ -using Duende.IdentityServer.Extensions; -using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Playwright; -using NUnit.Framework.Internal; using PlaywrightTests.Utils; using PlaywrightTests.Utils.PageTests; + namespace PlaywrightTests.UITests; -public class CommentTests : PageTestWithRazorPlaywrightWebApplicationFactory +public class CommentTests : PageTestWithRazorPlaywrightWebApplicationFactory { private TestAuthor _testAuthor; - + [SetUp] public async Task SetUp() { @@ -17,16 +15,16 @@ public async Task SetUp() .WithUsernameAndEmail("author") .Create(); await GenerateCheep(_testAuthor.author); - } - + [Test] public async Task SpecificCheepViewExists() { await GenerateCheep(_testAuthor.author, "this is author"); await Page.GotoAsync("/"); - await Page.Locator("#messagelist div").Filter(new() { HasText = "—" }).GetByRole(AriaRole.Link).First.ClickAsync(); - await Expect( Page.GetByRole(AriaRole.Link, new() { Name = "author" })).ToBeVisibleAsync(); + await Page.Locator("#messagelist div").Filter(new() { HasText = "—" }).GetByRole(AriaRole.Link).First + .ClickAsync(); + await Expect(Page.GetByRole(AriaRole.Link, new() { Name = "author" })).ToBeVisibleAsync(); } [Test] @@ -37,7 +35,8 @@ public async Task CommentCountUpdates() await Page.GotoAsync("/"); await Expect(Page.Locator("li").Filter(new() { HasText = "author" }).Locator("small").Nth(1)) .ToHaveTextAsync("0"); - await Page.Locator("#messagelist div").Filter(new() { HasText = "—" }).GetByRole(AriaRole.Link).First.ClickAsync(); + await Page.Locator("#messagelist div").Filter(new() { HasText = "—" }).GetByRole(AriaRole.Link).First + .ClickAsync(); await Page.GetByPlaceholder("Write a comment").ClickAsync(); await Page.GetByPlaceholder("Write a comment").FillAsync("This is a comment"); await Page.GetByRole(AriaRole.Button, new() { Name = "Post" }).ClickAsync(); @@ -51,7 +50,8 @@ public async Task TestAddComment() await GenerateCheep(_testAuthor.author, "this is author"); await RazorPageUtils.Login(_testAuthor); await Page.GotoAsync("/"); - await Page.Locator("#messagelist div").Filter(new() { HasText = "—" }).GetByRole(AriaRole.Link).First.ClickAsync(); + await Page.Locator("#messagelist div").Filter(new() { HasText = "—" }).GetByRole(AriaRole.Link).First + .ClickAsync(); await Page.GetByPlaceholder("Write a comment").ClickAsync(); await Page.GetByPlaceholder("Write a comment").FillAsync("This is a comment"); await Page.GetByRole(AriaRole.Button, new() { Name = "Post" }).ClickAsync(); @@ -63,7 +63,8 @@ public async Task CannotPostCommentsWhenNotLoggedIn() { await GenerateCheep(_testAuthor.author, "this is author"); await Page.GotoAsync("/"); - await Page.Locator("#messagelist div").Filter(new() { HasText = "—" }).GetByRole(AriaRole.Link).First.ClickAsync(); + await Page.Locator("#messagelist div").Filter(new() { HasText = "—" }).GetByRole(AriaRole.Link).First + .ClickAsync(); await Expect(Page.GetByRole(AriaRole.Heading, new() { Name = "Log in to comment on this" })) .ToBeVisibleAsync(); } diff --git a/Chirp/test/RepositoryTests/CheepRepositoryUnitTest.cs b/Chirp/test/RepositoryTests/CheepRepositoryUnitTest.cs index 0a8aa760..573f7cd5 100644 --- a/Chirp/test/RepositoryTests/CheepRepositoryUnitTest.cs +++ b/Chirp/test/RepositoryTests/CheepRepositoryUnitTest.cs @@ -2,8 +2,6 @@ using Chirp.Core.DTO; using Chirp.Infrastructure; using Chirp.Infrastructure.Model; -using Microsoft.Data.Sqlite; -using Microsoft.EntityFrameworkCore; using RepositoryTests.Utils; using TestUtilities; @@ -14,7 +12,7 @@ public class CheepRepositoryUnitTest { protected ChirpDBContext Context { get; } protected ICheepRepository CheepRepository { get; } - + public CheepRepositoryUnitTest(InMemoryDBFixture fixture) { fixture.ResetDatabase(); @@ -60,7 +58,7 @@ public async Task GetCheepsByPage_NegativePageSize_ReturnsNull() //arrange var author = TestUtils.CreateTestAuthor("Mr. test"); var cheep = new Cheep { Author = author, Message = "test", TimeStamp = DateTime.Now }; - + Context.Cheeps.Add(cheep); await Context.SaveChangesAsync(); @@ -124,7 +122,7 @@ public async Task GetCheepsByPage_ReturnsCorrectSingleCheep() var author = TestUtils.CreateTestAuthor("Mr. test"); var timeStamp = new DateTime(2000, 01, 01); var cheep = new Cheep { Author = author, Message = "test", TimeStamp = timeStamp }; - + Context.Cheeps.Add(cheep); await Context.SaveChangesAsync(); @@ -135,7 +133,8 @@ public async Task GetCheepsByPage_ReturnsCorrectSingleCheep() Assert.NotNull(result); var resultList = result.ToList(); - var expected = new CheepDTO(null, author.UserName!, cheep.Message, ((DateTimeOffset)timeStamp).ToUnixTimeSeconds()); + var expected = new CheepDTO(null, author.UserName!, cheep.Message, + ((DateTimeOffset)timeStamp).ToUnixTimeSeconds()); Assert.Single(resultList); var singleCheep = resultList.ElementAt(0); @@ -159,7 +158,7 @@ public async Task GetCheepsFromAuthorByPage_ReturnsCorrectCheepWhenMultipleAutho { var author = TestUtils.CreateTestAuthor($"name{i}"); authors.Add(author); - + var cheep = new Cheep { Author = author, Message = $"test{i}", TimeStamp = DateTime.Now }; cheeps.Add(cheep); } @@ -167,7 +166,7 @@ public async Task GetCheepsFromAuthorByPage_ReturnsCorrectCheepWhenMultipleAutho Context.Authors.AddRange(authors); Context.Cheeps.AddRange(cheeps); await Context.SaveChangesAsync(); - + for (int i = 0; i < 5; i++) { //act @@ -188,13 +187,13 @@ public async Task GetCheepsFromAuthor_ReturnsAllCheeps() List cheeps = []; for (int i = 0; i < 5; i++) { - cheeps.Add(new Cheep{ Author = author, Message = "test" + i, TimeStamp = DateTime.Now }); + cheeps.Add(new Cheep { Author = author, Message = "test" + i, TimeStamp = DateTime.Now }); } Context.Authors.Add(author); Context.Cheeps.AddRange(cheeps); await Context.SaveChangesAsync(); - + var result = (await CheepRepository.GetCheepsFromAuthor(author.UserName!)).ToList(); cheeps.Reverse(); Assert.Equal(5, result.Count); @@ -202,8 +201,6 @@ public async Task GetCheepsFromAuthor_ReturnsAllCheeps() { Assert.Equal(cheeps.ElementAt(i).Message, result.ElementAt(i).Message); } - - } [Fact] @@ -217,7 +214,7 @@ public async Task GetCheepsFromAuthorByPage_ReturnsNoCheepsForNonexistentAuthor( { var author = TestUtils.CreateTestAuthor($"name{i}"); authors.Add(author); - + var cheep = new Cheep { Author = author, Message = $"test{i}", TimeStamp = DateTime.Now }; cheeps.Add(cheep); } @@ -239,7 +236,7 @@ public async Task GetCheepsFromAuthorByPage_ReturnsAllCheepsFromAuthorForLargeNu //arrange var authorA = TestUtils.CreateTestAuthor("Mr. test"); var authorB = TestUtils.CreateTestAuthor("Mr. fake"); - + var authTotal = 0; for (int i = 0; i < 100; i++) @@ -308,11 +305,11 @@ public async Task GetCheepsFromAuthorsByPage_ReturnsAllCheepsFromAuthorsFromMult var author2 = TestUtils.CreateTestAuthor("Mr. Test2"); List authors = new(); - - + + authors.Add(author1.UserName!); authors.Add(author2.UserName!); - + for (var i = 0; i < 100; i++) { if (i % 2 == 0) @@ -326,12 +323,12 @@ public async Task GetCheepsFromAuthorsByPage_ReturnsAllCheepsFromAuthorsFromMult Context.Cheeps.Add(cheep); } } - + await Context.SaveChangesAsync(); - + int pageNo = 1; int totalCount = 0; - + //Act while (true) { @@ -342,7 +339,7 @@ public async Task GetCheepsFromAuthorsByPage_ReturnsAllCheepsFromAuthorsFromMult totalCount += count; pageNo++; } - + //Assert Assert.Equal(100, totalCount); } @@ -367,12 +364,12 @@ public async Task GetCheepsFromAuthorsByPage_ReturnsNoCheepsFromAuthorWithNoChee cheeps.Add(cheep1); cheeps.Add(cheep2); } - + await Context.SaveChangesAsync(); - + //Act var result = await CheepRepository.GetCheepsFromAuthorsByPage(authors, 1, 20); - + //Assert Assert.Empty(result.ToList()); } @@ -388,25 +385,26 @@ public async Task GetCheepsFromAuthorsByPage_ReturnsInChronologicalOrder() for (var i = 0; i < 5; i++) { - var cheep = new Cheep { Author = author1, Message = $"{i}", TimeStamp = DateTimeOffset.FromUnixTimeSeconds(i * 60).DateTime }; + var cheep = new Cheep + { Author = author1, Message = $"{i}", TimeStamp = DateTimeOffset.FromUnixTimeSeconds(i * 60).DateTime }; Context.Cheeps.Add(cheep); } - + await Context.SaveChangesAsync(); - + //Act var result = await CheepRepository.GetCheepsFromAuthorsByPage(authors, 1, 20); var resultArray = result.ToArray(); //Assert Assert.True(resultArray[0].UnixTimestamp >= resultArray[1].UnixTimestamp); - + for (var i = 1; i < resultArray.ToList().Count; i++) { - Assert.True(resultArray[i-1].UnixTimestamp >= resultArray[i].UnixTimestamp); + Assert.True(resultArray[i - 1].UnixTimestamp >= resultArray[i].UnixTimestamp); } } - + #region comments [Fact] @@ -421,16 +419,17 @@ public async Task GetCheepById_ReturnsCorrectCheep() TimeStamp = DateTime.Now, Author = author }; - + Context.Authors.Add(author); Context.Cheeps.Add(cheep); await Context.SaveChangesAsync(); - + var result = await CheepRepository.GetCheepById(cheep.Id); Assert.NotNull(result); Assert.Equal(cheep.Id, result.Id); Assert.Equal(cheep.Message, result.Message); } + [Fact] public async Task GetCommentAmountOnCheep_ReturnsCorrectAmount() { @@ -452,7 +451,7 @@ public async Task GetCommentAmountOnCheep_ReturnsCorrectAmount() await CheepRepository.AddCommentToCheep(comment1); var count = await CheepRepository.GetCommentAmountOnCheep(cheep.Id); Assert.Equal(1, count); - + comment1 = new CommentDTO(author2.UserName!, cheep.Id, "test comment", 1234); await CheepRepository.AddCommentToCheep(comment1); count = await CheepRepository.GetCommentAmountOnCheep(cheep.Id); @@ -478,7 +477,6 @@ public async Task AddCommentToCheep_AddsCommentToCheep() var comment1 = new CommentDTO(author2.UserName!, cheep.Id, "test comment", 1234); await CheepRepository.AddCommentToCheep(comment1); Assert.True(Context.Comments.Count() == 1); - } [Fact] @@ -505,6 +503,6 @@ public async Task GetCommentsFromCheep_ReturnsCorrectComments() Assert.Equal(comment1.Message, first.Message); Assert.Equal(comment1.Author, first.Author); } - + #endregion } \ No newline at end of file diff --git a/Chirp/test/Web.UnitTests/RazorPageIntegrationTest.cs b/Chirp/test/Web.UnitTests/RazorPageIntegrationTest.cs index 2ee7f123..a0af89b5 100644 --- a/Chirp/test/Web.UnitTests/RazorPageIntegrationTest.cs +++ b/Chirp/test/Web.UnitTests/RazorPageIntegrationTest.cs @@ -1,9 +1,7 @@ -using System.Globalization; -using Chirp.Core; +using Chirp.Core; using Chirp.Core.DTO; using Moq; using Web.UnitTests.Utils; -using Program = Chirp.Web.Program; namespace Web.UnitTests;