Пример #1
0
        public async Task TestRemoveCarFromRace()
        {
            using (raceRepository)
            {
                Car car = new Car()
                {
                    Driver = "Mads", Name = "Suzuki"
                };
                var track = new Track()
                {
                    BestTime       = 121213123,
                    LengthInMeters = 123214,
                    MaxCars        = 50,
                    Name           = "RaceTrack"
                };
                var race = new Race()
                {
                    NumberOfLaps = 5,
                    PlannedEnd   = new DateTime(1920, 11, 11),
                    PlannedStart = new DateTime(1920, 11, 11),
                    Track        = track
                };
                var carInRace = new CarInRace()
                {
                    Car = car, Race = race
                };
                context.Add(carInRace);
                await context.SaveChangesAsync();

                await raceRepository.RemoveCarFromRaceAsync(car.Id, race.Id);


                Assert.Null(await context.CarsInRace.FindAsync(carInRace.CarId, carInRace.RaceId));
            }
        }
Пример #2
0
        public (bool ok, string error) RemoveCarFromRace(int carId, int raceId)
        {
            var car  = _context.Cars.Find(carId);
            var race = _context.Races.Find(raceId);

            if (car == null)
            {
                return(false, "Couldn't find car");
            }
            if (race == null)
            {
                return(false, "Couldn't find race");
            }

            if (race.ActualStart != null)
            {
                return(false, "Race has begun, cars cannot be removed");
            }

            var carRace = new CarInRace()
            {
                CarId  = carId,
                RaceId = raceId,
                Car    = car,
                Race   = race,
            };

            _context.CarsInRace.Add(carRace);
            _context.SaveChanges();

            return(true, "Car has been removed from race");
        }
Пример #3
0
        public async void UpdateCarInRaceAsync_given_car_updates_car()
        {
            var builder = new DbContextOptionsBuilder <SlotCarContext>().UseInMemoryDatabase("UpdateCarInRace");

            SlotCarContext context = new SlotCarContext(builder.Options);

            context.Database.EnsureCreated();

            using (var repository = new RaceRepository(context))
            {
                var track = new Track
                {
                    Name           = "Monte Carlo",
                    LengthInMeters = 1002,
                    MaxCars        = 20
                };

                var race = new Race
                {
                    Id           = 42,
                    NumberOfLaps = 5,
                    Track        = track
                };

                var car = new Car
                {
                    Id     = 42,
                    Name   = "Lynet McQueen",
                    Driver = "McQueen"
                };

                var carinrace = new CarInRace
                {
                    Car  = car,
                    Race = race
                };
                await context.Races.AddAsync(race);

                await context.Tracks.AddAsync(track);

                await context.Cars.AddAsync(car);

                await context.CarsInRace.AddAsync(carinrace);

                await context.SaveChangesAsync();

                var dto = new RaceCarDTO
                {
                    CarId         = 42,
                    RaceId        = 42,
                    StartPosition = 1
                };

                var result = await repository.UpdateCarInRaceAsync(dto);

                Assert.True(result.ok);
            }
        }
Пример #4
0
        public void Add_Car_To_Race_That_Is_Full_Returns_False()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();
            var builder = new DbContextOptionsBuilder <SlotCarContext>()
                          .UseSqlite(connection);

            _context = new SlotCarContext(builder.Options);
            _context.Database.EnsureCreated();

            var track = new Track()
            {
                Id             = 1,
                Name           = "Huge Lane",
                LengthInMeters = 1000,
                MaxCars        = 1
            };
            var race = new Race()
            {
                Id           = 1,
                TrackId      = 1,
                Track        = track,
                NumberOfLaps = 2,
            };
            var car = new Car()
            {
                Id = 1, Name = "BMW", Driver = "Hugh Hefner"
            };
            var car2 = new Car()
            {
                Id = 2, Name = "Opel", Driver = "Barack Obama"
            };
            var raceCar = new CarInRace()
            {
                CarId  = car.Id,
                RaceId = race.Id,
                Car    = car,
                Race   = race
            };

            _context.Cars.Add(car);
            _context.Cars.Add(car2);
            _context.Races.Add(race);
            _context.CarsInRace.Add(raceCar);
            _context.SaveChanges();

            using (var repository = new RaceRepository(_context))
            {
                var result = repository.AddCarToRace(2, 1);
                Assert.False(result.ok);
                Assert.Equal("Race is full", result.error);
            }
            connection.Close();
        }
Пример #5
0
        public async Task TestUpdateCarInRaceAsync()
        {
            using (raceRepository)
            {
                Car car = new Car()
                {
                    Driver = "Mads", Name = "Suzuki"
                };

                var track = new Track()
                {
                    BestTime       = 121213123,
                    LengthInMeters = 123214,
                    MaxCars        = 50,
                    Name           = "RaceTrack"
                };
                var race = new Race()
                {
                    NumberOfLaps = 5,
                    PlannedEnd   = new DateTime(1920, 11, 11),
                    PlannedStart = new DateTime(1920, 11, 11),
                    Track        = track
                };
                var carInRace = new CarInRace()
                {
                    Car = car, Race = race
                };
                await context.AddAsync(carInRace);

                await context.SaveChangesAsync();

                var UpdatedCarInRaceInfo = new RaceCarDTO()
                {
                    CarId         = car.Id,
                    RaceId        = race.Id,
                    EndPosition   = 2,
                    FastestLap    = 123123,
                    StartPosition = 1212,
                    TotalTime     = 2121212
                };
                await raceRepository.UpdateCarInRaceAsync(UpdatedCarInRaceInfo);

                Assert.Equal(UpdatedCarInRaceInfo.EndPosition, context.CarsInRace.Find(carInRace.RaceId, carInRace.CarId).EndPosition);
                Assert.Equal(UpdatedCarInRaceInfo.FastestLap, context.CarsInRace.Find(carInRace.RaceId, carInRace.CarId).FastestLap);
                Assert.Equal(UpdatedCarInRaceInfo.TotalTime, context.CarsInRace.Find(carInRace.RaceId, carInRace.CarId).TotalTime);
                Assert.Equal(UpdatedCarInRaceInfo.StartPosition, context.CarsInRace.Find(carInRace.RaceId, carInRace.CarId).StartPosition);
            }
        }
Пример #6
0
        public async Task <(bool ok, string error)> AddCarToRaceAsync(int carId, int raceId, int?startPosition = null)
        {
            var car  = _context.Cars.Find(carId);
            var race = _context.Races.Find(raceId);

            if (car == null)
            {
                return(false, "Couldn't find car");
            }
            if (race == null)
            {
                return(false, "Couldn't find race");
            }

            var currentNumberOfCars = (from c in _context.CarsInRace
                                       where c.Race == race
                                       select c).Count();

            if (race.ActualStart == null && race.Track.MaxCars > currentNumberOfCars)
            {
                var carRace = new CarInRace()
                {
                    CarId  = carId,
                    RaceId = raceId,
                    Car    = car,
                    Race   = race,
                };

                await _context.CarsInRace.AddAsync(carRace);

                await _context.SaveChangesAsync();

                return(true, "Car has been added");
            }
            else if (race.ActualStart != null)
            {
                return(false, "Race started");
            }
            else
            {
                return(false, "Race is full");
            }
        }
Пример #7
0
        public (bool ok, string error) UpdateCarInRace(RaceCarDTO car)
        {
            CarInRace toBeUpdated = (from races in context.CarsInRace
                                     where races.CarId == car.CarId && races.RaceId == car.RaceId
                                     select races).FirstOrDefault();

            if (toBeUpdated != null)
            {
                toBeUpdated.FastestLap    = car.FastestLap;
                toBeUpdated.EndPosition   = car.EndPosition;
                toBeUpdated.CarId         = car.CarId;
                toBeUpdated.RaceId        = car.RaceId;
                toBeUpdated.StartPosition = car.StartPosition;
                toBeUpdated.TotalTime     = car.TotalTime;
                context.SaveChanges();
                return(true, "");
            }
            return(false, "no Car In Race found");
        }
Пример #8
0
        public (bool ok, string error) RemoveCarFromRace(int carId, int raceId)
        {
            Race race = context.Races.Find(raceId);
            Car  car  = context.Cars.Find(carId);

            if (race != null && race.ActualStart == null && car != null)
            {
                CarInRace TestForCarInRace = (from carInRaces in context.CarsInRace
                                              where carInRaces.RaceId == raceId && carInRaces.CarId == carId
                                              select carInRaces).FirstOrDefault();
                if (TestForCarInRace != null)
                {
                    context.CarsInRace.Remove(TestForCarInRace);
                    context.SaveChanges();
                    return(true, "");
                }
                return(false, "The choosen car was not in the choosen race");
            }
            return(false, "Race not excisting or has started");
        }
Пример #9
0
        public (bool ok, string error) AddCarToRace(int carId, int raceId, int?startPosition = null)
        {
            var car  = _context.Cars.Find(carId);
            var race = _context.Races.Find(raceId);

            if (car == null)
            {
                return(false, "Couldn't find car");
            }
            if (race == null)
            {
                return(false, "Couldn't find race");
            }

            var currentNumberOfCars = (from c in _context.CarsInRace
                                       where c.Race == race
                                       select c).Count();

            if (race.ActualStart == null && race.Track.MaxCars > currentNumberOfCars)
            {
                var carRace = new CarInRace()
                {
                    CarId  = carId,
                    RaceId = raceId,
                    Car    = car,
                    Race   = race,
                };

                _context.CarsInRace.Add(carRace);
                _context.SaveChanges();
                return(true, "YAY! " + carRace.Car.Name + " " + carRace.Race.Track.Name);
            }
            else if (race.ActualStart != null)
            {
                return(false, "Race started");
            }
            else
            {
                return(false, "Race is full");
            }
        }
Пример #10
0
        public async Task <(bool ok, string error)> RemoveCarFromRaceAsync(int carId, int raceId)
        {
            Race race = await context.Races.FindAsync(raceId);

            Car car = await context.Cars.FindAsync(carId);

            if (race != null && race.ActualStart == null && car != null)
            {
                CarInRace TestForCarInRace = await context.CarsInRace.FindAsync(raceId, carId);

                if (TestForCarInRace != null)
                {
                    context.CarsInRace.Remove(TestForCarInRace);
                    await context.SaveChangesAsync();

                    return(true, "");
                }
                return(false, "The choosen car was not in the choosen race");
            }
            return(false, "Race not excisting or has started");
        }
Пример #11
0
        public async Task TestReadList()
        {
            using (raceRepository)
            {
                Car car = new Car()
                {
                    Driver = "Mads", Name = "Suzuki"
                };
                Car car1 = new Car()
                {
                    Driver = "Mads2", Name = "Suzuki"
                };
                Car car2 = new Car()
                {
                    Driver = "Mads3", Name = "Suzuki"
                };

                var track = new Track()
                {
                    BestTime       = 121213123,
                    LengthInMeters = 123214,
                    MaxCars        = 50,
                    Name           = "RaceTrack"
                };
                var race = new Race()
                {
                    NumberOfLaps = 5,
                    PlannedEnd   = new DateTime(1920, 11, 11),
                    PlannedStart = new DateTime(1920, 11, 11),
                    Track        = track
                };
                var carInRace = new CarInRace()
                {
                    Car = car, Race = race, TotalTime = 120210
                };
                var carInRace2 = new CarInRace()
                {
                    Car = car1, Race = race, TotalTime = 21314212421
                };
                var carInRace3 = new CarInRace()
                {
                    Car = car2, Race = race, TotalTime = 214124214141
                };
                context.Add(carInRace);
                context.Add(carInRace2);
                context.Add(carInRace3);
                await context.SaveChangesAsync();

                var raceList = new RaceListDTO()
                {
                    End           = race.PlannedEnd,
                    Start         = race.PlannedStart,
                    Id            = race.Id,
                    MaxCars       = race.Track.MaxCars,
                    NumberOfCars  = 3,
                    NumberOfLaps  = race.NumberOfLaps,
                    TrackName     = race.Track.Name,
                    WinningCar    = car.Name,
                    WinningDriver = car.Driver
                };

                Assert.Equal(new List <RaceListDTO> {
                    raceList
                }, await raceRepository.ReadAsync());
            }
        }
Пример #12
0
        public async void ReadAsync_returns_mapped_races()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();
            var builder = new DbContextOptionsBuilder <SlotCarContext>().UseSqlite(connection);

            _context = new SlotCarContext(builder.Options);
            _context.Database.EnsureCreated();

            var track = new Track()
            {
                Id             = 1,
                Name           = "Huge Lane",
                LengthInMeters = 1000,
                MaxCars        = 4
            };

            var race = new Race()
            {
                Id           = 1,
                TrackId      = 1,
                Track        = track,
                NumberOfLaps = 2,
                ActualStart  = new DateTime(2015, 09, 28),
                PlannedEnd   = new DateTime(2017, 09, 28)
            };

            var car = new Car()
            {
                Id = 1, Name = "BMW", Driver = "Hugh Hefner"
            };
            var car2 = new Car()
            {
                Id = 2, Name = "Opel", Driver = "Barack Obama"
            };

            var raceCar = new CarInRace()
            {
                CarId       = car.Id,
                RaceId      = race.Id,
                Car         = car,
                Race        = race,
                EndPosition = 2
            };

            var raceCar2 = new CarInRace()
            {
                CarId       = car2.Id,
                RaceId      = race.Id,
                Car         = car2,
                Race        = race,
                EndPosition = 1
            };

            await _context.Cars.AddAsync(car);

            await _context.Cars.AddAsync(car2);

            await _context.Races.AddAsync(race);

            await _context.CarsInRace.AddAsync(raceCar);

            await _context.CarsInRace.AddAsync(raceCar2);

            await _context.SaveChangesAsync();

            using (var repository = new RaceRepository(_context))
            {
                var dtos = await repository.ReadAsync();

                var result   = dtos.FirstOrDefault();
                var expected = new List <RaceListDTO>();
                expected.Add(new RaceListDTO()
                {
                    Id            = 1,
                    TrackName     = "Huge Lane",
                    NumberOfLaps  = 2,
                    Start         = new DateTime(2015, 09, 28),
                    End           = new DateTime(2017, 09, 28),
                    MaxCars       = 4,
                    NumberOfCars  = 2,
                    WinningCar    = "Opel",
                    WinningDriver = "Barack Obama"
                });

                var expectedFinal = expected.FirstOrDefault();
                Assert.Equal(expectedFinal.Id, result.Id);
                Assert.Equal(expectedFinal.TrackName, result.TrackName);
                Assert.Equal(expectedFinal.Start, result.Start);
                Assert.Equal(expectedFinal.End, result.End);
                Assert.Equal(expectedFinal.MaxCars, result.MaxCars);
                Assert.Equal(expectedFinal.NumberOfCars, result.NumberOfCars);
                Assert.Equal(expectedFinal.WinningCar, result.WinningCar);
                Assert.Equal(expectedFinal.WinningDriver, result.WinningDriver);
            }
        }
Пример #13
0
        public void UpdateCarInRace_given_RaceCarDTO_updates_car()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            var builder = new DbContextOptionsBuilder <SlotCarContext>()
                          .UseSqlite(connection);

            _context = new SlotCarContext(builder.Options);
            _context.Database.EnsureCreated();

            var track = new Track()
            {
                Id             = 10,
                Name           = "Awesome Track",
                LengthInMeters = 2000,
                MaxCars        = 12
            };

            var race = new Race()
            {
                Id           = 5,
                TrackId      = 10,
                Track        = track,
                NumberOfLaps = 3
            };

            var car = new Car()
            {
                Id = 20, Name = "Awesome Car", Driver = "Awesome Driver"
            };
            var car2 = new Car()
            {
                Id = 21, Name = "More Awesome Car", Driver = "Awesomest Driver"
            };

            var carInRace = new CarInRace()
            {
                CarId  = car.Id,
                RaceId = race.Id,
                Car    = car,
                Race   = race,
            };

            _context.Tracks.Add(track);
            _context.Races.Add(race);
            _context.Cars.Add(car);
            _context.Cars.Add(car2);
            _context.CarsInRace.Add(carInRace);
            _context.SaveChanges();

            car.Driver = "Awesomest Driver";

            var newCarInRace = new RaceCarDTO()
            {
                CarId  = car.Id,
                RaceId = race.Id,
            };

            using (var repository = new RaceRepository(_context))
            {
                var DBCarInRace = _context.CarsInRace.FirstOrDefault();
                var DBCar       = _context.Cars.Find(DBCarInRace.CarId);
                Assert.Equal(car.Name, DBCar.Name);
                Assert.Equal(car.Driver, DBCar.Driver);
            }
        }