async void SaveClicked(object sender, EventArgs e) { try { character.ClassID = getCharacterClassID(); character.Race = pickerRace.SelectedItem.ToString(); character.Gender = pickerGender.SelectedItem.ToString(); if (character.ClassID > 0) { CharacterRepository ch = new CharacterRepository(); if (character.ID == 0) { await ch.AddCharacter(character); } else { await ch.UpdateCharacter(character); } thisApp.needCharacterRefresh = true; await Navigation.PopAsync(); } else { await DisplayAlert("Class Not Selected:", "You must set the class.", "Ok"); } } catch (AggregateException ex) { string errMsg = ""; foreach (var exception in ex.InnerExceptions) { errMsg += Environment.NewLine + exception.Message; } await DisplayAlert("One or more exceptions has occurred:", errMsg, "Ok"); } catch (ApiException apiEx) { var sb = new StringBuilder(); sb.AppendLine("Errors:"); foreach (var error in apiEx.Errors) { sb.AppendLine("-" + error); } thisApp.needCharacterRefresh = true; await DisplayAlert("Problem Saving the Character:", sb.ToString(), "Ok"); } catch (Exception ex) { if (ex.GetBaseException().Message.Contains("connection with the server")) { await DisplayAlert("Error", "No connection with the server.", "Ok"); } else { await DisplayAlert("Error", "Could not complete operation.", "Ok"); } } }
public void DeleteCharacterFromDbAlsoDeletesCharStats() { // arrange // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { var rand = new RngProvider(); var options = new DbContextOptionsBuilder <ANightsTaleContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new ANightsTaleContext(options)) { context.Database.EnsureCreated(); } // Act // Run the test against one instance of the context using (var context = new ANightsTaleContext(options)) { var charRepo = new CharacterRepository(context); DataSeeding seed = new DataSeeding(context, charRepo); seed.SeedCharacterSupportClasses(); var character = seed.SeedCharacter(); charRepo.AddCharacter(character); charRepo.Save(); var stats = seed.SeedCharStats(character); charRepo.AddCharStats(stats); charRepo.Save(); charRepo.RemoveCharacter(1); charRepo.Save(); // Assert Assert.False(context.CharStats.Any()); } } finally { connection.Close(); } }
public void AddCharacterToDbIsSuccessful() { // arrange // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { var options = new DbContextOptionsBuilder <ANightsTaleContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new ANightsTaleContext(options)) { context.Database.EnsureCreated(); } // Act // Run the test against one instance of the context using (var context = new ANightsTaleContext(options)) { var charRepo = new CharacterRepository(context); DataSeeding seed = new DataSeeding(context, charRepo); seed.SeedCharacterSupportClasses(); var character = seed.SeedCharacter(); charRepo.AddCharacter(character); charRepo.Save(); // Assert Assert.Equal("Test", context.Character.First().Name); } } finally { connection.Close(); } }
public void AddNullCharacterThrowsNullException() { // arrange // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { var rand = new RngProvider(); var options = new DbContextOptionsBuilder <ANightsTaleContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new ANightsTaleContext(options)) { context.Database.EnsureCreated(); } // Act // Run the test against one instance of the context using (var context = new ANightsTaleContext(options)) { var charRepo = new CharacterRepository(context); // Assert Assert.ThrowsAny <ArgumentNullException>(() => charRepo.AddCharacter(null)); } } finally { connection.Close(); } }