diff --git a/Sources/App/Program.cs b/Sources/App/Program.cs index 564344e..bc5b01d 100644 --- a/Sources/App/Program.cs +++ b/Sources/App/Program.cs @@ -1,335 +1,387 @@ -using Data; -using Data.EF.Players; -using Model.Dice; -using Model.Dice.Faces; -using Model.Games; -using Model.Players; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Drawing; -using System.Linq; - -namespace App -{ - internal static class Program - { - static void Main(string[] args) - { - /* - * the DB stuff and the Model stuff are completely separate here - * - * that will change - */ - - // DB stuff - // if you've run the 'dotnet' 'ef' commands, you should have a DB with 1 table, and 4 players in it - using PlayerDBManager playerDBManager = new(); - - // we'll add a 5th player from the App - PlayerEntity playerEntity = new Player("Ernesto").ToEntity(); - - try - { - playerDBManager.Add(playerEntity); - } // what if there's already a player with that name? Exception (see PlayerEntity's annotations) - catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); } - catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); } - - try - { - IEnumerable allPlayersFromDB = playerDBManager.GetAll(); - - foreach (PlayerEntity entity in allPlayersFromDB) - { - Debug.WriteLine(entity); - } - } - catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Did you make sure that the DATABASE exists?"); } - - - // Model stuff - ILoader loader = new Stub(); - GameRunner gameRunner; - try - { - gameRunner = loader.LoadApp(); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - Console.WriteLine(ex.StackTrace); - gameRunner = new(new PlayerManager(), new DieManager(), null); - } - - string menuChoice = "nothing"; - - while (menuChoice != "q") - { - Console.WriteLine( - "l... load a game\n" + - "n... start new game\n" + - "d... delete a game\n" + - "c... create a group of dice\n" + - "q... QUIT\n" + - ">" - ); - - menuChoice = Console.ReadLine(); - - switch (menuChoice) - { - case "q": - break; - - case "l": - string loadName = ChooseGame(gameRunner); - if (gameRunner.GetOneByName(loadName) != null) - { - Play(gameRunner, loadName); - } - break; - - case "n": - - if (!gameRunner.GlobalDieManager.GetAll().Any()) - { - Console.WriteLine("make at least one dice group first, then try again"); - break; - } - IEnumerable newGameDice = PrepareDice(gameRunner); - - string newGameName; - Console.WriteLine("give this new game a name\n>"); - newGameName = Console.ReadLine(); - PlayerManager playerManager = PreparePlayers(gameRunner); - - gameRunner.StartNewGame(newGameName, playerManager, newGameDice); - Play(gameRunner, newGameName); - - break; - - case "d": - string deleteName = ChooseGame(gameRunner); - gameRunner.Remove(gameRunner.GetOneByName(deleteName)); - break; - - case "c": - string newGroupName; - Console.WriteLine("give this new dice group a name"); - newGroupName = Console.ReadLine(); - - List newGroupDice = new(); - string menuChoiceNewDice = ""; - while (!(menuChoiceNewDice.Equals("ok") && newGroupDice.Any())) - { - Die die = null; - Console.WriteLine("create a die you want to add (at least one), or enter 'ok' if you're finished"); - Console.WriteLine("what type of die ?\n" + - "n... number\n" + - "c... color\n" + - "i... image"); - menuChoiceNewDice = Console.ReadLine(); - switch (menuChoiceNewDice) - { - case "n": - die = MakeNumberDie(); - break; - - case "c": - die = MakeColorDie(); - break; - - case "i": - die = MakeImageDie(); - break; - } - // almost no checks, this is temporary - if (die is not null) - { - newGroupDice.Add(die); - } - } - gameRunner.GlobalDieManager.Add(new KeyValuePair>(newGroupName, newGroupDice)); - break; - - default: - Console.WriteLine("u wot m8?"); - break; - } - } - - } - - private static void Play(GameRunner gameRunner, string name) - { - string menuChoicePlay = ""; - while (menuChoicePlay != "q") - { - Game game = gameRunner.GetOneByName(name); - Console.WriteLine($"{game.GetWhoPlaysNow()}'s turn\n" + - "q... quit\n" + - "h... show history\n" + - "s... save\n" + - "any other... throw"); - menuChoicePlay = Console.ReadLine(); - switch (menuChoicePlay) - { - case "q": - break; - case "h": - foreach (Turn turn in game.GetHistory()) - { - Console.WriteLine(turn); - } - break; - case "s": - gameRunner.Add(game); - break; - default: - GameRunner.PlayGame(game); - Console.WriteLine(game.GetHistory().Last()); - break; - } - } - } - - private static string ChooseGame(GameRunner gameRunner) - { - string name; - Console.WriteLine("which of these games?\n(choose by name)\n>"); - foreach (Game game in gameRunner.GetAll()) - { - Console.WriteLine(game); - } - name = Console.ReadLine(); - return name; - } - - private static NumberDie MakeNumberDie() - { - NumberDie die; - List faces = new(); - string menuChoiceNewFaces = ""; - - while (menuChoiceNewFaces != "ok") - { - Console.WriteLine("create a face with a number, or enter 'ok' if you're finished"); - menuChoiceNewFaces = Console.ReadLine(); - - if (!menuChoiceNewFaces.Equals("ok") && int.TryParse(menuChoiceNewFaces, out int num)) - { - faces.Add(new(num)); - } - } - - die = new NumberDie(faces.ToArray()); - return die; - } - - private static ColorDie MakeColorDie() - { - ColorDie die; - List faces = new(); - string menuChoiceNewFaces = ""; - - while (!menuChoiceNewFaces.Equals("ok")) - { - Console.WriteLine("create a face with an color name, or enter 'ok' if you're finished"); - menuChoiceNewFaces = Console.ReadLine(); - if (menuChoiceNewFaces != "ok") faces.Add(new(Color.FromName(menuChoiceNewFaces))); - } - - die = new ColorDie(faces.ToArray()); - return die; - } - - private static ImageDie MakeImageDie() - { - ImageDie die; - List faces = new(); - string menuChoiceNewFaces = ""; - - while (!menuChoiceNewFaces.Equals("ok")) - { - Console.WriteLine("create a face with an image uri, or enter 'ok' if you're finished"); - menuChoiceNewFaces = Console.ReadLine(); - - if (menuChoiceNewFaces != "ok") - { - try - { - faces.Add(new(new Uri(menuChoiceNewFaces))); - } - catch (ArgumentNullException ex) - { - Console.WriteLine(ex.Message); - } - catch (UriFormatException ex) - { - Console.WriteLine("that URI was not valid"); - Console.WriteLine(ex.Message); - } - } - } - die = new ImageDie(faces.ToArray()); - return die; - } - - private static IEnumerable PrepareDice(GameRunner gameRunner) - { - List result = new(); - Console.WriteLine("add dice to the game"); - Console.WriteLine("all known dice or groups of dice:"); - foreach ((string name, IEnumerable dice) in gameRunner.GlobalDieManager.GetAll()) - { - Console.WriteLine($"{name} -- {dice}"); - } - string menuChoiceDice = ""; - while (!(menuChoiceDice.Equals("ok") && result.Any())) - { - Console.WriteLine("write the name of a dice group you want to add (at least one), or 'ok' if you're finished"); - menuChoiceDice = Console.ReadLine(); - // no checks, this is temporary - if (!menuChoiceDice.Equals("ok")) - { - IEnumerable chosenDice = gameRunner.GlobalDieManager.GetOneByName(menuChoiceDice).Value; - foreach (Die die in chosenDice) - { - result.Add(die); - } - } - } - return result.AsEnumerable(); - } - private static PlayerManager PreparePlayers(GameRunner gameRunner) - { - PlayerManager result = new(); - Console.WriteLine("add players to the game"); - Console.WriteLine("all known players:"); - foreach (Player player in gameRunner.GlobalPlayerManager.GetAll()) - { - Console.WriteLine(player); - } - string menuChoicePlayers = ""; - while (!(menuChoicePlayers.Equals("ok") && result.GetAll().Any())) - { - Console.WriteLine("write the name of a player you want to add (at least one), or 'ok' if you're finished"); - menuChoicePlayers = Console.ReadLine(); - if (!menuChoicePlayers.Equals("ok")) - { - Player player = new(menuChoicePlayers); - if (!gameRunner.GlobalPlayerManager.GetAll().Contains(player)) - { - // if the player didn't exist, now it does... this is temporary - gameRunner.GlobalPlayerManager.Add(player); - } - // almost no checks, this is temporary - result.Add(player); - } - } - - return result; - } - } +using Data; +using Data.EF; +using Data.EF.Players; +using Model.Dice; +using Model.Dice.Faces; +using Model.Games; +using Model.Players; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing; +using System.Linq; + +namespace App +{ + internal static class Program + { + static void Main(string[] args) + { + // MODEL stuff + ILoader loader = new Stub(); + GameRunner gameRunner; + try + { + gameRunner = loader.LoadApp(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + Console.WriteLine(ex.StackTrace); + gameRunner = new(new PlayerManager(), new DieManager(), null); + } + + try + { + // DB stuff when the app opens + using (DiceAppDbContext db = new()) + { + // Later, we'll use the DiceAppDbContext to get a GameDbRunner + + // get all the players from the DB + IEnumerable entities = db.Players; + + Debug.WriteLine("Loading players"); + + foreach (PlayerEntity entity in entities) + { + try + { + // persist them as models ! + gameRunner.GlobalPlayerManager.Add(entity.ToModel()); + Debug.WriteLine($"{entity.ID} -- {entity.Name}"); + } + catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); } + } + } + } + catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); } + + string menuChoice = "nothing"; + + while (menuChoice != "q") + { + Console.WriteLine( + "l... load a game\n" + + "n... start new game\n" + + "d... delete a game\n" + + "i... see all dice\n" + + "c... create a group of dice\n" + + "p... see all players\n" + + "y... create players\n" + + "q... QUIT\n" + + ">" + ); + + menuChoice = Console.ReadLine(); + + switch (menuChoice) + { + case "q": + break; + + case "l": + string loadName = ChooseGame(gameRunner); + if (gameRunner.GetOneByName(loadName) != null) + { + Play(gameRunner, loadName); + } + break; + + case "n": + + if (!gameRunner.GlobalDieManager.GetAll().Any()) + { + Console.WriteLine("make at least one dice group first, then try again"); + break; + } + Console.WriteLine("add dice to the game"); + IEnumerable newGameDice = PrepareDice(gameRunner); + + string newGameName; + Console.WriteLine("give this new game a name\n>"); + newGameName = Console.ReadLine(); + + Console.WriteLine("add players to the game"); + PlayerManager playerManager = PreparePlayers(gameRunner); + + gameRunner.StartNewGame(newGameName, playerManager, newGameDice); + Play(gameRunner, newGameName); + + break; + + case "d": + string deleteName = ChooseGame(gameRunner); + gameRunner.Remove(gameRunner.GetOneByName(deleteName)); + break; + + case "c": + string newGroupName; + Console.WriteLine("give this new dice group a name"); + newGroupName = Console.ReadLine(); + + List newGroupDice = new(); + string menuChoiceNewDice = ""; + while (!(menuChoiceNewDice.Equals("ok") && newGroupDice.Any())) + { + Die die = null; + Console.WriteLine("create a die you want to add (at least one), or enter 'ok' if you're finished"); + Console.WriteLine("what type of die ?\n" + + "n... number\n" + + "c... color\n" + + "i... image"); + menuChoiceNewDice = Console.ReadLine(); + switch (menuChoiceNewDice) + { + case "n": + die = MakeNumberDie(); + break; + + case "c": + die = MakeColorDie(); + break; + + case "i": + die = MakeImageDie(); + break; + } + // almost no checks, this is temporary + if (die is not null) + { + newGroupDice.Add(die); + } + } + gameRunner.GlobalDieManager.Add(new KeyValuePair>(newGroupName, newGroupDice)); + break; + + case "p": + ShowPlayers(gameRunner); + break; + + case "i": + ShowDice(gameRunner); + break; + + case "y": + PreparePlayers(gameRunner); + break; + + default: + Console.WriteLine("u wot m8?"); + break; + } + } + + try + { + // DB stuff when the app closes + using (DiceAppDbContext db = new()) + { + // get all the players from the app's memory + IEnumerable models = gameRunner.GlobalPlayerManager.GetAll(); + + // create a PlayerDbManager (and inject it with the DB) + PlayerDbManager playerDbManager = new(db); + + Debug.WriteLine("Saving players"); + + foreach (Player model in models) + { + try // to persist them + { // as entities ! + PlayerEntity entity = model.ToEntity(); + playerDbManager.Add(entity); + Debug.WriteLine($"{entity.ID} -- {entity.Name}"); + } + // what if there's already a player with that name? Exception (see PlayerEntity's annotations) + catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); } + } + } + } catch (Exception ex) { Debug.WriteLine($"{ex.Message}\n... Couldn't use the database"); } + } + + private static void Play(GameRunner gameRunner, string name) + { + string menuChoicePlay = ""; + while (menuChoicePlay != "q") + { + Game game = gameRunner.GetOneByName(name); + Console.WriteLine($"{game.GetWhoPlaysNow()}'s turn\n" + + "q... quit\n" + + "h... show history\n" + + "s... save\n" + + "any other... throw"); + menuChoicePlay = Console.ReadLine(); + switch (menuChoicePlay) + { + case "q": + break; + case "h": + foreach (Turn turn in game.GetHistory()) + { + Console.WriteLine(turn); + } + break; + case "s": + gameRunner.Add(game); + break; + default: + GameRunner.PlayGame(game); + Console.WriteLine(game.GetHistory().Last()); + break; + } + } + } + + private static string ChooseGame(GameRunner gameRunner) + { + string name; + Console.WriteLine("which of these games?\n(choose by name)\n>"); + foreach (Game game in gameRunner.GetAll()) + { + Console.WriteLine(game); + } + name = Console.ReadLine(); + return name; + } + + private static void ShowPlayers(GameRunner gameRunner) + { + Console.WriteLine("Look at all them players!"); + foreach (Player player in gameRunner.GlobalPlayerManager.GetAll()) + { + Console.WriteLine(player); + } + } + + private static void ShowDice(GameRunner gameRunner) + { + foreach ((string name, IEnumerable dice) in gameRunner.GlobalDieManager.GetAll()) + { + Console.WriteLine($"{name} -- {dice}"); + } + } + + private static NumberDie MakeNumberDie() + { + NumberDie die; + List faces = new(); + string menuChoiceNewFaces = ""; + + while (menuChoiceNewFaces != "ok") + { + Console.WriteLine("create a face with a number, or enter 'ok' if you're finished"); + menuChoiceNewFaces = Console.ReadLine(); + + if (!menuChoiceNewFaces.Equals("ok") && int.TryParse(menuChoiceNewFaces, out int num)) + { + faces.Add(new(num)); + } + } + + die = new NumberDie(faces.ToArray()); + return die; + } + + private static ColorDie MakeColorDie() + { + ColorDie die; + List faces = new(); + string menuChoiceNewFaces = ""; + + while (!menuChoiceNewFaces.Equals("ok")) + { + Console.WriteLine("create a face with an color name, or enter 'ok' if you're finished"); + menuChoiceNewFaces = Console.ReadLine(); + if (menuChoiceNewFaces != "ok") faces.Add(new(Color.FromName(menuChoiceNewFaces))); + } + + die = new ColorDie(faces.ToArray()); + return die; + } + + private static ImageDie MakeImageDie() + { + ImageDie die; + List faces = new(); + string menuChoiceNewFaces = ""; + + while (!menuChoiceNewFaces.Equals("ok")) + { + Console.WriteLine("create a face with an image uri, or enter 'ok' if you're finished"); + menuChoiceNewFaces = Console.ReadLine(); + + if (menuChoiceNewFaces != "ok") + { + try + { + faces.Add(new(new Uri(menuChoiceNewFaces))); + } + catch (ArgumentNullException ex) + { + Console.WriteLine(ex.Message); + } + catch (UriFormatException ex) + { + Console.WriteLine("that URI was not valid"); + Console.WriteLine(ex.Message); + } + } + } + die = new ImageDie(faces.ToArray()); + return die; + } + + private static IEnumerable PrepareDice(GameRunner gameRunner) + { + List result = new(); + Console.WriteLine("all known dice or groups of dice:"); + ShowDice(gameRunner); + string menuChoiceDice = ""; + while (!(menuChoiceDice.Equals("ok") && result.Any())) + { + Console.WriteLine("write the name of a dice group you want to add (at least one), or 'ok' if you're finished"); + menuChoiceDice = Console.ReadLine(); + if (!menuChoiceDice.Equals("ok")) + { + IEnumerable chosenDice = gameRunner.GlobalDieManager.GetOneByName(menuChoiceDice).Value; + foreach (Die die in chosenDice) + { + result.Add(die); + } + } + } + return result.AsEnumerable(); + } + private static PlayerManager PreparePlayers(GameRunner gameRunner) + { + PlayerManager result = new(); + Console.WriteLine("all known players:"); + ShowPlayers(gameRunner); + string menuChoicePlayers = ""; + while (!(menuChoicePlayers.Equals("ok") && result.GetAll().Any())) + { + Console.WriteLine("write the name of a player you want to add (at least one), or 'ok' if you're finished"); + menuChoicePlayers = Console.ReadLine(); + if (!menuChoicePlayers.Equals("ok")) + { + Player player = new(menuChoicePlayers); + if (!gameRunner.GlobalPlayerManager.GetAll().Contains(player)) + { + // if the player didn't exist, now it does... this is temporary + gameRunner.GlobalPlayerManager.Add(player); + } + // almost no checks, this is temporary + try + { + result.Add(player); + } + catch (ArgumentException ex) { Debug.WriteLine($"{ex.Message}\n... Never mind"); } + + } + } + + return result; + } + } } \ No newline at end of file diff --git a/Sources/Data/Data.csproj b/Sources/Data/Data.csproj index 4018f87..1aa3210 100644 --- a/Sources/Data/Data.csproj +++ b/Sources/Data/Data.csproj @@ -3,7 +3,6 @@ net6.0 enable - enable $(MSBuildProjectDirectory) diff --git a/Sources/Data/EF/DiceAppDbContext.cs b/Sources/Data/EF/DiceAppDbContext.cs index 1c35807..f52233f 100644 --- a/Sources/Data/EF/DiceAppDbContext.cs +++ b/Sources/Data/EF/DiceAppDbContext.cs @@ -1,14 +1,23 @@ using Data.EF.Players; using Microsoft.EntityFrameworkCore; +using Model.Games; namespace Data.EF { - public class DiceAppDbContext : DbContext + public class DiceAppDbContext : DbContext, ILoader { - public DbSet? Players { get; set; } + public virtual GameRunner LoadApp() { throw new NotImplementedException(); } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - => optionsBuilder.UseSqlite("Data Source=EFDice.DiceApp.db"); + public DbSet Players { get; set; } + + public DiceAppDbContext() { } + public DiceAppDbContext(DbContextOptions options) + : base(options) { } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) optionsBuilder.UseSqlite("Data Source=EFDice.DiceApp.db").EnableSensitiveDataLogging(); + } } } diff --git a/Sources/Data/EF/DiceAppDbContextWithStub.cs b/Sources/Data/EF/DiceAppDbContextWithStub.cs index 0e22963..059dddf 100644 --- a/Sources/Data/EF/DiceAppDbContextWithStub.cs +++ b/Sources/Data/EF/DiceAppDbContextWithStub.cs @@ -1,20 +1,29 @@ using Data.EF.Players; using Microsoft.EntityFrameworkCore; +using Model.Games; +using System.Security.Cryptography.X509Certificates; namespace Data.EF { public class DiceAppDbContextWithStub : DiceAppDbContext { + public override GameRunner LoadApp() { throw new NotImplementedException(); } + + public DiceAppDbContextWithStub() { } + + public DiceAppDbContextWithStub(DbContextOptions options) + : base(options) { } + protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity().HasData( - new PlayerEntity { ID = Guid.Parse("e3b42372-0186-484c-9b1c-01618fbfac44"), Name = "Alice" }, - new PlayerEntity { ID = Guid.Parse("73265e15-3c43-45f8-8f5d-d02feaaf7620"), Name = "Bob" }, - new PlayerEntity { ID = Guid.Parse("5198ba9d-44d6-4660-85f9-1843828c6f0d"), Name = "Clyde" }, - new PlayerEntity { ID = Guid.Parse("386cec27-fd9d-4475-8093-93c8b569bf2e"), Name = "Dahlia" } - ); + new PlayerEntity { ID = Guid.NewGuid(), Name = "Alice" }, // some tests depend on this name + new PlayerEntity { ID = Guid.NewGuid(), Name = "Bob" }, // some tests depend on this name + 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 + ); } } } diff --git a/Sources/Data/EF/Players/PlayerDBManager.cs b/Sources/Data/EF/Players/PlayerDBManager.cs index 47d15dd..5b50ce8 100644 --- a/Sources/Data/EF/Players/PlayerDBManager.cs +++ b/Sources/Data/EF/Players/PlayerDBManager.cs @@ -1,52 +1,164 @@ -using Microsoft.EntityFrameworkCore.ChangeTracking; -using Model; - -namespace Data.EF.Players -{ - public sealed class PlayerDBManager : IManager, IDisposable - { - private readonly DiceAppDbContext db = new DiceAppDbContextWithStub(); - - public void Dispose() - { - db.Dispose(); - } - - public PlayerEntity Add(PlayerEntity toAdd) - { - if (db.Players!.Where(entity => entity.Name == toAdd.Name).Any()) - { - throw new ArgumentException("this username is already taken", nameof(toAdd)); - } - EntityEntry ee = db.Players!.Add(toAdd); - db.SaveChanges(); - return (PlayerEntity)ee.Entity; - } - - - public IEnumerable GetAll() - { - return db.Players!.AsEnumerable(); - } - - public PlayerEntity GetOneByName(string name) - { - throw new NotImplementedException(); - } - - public void Remove(PlayerEntity toRemove) - { - throw new NotImplementedException(); - } - - public PlayerEntity Update(PlayerEntity before, PlayerEntity after) - { - throw new NotImplementedException(); - } - - public PlayerEntity GetOneByID(Guid ID) - { - throw new NotImplementedException(); - } - } -} +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; +using Model; +using Model.Players; +using System.Runtime.Intrinsics.Arm; + +namespace Data.EF.Players +{ + public sealed class PlayerDbManager : IManager + { + private readonly DiceAppDbContext db; + + public PlayerDbManager(DiceAppDbContext db) + { + if (db is null) + { + throw new ArgumentNullException(nameof(db), "param should not be null"); + } + this.db = db; + } + + /// + /// side effect: entity's name is trimmed. + /// + /// + /// + /// + private static void CleanPlayerEntity(PlayerEntity entity) + { + if (entity is null) + { + throw new ArgumentNullException(nameof(entity), "param should not be null"); + } + if (string.IsNullOrWhiteSpace(entity.Name)) + { + throw new ArgumentException("Name property should not be null or whitespace", nameof(entity)); + } + entity.Name = entity.Name.Trim(); + } + + /// + /// adds a non-null PlayerEntity with a valid name to this mgr's context + /// + /// the entity to add + /// + /// + /// + public PlayerEntity Add(PlayerEntity toAdd) + { + CleanPlayerEntity(toAdd); + + if (db.Players.Where(entity => entity.Name == toAdd.Name).Any()) + { + throw new ArgumentException("this username is already taken", nameof(toAdd)); + } + + EntityEntry ee = db.Players.Add(toAdd); + db.SaveChanges(); + return (PlayerEntity)ee.Entity; + } + + public IEnumerable GetAll() + { + return db.Players.AsEnumerable(); + } + + /// + /// 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() + /// + /// + /// + /// + /// + public PlayerEntity GetOneByName(string name) + { + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException("Name property should not be null or whitespace", nameof(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(); + } + + /// + /// removes a non-null PlayerEntity with a valid name from this mgr's context + /// + /// the entity to remove + /// + /// + public void Remove(PlayerEntity toRemove) + { + CleanPlayerEntity(toRemove); + + if (IsPresentByID(toRemove.ID)) + { + db.Players.Remove(toRemove); + db.SaveChanges(); + } + } + + /// + /// updates a non-null PlayerEntity with a valid name in this mgr's context. This cannot update an ID + /// + /// the entity to update + /// the entity to replace 'before' + /// the updated entity + /// + /// + public PlayerEntity Update(PlayerEntity before, PlayerEntity after) + { + 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; + + } + + /// + /// 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() + /// + /// the ID to look for + /// PlayerEntity with that ID + /// + public PlayerEntity GetOneByID(Guid ID) + { + return db.Players.First(p => p.ID == ID); + } + + public bool IsPresentByID(Guid ID) + { + return db.Players.Where(p => p.ID == ID).Any(); + } + } +} diff --git a/Sources/Data/EF/Players/PlayerDbManager.cs b/Sources/Data/EF/Players/PlayerDbManager.cs new file mode 100644 index 0000000..5b50ce8 --- /dev/null +++ b/Sources/Data/EF/Players/PlayerDbManager.cs @@ -0,0 +1,164 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; +using Model; +using Model.Players; +using System.Runtime.Intrinsics.Arm; + +namespace Data.EF.Players +{ + public sealed class PlayerDbManager : IManager + { + private readonly DiceAppDbContext db; + + public PlayerDbManager(DiceAppDbContext db) + { + if (db is null) + { + throw new ArgumentNullException(nameof(db), "param should not be null"); + } + this.db = db; + } + + /// + /// side effect: entity's name is trimmed. + /// + /// + /// + /// + private static void CleanPlayerEntity(PlayerEntity entity) + { + if (entity is null) + { + throw new ArgumentNullException(nameof(entity), "param should not be null"); + } + if (string.IsNullOrWhiteSpace(entity.Name)) + { + throw new ArgumentException("Name property should not be null or whitespace", nameof(entity)); + } + entity.Name = entity.Name.Trim(); + } + + /// + /// adds a non-null PlayerEntity with a valid name to this mgr's context + /// + /// the entity to add + /// + /// + /// + public PlayerEntity Add(PlayerEntity toAdd) + { + CleanPlayerEntity(toAdd); + + if (db.Players.Where(entity => entity.Name == toAdd.Name).Any()) + { + throw new ArgumentException("this username is already taken", nameof(toAdd)); + } + + EntityEntry ee = db.Players.Add(toAdd); + db.SaveChanges(); + return (PlayerEntity)ee.Entity; + } + + public IEnumerable GetAll() + { + return db.Players.AsEnumerable(); + } + + /// + /// 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() + /// + /// + /// + /// + /// + public PlayerEntity GetOneByName(string name) + { + if (string.IsNullOrWhiteSpace(name)) + { + throw new ArgumentException("Name property should not be null or whitespace", nameof(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(); + } + + /// + /// removes a non-null PlayerEntity with a valid name from this mgr's context + /// + /// the entity to remove + /// + /// + public void Remove(PlayerEntity toRemove) + { + CleanPlayerEntity(toRemove); + + if (IsPresentByID(toRemove.ID)) + { + db.Players.Remove(toRemove); + db.SaveChanges(); + } + } + + /// + /// updates a non-null PlayerEntity with a valid name in this mgr's context. This cannot update an ID + /// + /// the entity to update + /// the entity to replace 'before' + /// the updated entity + /// + /// + public PlayerEntity Update(PlayerEntity before, PlayerEntity after) + { + 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; + + } + + /// + /// 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() + /// + /// the ID to look for + /// PlayerEntity with that ID + /// + public PlayerEntity GetOneByID(Guid ID) + { + return db.Players.First(p => p.ID == ID); + } + + public bool IsPresentByID(Guid ID) + { + return db.Players.Where(p => p.ID == ID).Any(); + } + } +} diff --git a/Sources/Data/EF/Players/PlayerEntity.cs b/Sources/Data/EF/Players/PlayerEntity.cs index 45af6b3..b7f1714 100644 --- a/Sources/Data/EF/Players/PlayerEntity.cs +++ b/Sources/Data/EF/Players/PlayerEntity.cs @@ -7,9 +7,9 @@ namespace Data.EF.Players { public Guid ID { get; set; } - public string? Name { get; set; } + public string Name { get; set; } - public override bool Equals(object? obj) + public override bool Equals(object obj) { if (obj is not PlayerEntity) { @@ -18,9 +18,9 @@ namespace Data.EF.Players return Equals(obj as PlayerEntity); } - public bool Equals(PlayerEntity? other) + public bool Equals(PlayerEntity other) { - return other is not null && this.ID == other!.ID && this.Name == other.Name; + return other is not null && this.ID == other.ID && this.Name == other.Name; } public override int GetHashCode() @@ -28,7 +28,7 @@ namespace Data.EF.Players return HashCode.Combine(ID, Name); } - public override string? ToString() + public override string ToString() { return $"{ID.ToString().ToUpper()} -- {Name}"; } diff --git a/Sources/Data/Stub.cs b/Sources/Data/Stub.cs index b841c92..dec1784 100644 --- a/Sources/Data/Stub.cs +++ b/Sources/Data/Stub.cs @@ -12,7 +12,7 @@ namespace Data { GameRunner gr = new(new PlayerManager(), new DieManager()); - Player player1 = new("Alice"), player2 = new("Bob"), player3 = new("Clyde"); + Player player1 = new("Alice(Old Stub)"), player2 = new("Bob(Old Stub)"), player3 = new("Clyde(Old Stub)"); gr.GlobalPlayerManager.Add(player1); gr.GlobalPlayerManager.Add(player2); diff --git a/Sources/Tests/Data_UTs/Players/PlayerDbManagerTest.cs b/Sources/Tests/Data_UTs/Players/PlayerDbManagerTest.cs new file mode 100644 index 0000000..53ca4b1 --- /dev/null +++ b/Sources/Tests/Data_UTs/Players/PlayerDbManagerTest.cs @@ -0,0 +1,722 @@ +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 options; + + public PlayerDbManagerTest() + { + connection.Open(); + + options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .EnableSensitiveDataLogging() + .Options; + } + + [Theory] + [InlineData("Alice")] + [InlineData("Bob")] + [InlineData("Clyde")] + [InlineData("Dahlia")] + public void TestDbStubContainsAll(string name) + { + // Arrange + + PlayerDbManager mgr; + + // Act + + using (DiceAppDbContextWithStub db = new(options)) + { + db.Database.EnsureCreated(); + mgr = new(db); + + // Assert + + Assert.True(mgr.IsPresentByName(name)); + } + } + + [Fact] + public void TestConstructorWhenGivenContextThenConstructs() + { + // Arrange + + PlayerDbManager mgr; + // Act + + using (DiceAppDbContext db = new(options)) + { + db.Database.EnsureCreated(); + mgr = new(db); + + // Assert + + Assert.Equal(new Collection(), mgr.GetAll()); + } + } + + [Fact] + public void TestConstructorWhenGivenNullThrowsException() + { + // Arrange + + PlayerDbManager mgr; + + // Act + + void action() => mgr = new PlayerDbManager(null); + + // Assert + + Assert.Throws(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(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(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(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(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(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(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(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(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(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(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)); + } + } + } +} diff --git a/Sources/Tests/Data_UTs/Players/PlayerEntityTest.cs b/Sources/Tests/Data_UTs/Players/PlayerEntityTest.cs index b9d41a9..db482cc 100644 --- a/Sources/Tests/Data_UTs/Players/PlayerEntityTest.cs +++ b/Sources/Tests/Data_UTs/Players/PlayerEntityTest.cs @@ -27,7 +27,7 @@ namespace Tests.Data_UTs.Players { // Arrange PlayerEntity player = new(); - Guid expected = new("c8f60957-dd36-4e47-a7ce-1281f4f8bea4"); + Guid expected = Guid.NewGuid(); // Act player.ID = expected; @@ -107,10 +107,16 @@ namespace Tests.Data_UTs.Players PlayerEntity p2; PlayerEntity p3; + Guid id1 = Guid.NewGuid(); + Guid id2 = Guid.NewGuid(); + + string name1 = "Panama"; + string name2 = "Clyde"; + // Act - p1 = new() { ID = new Guid("ae04ef10-bd25-4f4e-b4c1-4860fe3daaa0"), Name = "Panama" }; - p2 = new() { ID = new Guid("ae04ef10-bd25-4f4e-b4c1-4860fe3daaa0"), Name = "Clyde" }; - p3 = new() { ID = new Guid("846d332f-56ca-44fc-8170-6cfd28dab88b"), Name = "Clyde" }; + p1 = new() { ID = id1, Name = name1 }; + p2 = new() { ID = id1, Name = name2 }; + p3 = new() { ID = id2, Name = name2 }; // Assert Assert.False(p1.Equals(p2)); @@ -127,10 +133,12 @@ namespace Tests.Data_UTs.Players // Arrange PlayerEntity p1; PlayerEntity p2; + Guid id = Guid.NewGuid(); + string name = "Marley"; // Act - p1 = new() { ID = new Guid("ae04ef10-bd25-4f4e-b4c1-4860fe3daaa0"), Name = "Marley" }; - p2 = new() { ID = new Guid("ae04ef10-bd25-4f4e-b4c1-4860fe3daaa0"), Name = "Marley" }; + p1 = new() { ID = id, Name = name }; + p2 = new() { ID = id, Name = name }; // Assert Assert.True(p1.Equals(p2)); @@ -145,10 +153,16 @@ namespace Tests.Data_UTs.Players PlayerEntity p2; PlayerEntity p3; + Guid id1 = Guid.NewGuid(); + Guid id2 = Guid.NewGuid(); + + string name1 = "Panama"; + string name2 = "Clyde"; + // Act - p1 = new() { ID = new Guid("ae04ef10-bd25-4f4e-b4c1-4860fe3daaa0"), Name = "Panama" }; - p2 = new() { ID = new Guid("ae04ef10-bd25-4f4e-b4c1-4860fe3daaa0"), Name = "Clyde" }; - p3 = new() { ID = new Guid("846d332f-56ca-44fc-8170-6cfd28dab88b"), Name = "Clyde" }; + p1 = new() { ID = id1, Name = name1 }; + p2 = new() { ID = id1, Name = name2 }; + p3 = new() { ID = id2, Name = name2 }; // Assert Assert.False(p1.GetHashCode().Equals(p2.GetHashCode())); @@ -165,10 +179,12 @@ namespace Tests.Data_UTs.Players // Arrange PlayerEntity p1; PlayerEntity p2; + Guid id = Guid.NewGuid(); + string name = "Marley"; // Act - p1 = new() { ID = new Guid("ae04ef10-bd25-4f4e-b4c1-4860fe3daaa0"), Name = "Marley" }; - p2 = new() { ID = new Guid("ae04ef10-bd25-4f4e-b4c1-4860fe3daaa0"), Name = "Marley" }; + p1 = new() { ID = id, Name = name }; + p2 = new() { ID = id, Name = name }; // Assert Assert.True(p1.GetHashCode().Equals(p2.GetHashCode())); diff --git a/Sources/Tests/Model_UTs/Games/GameRunnerTest.cs b/Sources/Tests/Model_UTs/Games/GameRunnerTest.cs index 1383541..836335a 100644 --- a/Sources/Tests/Model_UTs/Games/GameRunnerTest.cs +++ b/Sources/Tests/Model_UTs/Games/GameRunnerTest.cs @@ -79,6 +79,20 @@ namespace Tests.Model_UTs.Games Assert.DoesNotContain(null, stubGameRunner.GetAll()); } + [Fact] + public void TestGetOneByIdThrowsException() + { + // Arrange + GameRunner gameRunner = stubGameRunner; + + // Act + + void action() => gameRunner.GetOneByID(new("62bb72e6-f9fb-442e-a879-e1b70f8f52f3")); + + // Assert + Assert.Throws(action); + } + [Theory] [InlineData("")] [InlineData(null)] diff --git a/Sources/Tests/Model_UTs/Players/PlayerManagerTest.cs b/Sources/Tests/Model_UTs/Players/PlayerManagerTest.cs index 2cdb994..93fe366 100644 --- a/Sources/Tests/Model_UTs/Players/PlayerManagerTest.cs +++ b/Sources/Tests/Model_UTs/Players/PlayerManagerTest.cs @@ -76,6 +76,21 @@ namespace Tests.Model_UTs.Players Assert.Throws(action); } + + [Fact] + public void TestGetOneByIdThrowsException() + { + // Arrange + PlayerManager playerManager = new(); + + // Act + + void action() => playerManager.GetOneByID(new("1a276327-75fc-45b9-8854-e7c4101088f8")); + + // Assert + Assert.Throws(action); + } + [Theory] [InlineData("")] [InlineData(null)] @@ -165,14 +180,12 @@ namespace Tests.Model_UTs.Players Player player = new("Dylan"); playerManager.Add(player); Player notPlayer = new("Eric"); - IEnumerable expected = new Collection { player }; // Act playerManager.Remove(notPlayer); - IEnumerable actual = playerManager.GetAll(); // Assert - Assert.Equal(actual, expected); + Assert.DoesNotContain(notPlayer, playerManager.GetAll()); } [Fact]