예제 #1
0
        public void ToEntity_WithNew()
        {
            var mock = new MockModel()
            {
                Field1       = Guid.NewGuid(),
                Field2       = 1,
                Field3       = "field3",
                RelatedMock  = new MockRelatedModel(),
                RelatedMocks = new List <MockRelatedModel>()
                {
                    new MockRelatedModel()
                },
            };

            var mockEntity        = new Entity("mock");
            var relatedMockEntity = new Entity("mock")
            {
                EntityState = EntityState.Created
            };

            _mockService.Setup(m => m.NewEntity("mock", _context)).Returns(mockEntity);
            _mockService.Setup(m => m.NewEntity("relatedMock", _context)).Returns(relatedMockEntity);

            mock.ToEntity(_crm, _context);

            mockEntity.GetAttributeValue <EntityReference>("dfe_field1").Id.Should().Be((Guid)mock.Field1);
            mockEntity.GetAttributeValue <EntityReference>("dfe_field1").LogicalName.Should().Be("dfe_list");
            mockEntity.GetAttributeValue <OptionSetValue>("dfe_field2").Value.Should().Be(mock.Field2);
            mockEntity.GetAttributeValue <string>("dfe_field3").Should().Be(mock.Field3);

            _mockService.Verify(m => m.AddLink(mockEntity, new Relationship("dfe_mock_dfe_relatedmock_mock"),
                                               relatedMockEntity, _context));
            _mockService.Verify(m => m.AddLink(mockEntity, new Relationship("dfe_mock_dfe_relatedmock_mocks"),
                                               relatedMockEntity, _context));
        }
예제 #2
0
        public void ToEntity_DoesNotWriteNullRelationships()
        {
            var mock = new MockModel()
            {
                Id = Guid.NewGuid()
            };

            var mockEntity = new Entity("mock", (Guid)mock.Id);

            _mockService.Setup(m => m.BlankExistingEntity("mock", mockEntity.Id, _context)).Returns(mockEntity);
            _mockService.Setup(m => m.NewEntity("relatedMock", _context)).Returns <Entity>(null);

            mock.ToEntity(_crm, _context);

            _mockService.Verify(m => m.AddLink(mockEntity, It.IsAny <Relationship>(), It.IsAny <Entity>(), _context), Times.Never());
        }
예제 #3
0
        public void ToEntity_DoesNotWriteUnchangedProperties()
        {
            var mock = new MockModel()
            {
                Id = Guid.NewGuid(), Field1 = null, Field3 = "new value"
            };

            var mockEntity = new Entity("mock", (Guid)mock.Id);

            _mockService.Setup(m => m.BlankExistingEntity("mock", mockEntity.Id, _context)).Returns(mockEntity);

            mock.ToEntity(_crm, _context);

            var numberOfChangedAttributes = mockEntity.Attributes.Count();

            numberOfChangedAttributes.Should().Be(2);
        }
        public void ToEntity_WithNewThatHasExistingRelationships()
        {
            var mock = new MockModel()
            {
                RelatedMock = new MockRelatedModel(),
            };

            var mockEntity        = new Entity("mock");
            var relatedMockEntity = new Entity("mock");

            _mockService.Setup(m => m.NewEntity("mock", _context)).Returns(mockEntity);
            _mockService.Setup(m => m.NewEntity("relatedMock", _context)).Returns(relatedMockEntity);

            mock.ToEntity(_crm, _context);

            _mockService.Verify(m => m.AddLink(mockEntity, new Relationship("dfe_mock_dfe_relatedmock_mock"),
                                               relatedMockEntity, _context), Times.Never);
        }
예제 #5
0
        public void ToEntity_WhenRelatedModelToEntityReturnsNull_DoesNotWriteNullRelationship()
        {
            var relatedMock = new Mock <MockRelatedModel>();
            var mock        = new MockModel()
            {
                RelatedMock = relatedMock.Object,
            };

            var mockEntity        = new Entity("mock");
            var relatedMockEntity = new Entity("mock");

            _mockService.Setup(m => m.NewEntity("mock", _context)).Returns(mockEntity);
            _mockService.Setup(m => m.NewEntity("relatedMock", _context)).Returns(relatedMockEntity);
            relatedMock.Setup(m => m.ToEntity(_crm, _context)).Returns <Entity>(null);

            mock.ToEntity(_crm, _context);

            _mockService.Verify(m => m.AddLink(mockEntity, new Relationship("dfe_mock_dfe_relatedmock_mock"),
                                               relatedMockEntity, _context), Times.Never);
        }
예제 #6
0
        public void ToEntity_WithNewThatHasExistingRelationships()
        {
            var mock = new MockModel()
            {
                RelatedMock = new MockRelatedModel(),
            };

            var mockEntity        = new Entity("mock");
            var relatedMockEntity = new Entity("mock");

            _mockService.Setup(m => m.NewEntity("mock", _context)).Returns(mockEntity);
            _mockService.Setup(m => m.NewEntity("relatedMock", _context)).Returns(relatedMockEntity);

            mock.ToEntity(_crm, _context);

            // Ensure a pre-existing related entity is linked (if it is already linked to the entity this
            // will have no effect, which is desirable).
            _mockService.Verify(m => m.AddLink(mockEntity, new Relationship("dfe_mock_dfe_relatedmock_mock"),
                                               relatedMockEntity, _context), Times.Once);
        }
예제 #7
0
        public void ToEntity_WritesNullProperties()
        {
            var mock = new MockModel()
            {
                Id     = Guid.NewGuid(),
                Field1 = null,
                Field2 = null,
                Field3 = null,
            };

            var mockEntity = new Entity("mock", (Guid)mock.Id);

            _mockService.Setup(m => m.BlankExistingEntity("mock", mockEntity.Id, _context)).Returns(mockEntity);

            mock.ToEntity(_crm, _context);

            mockEntity.GetAttributeValue <EntityReference>("dfe_field1").Should().BeNull();
            mockEntity.GetAttributeValue <OptionSetValue>("dfe_field2").Should().BeNull();
            mockEntity.GetAttributeValue <string>("dfe_field3").Should().BeNull();

            var numberOfChangedAttributes = mockEntity.Attributes.Count();

            numberOfChangedAttributes.Should().Be(3);
        }