public List<Athlete> CreateRandomAthletes(int numberOfAthletes) { var r = new RandomProvider(); var dateMin = new DateTime(1920, 1, 1); var dateMax = new DateTime(2020, 1, 1); var athletes = new List<Athlete>(); using (var db = new DbDataContext()) { var athleteCountBefore = db.Athletes.Count(); for (var i = 0; i < numberOfAthletes; i++) { var firstName = _randomFirstNamePrefix + i; var lastName = _randomLastNamePrefix + i; //var guid = Guid.NewGuid().ToString(); var user = new User { Password = "******", UserName = firstName, Roles = "" }; var athlete = new Athlete { FirstName = firstName, LastName = lastName, DateOfBirth = r.Next(dateMin, dateMax), User = user }; athletes.Add(athlete); db.Users.InsertOnSubmit(user); } db.SubmitChanges(); var athleteCountAfter = db.Athletes.Count(); Assert.AreEqual(numberOfAthletes, athletes.Count); Assert.AreEqual(athleteCountAfter, athleteCountBefore + numberOfAthletes); } return athletes; }
public TrainingPlan WithAthlete() { _athlete = new AthleteBuilder().Build(); var plan = Build(); plan.Athlete = _athlete; return plan; }
private void InsertTestHealthHistory(Athlete athlete, int noOfInstances) { for (var i = 0; i < noOfInstances; i++) { var athleteHealth = new AthleteHealth {Athlete = athlete}; MemoryDataContext.Add(athleteHealth); } }
public GetBestRunsTester() { _athleteBuilder = new AthleteBuilder(); _testRunStart = new DateTime(2008, 1, 1); MemoryDataContext.Clear(); _athlete = _athleteBuilder.Build(); InsertRuns(_athlete); }
public static void CopyPropertiesExceptVersion(Athlete from, Athlete to) { foreach ( var info in from.GetType().GetProperties(). Where(x => x.CanWrite && x.Name != "RtVersion" && x.Name != "Version") ) { info.SetValue(to, info.GetValue(from, null), null); } }
public User CreateUserWithDefaultAthlete(string emailAsUserName, string password) { var user = new User {Password = password, UserName = emailAsUserName}; DataContext.InsertOnSubmit(user); DataContext.Commit(); user.CustomerID = user.ID.ToString(); DataContext.Commit(); var athlete = new Athlete {User = user}; DataContext.InsertOnSubmit(athlete); DataContext.Commit(); return user; }
public void WhenGoalIsNewShouldAddInstance() { var dataAccess = new AthleteDataAccess(); var athlete = new Athlete { ID = 1 }; MemoryDataContext.Clear(); MemoryDataContext.InsertOnSubmit(athlete); MemoryDataContext.Commit(); Assert.AreEqual(0, MemoryDataContext.Queryable<Goal>().Count()); var goal = new Goal {Athlete = athlete}; dataAccess.SaveGoal(goal); Assert.AreEqual(1, MemoryDataContext.Queryable<Goal>().Count()); }
public Athlete Build() { var user = new User {UserName = "******", ID = 10}; var dateOfBirth = DateTime.MinValue; var athlete = new Athlete { DateOfBirth = dateOfBirth, FirstName = "Gryffe", ID = 1, LastName = null, User = user, }; return athlete; }
public void WhenGoalExistShouldUpdateExistingGoal() { var dataAccess = new AthleteDataAccess(); var athlete = new Athlete { ID = 1 }; MemoryDataContext.Clear(); MemoryDataContext.InsertOnSubmit(athlete); MemoryDataContext.Commit(); var goal = new Goal { Athlete = athlete }; dataAccess.SaveGoal(goal); goal.ID = 1; Assert.AreEqual(1, MemoryDataContext.Queryable<Goal>().Count()); dataAccess.SaveGoal(goal); Assert.AreEqual(1, MemoryDataContext.Queryable<Goal>().Count()); }
public void WhenExistingAthleteIsSavedAndWeightIsUnChangedShouldNotAddWeightHistory() { const int weight = 90; MemoryDataContext.Clear(); InsertDefaultAthlete(weight); _athlete.ID = 1; //saved var db = new AthleteDataAccess(); Assert.AreEqual(0, MemoryDataContext.Queryable<AthleteHealth>().Count()); var disconnectedAthleteWithSameWeight = new Athlete { ID = _athlete.ID, User = _athlete.User, Weight = weight }; db.SaveAthlete(disconnectedAthleteWithSameWeight); Assert.AreEqual(0, MemoryDataContext.Queryable<AthleteHealth>().Count()); }
public void WhenExistingAthleteIsSavedAndWeightIsNullShouldNotAddWeightHistory() { const int weightBefore = 90; MemoryDataContext.Clear(); InsertDefaultAthlete(weightBefore); _athlete.ID = 1; //saved var db = new AthleteDataAccess(); Assert.AreEqual(0, MemoryDataContext.Queryable<AthleteHealth>().Count()); var disconnectedAthleteThatHasGainedWeight = new Athlete { ID = _athlete.ID, User = _athlete.User, Weight = Athlete.NullIntValue }; db.SaveAthlete(disconnectedAthleteThatHasGainedWeight); Assert.AreEqual(0, MemoryDataContext.Queryable<AthleteHealth>().Count()); }
public string DownloadMyRuns(Athlete athlete) { var fileSystem = ResolveFileSystem(); var csvData = ResolveCsvData(); var rundataAccess = new RunDataAccess(); var runs = rundataAccess.GetRuns(athlete); var fileName = athlete.User.UserName + ".csv"; var filepath = Path.Combine( RunTrackEnvironment.MappedFullyQualifiedDownloadDirectory, fileName); var content = csvData.AsFileContent(runs); fileSystem.WriteContent(filepath, content); var downloadPathForClient = RunTrackEnvironment.FullyQualifiedDownloadDirectoryUrl + "/" + fileName; return downloadPathForClient; }
public Athlete SaveAthlete(Athlete athlete) { if (athlete.User == null) { throw new InvalidOperationException("athlete.User==null"); } if (athlete.User.ID <= 0) { throw new InvalidOperationException("athlete.User.ID <= 0"); } DataContext.RollbackAndAttach(athlete.User); if (athlete.ID > 0) { UpdateExistingAthlete(ref athlete); } else { DataContext.InsertOnSubmit(athlete); } DataContext.Commit(); return athlete; }
private void InsertDefaultAthlete(int weight) { var user = new User { ID = 1, Password = "******", UserName = "******" }; MemoryDataContext.InsertOnSubmit(user); MemoryDataContext.Commit(); new UserDataAccess().Save(user); _athlete = new Athlete { ID = 0, User = user, Weight = weight}; new AthleteDataAccess().SaveAthlete(_athlete); }
public IQueryable<Run> GetRuns(Athlete athlete) { var runs = GetUnTrashedRuns().Where(run => run.Athlete.Equals(athlete)); return runs; }
void InsertRuns(Athlete athlete) { for (int i = 0; i < 10; i++) { var run = new Run { Athlete = athlete, Distance = i, Start = _testRunStart, Time = 1 }; _testRunStart = _testRunStart.AddDays(7); MemoryDataContext.InsertOnSubmit(run); } MemoryDataContext.Commit(); }
public List<Shoe> GetShoes(Athlete athlete) { if (athlete.ID <= 0) { Error.ObjectIdShouldNotBeZero(athlete); } return DoGetShoes(athlete).ToList(); }
partial void UpdateAthlete(Athlete instance);
public List<TrainingPlan> GetAthleteTrainingPlans(Athlete athlete) { var plans = DataContext.Queryable<TrainingPlan>().Where(x => x.Athlete.Equals(athlete)); return plans.ToList(); }
private void UpdateHealthHistory(Athlete existingAthlete) { var athleteHealth = existingAthlete.HealthSnapshot; athleteHealth.RegisteredAt = DateTime.Now; DataContext.InsertOnSubmit(athleteHealth); DataContext.Commit(); }
private void UpdateExistingAthlete(ref Athlete athlete) { var athleteID = athlete.ID; var existing = DataContext.Queryable<Athlete>().Single(x => x.ID == athleteID); var updateHealth = !athlete.HasSameHealthAs(existing) && athlete.HealthSnapshot.HasSomeHealthDataSet; Athlete.CopyPropertiesExceptVersion(athlete, existing); if (updateHealth) { UpdateHealthHistory(existing); } athlete = existing; }
internal IQueryable<AthleteHealth> GetHealthHistory(Athlete athlete) { return DataContext.Queryable<AthleteHealth>().Where(x => x.AthleteID == athlete.ID); }
private IEnumerable<Shoe> DoGetShoes(Athlete athlete) { var shoes = DataContext.Queryable<Shoe>().Where(x => x.Athlete == athlete); var rundataAcess = ResolveRunDataAccess(); foreach (var shoe in shoes) { var runs = rundataAcess.GetRuns(athlete).Where(y => y.Shoe.Equals(shoe)); shoe.Usage = runs.Count() == 0 ? 0 : runs.Select(x => x.Distance).Sum(); yield return shoe; } }
partial void InsertAthlete(Athlete instance);
private List<Shoe> SaveShoes(Athlete attachedAthlete, IEnumerable<Shoe> shoes) { foreach (var shoe in shoes) { shoe.Athlete = attachedAthlete; DoSaveShoe(shoe); } return GetShoes(attachedAthlete); }
public bool HasSameHealthAs(Athlete other) { var myHealth = HealthSnapshot; var otherHealth = other.HealthSnapshot; return myHealth.HasSameHealthDataAs(otherHealth); }
private static TrainingPlan CreatePlan(Athlete athlete, WeekdaySelectionEnumeration trainingDays) { var trainingPlan = new TrainingPlan { Workload = 50, Goal = CreateGoal(), Athlete = athlete, Start = new DateTime(2009, 1, 1), StartDistance = 2000, PreferredWeekdays = (int) trainingDays }; return trainingPlan; }
private void InsertTestWorkouts(Athlete athlete, int noOfWorkouts) { for (int i = 0; i < noOfWorkouts; i++) { var workout = new Workout { Athlete = athlete }; MemoryDataContext.Add(workout); } }
private static void InsertAthleteRuns(Athlete athlete, int numberOfRunsToInsert) { var r = new RandomProvider(); using (var db = new DbDataContext()) { var timeMin = 1000; var timeMax = 10000; var distanceMin = 5000; var distanceMax = 12000; var dateMin = new DateTime(1920, 1, 1); var dateMax = new DateTime(2020, 1, 1); var runCountBefore = db.Runs.Count(); const bool athleteModified = false; db.Athletes.Attach(athlete, athleteModified); var runs = new List<Run>(); for (var i = 0; i < numberOfRunsToInsert; i++) { var run = new Run { Athlete = athlete, Time = r.Next(timeMin, timeMax), Distance = r.Next(distanceMin, distanceMax), Start = r.Next(dateMin, dateMax) }; runs.Add(run); if (runs.Count > 5000) { db.Runs.InsertAllOnSubmit(runs); db.SubmitChanges(); runs.Clear(); } } if (runs.Count > 0) { db.Runs.InsertAllOnSubmit(runs); db.SubmitChanges(); runs.Clear(); } var afterRunsInsertedCount = db.Runs.Count(); Assert.AreEqual(runCountBefore, afterRunsInsertedCount - numberOfRunsToInsert); } }
partial void DeleteAthlete(Athlete instance);