コード例 #1
0
        public void Create_WithCollectorAndLayers_ReturnsPipingSoilProfileEntityWithPropertiesAndPipingSoilLayerEntitiesSet()
        {
            // Setup
            var random = new Random(21);

            const string testName = "testName";
            double       bottom   = random.NextDouble();
            var          layers   = new[]
            {
                new PipingSoilLayer(bottom + 1),
                new PipingSoilLayer(bottom + 2)
            };
            var soilProfileType = random.NextEnumValue <SoilProfileType>();
            var soilProfile     = new PipingSoilProfile(testName, bottom, layers, soilProfileType);
            var registry        = new PersistenceRegistry();

            // Call
            PipingSoilProfileEntity entity = soilProfile.Create(registry);

            // Assert
            Assert.IsNotNull(entity);
            Assert.AreEqual(bottom, entity.Bottom);
            Assert.AreEqual(Convert.ToByte(soilProfileType), entity.SourceType);
            Assert.AreEqual(testName, entity.Name);
            Assert.AreEqual(2, entity.PipingSoilLayerEntities.Count);
        }
コード例 #2
0
        /// <summary>
        /// Reads the <see cref="PipingSoilProfileEntity"/> and use the information to construct a <see cref="PipingSoilProfile"/>.
        /// </summary>
        /// <param name="entity">The <see cref="PipingSoilProfileEntity"/> to create <see cref="PipingSoilProfile"/> for.</param>
        /// <param name="collector">The object keeping track of read operations.</param>
        /// <returns>A new <see cref="PipingSoilProfile"/> or one from the <paramref name="collector"/> if the
        /// <see cref="PipingSoilProfileEntity"/> has been read before.</returns>
        /// <exception cref="ArgumentNullException">Thrown when any input parameter is <c>null</c>.</exception>
        public static PipingSoilProfile Read(this PipingSoilProfileEntity 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));
            }

            IEnumerable <PipingSoilLayer> layers = entity.PipingSoilLayerEntities.OrderBy(sl => sl.Order)
                                                   .Select(sl => sl.Read())
                                                   .ToArray();
            var pipingSoilProfile = new PipingSoilProfile(entity.Name,
                                                          entity.Bottom.ToNullAsNaN(),
                                                          layers,
                                                          (SoilProfileType)entity.SourceType);

            collector.Read(entity, pipingSoilProfile);
            return(pipingSoilProfile);
        }
コード例 #3
0
        public void GivenReadObject_WhenReadCalledOnSameEntity_ThenSameObjectInstanceReturned()
        {
            // Given
            var random = new Random(21);
            var entity = new PipingSoilProfileEntity
            {
                Name       = "testName",
                Bottom     = random.NextDouble(),
                SourceType = Convert.ToByte(random.NextEnumValue <SoilProfileType>()),
                PipingSoilLayerEntities =
                {
                    new PipingSoilLayerEntity
                    {
                        Top = random.NextDouble() + 1
                    }
                }
            };
            var collector = new ReadConversionCollector();

            PipingSoilProfile profile = entity.Read(collector);

            // When
            PipingSoilProfile secondProfile = entity.Read(collector);

            // Then
            Assert.AreSame(profile, secondProfile);
        }
コード例 #4
0
        public void Read_WithNullValues_ReturnsPipingSoilProfileWithNaNValues()
        {
            // Setup
            var entity = new PipingSoilProfileEntity
            {
                Name = nameof(PipingSoilProfileEntity),
                PipingSoilLayerEntities =
                {
                    new PipingSoilLayerEntity
                    {
                        MaterialName = nameof(PipingSoilLayerEntity)
                    }
                }
            };
            var collector = new ReadConversionCollector();

            // Call
            PipingSoilProfile profile = entity.Read(collector);

            // Assert
            Assert.IsNotNull(profile);
            Assert.AreEqual(entity.Name, profile.Name);
            Assert.IsNaN(profile.Bottom);
            Assert.AreEqual(entity.PipingSoilLayerEntities.Count, profile.Layers.Count());

            PipingSoilLayer layer = profile.Layers.ElementAt(0);

            Assert.AreEqual(entity.PipingSoilLayerEntities.First().MaterialName, layer.MaterialName);
        }
コード例 #5
0
        public void Read_DifferentStochasticSoilProfileEntitiesWithSameSoilProfileEntity_ReturnsStochasticSoilProfilesWithSamePipingSoilProfile()
        {
            // Setup
            var    random            = new Random(21);
            double probability       = random.NextDouble();
            var    soilProfileEntity = new PipingSoilProfileEntity
            {
                Name       = "StochasticSoilProfile",
                SourceType = Convert.ToByte(random.NextEnumValue <SoilProfileType>()),
                PipingSoilLayerEntities =
                {
                    new PipingSoilLayerEntity()
                }
            };
            var firstEntity = new PipingStochasticSoilProfileEntity
            {
                Probability             = probability,
                PipingSoilProfileEntity = soilProfileEntity
            };
            var secondEntity = new PipingStochasticSoilProfileEntity
            {
                Probability             = 1 - probability,
                PipingSoilProfileEntity = soilProfileEntity
            };
            var collector = new ReadConversionCollector();

            PipingStochasticSoilProfile firstProfile = firstEntity.Read(collector);

            // Call
            PipingStochasticSoilProfile secondProfile = secondEntity.Read(collector);

            // Assert
            Assert.AreNotSame(firstProfile, secondProfile);
            Assert.AreSame(firstProfile.SoilProfile, secondProfile.SoilProfile);
        }
コード例 #6
0
        /// <summary>
        /// Creates a <see cref="PipingSoilProfileEntity"/> based on the information of the <see cref="PipingSoilProfile"/>.
        /// </summary>
        /// <param name="profile">The profile to create a database entity for.</param>
        /// <param name="registry">The object keeping track of create operations.</param>
        /// <returns>A new <see cref="PipingSoilProfileEntity"/> or one from the <paramref name="registry"/>
        /// if it was created for the <see cref="profile"/> earlier.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="registry"/> is <c>null</c>.</exception>
        internal static PipingSoilProfileEntity Create(this PipingSoilProfile profile,
                                                       PersistenceRegistry registry)
        {
            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }

            if (registry.Contains(profile))
            {
                return(registry.Get(profile));
            }

            var entity = new PipingSoilProfileEntity
            {
                Name       = profile.Name.DeepClone(),
                Bottom     = profile.Bottom.ToNaNAsNull(),
                SourceType = Convert.ToByte(profile.SoilProfileSourceType)
            };

            AddEntitiesForPipingSoilLayers(profile, entity);

            registry.Register(entity, profile);
            return(entity);
        }
コード例 #7
0
        private static void AddEntitiesForPipingSoilLayers(PipingSoilProfile profile,
                                                           PipingSoilProfileEntity entity)
        {
            var index = 0;

            foreach (PipingSoilLayer pipingSoilLayer in profile.Layers)
            {
                entity.PipingSoilLayerEntities.Add(pipingSoilLayer.Create(index++));
            }
        }
コード例 #8
0
        public void Read_CollectorNull_ThrowsArgumentNullException()
        {
            // Setup
            var entity = new PipingSoilProfileEntity();

            // Call
            TestDelegate test = () => entity.Read(null);

            // Assert
            string parameter = Assert.Throws <ArgumentNullException>(test).ParamName;

            Assert.AreEqual("collector", parameter);
        }
コード例 #9
0
        public void Create_ForTheSameEntityTwice_ReturnsSamePipingSoilProfileEntityInstance()
        {
            // Setup
            PipingSoilProfile soilProfile = PipingSoilProfileTestFactory.CreatePipingSoilProfile();
            var registry = new PersistenceRegistry();

            PipingSoilProfileEntity firstEntity = soilProfile.Create(registry);

            // Call
            PipingSoilProfileEntity secondEntity = soilProfile.Create(registry);

            // Assert
            Assert.AreSame(firstEntity, secondEntity);
        }
コード例 #10
0
        public void Create_StringPropertiesDoNotShareReference()
        {
            // Setup
            var          random   = new Random(21);
            const string testName = "testName";
            var          layers   = new[]
            {
                new PipingSoilLayer(1),
                new PipingSoilLayer(2)
            };
            var soilProfile = new PipingSoilProfile(testName, 0, layers, random.NextEnumValue <SoilProfileType>());
            var registry    = new PersistenceRegistry();

            // Call
            PipingSoilProfileEntity entity = soilProfile.Create(registry);

            // Assert
            TestHelper.AssertAreEqualButNotSame(testName, entity.Name);
        }
コード例 #11
0
        public void Read_WithCollector_ReturnsNewPipingSoilProfileWithPropertiesSet()
        {
            // Setup
            var    random     = new Random(21);
            double bottom     = random.NextDouble();
            var    sourceType = random.NextEnumValue <SoilProfileType>();
            var    entity     = new PipingSoilProfileEntity
            {
                Name       = "testName",
                Bottom     = bottom,
                SourceType = Convert.ToByte(sourceType),
                PipingSoilLayerEntities =
                {
                    new PipingSoilLayerEntity
                    {
                        Top          = bottom + 0.5,
                        MaterialName = "A",
                        Order        = 1
                    },
                    new PipingSoilLayerEntity
                    {
                        Top          = bottom + 1.2,
                        MaterialName = "B",
                        Order        = 0
                    }
                }
            };
            var collector = new ReadConversionCollector();

            // Call
            PipingSoilProfile profile = entity.Read(collector);

            // Assert
            Assert.IsNotNull(profile);
            Assert.AreEqual(entity.Name, profile.Name);
            Assert.AreEqual(bottom, profile.Bottom, 1e-6);
            Assert.AreEqual(sourceType, profile.SoilProfileSourceType);
            CollectionAssert.AreEqual(new[]
            {
                "B",
                "A"
            }, profile.Layers.Select(l => l.MaterialName));
        }
コード例 #12
0
 /// <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="PipingSoilProfileEntity"/> to be registered.</param>
 /// <param name="model">The <see cref="PipingSoilProfile"/> to be registered.</param>
 /// <exception cref="ArgumentNullException">Thrown when any of the input parameters is <c>null</c>.</exception>
 internal void Register(PipingSoilProfileEntity entity, PipingSoilProfile model)
 {
     Register(pipingSoilProfiles, entity, model);
 }