Skip to content

Commit

Permalink
Started UnitTest and Refactored
Browse files Browse the repository at this point in the history
Added CommandTest
  • Loading branch information
baristutakli committed Jan 30, 2022
1 parent b9bb017 commit ed56a41
Show file tree
Hide file tree
Showing 45 changed files with 175 additions and 47 deletions.
Binary file modified .vs/BookStoreSln/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file modified .vs/BookStoreSln/v16/.suo
Binary file not shown.
28 changes: 23 additions & 5 deletions BookStoreSln.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi", "WebApi\WebApi.csproj", "{AACF9BDF-F134-40F0-879A-FE4DFC341FCB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi", "WebApi\WebApi.csproj", "{AACF9BDF-F134-40F0-879A-FE4DFC341FCB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{06A4A5DC-CE59-437C-B244-E7E56ED547EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi.UnitTests", "Tests\WebApi.UnitTests\WebApi.UnitTests.csproj", "{44FB7EF5-3C4C-4A2C-B978-311AE966C683}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.UnitTests", "Tests\WebApi.UnitTests\WebApi.UnitTests.csproj", "{44FB7EF5-3C4C-4A2C-B978-311AE966C683}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Application", "Application", "{7C5F1C80-DF5B-44DD-98F1-C0B441BCFEF9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BookOperations", "BookOperations", "{75E6EF46-506D-4CDA-863F-772433F9A0C4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Commands", "Commands", "{169C9F66-5BB5-421C-A705-2802B65BD536}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CreateCommand", "CreateCommand", "{B359AB87-C3FF-4F3E-9077-C63DF70D6D54}"
ProjectSection(SolutionItems) = preProject
CreateCommandTests.cs = CreateCommandTests.cs
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -18,9 +29,6 @@ Global
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AACF9BDF-F134-40F0-879A-FE4DFC341FCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AACF9BDF-F134-40F0-879A-FE4DFC341FCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand All @@ -47,7 +55,17 @@ Global
{44FB7EF5-3C4C-4A2C-B978-311AE966C683}.Release|x86.ActiveCfg = Release|Any CPU
{44FB7EF5-3C4C-4A2C-B978-311AE966C683}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{44FB7EF5-3C4C-4A2C-B978-311AE966C683} = {06A4A5DC-CE59-437C-B244-E7E56ED547EF}
{7C5F1C80-DF5B-44DD-98F1-C0B441BCFEF9} = {06A4A5DC-CE59-437C-B244-E7E56ED547EF}
{75E6EF46-506D-4CDA-863F-772433F9A0C4} = {7C5F1C80-DF5B-44DD-98F1-C0B441BCFEF9}
{169C9F66-5BB5-421C-A705-2802B65BD536} = {75E6EF46-506D-4CDA-863F-772433F9A0C4}
{B359AB87-C3FF-4F3E-9077-C63DF70D6D54} = {169C9F66-5BB5-421C-A705-2802B65BD536}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {70C78363-AB4C-4F1E-B8C6-2791473BF59A}
EndGlobalSection
EndGlobal
Empty file added CreateBookCommandTest.cs
Empty file.
7 changes: 7 additions & 0 deletions CreateCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Application.BookOperations.Commands.CreateCommand
{
public class CreateBookCommandTests
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using AutoMapper;
using FluentAssertions;
using TestSetup;
using WebApi.Application.BookOperations.Commands.CreateBook;
using WebApi.DBOperations;
using WebApi.Entities;
using Xunit;

namespace Application.BookOperations.Commands.CreateCommand{
public class CreateBookCommandTests:IClassFixture<CommonTestFixture>
{

private readonly BookStoreDbContext _context;
private readonly IMapper _mapper;

public CreateBookCommandTests(CommonTestFixture fixture)
{
_context = fixture.Context;
_mapper = fixture.Mapper;
}
[Fact]
public void WhenAlreadExistBooktitleIsGiven_InvalidOperationException_ShouldBeReturn(){

CreateBookCommand command = new CreateBookCommand(_context, _mapper);
var book= new Book { Title = "WhenAlreadExistBooktitleIsGiven_InvalidOperationException_ShouldBeReturn", GendreId = 1, PageCount = 200, PublishDate = new DateTime(2000, 10, 10) };
_context.Books.Add(book);
_context.SaveChanges();
command.Model = new CreateBookModel{Title=book.Title};

FluentActions.Invoking(()=>command.Handle())
.Should().Throw<InvalidOperationException>().And.Message.Should().Be("Kitap zaten mevcut");


command.Handle();
}
}
}
16 changes: 16 additions & 0 deletions Tests/WebApi.UnitTests/TestSetup/Books.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using WebApi.DBOperations;
using WebApi.Entities;

namespace TestSetup{
public static class Books{

public static void AddBooks(this BookStoreDbContext context){
context.Books.AddRange(
new Book { Title = "Learn Startup", GendreId = 1, PageCount = 200, PublishDate = new DateTime(2000, 10, 10) },
new Book { Title = "Herland", GendreId = 2, PageCount = 250, PublishDate = new DateTime(2010, 5, 20) },
new Book { Title = "Dune", GendreId = 2, PageCount = 2540, PublishDate = new DateTime(2020, 5, 20) });

}
}
}
21 changes: 21 additions & 0 deletions Tests/WebApi.UnitTests/TestSetup/CommonTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using WebApi.Common;
using WebApi.DBOperations;

namespace TestSetup{
public class CommonTestFixture{
public BookStoreDbContext Context{get;set;}
public IMapper Mapper {get;set;}
public CommonTestFixture()
{
var options= new DbContextOptionsBuilder<BookStoreDbContext>().UseInMemoryDatabase(databaseName:"BookStoreTestDB").Options;
Context =new BookStoreDbContext(options);
Context.Database.EnsureCreated();
Context.AddBooks();
Context.AddGenres();

Mapper = new MapperConfiguration(cfg=>{cfg.AddProfile<MappingProfile>();}).CreateMapper();
}
}
}
19 changes: 19 additions & 0 deletions Tests/WebApi.UnitTests/TestSetup/Genres
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using WebApi.DBOperations;
using WebApi.Entities;

namespace TestSetup{
public static class Genres{

public static void AddGenres(this BookStoreDbContext context){
context.Genres.AddRange(
new Genre { Name = "PersonalGrowth" },
new Genre { Name = "ScienceFiction" },
new Genre { Name = "Noval" });
context.Authors.AddRange(
new Author { FirstName="Orhan", LastName="Pamuk", BirthDate=DateTime.Parse("07/07/1952") }
);

}
}
}
14 changes: 0 additions & 14 deletions Tests/WebApi.UnitTests/UnitTest1.cs

This file was deleted.

Binary file not shown.
Binary file modified Tests/WebApi.UnitTests/bin/Debug/net5.0/WebApi.UnitTests.dll
Binary file not shown.
Binary file modified Tests/WebApi.UnitTests/bin/Debug/net5.0/WebApi.UnitTests.pdb
Binary file not shown.
Binary file modified Tests/WebApi.UnitTests/bin/Debug/net5.0/WebApi.dll
Binary file not shown.
Binary file modified Tests/WebApi.UnitTests/bin/Debug/net5.0/WebApi.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ build_property.PublishSingleFile =
build_property.IncludeAllContentForSelfExtract =
build_property._SupportedPlatformList = Android,iOS,Linux,macOS,Windows
build_property.RootNamespace = WebApi.UnitTests
build_property.ProjectDir = C:\Users\kbez1\Documents\kodlar\BookStore\Tests\WebApi.UnitTests\
build_property.ProjectDir = c:\Users\kbez1\Documents\kodlar\BookStore\Tests\WebApi.UnitTests\
Binary file not shown.
Binary file not shown.
Binary file modified Tests/WebApi.UnitTests/obj/Debug/net5.0/WebApi.UnitTests.dll
Binary file not shown.
Binary file modified Tests/WebApi.UnitTests/obj/Debug/net5.0/WebApi.UnitTests.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace WebApi.Application.BookOperations.Commands.CreateBook
public class CreateBookCommand
{
public CreateBookModel Model{ get; set; }
private readonly BookStoreDbContext _dbcontext;
private readonly IBookStoreDbContext _dbcontext;
private readonly IMapper _mapper;
public CreateBookCommand(BookStoreDbContext context, IMapper mapper)
public CreateBookCommand(IBookStoreDbContext context, IMapper mapper)
{
_dbcontext = context;
_mapper = mapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace WebApi.Application.BookOperations.Commands.DeleteBook
{
public class DeleteBookCommand
{
private readonly BookStoreDbContext _context;
private readonly IBookStoreDbContext _context;
public int BookId { get; set; }
public DeleteBookCommand(BookStoreDbContext context)
public DeleteBookCommand(IBookStoreDbContext context)
{
_context = context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace WebApi.Application.BookOperations.Commands.UpdateBook
{
public class UpdateBookCommand
{
private readonly BookStoreDbContext _context;
private readonly IBookStoreDbContext _context;
public int BookId;
public UpdateBookModel Model { get; set; }
public UpdateBookCommand(BookStoreDbContext context)
public UpdateBookCommand(IBookStoreDbContext context)
{
_context = context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace WebApi.Application.BookOperations.Queries.GetBookDetail
public partial class GetBookDetailQuery
{

private readonly BookStoreDbContext _dbcontext;
private readonly IBookStoreDbContext _dbcontext;
public int BookId { get; set; }
private readonly IMapper _mapper;
public GetBookDetailQuery(BookStoreDbContext context,IMapper mapper)
public GetBookDetailQuery(IBookStoreDbContext context,IMapper mapper)
{
_dbcontext = context;
_mapper = mapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace WebApi.Application.BookOperations.Queries.GetBooks
{
public partial class GetBooksQuery
{
private readonly BookStoreDbContext _dbcontext;
private readonly IBookStoreDbContext _dbcontext;
private readonly IMapper _mapper;
public GetBooksQuery(BookStoreDbContext context,IMapper mapper)
public GetBooksQuery(IBookStoreDbContext context,IMapper mapper)
{
_dbcontext = context;
_mapper = mapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace WebApi.Application.GenreOperations.Commands.CreateGenre
{
public class CreateGenreCommand
{
private readonly BookStoreDbContext _context;
private readonly IBookStoreDbContext _context;

public CreateGenreModel Model { get; set; }

public CreateGenreCommand(BookStoreDbContext context)
public CreateGenreCommand(IBookStoreDbContext context)
{
_context = context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class DeleteGenreCommand
{
public int GenreId { get; set; }

private readonly BookStoreDbContext _context;
private readonly IBookStoreDbContext _context;

public DeleteGenreCommand(BookStoreDbContext context)
public DeleteGenreCommand(IBookStoreDbContext context)
{
_context = context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace WebApi.Application.GenreOperations.Commands.UpdateGenre
{
public class UpdateGenreCommand
{
private readonly BookStoreDbContext _context;
private readonly IBookStoreDbContext _context;

public UpdateGenreModel Model { get; set; }
public int GenreId { get; set; }

public UpdateGenreCommand(BookStoreDbContext context)
public UpdateGenreCommand(IBookStoreDbContext context)
{
_context = context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace WebApi.Application.GenreOperations.Queries.GetGenreDetail
{
public class GetGenreDetailQuery
{
private readonly BookStoreDbContext _context;
private readonly IBookStoreDbContext _context;
private readonly IMapper _mapper;
public int GenreId { get; set; }


public GetGenreDetailQuery(BookStoreDbContext context, IMapper mapper)
public GetGenreDetailQuery(IBookStoreDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace WebApi.Application.GenreOperations.Queries.GetGenres
{
public partial class GetGenresQuery
{
public readonly BookStoreDbContext _context;
public readonly IBookStoreDbContext _context;
private readonly IMapper _mapper;

public GetGenresQuery(BookStoreDbContext context, IMapper mapper)
public GetGenresQuery(IBookStoreDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
Expand Down
4 changes: 2 additions & 2 deletions WebApi/Controllers/BooksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace WebApi.AddControllers
public class BooksController : Controller
{

private readonly BookStoreDbContext _context;
private readonly IBookStoreDbContext _context;

private readonly IMapper _mapper;

public BooksController(BookStoreDbContext context, IMapper mapper)
public BooksController(IBookStoreDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
Expand Down
4 changes: 2 additions & 2 deletions WebApi/Controllers/GenreController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ namespace WebApi.Controllers
[ApiController]
public class GenreController : ControllerBase
{
private readonly BookStoreDbContext _context;
private readonly IBookStoreDbContext _context;
private readonly IMapper _mapper;

public GenreController(BookStoreDbContext context, IMapper mapper)
public GenreController(IBookStoreDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
Expand Down
6 changes: 5 additions & 1 deletion WebApi/DBOperations/BookStoreDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

namespace WebApi.DBOperations
{
public class BookStoreDbContext : DbContext
public class BookStoreDbContext : DbContext, IBookStoreDbContext
{
public BookStoreDbContext(DbContextOptions<BookStoreDbContext> options) : base(options)
{ }
public DbSet<Book> Books { get; set; }
public DbSet<Genre> Genres { get; set; }
public DbSet<Author> Authors { get; set; }

public override int SaveChanges()
{
return base.SaveChanges();
}
}
}
17 changes: 17 additions & 0 deletions WebApi/DBOperations/IBookStoreDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApi.Entities;

namespace WebApi.DBOperations
{
public interface IBookStoreDbContext
{
public DbSet<Book> Books { get; set; }
public DbSet<Genre> Genres { get; set; }

int SaveChanges();
}
}
2 changes: 2 additions & 0 deletions WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void ConfigureServices(IServiceCollection services)
});
services.AddDbContext<BookStoreDbContext>(options => options.UseInMemoryDatabase(databaseName: "BookStoreDB"));
services.AddAutoMapper(Assembly.GetExecutingAssembly());
// Sana bir servis vericeðim bunu inject ettiðim yerlerde bu interface bu servise denk gelecek
services.AddScoped<IBookStoreDbContext>(provider=>provider.GetService<BookStoreDbContext>());
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Binary file modified WebApi/bin/Debug/net5.0/WebApi.dll
Binary file not shown.
Binary file modified WebApi/bin/Debug/net5.0/WebApi.pdb
Binary file not shown.
Binary file modified WebApi/bin/Debug/net5.0/ref/WebApi.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ build_property.PublishSingleFile =
build_property.IncludeAllContentForSelfExtract =
build_property._SupportedPlatformList = Android,iOS,Linux,macOS,Windows
build_property.RootNamespace = WebApi
build_property.ProjectDir = C:\Users\kbez1\Documents\kodlar\BookStore\WebApi\
build_property.ProjectDir = c:\Users\kbez1\Documents\kodlar\BookStore\WebApi\
Binary file modified WebApi/obj/Debug/net5.0/WebApi.assets.cache
Binary file not shown.
Binary file modified WebApi/obj/Debug/net5.0/WebApi.csproj.AssemblyReference.cache
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3e8319b82519f1310e527c1bfd17d65fc8e54410
63ee7d3da1cf722091e12be40d42cc4cfe83bcbd
Binary file modified WebApi/obj/Debug/net5.0/WebApi.dll
Binary file not shown.
Binary file modified WebApi/obj/Debug/net5.0/WebApi.pdb
Binary file not shown.
Binary file modified WebApi/obj/Debug/net5.0/ref/WebApi.dll
Binary file not shown.

0 comments on commit ed56a41

Please sign in to comment.