public async Task GetObservations_DbSeeded_ReturnsDtoWeatherObservationsWithCorrectDates() { // Arrange await using (var context = new ApplicationDbContext(_options)) { _uut = new WeatherObservationsController(context, _mockHub); // Act var result = await _uut.GetObservations(); var returnedList = result.Value.ToList(); // Assert var expectedDtoWeatherObservations = GetDtoWeatherObservationsForTest().OrderByDescending(o => o.Date).Take(3).ToList(); Assert.Multiple((() => { Assert.That(returnedList.Count, Is.EqualTo(3)); Assert.That(returnedList[0].Date, Is.EqualTo(expectedDtoWeatherObservations[0].Date)); Assert.That(returnedList[1].Date, Is.EqualTo(expectedDtoWeatherObservations[1].Date)); Assert.That(returnedList[2].Date, Is.EqualTo(expectedDtoWeatherObservations[2].Date)); })); } }
public UnitTest1() { var connection = new SqliteConnection("Data Source=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <MyDbContext>().UseSqlite(connection).Options; dbcontext = new MyDbContext(options); uut = new WeatherObservationsController(dbcontext, null); }
public async Task GetWeatherObservationPeriodFromADateInterval() { using (var context = new ApplicationContext(_options)) { context.Database.EnsureCreated(); WeatherObservationsController uut = new WeatherObservationsController(context, _hubContext); var result = await uut.GetWeatherObservationPeriod(DateTime.Today.AddDays(-11), DateTime.Today.AddDays(-10)); Assert.Equal("Denmark", result.Value.FirstOrDefault().LocationName); } }
public async Task GetWeatherObservationsGet3() { using (var context = new ApplicationContext(_options)) { context.Database.EnsureCreated(); WeatherObservationsController uut = new WeatherObservationsController(context, _hubContext); var result = await uut.GetWeatherObservations(); Assert.Equal(3, result.Value.Count()); } }
public async Task PostWeatherObservationAddsToDb() { using (var context = new ApplicationContext(_options)) { context.Database.EnsureCreated(); int initial = context.WeatherObservations.ToList().Count; WeatherObservationsController uut = new WeatherObservationsController(context, _hubContext); WeatherObservation entry = new WeatherObservation() { LocationName = "Poland", Temperature = 120 }; await uut.PostWeatherObservation(entry); Assert.Equal(initial + 1, context.WeatherObservations.ToList().Count); } }
public async Task GetObservations_WithDateInitialTimePlus1Day_DbSeeded_ReturnsOneDtoWeatherObservationWithCorrectDate() { // Arrange DateTime date = _initialTime.AddDays(1).Date; // Test data should only contain one observation with this date await using (var context = new ApplicationDbContext(_options)) { _uut = new WeatherObservationsController(context, _mockHub); // Act var result = await _uut.GetObservations(date); var returnedList = result.Value.ToList(); // Assert Assert.Multiple((() => { Assert.That(returnedList.Count, Is.EqualTo(1)); Assert.That(returnedList[0].Date.Date, Is.EqualTo(date)); })); } }
public async Task GetObservations_WithTimeSpanThreeHours_DbSeeded_ReturnsCorrectDtoWeatherObservations() { // Arrange DateTime startTime = _initialTime; DateTime endTime = _initialTime.AddHours(3); //Should only contain 2 weatherObservations in this time-range await using (var context = new ApplicationDbContext(_options)) { _uut = new WeatherObservationsController(context, _mockHub); // Act var result = await _uut.GetObservations(startTime, endTime); var returnedList = result.Value.ToList(); // Assert Assert.Multiple((() => { Assert.That(returnedList.Count, Is.EqualTo(2)); Assert.That(returnedList[0].Date, Is.GreaterThanOrEqualTo(startTime).And.LessThanOrEqualTo(endTime)); Assert.That(returnedList[1].Date, Is.GreaterThanOrEqualTo(startTime).And.LessThanOrEqualTo(endTime)); })); } }