Skip to content

Commit

Permalink
Added Exercise 02 before and after.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibor Jašek committed Feb 22, 2019
1 parent 6890ebc commit 2cd57d3
Show file tree
Hide file tree
Showing 21 changed files with 891 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CookBook.DAL\CookBook.DAL.csproj" />
</ItemGroup>

</Project>
116 changes: 116 additions & 0 deletions Labs/Exercise-02-after/CookBook.DAL.Tests/CookBookDbContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using CookBook.DAL.Entities;
using CookBook.DAL.Enums;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Xunit;

namespace CookBook.DAL.Tests
{
public class CookBookDbContextTests : IClassFixture<CookBookDbContextTestsClassSetupFixture>
{
public CookBookDbContextTests(CookBookDbContextTestsClassSetupFixture testContext)
{
_testContext = testContext;
}

private readonly CookBookDbContextTestsClassSetupFixture _testContext;

[Fact]
public void AddIngredientTest()
{
//Arrange
var ingredientEntity = new IngredientEntity
{
Name = "Salt",
Description = "Mountain salt"
};

//Act
_testContext.CookBookDbContextSUT.Ingredients.Add(ingredientEntity);
_testContext.CookBookDbContextSUT.SaveChanges();


//Assert
using (var dbx = _testContext.CreateCookBookDbContext())
{
var retrievedIngredient = dbx.Ingredients.First(entity => entity.Id == ingredientEntity.Id);
Assert.Equal(ingredientEntity, retrievedIngredient, IngredientEntity.DescriptionNameIdComparer);
}
}

[Fact]
public void AddRecipeTest()
{
//Arrange
var recipeEntity = new RecipeEntity
{
Name = "Chicken soup",
Description = "Grandma's delicious chicken soup."
};

//Act
_testContext.CookBookDbContextSUT.Recipes.Add(recipeEntity);
_testContext.CookBookDbContextSUT.SaveChanges();


//Assert
using (var dbx = _testContext.CreateCookBookDbContext())
{
var retrievedRecipe = dbx.Recipes
.Include(entity => entity.Ingredients)
.ThenInclude(amount => amount.Ingredient)
.First(entity => entity.Id == recipeEntity.Id);
Assert.Equal(recipeEntity, retrievedRecipe, RecipeEntity.RecipeEntityComparer);
}
}

[Fact]
public void AddRecipeWithIngredientsTest()
{
//Arrange
var recipeEntity = new RecipeEntity
{
Name = "Lemonade",
Description = "Simple lemon lemonade",
Ingredients =
{
new IngredientAmountEntity
{
Amount = 1,
Unit = Unit.L,
Ingredient = new IngredientEntity
{
Name = "Water",
Description = "Filtered Water"
}
},
new IngredientAmountEntity()
{
Amount = 50,
Unit = Unit.Ml,
Ingredient = new IngredientEntity()
{
Name = "Lime-juice",
Description = "Fresh lime-juice"
}
}
}
};

//Act
_testContext.CookBookDbContextSUT.Recipes.Add(recipeEntity);
_testContext.CookBookDbContextSUT.SaveChanges();


//Assert
using (var dbx = _testContext.CreateCookBookDbContext())
{
var retrievedRecipe = dbx.Recipes
.Include(entity => entity.Ingredients)
.ThenInclude(amounts => amounts.Ingredient)
.First(entity => entity.Id == recipeEntity.Id);
Assert.Equal(recipeEntity, retrievedRecipe, RecipeEntity.RecipeEntityComparer);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using System;

namespace CookBook.DAL.Tests
{
public class CookBookDbContextTestsClassSetupFixture : IDisposable
{
public CookBookDbContext CookBookDbContextSUT { get; set; }

public CookBookDbContextTestsClassSetupFixture()
{
this.CookBookDbContextSUT = CreateCookBookDbContext();
}

public CookBookDbContext CreateCookBookDbContext()
{
return new CookBookDbContext(CreateDbContextOptions());
}

private DbContextOptions<CookBookDbContext> CreateDbContextOptions()
{
var contextOptionsBuilder = new DbContextOptionsBuilder<CookBookDbContext>();
contextOptionsBuilder.UseInMemoryDatabase("CookBook");
return contextOptionsBuilder.Options;
}

public void Dispose()
{
CookBookDbContextSUT?.Dispose();
}
}
}
16 changes: 16 additions & 0 deletions Labs/Exercise-02-after/CookBook.DAL/CookBook.DAL.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions Labs/Exercise-02-after/CookBook.DAL/CookBookDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using CookBook.DAL.Entities;
using Microsoft.EntityFrameworkCore;

namespace CookBook.DAL
{
public class CookBookDbContext : DbContext
{
public CookBookDbContext()
{

}
public CookBookDbContext(DbContextOptions<CookBookDbContext> contextOptions)
: base(contextOptions)
{
}

//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//{
// optionsBuilder.UseSqlServer(
// @"Data Source=(LocalDB)\MSSQLLocalDB;
// Initial Catalog = CookBook;
// MultipleActiveResultSets = True;
// Integrated Security = True; ");
//}

public DbSet<RecipeEntity> Recipes { get; set; }
public DbSet<IngredientEntity> Ingredients { get; set; }
}
}
9 changes: 9 additions & 0 deletions Labs/Exercise-02-after/CookBook.DAL/Entities/EntityBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace CookBook.DAL.Entities
{
public abstract class EntityBase : IEntity
{
public Guid Id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using CookBook.DAL.Enums;
using System.Collections.Generic;

namespace CookBook.DAL.Entities
{
public class IngredientAmountEntity : EntityBase
{
public double Amount { get; set; }
public Unit Unit { get; set; }
public RecipeEntity Recipe { get; set; }
public IngredientEntity Ingredient { get; set; }

private sealed class IngredientAmountEqualityComparer : IEqualityComparer<IngredientAmountEntity>
{
public bool Equals(IngredientAmountEntity x, IngredientAmountEntity y)
{
if (ReferenceEquals(x, y))
{
return true;
}

if (ReferenceEquals(x, null))
{
return false;
}

if (ReferenceEquals(y, null))
{
return false;
}

if (x.GetType() != y.GetType())
{
return false;
}
return x.Amount.Equals(y.Amount) &&
IngredientEntity.DescriptionNameIdComparer.Equals(x.Ingredient, y.Ingredient) &&
RecipeEntity.WithoutIngredientsComparer.Equals(x.Recipe, y.Recipe) && x.Unit == y.Unit &&
x.Id.Equals(y.Id);
}

public int GetHashCode(IngredientAmountEntity obj)
{
unchecked
{
var hashCode = obj.Amount.GetHashCode();
hashCode = (hashCode * 397) ^ (obj.Ingredient != null ? obj.Ingredient.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (obj.Recipe != null ? obj.Recipe.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (int)obj.Unit;
hashCode = (hashCode * 397) ^ obj.Id.GetHashCode();
return hashCode;
}
}
}

public static IEqualityComparer<IngredientAmountEntity> IngredientAmountComparer { get; } = new IngredientAmountEqualityComparer();
}
}
50 changes: 50 additions & 0 deletions Labs/Exercise-02-after/CookBook.DAL/Entities/IngredientEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;

namespace CookBook.DAL.Entities
{
public class IngredientEntity : EntityBase
{
public string Name { get; set; }
public string Description { get; set; }

private sealed class DescriptionNameIdEqualityComparer : IEqualityComparer<IngredientEntity>
{
public bool Equals(IngredientEntity x, IngredientEntity y)
{
if (ReferenceEquals(x, y))
{
return true;
}

if (ReferenceEquals(x, null))
{
return false;
}

if (ReferenceEquals(y, null))
{
return false;
}

if (x.GetType() != y.GetType())
{
return false;
}
return string.Equals(x.Description, y.Description) && string.Equals(x.Name, y.Name) && x.Id.Equals(y.Id);
}

public int GetHashCode(IngredientEntity obj)
{
unchecked
{
var hashCode = (obj.Description != null ? obj.Description.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (obj.Name != null ? obj.Name.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ obj.Id.GetHashCode();
return hashCode;
}
}
}

public static IEqualityComparer<IngredientEntity> DescriptionNameIdComparer { get; } = new DescriptionNameIdEqualityComparer();
}
}
Loading

0 comments on commit 2cd57d3

Please sign in to comment.