Skip to content

Commit

Permalink
Merge pull request #1 from davewalker5/async-methods
Browse files Browse the repository at this point in the history
Added async support ; fix SQLite filtering error
  • Loading branch information
davewalker5 committed Feb 13, 2020
2 parents 2570850 + 498b5d5 commit 8216515
Show file tree
Hide file tree
Showing 28 changed files with 1,162 additions and 139 deletions.
3 changes: 2 additions & 1 deletion src/DroneFlightLog.Data.Migrations/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DroneFlightLog.Data.Sqlite;
using System;
using DroneFlightLog.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

namespace DroneFlightLog.Data.Migrations
Expand Down
42 changes: 42 additions & 0 deletions src/DroneFlightLog.Data.Tests/AddressManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace DroneFlightLog.Data.Tests
public class AddressManagerTests
{
private const string Number = "1";
private const string SecondNumber = "2";
private const string Street = "Some Street";
private const string Town = "Some Town";
private const string County = "Some County";
Expand Down Expand Up @@ -46,6 +47,21 @@ public void AddAddressTest()
Assert.AreEqual(Country, _factory.Context.Addresses.First().Country);
}

[TestMethod]
public async void AddAddressAsyncTest()
{
Address address = await _factory.Addresses.AddAddressAsync(SecondNumber, Street, Town, County, Postcode, Country);
await _factory.Context.SaveChangesAsync();
Assert.AreEqual(2, _factory.Context.Addresses.Count());
Assert.AreNotEqual(_addressId, address.Id);
Assert.AreEqual(SecondNumber, address.Number);
Assert.AreEqual(Street, address.Street);
Assert.AreEqual(Town, address.Town);
Assert.AreEqual(County, address.County);
Assert.AreEqual(Postcode, address.Postcode);
Assert.AreEqual(Country, address.Country);
}

[TestMethod, ExpectedException(typeof(AddressExistsException))]
public void AddExistingAddressTest()
{
Expand All @@ -65,6 +81,19 @@ public void GetAddressByIdTest()
Assert.AreEqual(Country, address.Country);
}

[TestMethod]
public async void GetAddressByIdAsyncTest()
{
Address address = await _factory.Addresses.GetAddressAsync(_addressId);
Assert.AreEqual(_addressId, address.Id);
Assert.AreEqual(Number, address.Number);
Assert.AreEqual(Street, address.Street);
Assert.AreEqual(Town, address.Town);
Assert.AreEqual(County, address.County);
Assert.AreEqual(Postcode, address.Postcode);
Assert.AreEqual(Country, address.Country);
}

[TestMethod, ExpectedException(typeof(AddressNotFoundException))]
public void GetMissingAddressByIdTest()
{
Expand All @@ -84,6 +113,19 @@ public void FindAddressTest()
Assert.AreEqual(Country, address.Country);
}

[TestMethod]
public async void FindAddressAsyncTest()
{
Address address = await _factory.Addresses.FindAddressAsync(Number, Postcode, Country);
Assert.AreEqual(_addressId, address.Id);
Assert.AreEqual(Number, address.Number);
Assert.AreEqual(Street, address.Street);
Assert.AreEqual(Town, address.Town);
Assert.AreEqual(County, address.County);
Assert.AreEqual(Postcode, address.Postcode);
Assert.AreEqual(Country, address.Country);
}

[TestMethod]
public void FindMissingAddressTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Linq.Async" Version="4.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
43 changes: 43 additions & 0 deletions src/DroneFlightLog.Data.Tests/DroneManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DroneManagerTest
private const string ModelName = "Some Model";
private const string DroneName = "Some Drone";
private const string DroneSerialNumber = "1234567890";
private const string SecondDroneSerialNumber = "0987654321";

private IDroneFlightLogFactory<DroneFlightLogDbContext> _factory;
private int _modelId;
Expand Down Expand Up @@ -50,6 +51,18 @@ public void AddDroneTest()
Assert.AreEqual(_modelId, _factory.Context.Drones.First().ModelId);
}

[TestMethod]
public async void AddDroneAsyncTest()
{
Drone drone = await _factory.Drones.AddDroneAsync(DroneName, SecondDroneSerialNumber, _modelId);
await _factory.Context.SaveChangesAsync();
Assert.AreEqual(2, _factory.Context.Drones.Count());
Assert.AreNotEqual(_droneId, drone.Id);
Assert.AreEqual(DroneName, drone.Name);
Assert.AreEqual(SecondDroneSerialNumber, drone.SerialNumber);
Assert.AreEqual(_modelId, drone.ModelId);
}

[TestMethod, ExpectedException(typeof(DroneExistsException))]
public void AddExistingDroneTest()
{
Expand All @@ -69,6 +82,13 @@ public void GetDroneByIdTest()
ValidateDrone(drone);
}

[TestMethod]
public async void GetDroneByIdAsyncTest()
{
Drone drone = await _factory.Drones.GetDroneAsync(_droneId);
ValidateDrone(drone);
}

[TestMethod, ExpectedException(typeof(DroneNotFoundException))]
public void GetMissingDroneByIdTest()
{
Expand All @@ -83,6 +103,14 @@ public void GetAllDronesTest()
ValidateDrone(drones.First());
}

[TestMethod]
public async void GetAllDronesAsyncTest()
{
List<Drone> drones = await _factory.Drones.GetDronesAsync(null).ToListAsync();
Assert.AreEqual(1, drones.Count());
ValidateDrone(drones.First());
}

[TestMethod]
public void GetDronesByModelTest()
{
Expand All @@ -91,6 +119,14 @@ public void GetDronesByModelTest()
ValidateDrone(drones.First());
}

[TestMethod]
public async void GetDronesByModelAsyncTest()
{
List<Drone> drones = await _factory.Drones.GetDronesAsync(_modelId).ToListAsync();
Assert.AreEqual(1, drones.Count());
ValidateDrone(drones.First());
}

[TestMethod]
public void GetDronesByMissingModelTest()
{
Expand All @@ -105,6 +141,13 @@ public void FindDroneTest()
ValidateDrone(drone);
}

[TestMethod]
public async void FindDroneAsyncTest()
{
Drone drone = await _factory.Drones.FindDroneAsync(DroneSerialNumber, _modelId);
ValidateDrone(drone);
}

[TestMethod]
public void FindDroneByMissingSerialNumberTest()
{
Expand Down
19 changes: 19 additions & 0 deletions src/DroneFlightLog.Data.Tests/FlightManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public void AddFlightTest()
Assert.AreEqual(_operatorId, _factory.Context.Flights.First().OperatorId);
}

[TestMethod]
public async void AddFlightAsyncTest()
{
Flight flight = await _factory.Flights.AddFlightAsync(_operatorId, _droneId, _locationId, StartDate, EndDate);
await _factory.Context.SaveChangesAsync();
Assert.AreEqual(2, _factory.Context.Flights.Count());
Assert.AreEqual(_droneId, flight.DroneId);
Assert.AreEqual(_locationId, flight.LocationId);
Assert.AreEqual(_operatorId, flight.OperatorId);
}

[TestMethod]
public void FindFlightByOperatorIdTest()
{
Expand All @@ -93,6 +104,14 @@ public void FindFlightByOperatorIdTest()
ValidateFlight(flights.First());
}

[TestMethod]
public async void FindFlightByOperatorIdAsyncTest()
{
List<Flight> flights = await _factory.Flights.FindFlightsAsync(_operatorId, null, null, null, null, 1, 2).ToListAsync();
Assert.AreEqual(1, flights.Count());
ValidateFlight(flights.First());
}

[TestMethod]
public void FindFlightByDroneIdTest()
{
Expand Down
47 changes: 46 additions & 1 deletion src/DroneFlightLog.Data.Tests/FlightPropertyManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class FlightPropertyManagerTest
private const string StringPropertyName = "Some String Property";
private const string StringPropertyValue = "Some Value";

private const string AsyncPropertyName = "Some Async Property";
private const decimal AsyncPropertyValue = 6.45M;

private IDroneFlightLogFactory<DroneFlightLogDbContext> _factory;
private int _flightId;
private int _propertyId;
Expand Down Expand Up @@ -78,7 +81,7 @@ public void TestInitialize()
_factory.Context.SaveChanges();
_propertyId = property.Id;

FlightPropertyValue value = _factory.Properties.AddPropertyValue(_flightId, _propertyId, PropertyValue);
_factory.Properties.AddPropertyValue(_flightId, _propertyId, PropertyValue);
_factory.Context.SaveChanges();
}

Expand All @@ -93,6 +96,17 @@ public void AddPropertyTest()
Assert.IsTrue(_factory.Context.FlightProperties.First().IsSingleInstance);
}

[TestMethod]
public async void AddPropertyAsyncTest()
{
FlightProperty property = await _factory.Properties.AddPropertyAsync(AsyncPropertyName, PropertyType, true);
await _factory.Context.SaveChangesAsync();
Assert.AreEqual(2, _factory.Context.FlightProperties.Count());
Assert.AreEqual(AsyncPropertyName, property.Name);
Assert.AreEqual(PropertyType, property.DataType);
Assert.IsTrue(property.IsSingleInstance);
}

[TestMethod, ExpectedException(typeof(PropertyExistsException))]
public void AddExistingPropertyTest()
{
Expand All @@ -109,6 +123,16 @@ public void GetPropertiesTest()
Assert.IsTrue(properties.First().IsSingleInstance);
}

[TestMethod]
public async void GetPropertiesAsyncTest()
{
List<FlightProperty> properties = await _factory.Properties.GetPropertiesAsync().ToListAsync();
Assert.AreEqual(1, properties.Count());
Assert.AreEqual(PropertyName, properties.First().Name);
Assert.AreEqual(PropertyType, properties.First().DataType);
Assert.IsTrue(properties.First().IsSingleInstance);
}

[TestMethod]
public void AddPropertyValueTest()
{
Expand All @@ -120,6 +144,17 @@ public void AddPropertyValueTest()
Assert.AreEqual(PropertyValue, _factory.Context.FlightPropertyValues.First().NumberValue);
}

[TestMethod]
public async void AddPropertyValueAsyncTest()
{
FlightPropertyValue value = await _factory.Properties.AddPropertyValueAsync(_flightId, _propertyId, AsyncPropertyValue);
await _factory.Context.SaveChangesAsync();
Assert.AreEqual(2, _factory.Context.FlightPropertyValues.Count());
Assert.AreEqual(_flightId, value.FlightId);
Assert.AreEqual(_propertyId, value.PropertyId);
Assert.AreEqual(AsyncPropertyValue, value.NumberValue);
}

[TestMethod, ExpectedException(typeof(ValueExistsException))]
public void AddDuplicateSingleInstanceValueTest()
{
Expand All @@ -136,6 +171,16 @@ public void GetPropertyValuesTest()
Assert.AreEqual(PropertyValue, values.First().NumberValue);
}

[TestMethod]
public async void GetPropertyValuesAsyncTest()
{
List<FlightPropertyValue> values = await _factory.Properties.GetPropertyValuesAsync(_flightId).ToListAsync();
Assert.AreEqual(1, values.Count());
Assert.AreEqual(_flightId, values.First().FlightId);
Assert.AreEqual(_propertyId, values.First().PropertyId);
Assert.AreEqual(PropertyValue, values.First().NumberValue);
}

[TestMethod]
public void GetPropertyValuesForMissingFlightTest()
{
Expand Down
35 changes: 35 additions & 0 deletions src/DroneFlightLog.Data.Tests/LocationManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace DroneFlightLog.Data.Tests
public class LocationManagerTests
{
private const string Name = "My Local Drone Flight Location";
private const string AsyncName = "My Local Async Drone Flight Location";

private IDroneFlightLogFactory<DroneFlightLogDbContext> _factory;
private int _locationId;
Expand All @@ -37,6 +38,15 @@ public void AddLocationTest()
Assert.AreEqual(Name, _factory.Context.Locations.First().Name);
}

[TestMethod]
public async void AddLocationAsyncTest()
{
Location location = _factory.Locations.AddLocation(AsyncName);
await _factory.Context.SaveChangesAsync();
Assert.AreEqual(2, _factory.Context.Locations.Count());
Assert.AreEqual(AsyncName, location.Name);
}

[TestMethod, ExpectedException(typeof(LocationExistsException))]
public void AddExistingLocationTest()
{
Expand All @@ -51,6 +61,14 @@ public void GetLocationByIdTest()
Assert.AreEqual(Name, location.Name);
}

[TestMethod]
public async void GetLocationByIdAsyncTest()
{
Location location = await _factory.Locations.GetLocationAsync(_locationId);
Assert.AreEqual(_locationId, location.Id);
Assert.AreEqual(Name, location.Name);
}

[TestMethod, ExpectedException(typeof(LocationNotFoundException))]
public void GetMissingLocationByIdTest()
{
Expand All @@ -67,13 +85,30 @@ public void GetAllLocationsTest()
Assert.AreEqual(Name, locations.First().Name);
}

[TestMethod]
public async void GetAllLocationsAsyncTest()
{
List<Location> locations = await _factory.Locations.GetLocationsAsync().ToListAsync();
Assert.AreEqual(1, locations.Count());
int locationId = _factory.Context.Locations.First().Id;
Assert.AreEqual(locationId, locations.First().Id);
Assert.AreEqual(Name, locations.First().Name);
}

[TestMethod]
public void FindLocationTest()
{
Location location = _factory.Locations.FindLocation(Name);
Assert.AreEqual(location.Name, Name);
}

[TestMethod]
public async void FindLocationAsyncTest()
{
Location location = await _factory.Locations.FindLocationAsync(Name);
Assert.AreEqual(location.Name, Name);
}

[TestMethod]
public void FindMissingLocationTest()
{
Expand Down
Loading

0 comments on commit 8216515

Please sign in to comment.