public void Create_WithValidCollection_ReturnsEntityWithExpectedResults() { // Setup var random = new Random(21); int order = random.Next(); var duneLocation = new TestDuneLocation(); var calculation = new DuneLocationCalculation(duneLocation); var calculations = new DuneLocationCalculationsForTargetProbability(random.NextDouble(0, 0.1)) { DuneLocationCalculations = { calculation } }; var duneLocationEntity = new DuneLocationEntity(); var registry = new PersistenceRegistry(); registry.Register(duneLocationEntity, duneLocation); // Call DuneLocationCalculationForTargetProbabilityCollectionEntity entity = calculations.Create(order, registry); // Assert Assert.IsNotNull(entity); Assert.AreEqual(order, entity.Order); Assert.AreEqual(calculations.TargetProbability, entity.TargetProbability); DuneLocationCalculationEntity duneLocationCalculationEntity = entity.DuneLocationCalculationEntities.Single(); Assert.AreSame(duneLocationEntity, duneLocationCalculationEntity.DuneLocationEntity); CollectionAssert.IsEmpty(duneLocationCalculationEntity.DuneLocationCalculationOutputEntities); }
public void Create_WithPersistenceRegistry_ReturnsDuneLocationEntityWithPropertiesSet() { // Setup const string testName = "testName"; var random = new Random(21); double coordinateX = random.NextDouble(); double coordinateY = random.NextDouble(); int id = random.Next(0, 150); int order = random.Next(); var registry = new PersistenceRegistry(); var location = new DuneLocation(id, testName, new Point2D(coordinateX, coordinateY), new DuneLocation.ConstructionProperties { CoastalAreaId = random.Next(), Offset = random.NextDouble(), Orientation = random.NextDouble(), D50 = random.NextDouble() }); // Call DuneLocationEntity entity = location.Create(registry, order); // Assert Assert.IsNotNull(entity); Assert.AreEqual(id, entity.LocationId); Assert.AreEqual(testName, entity.Name); Assert.AreEqual(coordinateX, entity.LocationX); Assert.AreEqual(coordinateY, entity.LocationY); Assert.AreEqual(location.CoastalAreaId, entity.CoastalAreaId); Assert.AreEqual(location.Offset, entity.Offset, location.Offset.GetAccuracy()); Assert.AreEqual(location.Orientation, entity.Orientation, location.Orientation.GetAccuracy()); Assert.AreEqual(location.D50, entity.D50, location.D50.GetAccuracy()); Assert.AreEqual(order, entity.Order); }
public void Create_WithDuneLocations_ReturnsEntityWithDuneLocations() { // Setup var duneLocation = new TestDuneLocation(); var failureMechanism = new DuneErosionFailureMechanism { DuneLocationCalculationsForUserDefinedTargetProbabilities = { new DuneLocationCalculationsForTargetProbability(0.1), new DuneLocationCalculationsForTargetProbability(0.01) } }; failureMechanism.SetDuneLocations(new[] { duneLocation }); var duneLocationEntity = new DuneLocationEntity(); var registry = new PersistenceRegistry(); registry.Register(duneLocationEntity, duneLocation); // Call FailureMechanismEntity entity = failureMechanism.Create(registry); // Assert DuneLocationEntity actualDuneLocationEntity = entity.DuneLocationEntities.Single(); Assert.AreSame(duneLocationEntity, actualDuneLocationEntity); DuneErosionFailureMechanismMetaEntity metaEntity = entity.DuneErosionFailureMechanismMetaEntities.Single(); AssertDuneLocationCalculationCollectionEntities(failureMechanism, metaEntity); }
/// <summary> /// Creates a <see cref="DuneLocationEntity"/> based on the information of the <see cref="DuneLocation"/>. /// </summary> /// <param name="location">The location to create a database entity for.</param> /// <param name="registry">The object keeping track of create operations.</param> /// <param name="order">Index at which this instance resides inside its parent container.</param> /// <returns>A new <see cref="DuneLocationEntity"/>.</returns> /// <exception cref="ArgumentNullException">Thrown when any parameter is <c>null</c>.</exception> internal static DuneLocationEntity Create(this DuneLocation location, PersistenceRegistry registry, int order) { if (location == null) { throw new ArgumentNullException(nameof(location)); } if (registry == null) { throw new ArgumentNullException(nameof(registry)); } if (registry.Contains(location)) { return(registry.Get(location)); } var entity = new DuneLocationEntity { LocationId = location.Id, Name = location.Name.DeepClone(), LocationX = location.Location.X.ToNaNAsNull(), LocationY = location.Location.Y.ToNaNAsNull(), CoastalAreaId = location.CoastalAreaId, Offset = location.Offset.ToNaNAsNull(), Orientation = location.Orientation.ToNaNAsNull(), D50 = location.D50.ToNaNAsNull(), Order = order }; registry.Register(entity, location); return(entity); }
public void Create_WithNaNValues_ReturnsDuneLocationEntityWithNullPropertiesSet() { // Setup var random = new Random(28); int id = random.Next(0, 150); int order = random.Next(); var registry = new PersistenceRegistry(); var location = new DuneLocation(id, string.Empty, new Point2D(double.NaN, double.NaN), new DuneLocation.ConstructionProperties { Offset = double.NaN, Orientation = double.NaN, D50 = double.NaN }); // Call DuneLocationEntity entity = location.Create(registry, order); // Assert Assert.IsNotNull(entity); Assert.AreEqual(id, entity.LocationId); Assert.IsEmpty(entity.Name); Assert.IsNull(entity.LocationX); Assert.IsNull(entity.LocationY); Assert.IsNull(entity.Offset); Assert.IsNull(entity.Orientation); Assert.IsNull(entity.D50); Assert.AreEqual(order, entity.Order); }
/// <summary> /// Read the <see cref="DuneLocationEntity"/> and use the information to construct a <see cref="DuneLocation"/>. /// </summary> /// <param name="entity">The <see cref="DuneLocationEntity"/> to create <see cref="DuneLocation"/> for.</param> /// <param name="collector">The object keeping track of read operations.</param> /// <returns>A new <see cref="DuneLocation"/>.</returns> /// <exception cref="ArgumentNullException">Thrown when any input parameter is <c>null</c>.</exception> internal static DuneLocation Read(this DuneLocationEntity entity, ReadConversionCollector collector) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (collector == null) { throw new ArgumentNullException(nameof(collector)); } if (collector.Contains(entity)) { return(collector.Get(entity)); } var duneLocation = new DuneLocation(entity.LocationId, entity.Name, new Point2D(entity.LocationX.ToNullAsNaN(), entity.LocationY.ToNullAsNaN()), new DuneLocation.ConstructionProperties { CoastalAreaId = entity.CoastalAreaId, Offset = entity.Offset.ToNullAsNaN(), Orientation = entity.Orientation.ToNullAsNaN(), D50 = entity.D50.ToNullAsNaN() }); collector.Read(entity, duneLocation); return(duneLocation); }
public void Read_EntityWithValidValues_SetsCalculationsWithExpectedValues() { // Setup var random = new Random(21); var duneLocationEntityOne = new DuneLocationEntity(); var calculationEntityWithoutOutput = new DuneLocationCalculationEntity { DuneLocationEntity = duneLocationEntityOne }; var duneLocationEntityTwo = new DuneLocationEntity(); var calculationEntityWithOutput = new DuneLocationCalculationEntity { DuneLocationEntity = duneLocationEntityTwo, DuneLocationCalculationOutputEntities = { new DuneLocationCalculationOutputEntity() } }; var collectionEntity = new DuneLocationCalculationForTargetProbabilityCollectionEntity { TargetProbability = random.NextDouble(0, 0.1), DuneLocationCalculationEntities = { calculationEntityWithoutOutput, calculationEntityWithOutput } }; var duneLocationOne = new TestDuneLocation("1"); var duneLocationTwo = new TestDuneLocation("2"); var collector = new ReadConversionCollector(); collector.Read(duneLocationEntityOne, duneLocationOne); collector.Read(duneLocationEntityTwo, duneLocationTwo); // Call DuneLocationCalculationsForTargetProbability calculations = collectionEntity.Read(collector); // Assert Assert.AreEqual(collectionEntity.TargetProbability, calculations.TargetProbability); IEnumerable <DuneLocationCalculation> duneLocationCalculations = calculations.DuneLocationCalculations; Assert.AreEqual(collectionEntity.DuneLocationCalculationEntities.Count, duneLocationCalculations.Count()); DuneLocationCalculation calculationOne = duneLocationCalculations.ElementAt(0); Assert.AreSame(collector.Get(duneLocationEntityOne), calculationOne.DuneLocation); Assert.IsNull(calculationOne.Output); DuneLocationCalculation calculationTwo = duneLocationCalculations.ElementAt(1); Assert.AreSame(collector.Get(duneLocationEntityTwo), calculationTwo.DuneLocation); Assert.IsNotNull(calculationTwo.Output); }
public void Read_CollectorIsNull_ThrowArgumentNullException() { // Setup var entity = new DuneLocationEntity(); // Call TestDelegate call = () => entity.Read(null); // Assert string paramName = Assert.Throws <ArgumentNullException>(call).ParamName; Assert.AreEqual("collector", paramName); }
public void Create_DuneLocationSavedMultipleTimes_ReturnSameEntity() { // Setup var location = new TestDuneLocation(); var registry = new PersistenceRegistry(); // Call DuneLocationEntity entity1 = location.Create(registry, 0); DuneLocationEntity entity2 = location.Create(registry, 1); // Assert Assert.AreSame(entity1, entity2); }
public void Create_StringPropertiesDoNotShareReferences() { // Setup const string testName = "original name"; var location = new DuneLocation(1, testName, new Point2D(0, 0), new DuneLocation.ConstructionProperties()); var registry = new PersistenceRegistry(); // Call DuneLocationEntity entity = location.Create(registry, 0); // Assert Assert.IsNotNull(entity); Assert.AreNotSame(testName, entity.Name, "To create stable binary representations/fingerprints, it's really important that strings are not shared."); Assert.AreEqual(testName, entity.Name); }
public void Read_SameDuneLocationEntityTwice_ReturnSameDuneLocation() { // Setup var entity = new DuneLocationEntity { Name = "A" }; var collector = new ReadConversionCollector(); // Call DuneLocation location1 = entity.Read(collector); DuneLocation location2 = entity.Read(collector); // Assert Assert.AreSame(location1, location2); }
public void Create_CalculationWithoutOutput_ReturnsDuneLocationCalculationEntity() { // Setup var duneLocation = new TestDuneLocation(); var calculation = new DuneLocationCalculation(duneLocation); var registry = new PersistenceRegistry(); var duneLocationEntity = new DuneLocationEntity(); registry.Register(duneLocationEntity, duneLocation); // Call DuneLocationCalculationEntity entity = calculation.Create(registry); // Assert Assert.IsNotNull(entity); CollectionAssert.IsEmpty(entity.DuneLocationCalculationOutputEntities); }
public void Create_CalculationWithAlreadyRegisteredDuneLocation_ReturnsEntityWithDuneLocationEntity() { // Setup var duneLocation = new TestDuneLocation(); var calculation = new DuneLocationCalculation(duneLocation); var registry = new PersistenceRegistry(); var duneLocationEntity = new DuneLocationEntity(); registry.Register(duneLocationEntity, duneLocation); // Call DuneLocationCalculationEntity entity = calculation.Create(registry); // Assert Assert.IsNotNull(entity); Assert.AreSame(duneLocationEntity, entity.DuneLocationEntity); }
public void Create_CalculationWithOutput_ReturnsDuneLocationCalculationEntityWithOutput() { // Setup var random = new Random(21); var output = new DuneLocationCalculationOutput(random.NextEnumValue <CalculationConvergence>(), new DuneLocationCalculationOutput.ConstructionProperties { WaterLevel = random.NextDouble(), WaveHeight = random.NextDouble(), WavePeriod = random.NextDouble(), CalculatedProbability = random.NextDouble(), CalculatedReliability = random.NextDouble(), TargetProbability = random.NextDouble(), TargetReliability = random.NextDouble() }); var duneLocation = new TestDuneLocation(); var calculation = new DuneLocationCalculation(duneLocation) { Output = output }; var registry = new PersistenceRegistry(); var duneLocationEntity = new DuneLocationEntity(); registry.Register(duneLocationEntity, duneLocation); // Call DuneLocationCalculationEntity entity = calculation.Create(registry); // Assert Assert.IsNotNull(entity); DuneLocationCalculationOutputEntity outputEntity = entity.DuneLocationCalculationOutputEntities.Single(); Assert.AreEqual(output.WaterLevel, outputEntity.WaterLevel, output.WaterLevel.GetAccuracy()); Assert.AreEqual(output.WaveHeight, outputEntity.WaveHeight, output.WaveHeight.GetAccuracy()); Assert.AreEqual(output.WavePeriod, outputEntity.WavePeriod, output.WavePeriod.GetAccuracy()); Assert.AreEqual(output.TargetProbability, outputEntity.TargetProbability); Assert.AreEqual(output.TargetReliability, outputEntity.TargetReliability, output.TargetReliability.GetAccuracy()); Assert.AreEqual(output.CalculatedProbability, outputEntity.CalculatedProbability); Assert.AreEqual(output.CalculatedReliability, outputEntity.CalculatedReliability, output.CalculatedReliability.GetAccuracy()); Assert.AreEqual(Convert.ToByte(output.CalculationConvergence), outputEntity.CalculationConvergence); }
public void Read_WithValidData_ReturnsDuneLocationWithPropertiesSetAndEntityRegistered() { // Setup const string testName = "testName"; var random = new Random(21); long locationId = random.Next(0, 400); double x = random.NextDouble(); double y = random.NextDouble(); int coastalAreaId = random.Next(); double offset = random.NextDouble(); double orientation = random.NextDouble(); double d50 = random.NextDouble(); var entity = new DuneLocationEntity { LocationId = locationId, Name = testName, LocationX = x, LocationY = y, CoastalAreaId = coastalAreaId, Offset = offset, Orientation = orientation, D50 = d50 }; var collector = new ReadConversionCollector(); // Call DuneLocation location = entity.Read(collector); // Assert Assert.IsNotNull(location); Assert.AreEqual(locationId, location.Id); Assert.AreEqual(testName, location.Name); Assert.AreEqual(x, location.Location.X, 1e-6); Assert.AreEqual(y, location.Location.Y, 1e-6); Assert.AreEqual(coastalAreaId, location.CoastalAreaId); Assert.AreEqual(offset, location.Offset, location.Offset.GetAccuracy()); Assert.AreEqual(orientation, location.Orientation, location.Orientation.GetAccuracy()); Assert.AreEqual(d50, location.D50, location.D50.GetAccuracy()); Assert.IsTrue(collector.Contains(entity)); }
public void Read_WithNullData_ReturnsDuneLocationWithNaNPropertiesSet() { // Setup const string testName = "testName"; var random = new Random(22); long locationId = random.Next(0, 400); int coastalAreaId = random.Next(); var entity = new DuneLocationEntity { LocationId = locationId, Name = testName, LocationX = null, LocationY = null, CoastalAreaId = coastalAreaId, Offset = null, Orientation = null, D50 = null }; var collector = new ReadConversionCollector(); // Call DuneLocation location = entity.Read(collector); // Assert Assert.IsNotNull(location); Assert.AreEqual(locationId, location.Id); Assert.AreEqual(testName, location.Name); Assert.IsNaN(location.Location.X); Assert.IsNaN(location.Location.Y); Assert.AreEqual(coastalAreaId, location.CoastalAreaId); Assert.IsNaN(location.Offset); Assert.IsNaN(location.Orientation); Assert.IsNaN(location.D50); Assert.IsTrue(collector.Contains(entity)); }
/// <summary> /// Registers a create operation for <paramref name="model"/> and the <paramref name="entity"/> /// that was constructed with the information. /// </summary> /// <param name="entity">The <see cref="DuneLocationEntity"/> to be registered.</param> /// <param name="model">The <see cref="DuneLocation"/> to be registered.</param> /// <exception cref="ArgumentNullException">Thrown when any of the input parameters is <c>null</c>.</exception> internal void Register(DuneLocationEntity entity, DuneLocation model) { Register(duneLocations, entity, model); }