diff --git a/Tests/Controllers/CatsControllerTest.cs b/Tests/Controllers/CatsControllerTest.cs index e2c8273..2a50b2b 100644 --- a/Tests/Controllers/CatsControllerTest.cs +++ b/Tests/Controllers/CatsControllerTest.cs @@ -11,11 +11,14 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using FluentAssertions; + namespace cat_cafe.Controllers.Tests { @@ -28,12 +31,12 @@ namespace cat_cafe.Controllers.Tests 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 IMapper mapper; + private readonly CatContext context; private readonly CatsController controller; @@ -47,32 +50,54 @@ namespace cat_cafe.Controllers.Tests [TestInitialize] - public void StartUp() + public void BeforeEach() { context.Database.EnsureCreated(); context.Cats.AddRange( new Cat - { - Id = 1, - Name = "Alice", - Age = 5 - }, + { + Id = 1, + Name = "Alice", + Age = 5 + }, new Cat - { - Id = 2, - Name = "Bob", - Age = 3 - }); + { + Id = 2, + Name = "Bob", + Age = 3 + }); context.SaveChanges(); + } - // TODO tear down and drop all before each test method + [TestCleanup] + public void AfterEach() + { + context.Database.EnsureDeleted(); } [TestMethod()] public async Task GetCatsTest() { - ActionResult> actual = await controller.GetCats(); - Assert.Equals(200, actual.Result); + var actual = await controller.GetCats(); + + actual.Result.Should().BeOfType(); + + var actualResult = actual.Result as OkObjectResult; + + actualResult.Should().NotBeNull(); + actualResult!.Value.Should().BeEquivalentTo(new List() + { + new CatDto + { + Id = 1, + Name = "Alice", + }, + new CatDto + { + Id = 2, + Name = "Bob", + } + }.AsEnumerable()); } [TestMethod()] @@ -98,12 +123,5 @@ namespace cat_cafe.Controllers.Tests { Assert.Fail(); } - - [TestMethod()] - public void CatExistsTest() - { - Assert.Fail(); - } - } } \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 66038da..92027b6 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/cat_cafe/Controllers/CatsController.cs b/cat_cafe/Controllers/CatsController.cs index f78e672..db5f26b 100644 --- a/cat_cafe/Controllers/CatsController.cs +++ b/cat_cafe/Controllers/CatsController.cs @@ -11,6 +11,7 @@ using AutoMapper; using cat_cafe.Dto; using Serilog; using Newtonsoft.Json; +using Microsoft.Extensions.Logging.Abstractions; namespace cat_cafe.Controllers { @@ -33,35 +34,24 @@ namespace cat_cafe.Controllers [HttpGet] public async Task>> GetCats() { - Log.Information(this.Request.Method + " => get All customers"); var cats = await _context.Cats.ToListAsync(); - Log.Information(this.Request.Method + " => " - + this.Response.StatusCode.ToString() + " " - + cats.GetType().ToString() + " length[" - + cats.Count + "]"); return Ok(_mapper.Map>(cats)); - } // GET: api/Cats/5 [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/Program.cs b/cat_cafe/Program.cs index 307d218..ab625cb 100644 --- a/cat_cafe/Program.cs +++ b/cat_cafe/Program.cs @@ -5,37 +5,21 @@ using Serilog.Sinks.File; var builder = WebApplication.CreateBuilder(args); -/* -ILoggerFactory loggerFactory=new LoggerFactory(); -loggerFactory.AddFile($@"{Directory.GetCurrentDirectory}\Logs\logs.txt"); -*/ - Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.File("log.txt").CreateLogger(); // Add services to the container. builder.Services.AddControllers(); -builder.Services.AddDbContext(opt => -opt.UseInMemoryDatabase("CatCafe")); -builder.Services.AddDbContext(opt => -opt.UseInMemoryDatabase("CatCafe")); -builder.Services.AddDbContext(opt => -opt.UseInMemoryDatabase("CatCafe")); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("CatCafe")); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); - builder.Services.AddAutoMapper(typeof(Program)); builder.Services.AddControllersWithViews(); var app = builder.Build(); -/*var loggerFactory = app.Services.GetService(); -loggerFactory.AddFile(builder.Configuration["Logging:LogFilePath"].ToString()); - -app.Services.GetService().AddFile(builder.Configuration["Logging:LogFilePath"].ToString());*/ - app.UseHttpLogging(); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -50,4 +34,5 @@ app.UseAuthorization(); app.MapControllers(); Log.Information("program start"); + app.Run(); diff --git a/cat_cafe/Repositories/CatContext.cs b/cat_cafe/Repositories/CatContext.cs index f0ca879..2a67d5a 100644 --- a/cat_cafe/Repositories/CatContext.cs +++ b/cat_cafe/Repositories/CatContext.cs @@ -11,6 +11,8 @@ namespace cat_cafe.Repositories } public DbSet Cats { get; set; } = null!; + public DbSet Bars { get; set; } = null!; + public DbSet Customers { get; set; } = null!; } }