From c2457e339d2e19b9c218f863e368abfd5afd92ef Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Sat, 21 Jan 2023 12:30:21 +0100 Subject: [PATCH] :construction: Work on UTs --- .gitignore | 1 + Tests/Controllers/CatsControllerTest.cs | 66 +++++++++++++++++++++++-- Tests/Tests.csproj | 5 ++ cat_cafe/Controllers/CatsController.cs | 24 +++++++-- cat_cafe/Entities/Cat.cs | 5 +- 5 files changed, 91 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 48597ec..cdeea09 100644 --- a/.gitignore +++ b/.gitignore @@ -427,3 +427,4 @@ FodyWeavers.xsd # Additional files built by Visual Studio # End of https://www.toptal.com/developers/gitignore/api/dotnetcore,visualstudio,visualstudiocode +/Tests/Tests - Backup.csproj diff --git a/Tests/Controllers/CatsControllerTest.cs b/Tests/Controllers/CatsControllerTest.cs index 8f6c3f2..e2c8273 100644 --- a/Tests/Controllers/CatsControllerTest.cs +++ b/Tests/Controllers/CatsControllerTest.cs @@ -1,6 +1,16 @@ -using cat_cafe.Controllers; +using AutoMapper; +using Castle.Core.Logging; +using cat_cafe.Controllers; +using cat_cafe.Dto; +using cat_cafe.Entities; +using cat_cafe.Mappers; +using cat_cafe.Repositories; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using NuGet.Protocol.Core.Types; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; @@ -9,13 +19,60 @@ using System.Threading.Tasks; namespace cat_cafe.Controllers.Tests { + [TestClass()] public class CatsControllerTest { + + private readonly ILogger logger = new NullLogger(); + + private readonly MapperConfiguration mapperConf = new(mapper => mapper.AddProfile(typeof(CatMapper))); + + private readonly IMapper mapper; + + private readonly DbContextOptions options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "CatCafeTests") + .Options; + + private readonly CatContext context; + + private readonly CatsController controller; + + public CatsControllerTest() + { + mapper = mapperConf.CreateMapper(); + context = new CatContext(options); + controller = new CatsController(context, mapper, logger); + } + + + [TestInitialize] + public void StartUp() + { + context.Database.EnsureCreated(); + context.Cats.AddRange( + new Cat + { + Id = 1, + Name = "Alice", + Age = 5 + }, + new Cat + { + Id = 2, + Name = "Bob", + Age = 3 + }); + context.SaveChanges(); + + // TODO tear down and drop all before each test method + } + [TestMethod()] - public void GetCatsTest() + public async Task GetCatsTest() { - Assert.Fail(); + ActionResult> actual = await controller.GetCats(); + Assert.Equals(200, actual.Result); } [TestMethod()] @@ -47,5 +104,6 @@ namespace cat_cafe.Controllers.Tests { Assert.Fail(); } + } } \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 7b01765..66038da 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -10,9 +10,14 @@ + + + + + diff --git a/cat_cafe/Controllers/CatsController.cs b/cat_cafe/Controllers/CatsController.cs index f85aa07..f78e672 100644 --- a/cat_cafe/Controllers/CatsController.cs +++ b/cat_cafe/Controllers/CatsController.cs @@ -9,6 +9,8 @@ using cat_cafe.Entities; using cat_cafe.Repositories; using AutoMapper; using cat_cafe.Dto; +using Serilog; +using Newtonsoft.Json; namespace cat_cafe.Controllers { @@ -18,20 +20,27 @@ namespace cat_cafe.Controllers { private readonly CatContext _context; private readonly IMapper _mapper; + private readonly ILogger _logger; - public CatsController(CatContext context, IMapper mapper) + public CatsController(CatContext context, IMapper mapper, ILogger logger) { _mapper = mapper; _context = context; + _logger = logger; } // GET: api/Cats [HttpGet] public async Task>> GetCats() { + Log.Information(this.Request.Method + " => get All customers"); var cats = await _context.Cats.ToListAsync(); - - return Ok(_mapper.Map>(cats)); + + Log.Information(this.Request.Method + " => " + + this.Response.StatusCode.ToString() + " " + + cats.GetType().ToString() + " length[" + + cats.Count + "]"); + return Ok(_mapper.Map>(cats)); } @@ -39,15 +48,20 @@ namespace cat_cafe.Controllers [HttpGet("{id}")] public async Task> GetCat(long id) { + Log.Information(this.Request.Method + " => get by ID {@id}", id); var cat = await _context.Cats.FindAsync(id); if (cat == null) { + Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString()); return NotFound(); } - + Log.Information(this.Request.Method + " => " + + this.Response.StatusCode.ToString() + " " + + cat.GetType().ToString() + " " + + JsonConvert.SerializeObject(cat).ToString()); return Ok(_mapper.Map(cat)); - } + } //TODO finish logging // PUT: api/Cats/5 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 diff --git a/cat_cafe/Entities/Cat.cs b/cat_cafe/Entities/Cat.cs index 25999eb..d16b49d 100644 --- a/cat_cafe/Entities/Cat.cs +++ b/cat_cafe/Entities/Cat.cs @@ -1,9 +1,12 @@ -namespace cat_cafe.Entities +using System.ComponentModel.DataAnnotations; + +namespace cat_cafe.Entities { public class Cat { public long Id { get; set; } public string? Name { get; set; } + [Required] public int Age { get; set; } = 0; public string Meow() { return "meow"; }