public void AddANewTrainingPlan(TrainingPlan plan) { // Validations if (plan == null) { throw new ArgumentNullException("plan"); } if (string.IsNullOrWhiteSpace(plan.Name)) { throw new ArgumentException("Training plan must have a name."); } if (string.IsNullOrWhiteSpace(plan.Details)) { throw new ArgumentException("Training plan must have details."); } if (plan.TrainerID <= 0) { throw new ArgumentNullException("Training plan does not have TrainingID"); } // Check if Training name already exists. if (this._repTrainingPlan.GetByName(plan.TrainerID, plan.Name) != null) { throw new DuplicateRecordException(String.Format("Training Plan '{0}' already exists for current training.", plan.Name)); } // Add a new Training instance. this._repTrainingPlan.AddNew(plan); // Persist changes to database. this._repTrainingPlan.SaveChanges(); }
public void Valid_Add() { TrainingPlan tp = new TrainingPlan(); tp.Name = "Valid Training Plan"; tp.Details = "This is a valid training plan"; tp.TrainerID = 1; this._sTrainingPlan.AddANewTrainingPlan(tp); Assert.AreEqual(true, this._dbCtx.IsSaved); Assert.AreEqual(1, this._dbCtx.Added.Count); }
public void Check_Required_Fields_Set() { try { // No Name TrainingPlan tp = new TrainingPlan(); tp.Name = null; tp.Details = "Details"; tp.TrainerID = 1; this._sTrainingPlan.AddANewTrainingPlan(tp); Assert.Fail(); } catch (ArgumentException) { // Must come here } catch (Exception) { Assert.Fail(); } try { // No Details TrainingPlan tp = new TrainingPlan(); tp.Name = "Name"; tp.Details = null; tp.TrainerID = 1; this._sTrainingPlan.AddANewTrainingPlan(tp); Assert.Fail(); } catch (ArgumentException) { // Must come here } catch (Exception) { Assert.Fail(); } try { // No Training TrainingPlan tp = new TrainingPlan(); tp.Name = "Name"; tp.Details = "Details"; tp.Trainer = null; this._sTrainingPlan.AddANewTrainingPlan(tp); Assert.Fail(); } catch (ArgumentNullException) { // Must come here } catch (Exception) { Assert.Fail(); } }
public void Prevent_Duplicates() { TrainingPlan tp = new TrainingPlan(); tp.Name = "Pull ups"; // Name already exists tp.Details = "Details"; tp.TrainerID = 1; try { this._sTrainingPlan.AddANewTrainingPlan(tp); } catch (DuplicateRecordException) { Assert.AreEqual(false, this._dbCtx.IsSaved); Assert.AreEqual(0, this._dbCtx.Added.Count); return; } Assert.Fail(); }