Ejemplo n.º 1
0
        public async Task TestCreateRace()
        {
            using (raceRepository)
            {
                var track = new Track()
                {
                    BestTime       = 121213123,
                    LengthInMeters = 123214,
                    MaxCars        = 50,
                    Name           = "RaceTrack"
                };
                context.Add(track);
                await context.SaveChangesAsync();

                var raceDTO = new RaceCreateDTO()
                {
                    ActualEnd    = new DateTime(1920, 11, 11),
                    ActualStart  = new DateTime(1920, 11, 11),
                    NumberOfLaps = 5,
                    PlannedEnd   = new DateTime(1920, 11, 11),
                    PlannedStart = new DateTime(1920, 11, 11),
                    TrackId      = track.Id
                };

                Assert.NotNull(await context.Races.FindAsync(await raceRepository.CreateAsync(raceDTO)));
            }
        }
Ejemplo n.º 2
0
        public void Create_New_Car_In_Create_Race_Repo()
        {
            var race = default(Race);
            var dto  = new RaceCreateDTO
            {
                Id           = 1,
                TrackId      = 2,
                NumberOfLaps = 7
            };

            var mock = new Mock <ISlotCarContext>();

            mock.Setup(s => s.Races.Add(It.IsAny <Race>()))
            .Callback <Race>(r => race = r);

            using (var repository = new RaceRepository(mock.Object))
            {
                repository.Create(dto);
            }

            if (race != null)
            {
                Assert.Equal(1, race.Id);
                Assert.Equal(2, race.TrackId);
                Assert.Equal(7, race.NumberOfLaps);
            }
            else
            {
                Assert.True(true);
            }
        }
Ejemplo n.º 3
0
        public async Task TestRead()
        {
            using (raceRepository)
            {
                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
                };
                context.Add(race);
                await context.SaveChangesAsync();

                var raceCreate = new RaceCreateDTO()
                {
                    PlannedEnd   = race.PlannedEnd,
                    PlannedStart = race.PlannedStart,
                    Id           = race.Id,
                    ActualEnd    = race.ActualEnd,
                    ActualStart  = race.ActualStart,
                    NumberOfLaps = race.NumberOfLaps,
                    TrackId      = race.Track.Id
                };

                Assert.Equal(raceCreate, await raceRepository.ReadAsync(race.Id));
            }
        }
Ejemplo n.º 4
0
        public async Task <int> CreateAsync(RaceCreateDTO race)
        {
            if (race != null)
            {
                Track track = await context.Tracks.FindAsync(race.TrackId);

                if (track != null)
                {
                    Race createdRace = new Race()
                    {
                        ActualEnd    = race.ActualEnd,
                        ActualStart  = race.ActualStart,
                        NumberOfLaps = race.NumberOfLaps,
                        PlannedEnd   = race.PlannedEnd,
                        PlannedStart = race.PlannedStart,
                        Track        = track
                    };
                    await context.Races.AddAsync(createdRace);

                    await context.SaveChangesAsync();

                    return(createdRace.Id);
                }
                return(0);
            }
            return(0);
        }
Ejemplo n.º 5
0
        public async void CreateAsync_given_race_returns_raceid()
        {
            var race = default(Race);
            var dto  = new RaceCreateDTO
            {
                Id           = 1,
                TrackId      = 2,
                NumberOfLaps = 7
            };

            var mock = new Mock <ISlotCarContext>();

            mock.Setup(s => s.Races.Add(It.IsAny <Race>())).Callback <Race>(r => race = r);

            using (var repository = new RaceRepository(mock.Object))
            {
                await repository.CreateAsync(dto);
            }

            if (race != null)
            {
                Assert.Equal(2, race.TrackId);
                Assert.Equal(7, race.NumberOfLaps);
            }
            else
            {
                Assert.True(true);
            }
        }
Ejemplo n.º 6
0
        public async Task <RaceCreateDTO> ReadAsync(int raceId)
        {
            Race race = await context.Races.FindAsync(raceId);

            RaceCreateDTO raceList = new RaceCreateDTO
            {
                ActualEnd    = race.ActualEnd,
                ActualStart  = race.ActualStart,
                Id           = race.Id,
                NumberOfLaps = race.NumberOfLaps,
                PlannedEnd   = race.PlannedEnd,
                PlannedStart = race.PlannedStart,
                TrackId      = race.TrackId
            };

            return(raceList);
        }
Ejemplo n.º 7
0
        public RaceCreateDTO Read(int raceId)
        {
            Race race = context.Races.Find(raceId);

            RaceCreateDTO raceList = new RaceCreateDTO
            {
                ActualEnd    = race.ActualEnd,
                ActualStart  = race.ActualStart,
                Id           = race.Id,
                NumberOfLaps = race.NumberOfLaps,
                PlannedEnd   = race.PlannedEnd,
                PlannedStart = race.PlannedStart,
                TrackId      = race.TrackId
            };

            return(raceList);
        }
Ejemplo n.º 8
0
        public void TestCreateRaceFailsStarted()
        {
            using (raceRepository)
            {
                var track = new Track()
                {
                    BestTime       = 121213123,
                    LengthInMeters = 123214,
                    MaxCars        = 50,
                    Name           = "RaceTrack"
                };
                context.Add(track);
                context.SaveChanges();
                RaceCreateDTO raceDTO = null;

                Assert.Equal(0, raceRepository.Create(raceDTO));
            }
        }
Ejemplo n.º 9
0
        public int Create(RaceCreateDTO race)
        {
            var entity = new Race
            {
                Id           = race.Id,
                TrackId      = race.TrackId,
                NumberOfLaps = race.NumberOfLaps,
                PlannedStart = race.PlannedStart,
                ActualStart  = race.ActualStart,
                PlannedEnd   = race.PlannedEnd,
                ActualEnd    = race.PlannedEnd
            };

            _context.Races.Add(entity);
            _context.SaveChanges();

            return(0);
        }
Ejemplo n.º 10
0
        public async Task <(bool ok, string error)> UpdateAsync(RaceCreateDTO race)
        {
            Race choosen = await context.Races.FindAsync(race.Id);

            if (choosen != null)
            {
                choosen.ActualStart  = race.ActualStart;
                choosen.ActualEnd    = race.ActualEnd;
                choosen.PlannedEnd   = race.PlannedEnd;
                choosen.PlannedStart = race.PlannedStart;
                choosen.TrackId      = race.TrackId;
                choosen.NumberOfLaps = race.NumberOfLaps;
                await context.SaveChangesAsync();

                return(true, "");
            }
            return(false, "no race found");
        }
Ejemplo n.º 11
0
        public async Task <int> CreateAsync(RaceCreateDTO race)
        {
            var entity = new Race
            {
                TrackId      = race.TrackId,
                NumberOfLaps = race.NumberOfLaps,
                PlannedStart = race.PlannedStart,
                ActualStart  = race.ActualStart,
                PlannedEnd   = race.PlannedEnd,
                ActualEnd    = race.PlannedEnd
            };

            await _context.Races.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(entity.Id);
        }
Ejemplo n.º 12
0
        public async Task TestUpdate()
        {
            using (raceRepository)
            {
                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 trackupdated = new Track()
                {
                    BestTime       = 12121312321121,
                    LengthInMeters = 1232142112,
                    MaxCars        = 40,
                    Name           = "RaceTrackupdated"
                };
                context.Add(race);
                context.Add(trackupdated);
                await context.SaveChangesAsync();

                var RaceCreate = new RaceCreateDTO()
                {
                    Id           = race.Id,
                    NumberOfLaps = 5,
                    PlannedEnd   = new DateTime(1920, 11, 11),
                    PlannedStart = new DateTime(1920, 11, 11),
                    TrackId      = trackupdated.Id,
                    ActualEnd    = new DateTime(1221, 1, 1),
                    ActualStart  = new DateTime(2412, 2, 2)
                };
                await raceRepository.UpdateAsync(RaceCreate);

                Assert.Equal((await context.Races.FindAsync(race.Id)).Track, trackupdated);
            }
        }
Ejemplo n.º 13
0
        public (bool ok, string error) Update(RaceCreateDTO race)
        {
            Race choosen = (from races in context.Races
                            where races.Id == race.Id
                            select races).FirstOrDefault();

            if (choosen != null)
            {
                choosen.ActualStart  = race.ActualStart;
                choosen.ActualEnd    = race.ActualEnd;
                choosen.PlannedEnd   = race.PlannedEnd;
                choosen.PlannedStart = race.PlannedStart;
                choosen.TrackId      = race.TrackId;
                choosen.NumberOfLaps = race.NumberOfLaps;
                context.SaveChanges();
                return(true, "");
            }
            return(false, "no race found");
        }
Ejemplo n.º 14
0
 public int Create(RaceCreateDTO race)
 {
     if (race != null)
     {
         Track track       = context.Tracks.Find(race.TrackId);
         Race  createdRace = new Race()
         {
             ActualEnd    = race.ActualEnd,
             ActualStart  = race.ActualStart,
             NumberOfLaps = race.NumberOfLaps,
             PlannedEnd   = race.PlannedEnd,
             PlannedStart = race.PlannedStart,
             Track        = track
         };
         context.Races.Add(createdRace);
         context.SaveChanges();
         return(createdRace.Id);
     }
     return(0);
 }
Ejemplo n.º 15
0
        public (bool ok, string error) Update(RaceCreateDTO race)
        {
            var raceTrack = _context.Races.Find(race.Id);

            if (raceTrack == null)
            {
                return(false, "Race not found");
            }
            raceTrack.NumberOfLaps = race.NumberOfLaps;
            raceTrack.PlannedStart = (race.PlannedStart != null) ? race.PlannedStart : raceTrack.PlannedStart;
            raceTrack.ActualStart  = (race.ActualStart != null) ? race.ActualStart : raceTrack.ActualStart;
            raceTrack.PlannedEnd   = (race.PlannedEnd != null) ? race.PlannedEnd : raceTrack.PlannedEnd;
            raceTrack.ActualEnd    = (race.ActualEnd != null) ? race.ActualEnd : raceTrack.ActualEnd;
            if (raceTrack.TrackId != race.TrackId && _context.Tracks.Find(race.TrackId) != null)
            {
                raceTrack.TrackId = race.TrackId;
                raceTrack.Track   = _context.Tracks.Find(race.TrackId);
            }
            _context.SaveChanges();
            return(true, "Track is now updated ");
        }
Ejemplo n.º 16
0
        public void TestUpdateRaceNotFound()
        {
            var race = new Race()
            {
                NumberOfLaps = 5,
                PlannedEnd   = new DateTime(1920, 11, 11),
                PlannedStart = new DateTime(1920, 11, 11),
            };

            var RaceCreate = new RaceCreateDTO()
            {
                Id           = race.Id,
                NumberOfLaps = 5,
                PlannedEnd   = new DateTime(1920, 11, 11),
                PlannedStart = new DateTime(1920, 11, 1),
                ActualEnd    = new DateTime(1221, 1, 1),
                ActualStart  = new DateTime(2412, 2, 2)
            };

            Assert.Equal((false, "no race found"), raceRepository.Update(RaceCreate));
        }
Ejemplo n.º 17
0
        public async void UpdateAsync_given_race_updates_race()
        {
            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 track2 = new Track()
            {
                Id             = 2,
                Name           = "Little Lane",
                LengthInMeters = 3000,
                MaxCars        = 5
            };

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

            await _context.Tracks.AddAsync(track);

            await _context.Tracks.AddAsync(track2);

            await _context.Races.AddAsync(race);

            await _context.SaveChangesAsync();

            var newRace = new RaceCreateDTO()
            {
                Id           = 1,
                TrackId      = 2,
                NumberOfLaps = 5,
                PlannedStart = new DateTime(2015, 09, 28),
                PlannedEnd   = new DateTime(2017, 09, 28),
                ActualEnd    = new DateTime(2017, 09, 30)
            };

            using (var repository = new RaceRepository(_context))
            {
                await repository.UpdateAsync(newRace);

                var result = await _context.Races.FindAsync(1);

                Assert.Equal(2, result.TrackId);
                Assert.Equal(track2.Name, result.Track.Name);
                Assert.Equal(5, result.NumberOfLaps);
                Assert.Equal(new DateTime(2015, 09, 28), result.PlannedStart);
                Assert.Equal(new DateTime(2015, 09, 28), result.ActualStart);
                Assert.Equal(new DateTime(2017, 09, 28), result.PlannedEnd);
                Assert.Equal(new DateTime(2017, 09, 30), result.ActualEnd);
            }
        }