Example #1
0
        public void ProvidedAnExistingEntityWithACollectionOfEntitiesProjectionUpdatesEntityGraph_UpdatedEntity_NoException()
        {
            SimpleEntityWithCollection entity = new SimpleEntityWithCollection();
            entity.Id = 1;
            entity.Name = "I have a collection";
            entity.EntityList = new List<BasicEntity>()
            {
                new BasicEntity()
                {
                    Id = 2,
                    Name = "I am inside a collection"
                },
                new BasicEntity()
                {
                    Id = 3,
                    Name = "I am inside a collection too"
                },
                new BasicEntity()
                {
                    Id = 5,
                    Name = "I will disappear"
                }
            };

            SimpleProjectionDifferentTypesWithCollection projection = entity.Project().To<SimpleProjectionDifferentTypesWithCollection>();

            projection.Name = "I got changed";

            projection.EntityList.ElementAt(index: 0).Name = "I also got changed";
            projection.EntityList.ElementAt(index: 1).Name = "I got changed as the other two did";
            projection.EntityList.Remove(projection.EntityList.ElementAt(index: 2));
            projection.EntityList.Add(new BasicEntityProjection()
            {
                Id = 4,
                Name = "I am completely new"
            });
            projection.Project().To<SimpleEntityWithCollection>(entity);

            Assert.IsTrue(entity.Id.Equals(projection.Id), message: "Id should be exactly the same");
            Assert.IsTrue(entity.Name == projection.Name, message: "Properties should be exactly the same");
            Assert.IsTrue(entity.EntityList != null, message: "Collection should be respected as it existed before");
            Assert.IsTrue(entity.EntityList.Count() == projection.EntityList.Count(), message: "Collection should have same number of items");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 0).Id == projection.EntityList.ElementAt(index: 0).Id, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 0).Name == projection.EntityList.ElementAt(index: 0).Name, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 1).Id == projection.EntityList.ElementAt(index: 1).Id, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 1).Name == projection.EntityList.ElementAt(index: 1).Name, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 2).Id == projection.EntityList.ElementAt(index: 2).Id, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 2).Name == projection.EntityList.ElementAt(index: 2).Name, message: "Items inside the collection should be exactly the same");
        }
Example #2
0
        public void ProvidedAnEntityWithACollectionOfEntitiesAndAMatchingProjectionButNullCollectionProjectWorksBothWays_ProjectedEntity_NoException()
        {
            SimpleEntityWithCollection entity = new SimpleEntityWithCollection();
            entity.Id = 1;
            entity.Name = "I have a collection";
            entity.EntityList = null;

            SimpleProjectionWithCollection projection = entity.Project().To<SimpleProjectionWithCollection>();

            SimpleEntityWithCollection entityToProjectOver = projection.Project().To<SimpleEntityWithCollection>();

            Assert.IsTrue(entity.Id.Equals(entityToProjectOver.Id), message: "Id should be exactly the same");
            Assert.IsTrue(entity.Name == entityToProjectOver.Name, message: "Properties should be exactly the same");
            Assert.IsTrue(entityToProjectOver.EntityList == null, message: "Collection should be null as it was in source");
        }
Example #3
0
        public void ProvidedAnExistingEntityWithACollectionOfEntitiesWeCanManuallyUpdateEntityGraph_UpdatedEntity_NoException()
        {
            SimpleEntityWithCollection entity = new SimpleEntityWithCollection();
            entity.Id = 1;
            entity.Name = "I have a collection";
            entity.EntityList = new List<BasicEntity>()
            {
                new BasicEntity()
                {
                    Id = 2,
                    Name = "I am inside a collection"
                },
                new BasicEntity()
                {
                    Id = 3,
                    Name = "I am inside a collection too"
                },
                new BasicEntity()
                {
                    Id = 5,
                    Name = "I will disappear"
                }
            };

            SimpleProjectionDifferentTypesWithCollection projection = entity.Project().To<SimpleProjectionDifferentTypesWithCollection>();

            projection.Name = "I got changed";
            projection.EntityList.ElementAt(index: 0).Name = "I also got changed";
            projection.EntityList.ElementAt(index: 1).Name = "I got changed as the other two did";
            projection.EntityList.Remove(projection.EntityList.ElementAt(index: 2));
            projection.EntityList.Add(new BasicEntityProjection()
            {
                Id = 4,
                Name = "I am completely new"
            });

            entity.Id = projection.Id;
            entity.Name = projection.Name;

            ICollection<BasicEntityProjection> source = projection.EntityList;
            ICollection<BasicEntity> destination = entity.EntityList;

            List<BasicEntity> toRemove = destination.Where(d => !source.Select(s => s.Id).Contains(d.Id))
                                      .ToList();
            toRemove.ForEach(r => destination.Remove(r));

            IEnumerable<BasicEntityProjection> toAdd = source.Where(s => !destination.Select(d => d.Id).Contains(s.Id));
            toAdd.ForEach(a => destination.Add(a.Project().To<BasicEntity>()));

            source.Except(toAdd)
                    .ForEach(u => u.Project()
                                    .To<BasicEntity>(destination.
                                                        FirstOrDefault(d => d.Id == u.Id)));

            Assert.IsTrue(entity.Id.Equals(projection.Id), message: "Id should be exactly the same");
            Assert.IsTrue(entity.Name == projection.Name, message: "Properties should be exactly the same");
            Assert.IsTrue(entity.EntityList != null, message: "Collection should be respected as it existed before");
            Assert.IsTrue(entity.EntityList.Count() == projection.EntityList.Count(), message: "Collection should have same number of items");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 0).Id == projection.EntityList.ElementAt(index: 0).Id, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 0).Name == projection.EntityList.ElementAt(index: 0).Name, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 1).Id == projection.EntityList.ElementAt(index: 1).Id, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 1).Name == projection.EntityList.ElementAt(index: 1).Name, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 2).Id == projection.EntityList.ElementAt(index: 2).Id, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 2).Name == projection.EntityList.ElementAt(index: 2).Name, message: "Items inside the collection should be exactly the same");
        }
Example #4
0
        public void ProvidedAnEntityWithACollectionOfEntitiesAndAMatchingProjectionWithDifferentTypesProjectWorksBothWays_ProjectedEntity_NoException()
        {
            SimpleEntityWithCollection entity = new SimpleEntityWithCollection();
            entity.Id = 1;
            entity.Name = "I have a collection";
            entity.EntityList = new List<BasicEntity>()
            {
                new BasicEntity()
                {
                    Id = 2,
                    Name = "I am inside a collection"
                },
                new BasicEntity()
                {
                    Id = 3,
                    Name = "I am inside a collection too"
                }
            };

            SimpleProjectionDifferentTypesWithCollection projection = entity.Project().To<SimpleProjectionDifferentTypesWithCollection>();

            SimpleEntityWithCollection entityToProjectOver = projection.Project().To<SimpleEntityWithCollection>();

            Assert.IsTrue(entity.Id.Equals(entityToProjectOver.Id), message: "Id should be exactly the same");
            Assert.IsTrue(entity.Name == entityToProjectOver.Name, message: "Properties should be exactly the same");
            Assert.IsTrue(entityToProjectOver.EntityList != null, message: "Collection should have been created");
            Assert.IsTrue(entity.EntityList.Count() == entityToProjectOver.EntityList.Count(), message: "Collection should have same number of items");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 0).Id == entityToProjectOver.EntityList.ElementAt(index: 0).Id, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 0).Name == entityToProjectOver.EntityList.ElementAt(index: 0).Name, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 1).Id == entityToProjectOver.EntityList.ElementAt(index: 1).Id, message: "Items inside the collection should be exactly the same");
            Assert.IsTrue(entity.EntityList.ElementAt(index: 1).Name == entityToProjectOver.EntityList.ElementAt(index: 1).Name, message: "Items inside the collection should be exactly the same");
        }