Remove logger from controller, pass 1st UT

pull/52/head
Alexis Drai 2 years ago
parent c2457e339d
commit 87e39504f7

@ -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<CatContext> options = new DbContextOptionsBuilder<CatContext>()
.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<IEnumerable<CatDto>> actual = await controller.GetCats();
Assert.Equals(200, actual.Result);
var actual = await controller.GetCats();
actual.Result.Should().BeOfType<OkObjectResult>();
var actualResult = actual.Result as OkObjectResult;
actualResult.Should().NotBeNull();
actualResult!.Value.Should().BeEquivalentTo(new List<CatDto>()
{
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();
}
}
}

@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />

@ -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<ActionResult<IEnumerable<CatDto>>> 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<List<CatDto>>(cats));
}
// GET: api/Cats/5
[HttpGet("{id}")]
public async Task<ActionResult<CatDto>> 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<CatDto>(cat));
} //TODO finish logging
}
// PUT: api/Cats/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754

@ -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<CatContext>(opt =>
opt.UseInMemoryDatabase("CatCafe"));
builder.Services.AddDbContext<BarContext>(opt =>
opt.UseInMemoryDatabase("CatCafe"));
builder.Services.AddDbContext<CustomerContext>(opt =>
opt.UseInMemoryDatabase("CatCafe"));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddDbContext<CatContext>(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<ILoggerFactory>();
loggerFactory.AddFile(builder.Configuration["Logging:LogFilePath"].ToString());
app.Services.GetService<ILoggerFactory>().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();

@ -11,6 +11,8 @@ namespace cat_cafe.Repositories
}
public DbSet<Cat> Cats { get; set; } = null!;
public DbSet<Bar> Bars { get; set; } = null!;
public DbSet<Customer> Customers { get; set; } = null!;
}
}

Loading…
Cancel
Save