diff --git a/.gitignore b/.gitignore index 48597ec..5273f55 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ # Edit at https://www.toptal.com/developers/gitignore?templates=dotnetcore,visualstudio,visualstudiocode log.txt +cat_cafe/log.txt ### DotnetCore ### # .NET Core build folders @@ -427,3 +428,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 new file mode 100644 index 0000000..796fca7 --- /dev/null +++ b/Tests/Controllers/CatsControllerTest.cs @@ -0,0 +1,127 @@ +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 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 +{ + + [TestClass()] + public class CatsControllerTest + { + + private readonly ILogger logger = new NullLogger(); + + private readonly MapperConfiguration mapperConf = new(mapper => mapper.AddProfile(typeof(CatMapper))); + + private readonly DbContextOptions options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "CatCafeTests") + .Options; + + private readonly IMapper mapper; + + private readonly CatCafeContext context; + + private readonly CatsController controller; + + public CatsControllerTest() + { + mapper = mapperConf.CreateMapper(); + context = new CatCafeContext(options); + controller = new CatsController(context, mapper, logger); + } + + + [TestInitialize] + public void BeforeEach() + { + context.Database.EnsureCreated(); + context.Cats.AddRange( + new Cat + { + Id = 1, + Name = "Alice", + Age = 5 + }, + new Cat + { + Id = 2, + Name = "Bob", + Age = 3 + }); + context.SaveChanges(); + } + + [TestCleanup] + public void AfterEach() + { + context.Database.EnsureDeleted(); + } + + [TestMethod()] + public async Task GetCatsTest() + { + 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()] + public void GetCatTest() + { + Assert.IsTrue(true); + } + + [TestMethod()] + public void PutCatTest() + { + Assert.IsTrue(true); + } + + [TestMethod()] + public void PostCatTest() + { + Assert.IsTrue(true); + } + + [TestMethod()] + public void DeleteCatTest() + { + Assert.IsTrue(true); + } + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index fd2e07d..92027b6 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -9,24 +9,16 @@ + - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - + + + + - - - diff --git a/Tests/UnitTest1.cs b/Tests/UnitTest1.cs deleted file mode 100644 index d834b5f..0000000 --- a/Tests/UnitTest1.cs +++ /dev/null @@ -1,23 +0,0 @@ -using cat_cafe.Entities; - -namespace Tests -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - String name = "Margot"; - int id = 1337; - - Cat cat = new() - { - Id = id, - Name = name - }; - - Assert.Equal(name, cat.Name); - Assert.Equal(id, cat.Id); - } - } -} \ No newline at end of file diff --git a/Tests/Usings.cs b/Tests/Usings.cs index 8c927eb..ab67c7e 100644 --- a/Tests/Usings.cs +++ b/Tests/Usings.cs @@ -1 +1 @@ -global using Xunit; \ No newline at end of file +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file diff --git a/cat_cafe/Controllers/BarsController.cs b/cat_cafe/Controllers/BarsController.cs index db7c6bc..ec750c4 100644 --- a/cat_cafe/Controllers/BarsController.cs +++ b/cat_cafe/Controllers/BarsController.cs @@ -18,11 +18,11 @@ namespace cat_cafe.Controllers [ApiController] public class BarsController : ControllerBase { - private readonly BarContext _context; + private readonly CatCafeContext _context; private readonly IMapper _mapper; private readonly ILogger _logger; - public BarsController(BarContext context,IMapper mapper, ILogger logger) + public BarsController(CatCafeContext context,IMapper mapper, ILogger logger) { _context = context; _mapper = mapper; diff --git a/cat_cafe/Controllers/CatsController.cs b/cat_cafe/Controllers/CatsController.cs index b6f4160..0857fa9 100644 --- a/cat_cafe/Controllers/CatsController.cs +++ b/cat_cafe/Controllers/CatsController.cs @@ -9,6 +9,9 @@ using cat_cafe.Entities; using cat_cafe.Repositories; using AutoMapper; using cat_cafe.Dto; +using Serilog; +using Newtonsoft.Json; +using Microsoft.Extensions.Logging.Abstractions; namespace cat_cafe.Controllers { @@ -16,13 +19,15 @@ namespace cat_cafe.Controllers [ApiController] public class CatsController : ControllerBase { - private readonly CatContext _context; + private readonly CatCafeContext _context; private readonly IMapper _mapper; + private readonly ILogger _logger; - public CatsController(CatContext context, IMapper mapper) + public CatsController(CatCafeContext context, IMapper mapper, ILogger logger) { _mapper = mapper; _context = context; + _logger = logger; } // GET: api/Cats @@ -30,8 +35,8 @@ namespace cat_cafe.Controllers public async Task>> GetCats() { var cats = await _context.Cats.ToListAsync(); - return _mapper.Map>(cats); + return Ok(_mapper.Map>(cats)); } // GET: api/Cats/5 @@ -45,7 +50,7 @@ namespace cat_cafe.Controllers return NotFound(); } - return _mapper.Map(cat); + return Ok(_mapper.Map(cat)); } // PUT: api/Cats/5 diff --git a/cat_cafe/Controllers/CustomersController.cs b/cat_cafe/Controllers/CustomersController.cs index e2a5b21..3055732 100644 --- a/cat_cafe/Controllers/CustomersController.cs +++ b/cat_cafe/Controllers/CustomersController.cs @@ -18,11 +18,11 @@ namespace cat_cafe.Controllers [ApiController] public class CustomersController : ControllerBase { - private readonly CustomerContext _context; + private readonly CatCafeContext _context; private readonly IMapper _mapper; private readonly ILogger _logger; - public CustomersController(CustomerContext context,IMapper mapper,ILogger logger) + public CustomersController(CatCafeContext context,IMapper mapper,ILogger logger) { _context = context; _mapper = mapper; diff --git a/cat_cafe/Dto/CatDto.cs b/cat_cafe/Dto/CatDto.cs index 354e814..86cf631 100644 --- a/cat_cafe/Dto/CatDto.cs +++ b/cat_cafe/Dto/CatDto.cs @@ -5,6 +5,5 @@ namespace cat_cafe.Dto { public long Id { get; set; } public string? Name { get; set; } - public int Age { get; set; } } } 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"; } diff --git a/cat_cafe/Program.cs b/cat_cafe/Program.cs index 307d218..47a5e4b 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/BarContext.cs b/cat_cafe/Repositories/BarContext.cs deleted file mode 100644 index f0f4672..0000000 --- a/cat_cafe/Repositories/BarContext.cs +++ /dev/null @@ -1,17 +0,0 @@ -using cat_cafe.Entities; -using Microsoft.EntityFrameworkCore; -using System; -namespace cat_cafe.Repositories -{ - public class BarContext : DbContext - { - public BarContext(DbContextOptions options) - : base(options) - { - } - - public DbSet Bars { get; set; } = null!; - - } -} - diff --git a/cat_cafe/Repositories/CatContext.cs b/cat_cafe/Repositories/CatContext.cs index f0ca879..1ee329a 100644 --- a/cat_cafe/Repositories/CatContext.cs +++ b/cat_cafe/Repositories/CatContext.cs @@ -3,14 +3,16 @@ using Microsoft.EntityFrameworkCore; namespace cat_cafe.Repositories { - public class CatContext : DbContext + public class CatCafeContext : DbContext { - public CatContext(DbContextOptions options) + public CatCafeContext(DbContextOptions options) : base(options) { } public DbSet Cats { get; set; } = null!; + public DbSet Bars { get; set; } = null!; + public DbSet Customers { get; set; } = null!; } } diff --git a/cat_cafe/Repositories/CustomerContext.cs b/cat_cafe/Repositories/CustomerContext.cs deleted file mode 100644 index a96292f..0000000 --- a/cat_cafe/Repositories/CustomerContext.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using cat_cafe.Entities; -using Microsoft.EntityFrameworkCore; -namespace cat_cafe.Repositories -{ - public class CustomerContext:DbContext - { - public CustomerContext(DbContextOptions options) - : base(options) - { } - public DbSet Customers { get; set; } = null!; - } -} - diff --git a/cat_cafe/cat_cafe.sln b/cat_cafe/cat_cafe.sln index aa8e7c0..86b7799 100644 --- a/cat_cafe/cat_cafe.sln +++ b/cat_cafe/cat_cafe.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 17.2.32616.157 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cat_cafe", "cat_cafe.csproj", "{CC02D05A-3817-4D0A-8766-3FEE0C90941B}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "..\Tests\Tests.csproj", "{674DDACC-DB0E-473B-81E2-E3CABB8CB65B}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "..\Tests\Tests.csproj", "{039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +17,10 @@ Global {CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Debug|Any CPU.Build.0 = Debug|Any CPU {CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Release|Any CPU.ActiveCfg = Release|Any CPU {CC02D05A-3817-4D0A-8766-3FEE0C90941B}.Release|Any CPU.Build.0 = Release|Any CPU - {674DDACC-DB0E-473B-81E2-E3CABB8CB65B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {674DDACC-DB0E-473B-81E2-E3CABB8CB65B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {674DDACC-DB0E-473B-81E2-E3CABB8CB65B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {674DDACC-DB0E-473B-81E2-E3CABB8CB65B}.Release|Any CPU.Build.0 = Release|Any CPU + {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {039A9A95-25ED-4632-9C4B-0AB4E5B5A7B4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/cat_cafe/log.txt b/cat_cafe/log.txt deleted file mode 100644 index b50aa8e..0000000 --- a/cat_cafe/log.txt +++ /dev/null @@ -1,28 +0,0 @@ -2023-01-21 09:37:45.148 +01:00 [INF] program start -2023-01-28 08:16:52.320 +01:00 [INF] program start -2023-01-28 08:30:32.772 +01:00 [INF] program start -2023-01-28 08:31:51.147 +01:00 [INF] program start -2023-01-28 08:35:42.533 +01:00 [INF] program start -2023-01-28 08:37:29.403 +01:00 [INF] program start -2023-01-28 08:38:02.513 +01:00 [INF] program start -2023-01-28 08:38:24.151 +01:00 [INF] program start -2023-01-28 08:39:11.319 +01:00 [INF] program start -2023-01-28 08:54:25.005 +01:00 [INF] program start -2023-01-28 08:55:19.776 +01:00 [INF] program start -2023-01-28 08:57:05.150 +01:00 [INF] POST => post customer -2023-01-28 08:57:05.326 +01:00 [INF] POST => 201 cat_cafe.Entities.Customer {"Id":1,"FullName":"string","Age":0} -2023-01-28 08:57:13.908 +01:00 [INF] GET => get All customers -2023-01-28 08:57:13.997 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[1] -2023-01-28 08:58:25.856 +01:00 [INF] program start -2023-01-28 09:05:05.071 +01:00 [INF] program start -2023-01-28 09:13:54.542 +01:00 [INF] program start -2023-01-28 09:17:54.058 +01:00 [INF] program start -2023-01-28 09:24:15.797 +01:00 [INF] program start -2023-01-28 09:26:42.943 +01:00 [INF] program start -2023-01-28 09:31:25.523 +01:00 [INF] program start -2023-01-28 09:38:42.245 +01:00 [INF] program start -2023-01-28 09:40:40.846 +01:00 [INF] program start -2023-01-28 09:49:33.194 +01:00 [INF] program start -2023-01-28 09:52:36.098 +01:00 [INF] program start -2023-01-28 09:56:02.340 +01:00 [INF] program start -2023-01-28 09:57:05.155 +01:00 [INF] program start