Update and correct UTs
continuous-integration/drone/push Build is passing Details

Alexis Drai 2 years ago
parent 5b16c38916
commit 4d0aad2f5c

@ -6,6 +6,7 @@ namespace Data.EF
{
public class DiceAppDbContext : DbContext, ILoader
{
// will be async!
public virtual Task<MasterOfCeremonies> LoadApp() { throw new NotImplementedException(); }
public DbSet<PlayerEntity> Players { get; set; }

@ -8,6 +8,7 @@ namespace Data.EF
{
public class DiceAppDbContextWithStub : DiceAppDbContext
{
// will be async
public override Task<MasterOfCeremonies> LoadApp() { throw new NotImplementedException(); }
public DiceAppDbContextWithStub() { }
@ -21,7 +22,7 @@ namespace Data.EF
modelBuilder.Entity<PlayerEntity>().HasData(
new PlayerEntity { ID = Guid.NewGuid(), Name = "Alice" }, // some tests depend on this name
new PlayerEntity { ID = new("6e856818-92f1-4d7d-b35c-f9c6687ef8e1"), Name = "Bob" }, // some tests depend on this name
new PlayerEntity { ID = new("6e856818-92f1-4d7d-b35c-f9c6687ef8e1"), Name = "Bob" }, // some tests depend on this name and this ID
new PlayerEntity { ID = Guid.NewGuid(), Name = "Clyde" }, // some tests depend on this name
new PlayerEntity { ID = Guid.NewGuid(), Name = "Dahlia" } // some tests depend on this name
);

@ -1,8 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Model;
using Model.Players;
using System.Runtime.Intrinsics.Arm;
namespace Data.EF.Players
{
@ -114,10 +112,11 @@ namespace Data.EF.Players
public void Remove(PlayerEntity toRemove)
{
CleanPlayerEntity(toRemove);
if (IsPresentByID(toRemove.ID).Result)
bool isPresent = IsPresentByID(toRemove.ID).Result;
if (isPresent)
{
db.Players.Remove(toRemove);
db.SaveChangesAsync();
db.SaveChanges();
}
}

@ -1,8 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Model;
using Model.Players;
using System.Runtime.Intrinsics.Arm;
namespace Data.EF.Players
{
@ -114,10 +112,11 @@ namespace Data.EF.Players
public void Remove(PlayerEntity toRemove)
{
CleanPlayerEntity(toRemove);
if (IsPresentByID(toRemove.ID).Result)
bool isPresent = IsPresentByID(toRemove.ID).Result;
if (isPresent)
{
db.Players.Remove(toRemove);
db.SaveChangesAsync();
db.SaveChanges();
}
}

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tests.Data_UTs
{
public class DiceAppDbContextTest
{
}
}

@ -0,0 +1,49 @@
using Data.EF;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace Tests.Data_UTs
{
public class DiceAppDbContextWithStubTest
{
private readonly SqliteConnection connection = new("DataSource=:memory:");
private readonly DbContextOptions<DiceAppDbContext> options;
public DiceAppDbContextWithStubTest()
{
connection.Open();
options = new DbContextOptionsBuilder<DiceAppDbContext>()
.UseSqlite(connection)
.EnableSensitiveDataLogging()
.Options;
}
[Theory]
[InlineData("Alice")]
[InlineData("Bob")]
[InlineData("Clyde")]
[InlineData("Dahlia")]
public void TestDbStubContainsAll(string name)
{
// Arrange
using (DiceAppDbContextWithStub db = new(options))
{
db.Database.EnsureCreated();
// Assert
Assert.True(db.Players.Where(p => p.Name.Equals(name)).Any());
}
}
}
}

@ -27,30 +27,6 @@ namespace Tests.Data_UTs.Players
.Options;
}
[Theory]
[InlineData("Alice")]
[InlineData("Bob")]
[InlineData("Clyde")]
[InlineData("Dahlia")]
public async Task TestDbStubContainsAll(string name)
{
// Arrange
PlayerDbManager mgr;
// Act
using (DiceAppDbContextWithStub db = new(options))
{
db.Database.EnsureCreated();
mgr = new(db);
// Assert
Assert.True(await mgr.IsPresentByName(name));
}
}
[Fact]
public async Task TestConstructorWhenGivenContextThenConstructs()
{
@ -368,12 +344,12 @@ namespace Tests.Data_UTs.Players
[Fact]
public void TestRemoveWhenPreExistentThenRemoves()
{// should be async Task
{
// Arrange
PlayerDbManager mgr;
PlayerEntity toRemove = new() { ID = new("6e856818-92f1-4d7d-b35c-f9c6687ef8e1"), Name = "Bob" };
PlayerEntity toRemove = new() { ID = Guid.NewGuid(), Name = "Please!" };
// Act
@ -382,8 +358,8 @@ namespace Tests.Data_UTs.Players
{
db.Database.EnsureCreated();
mgr = new(db);
// what's wrong with mgr.Remove(toRemove); ?
mgr.Add(toRemove); // calls SaveChangesAsync()
mgr.Remove(toRemove);
}
// Assert
@ -393,8 +369,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
// should check if toRemove is now absent
Assert.True(true);
Assert.DoesNotContain(toRemove, db.Players);
}
}
@ -685,6 +660,9 @@ namespace Tests.Data_UTs.Players
Guid otherId = Guid.NewGuid();
PlayerEntity presentEntity;
PlayerEntity absentEntity;
// Act
using (DiceAppDbContext db = new(options))
@ -692,7 +670,10 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
await mgr.Add(new() { ID = id, Name = "Victor" });
presentEntity = new() { ID = id, Name = "Victor" };
await mgr.Add(presentEntity);
absentEntity = new() { ID = otherId, Name = "Victor" };
}
// Assert
@ -702,7 +683,7 @@ namespace Tests.Data_UTs.Players
db.Database.EnsureCreated();
mgr = new(db);
Assert.False(await mgr.IsPresentByID(otherId));
Assert.DoesNotContain(absentEntity, db.Players);
}
}
}

Loading…
Cancel
Save