Fully test PlayerDbManager, and improve it
continuous-integration/drone/push Build is passing Details

pull/110/head
Alexis Drai 2 years ago
parent 9a453da7bc
commit d8fead1051

@ -11,10 +11,23 @@ namespace Data.EF
throw new NotImplementedException();
}
public DbSet<PlayerEntity>? Players { get; set; }
public DbSet<PlayerEntity> Players { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite("Data Source=EFDice.DiceApp.db");
public DiceAppDbContext()
{ }
public DiceAppDbContext(DbContextOptions<DiceAppDbContext> options)
: base(options)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder
.UseSqlite("Data Source=EFDice.DiceApp.db")
.EnableSensitiveDataLogging();
}
}
}
}

@ -1,5 +1,8 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Model;
using Model.Players;
using System.Runtime.Intrinsics.Arm;
namespace Data.EF.Players
{
@ -9,74 +12,153 @@ namespace Data.EF.Players
public PlayerDbManager(DiceAppDbContext db)
{
if (db is null)
{
throw new ArgumentNullException(nameof(db), "param should not be null");
}
this.db = db;
}
public PlayerEntity Add(PlayerEntity toAdd)
/// <summary>
/// side effect: entity's name is trimmed.
/// </summary>
/// <param name="entity"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private static void CleanPlayerEntity(PlayerEntity entity)
{
if (db.Players!.Where(entity => entity.Name == toAdd.Name).Any())
if (entity is null)
{
throw new ArgumentException("this username is already taken", nameof(toAdd));
throw new ArgumentNullException(nameof(entity), "param should not be null");
}
if (toAdd is null)
if (string.IsNullOrWhiteSpace(entity.Name))
{
throw new ArgumentNullException(nameof(toAdd), "param should not be null");
throw new ArgumentException("Name property should not be null or whitespace", nameof(entity));
}
if (string.IsNullOrWhiteSpace(toAdd.Name))
entity.Name = entity.Name.Trim();
}
/// <summary>
/// adds a non-null PlayerEntity with a valid name to this mgr's context
/// </summary>
/// <param name="toAdd">the entity to add</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public PlayerEntity Add(PlayerEntity toAdd)
{
CleanPlayerEntity(toAdd);
if (db.Players.Where(entity => entity.Name == toAdd.Name).Any())
{
throw new ArgumentException("Name property should not be null or whitespace", nameof(toAdd));
throw new ArgumentException("this username is already taken", nameof(toAdd));
}
EntityEntry ee = db.Players!.Add(toAdd);
EntityEntry ee = db.Players.Add(toAdd);
db.SaveChanges();
return (PlayerEntity)ee.Entity;
}
public IEnumerable<PlayerEntity> GetAll()
{
return db.Players!.AsEnumerable();
return db.Players.AsEnumerable();
}
/// <summary>
/// Calls First(), which will throw an exception if no player with such name exists
/// This will throw an exception if no player with such name exists.
/// If you want to know whether any player with that name exists, call IsPresentByName()
/// </summary>
/// <param name="name"></param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
public PlayerEntity GetOneByName(string name)
{
if(string.IsNullOrWhiteSpace(name))
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Name property should not be null or whitespace", nameof(name));
}
return db.Players!.First(p => p.Name == name);
name = name.Trim();
return db.Players.Where(p => p.Name == name).First();
}
public bool IsPresentByName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
name = name.Trim();
return db.Players.Where(p => p.Name == name).Any();
}
/// <summary>
/// removes a non-null PlayerEntity with a valid name from this mgr's context
/// </summary>
/// <param name="toRemove">the entity to remove</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public void Remove(PlayerEntity toRemove)
{
if (toRemove is null)
CleanPlayerEntity(toRemove);
if (IsPresentByID(toRemove.ID))
{
throw new ArgumentNullException(nameof(toRemove), "param should not be null");
db.Players.Remove(toRemove);
db.SaveChanges();
}
db.Players!.Remove(toRemove);
db.SaveChanges();
}
/// <summary>
/// updates a non-null PlayerEntity with a valid name in this mgr's context. This cannot update an ID
/// </summary>
/// <param name="before">the entity to update</param>
/// <param name="after">the entity to replace 'before'</param>
/// <returns>the updated entity</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public PlayerEntity Update(PlayerEntity before, PlayerEntity after)
{
EntityEntry ee = db.Players!.Update(before);
PlayerEntity[] args = { before, after };
foreach (PlayerEntity entity in args)
{
CleanPlayerEntity(entity);
}
if (before.ID != after.ID)
{
throw new ArgumentException("ID cannot be updated", nameof(after));
}
string beforeName = before.Name;
before.Name = after.Name;
EntityEntry ee = db.Players.Update(before);
db.SaveChanges();
before.Name = beforeName;
return (PlayerEntity)ee.Entity;
}
/// <summary>
/// Calls First(), which will throw an exception if no player with such ID exists
/// This will throw an exception if no player with such ID exists.
/// If you want to know whether any player with that ID exists, call IsPresentByID()
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
/// <param name="ID">the ID to look for</param>
/// <returns>PlayerEntity with that ID</returns>
/// <exception cref="InvalidOperationException"></exception>
public PlayerEntity GetOneByID(Guid ID)
{
return db.Players!.First(p => p.ID == ID);
return db.Players.First(p => p.ID == ID);
}
public bool IsPresentByID(Guid ID)
{
return db.Players.Where(p => p.ID == ID).Any();
}
}
}

@ -1,5 +1,8 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Model;
using Model.Players;
using System.Runtime.Intrinsics.Arm;
namespace Data.EF.Players
{
@ -9,74 +12,153 @@ namespace Data.EF.Players
public PlayerDbManager(DiceAppDbContext db)
{
if (db is null)
{
throw new ArgumentNullException(nameof(db), "param should not be null");
}
this.db = db;
}
public PlayerEntity Add(PlayerEntity toAdd)
/// <summary>
/// side effect: entity's name is trimmed.
/// </summary>
/// <param name="entity"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
private static void CleanPlayerEntity(PlayerEntity entity)
{
if (db.Players!.Where(entity => entity.Name == toAdd.Name).Any())
if (entity is null)
{
throw new ArgumentException("this username is already taken", nameof(toAdd));
throw new ArgumentNullException(nameof(entity), "param should not be null");
}
if (toAdd is null)
if (string.IsNullOrWhiteSpace(entity.Name))
{
throw new ArgumentNullException(nameof(toAdd), "param should not be null");
throw new ArgumentException("Name property should not be null or whitespace", nameof(entity));
}
if (string.IsNullOrWhiteSpace(toAdd.Name))
entity.Name = entity.Name.Trim();
}
/// <summary>
/// adds a non-null PlayerEntity with a valid name to this mgr's context
/// </summary>
/// <param name="toAdd">the entity to add</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public PlayerEntity Add(PlayerEntity toAdd)
{
CleanPlayerEntity(toAdd);
if (db.Players.Where(entity => entity.Name == toAdd.Name).Any())
{
throw new ArgumentException("Name property should not be null or whitespace", nameof(toAdd));
throw new ArgumentException("this username is already taken", nameof(toAdd));
}
EntityEntry ee = db.Players!.Add(toAdd);
EntityEntry ee = db.Players.Add(toAdd);
db.SaveChanges();
return (PlayerEntity)ee.Entity;
}
public IEnumerable<PlayerEntity> GetAll()
{
return db.Players!.AsEnumerable();
return db.Players.AsEnumerable();
}
/// <summary>
/// Calls First(), which will throw an exception if no player with such name exists
/// This will throw an exception if no player with such name exists.
/// If you want to know whether any player with that name exists, call IsPresentByName()
/// </summary>
/// <param name="name"></param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
public PlayerEntity GetOneByName(string name)
{
if(string.IsNullOrWhiteSpace(name))
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Name property should not be null or whitespace", nameof(name));
}
return db.Players!.First(p => p.Name == name);
name = name.Trim();
return db.Players.Where(p => p.Name == name).First();
}
public bool IsPresentByName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return false;
}
name = name.Trim();
return db.Players.Where(p => p.Name == name).Any();
}
/// <summary>
/// removes a non-null PlayerEntity with a valid name from this mgr's context
/// </summary>
/// <param name="toRemove">the entity to remove</param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public void Remove(PlayerEntity toRemove)
{
if (toRemove is null)
CleanPlayerEntity(toRemove);
if (IsPresentByID(toRemove.ID))
{
throw new ArgumentNullException(nameof(toRemove), "param should not be null");
db.Players.Remove(toRemove);
db.SaveChanges();
}
db.Players!.Remove(toRemove);
db.SaveChanges();
}
/// <summary>
/// updates a non-null PlayerEntity with a valid name in this mgr's context. This cannot update an ID
/// </summary>
/// <param name="before">the entity to update</param>
/// <param name="after">the entity to replace 'before'</param>
/// <returns>the updated entity</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public PlayerEntity Update(PlayerEntity before, PlayerEntity after)
{
EntityEntry ee = db.Players!.Update(before);
PlayerEntity[] args = { before, after };
foreach (PlayerEntity entity in args)
{
CleanPlayerEntity(entity);
}
if (before.ID != after.ID)
{
throw new ArgumentException("ID cannot be updated", nameof(after));
}
string beforeName = before.Name;
before.Name = after.Name;
EntityEntry ee = db.Players.Update(before);
db.SaveChanges();
before.Name = beforeName;
return (PlayerEntity)ee.Entity;
}
/// <summary>
/// Calls First(), which will throw an exception if no player with such ID exists
/// This will throw an exception if no player with such ID exists.
/// If you want to know whether any player with that ID exists, call IsPresentByID()
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
/// <param name="ID">the ID to look for</param>
/// <returns>PlayerEntity with that ID</returns>
/// <exception cref="InvalidOperationException"></exception>
public PlayerEntity GetOneByID(Guid ID)
{
return db.Players!.First(p => p.ID == ID);
return db.Players.First(p => p.ID == ID);
}
public bool IsPresentByID(Guid ID)
{
return db.Players.Where(p => p.ID == ID).Any();
}
}
}

@ -0,0 +1,699 @@
using Data.EF;
using Data.EF.Players;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Model.Players;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Sdk;
namespace Tests.Data_UTs.Players
{
public class PlayerDbManagerTest
{
private readonly SqliteConnection connection = new("DataSource=:memory:");
private readonly DbContextOptions<DiceAppDbContext> options;
public PlayerDbManagerTest()
{
connection.Open();
options = new DbContextOptionsBuilder<DiceAppDbContext>()
.UseSqlite(connection)
.EnableSensitiveDataLogging()
.Options;
}
[Fact]
public void TestConstructorWhenGivenContextThenConstructs()
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
// Assert
Assert.Equal(new Collection<PlayerEntity>(), mgr.GetAll());
}
}
[Fact]
public void TestConstructorWhenGivenNullThrowsException()
{
// Arrange
PlayerDbManager mgr;
// Act
void action() => mgr = new PlayerDbManager(null);
// Assert
Assert.Throws<ArgumentNullException>(action);
}
[Fact]
public void TestAddWhenValidThenValid()
{
// Arrange
string expectedName = "Jeff";
int expectedCount = 2;
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new PlayerEntity() { Name = expectedName });
mgr.Add(new PlayerEntity() { Name = "whatever" });
// mgr takes care of the SaveChange() calls internally
// we might use Units of Work later, to optimize our calls to DB
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.Equal(expectedName, mgr.GetOneByName(expectedName).Name);
Assert.Equal(expectedCount, mgr.GetAll().Count());
}
}
[Fact]
public void TestAddWhenPreExistentThenException()
{
// Arrange
string name = "Flynt";
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new PlayerEntity() { Name = name });
void action() => mgr.Add(new PlayerEntity() { Name = name });
// Assert
Assert.Throws<ArgumentException>(action);
}
}
[Fact]
public void TestAddWhenNullThenException()
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
void action() => mgr.Add(null);
// Assert
Assert.Throws<ArgumentNullException>(action);
}
}
[Theory]
[InlineData(" ")]
[InlineData(null)]
[InlineData("")]
public void TestAddWhenInvalidNameThenException(string name)
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
void action() => mgr.Add(new PlayerEntity() { Name = name });
// Assert
Assert.Throws<ArgumentException>(action);
}
}
[Theory]
[InlineData(" ")]
[InlineData(null)]
[InlineData("")]
public void TestGetOneByNameWhenInvalidThenException(string name)
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Ernesto" });
mgr.Add(new() { Name = "Basil" });
void action() => mgr.GetOneByName(name);
// Assert
Assert.Throws<ArgumentException>(action);
}
}
[Theory]
[InlineData("Caroline")]
[InlineData("Caroline ")]
[InlineData(" Caroline")]
public void TestGetOneByNameWhenValidAndExistsThenGetsIt(string name)
{
// Arrange
PlayerDbManager mgr;
PlayerEntity actual;
PlayerEntity expected = new() { Name = name.Trim() };
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(expected);
mgr.Add(new() { Name = "Philip" });
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
actual = mgr.GetOneByName(name);
Assert.Equal(expected, actual);
}
}
[Fact]
public void TestGetOneByNameWhenValidAndNotExistsThenException()
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
//mgr.Add(expected);
mgr.Add(new() { Name = "Brett" });
mgr.Add(new() { Name = "Noah" });
void action() => mgr.GetOneByName("*r^a*éàru é^à");
// Assert
Assert.Throws<InvalidOperationException>(action);
}
}
[Fact]
public void TestIsPresentByNameWhenValidAndExistsThenTrue()
{
// Arrange
PlayerDbManager mgr;
string name = "Gerald";
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Philip" });
mgr.Add(new() { Name = name });
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.True(mgr.IsPresentByName(name));
}
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
[InlineData("Barbara")]
public void TestIsPresentByNameWhenInvalidOrNonExistentThenFalse(string name)
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Herman" });
mgr.Add(new() { Name = "Paulo" });
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.False(mgr.IsPresentByName(name));
}
}
[Fact]
public void TestRemoveWhenNullThenException()
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
void action() => mgr.Remove(null);
// Assert
Assert.Throws<ArgumentNullException>(action);
}
}
[Fact]
public void TestRemoveWhenPreExistentThenRemoves()
{
// Arrange
PlayerDbManager mgr;
PlayerEntity toRemove = new() { Name = "Filibert" };
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Xavier" });
mgr.Add(toRemove);
}
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Remove(toRemove);
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.DoesNotContain(toRemove, mgr.GetAll());
}
}
[Fact]
public void TestRemoveWhenNonExistentThenStillNonExistent()
{
// Arrange
PlayerDbManager mgr;
PlayerEntity toRemove = new() { Name = "Filibert" };
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { Name = "Bert" });
mgr.Remove(toRemove);
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.DoesNotContain(toRemove, mgr.GetAll());
}
}
[Theory]
[InlineData("filiBert")]
[InlineData("Bertrand")]
public void TestUpdateWhenValidThenUpdates(string name)
{
// Arrange
PlayerDbManager mgr;
Guid idBefore = Guid.NewGuid();
PlayerEntity before = new() { ID = idBefore, Name = "Filibert" };
PlayerEntity after = new() { ID = idBefore, Name = name };
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
mgr.Update(before, after);
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.DoesNotContain(before, mgr.GetAll());
Assert.Contains(after, mgr.GetAll());
}
}
[Theory]
[InlineData("Valerie")]
[InlineData("Valerie ")]
[InlineData(" Valerie")]
public void TestUpdateWhenSameThenKeepsAndWorks(string name)
{
// Arrange
PlayerDbManager mgr;
string nameBefore = "Valerie";
Guid idBefore = Guid.NewGuid();
PlayerEntity before = new() { ID = idBefore, Name = nameBefore };
PlayerEntity after = new() { ID = idBefore, Name = name };
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
mgr.Update(before, after);
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.Contains(before, mgr.GetAll());
Assert.Contains(after, mgr.GetAll());
}
}
[Fact]
public void TestUpdateWhenNewIDThenException()
{
// Arrange
PlayerDbManager mgr;
PlayerEntity before = new() { ID = Guid.NewGuid(), Name = "Nova" };
PlayerEntity after = new() { ID = Guid.NewGuid(), Name = "Jacquie" };
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
void action() => mgr.Update(before, after);
// Assert
Assert.Throws<ArgumentException>(action);
}
}
[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(null)]
public void TestUpdateWhenInvalidThenException(string name)
{
// Arrange
PlayerDbManager mgr;
Guid id = Guid.NewGuid();
PlayerEntity before = new() { ID = id, Name = "Llanfair­pwll­gwyn­gyll­go­gery­chwyrn­drobwll­llan­tysilio­gogo­goch" };
PlayerEntity after = new() { ID = id, Name = name };
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
void action() => mgr.Update(before, after);
// Assert
Assert.Throws<ArgumentException>(action);
}
}
[Fact]
public void TestUpdateWhenNullThenException()
{
// Arrange
PlayerDbManager mgr;
PlayerEntity before = new() { ID = Guid.NewGuid(), Name = "Dwight" };
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(before);
void action() => mgr.Update(before, null);
// Assert
Assert.Throws<ArgumentNullException>(action);
}
}
[Fact]
public void TestGetOneByIDWhenExistsThenGetsIt()
{
// Arrange
PlayerDbManager mgr;
Guid id = Guid.NewGuid();
PlayerEntity actual;
PlayerEntity expected = new() { ID = id, Name = "Hugh" };
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(expected);
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
actual = mgr.GetOneByID(id);
Assert.Equal(expected, actual);
}
}
[Fact]
public void TestGetOneByIDWhenNotExistsThenException()
{
// Arrange
PlayerDbManager mgr;
Guid id = Guid.NewGuid();
PlayerEntity expected = new() { ID = id, Name = "Kyle" };
Guid otherId = Guid.NewGuid();
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(expected);
void action() => mgr.GetOneByID(otherId);
// Assert
Assert.Throws<InvalidOperationException>(action);
}
}
[Fact]
public void TestIsPresentbyIdWhenExistsThenTrue()
{
// Arrange
PlayerDbManager mgr;
Guid id = Guid.NewGuid();
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { ID = id, Name = "Bobby" });
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.True(mgr.IsPresentByID(id));
}
}
[Fact]
public void TestIsPresentbyIdWhenExistsThenFalse()
{
// Arrange
PlayerDbManager mgr;
Guid id = Guid.NewGuid();
Guid otherId = Guid.NewGuid();
// Act
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
mgr.Add(new() { ID = id, Name = "Victor" });
}
// Assert
using (DiceAppDbContext db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
Assert.False(mgr.IsPresentByID(otherId));
}
}
}
}
Loading…
Cancel
Save