示例#1
0
        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;
        }
示例#2
0
 public TrainingPlan WithAthlete()
 {
     _athlete = new AthleteBuilder().Build();
     var plan = Build();
     plan.Athlete = _athlete;
     return plan;
 }
示例#3
0
 private void InsertTestHealthHistory(Athlete athlete, int noOfInstances)
 {
     for (var i = 0; i < noOfInstances; i++)
     {
         var athleteHealth =
             new AthleteHealth {Athlete = athlete};
         MemoryDataContext.Add(athleteHealth);
     }
 }
示例#4
0
        public GetBestRunsTester()
        {
            _athleteBuilder = new AthleteBuilder();

            _testRunStart = new DateTime(2008, 1, 1);

            MemoryDataContext.Clear();
            _athlete = _athleteBuilder.Build();
            InsertRuns(_athlete);
        }
示例#5
0
 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);
     }
 }
示例#6
0
        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;
        }
示例#7
0
        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());
        }
示例#8
0
        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;
        }
示例#9
0
        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());
        }
示例#10
0
        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());
        }
示例#11
0
        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());
        }
示例#12
0
        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;
        }
示例#13
0
 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;
 }
示例#14
0
        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);
        }
示例#15
0
 public IQueryable<Run> GetRuns(Athlete athlete)
 {
     var runs = GetUnTrashedRuns().Where(run => run.Athlete.Equals(athlete));
     return runs;
 }
示例#16
0
 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();
 }
示例#17
0
 public List<Shoe> GetShoes(Athlete athlete)
 {
     if (athlete.ID <= 0)
     {
         Error.ObjectIdShouldNotBeZero(athlete);
     }
     return DoGetShoes(athlete).ToList();
 }
示例#18
0
 partial void UpdateAthlete(Athlete instance);
示例#19
0
 public List<TrainingPlan> GetAthleteTrainingPlans(Athlete athlete)
 {
     var plans = DataContext.Queryable<TrainingPlan>().Where(x => x.Athlete.Equals(athlete));
     return plans.ToList();
 }
示例#20
0
 private void UpdateHealthHistory(Athlete existingAthlete)
 {
     var athleteHealth = existingAthlete.HealthSnapshot;
     athleteHealth.RegisteredAt = DateTime.Now;
     DataContext.InsertOnSubmit(athleteHealth);
     DataContext.Commit();
 }
示例#21
0
        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;
        }
示例#22
0
 internal IQueryable<AthleteHealth> GetHealthHistory(Athlete athlete)
 {
     return DataContext.Queryable<AthleteHealth>().Where(x => x.AthleteID == athlete.ID);
 }
示例#23
0
 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;
     }
 }
示例#24
0
 partial void InsertAthlete(Athlete instance);
示例#25
0
 private List<Shoe> SaveShoes(Athlete attachedAthlete, IEnumerable<Shoe> shoes)
 {
     foreach (var shoe in shoes)
     {
         shoe.Athlete = attachedAthlete;
         DoSaveShoe(shoe);
     }
     return GetShoes(attachedAthlete);
 }
示例#26
0
 public bool HasSameHealthAs(Athlete other)
 {
     var myHealth = HealthSnapshot;
     var otherHealth = other.HealthSnapshot;
     return myHealth.HasSameHealthDataAs(otherHealth);
 }
示例#27
0
 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;
 }
示例#28
0
 private void InsertTestWorkouts(Athlete athlete, int noOfWorkouts)
 {
     for (int i = 0; i < noOfWorkouts; i++)
     {
         var workout = new Workout { Athlete = athlete };
         MemoryDataContext.Add(workout);
     }
 }
示例#29
0
        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);
            }
        }
示例#30
0
 partial void DeleteAthlete(Athlete instance);