Пример #1
0
        private void CreateMocksWithTestSetup()
        {
            CreateMocks();

            ConnectorRepositoryMock
            .Setup(repository => repository.GetSingle(connector => connector.ConnectorID == TestConnectorID))
            .Returns(new Connector
            {
                ConnectorID       = TestConnectorID,
                ConnectorSystemID = TestConnectorSystemID
            });
            ConnectorProductAttributeRepositoryMock
            .Setup(repository => repository.GetMultiple(It.IsAny <Int32?>(), It.IsAny <Int32?>(), It.IsAny <Boolean?>()))
            .Returns(Enumerable.Empty <ConnectorProductAttribute>());
            ConnectorProductAttributeRepositoryMock
            .Setup(repository => repository.GetSingle(It.IsAny <Int32>(), It.IsAny <Int32>(), It.IsAny <Boolean>()))
            .Returns(default(ConnectorProductAttribute));

            ConnectorProductAttributeSettingRepositoryMock
            .Setup(repository => repository.GetMultiple(TestConnectorProductAttributeID, null))
            .Returns(Enumerable.Empty <ConnectorProductAttributeSetting>());

            ConnectorProductAttributeSettingTemplateRepositoryMock
            .Setup(repository => repository.GetMultiple(TestConnectorSystemID, null))
            .Returns(Enumerable.Empty <ConnectorProductAttributeSettingTemplate>());

            ProductAttributeRepositoryMock
            .Setup(repository => repository.GetSingle(attribute => attribute.AttributeID == TestProductAttributeID))
            .Returns(new ProductAttributeMetaData
            {
                AttributeCode = TestProductAttributeCode,
                AttributeID   = TestProductAttributeID
            });
        }
Пример #2
0
        public void CreateMappingWithSettingsWithSettingTemplates()
        {
            CreateMocksWithTestSetup();

            var defaultValue = 42;
            var isFilter     = false;

            var connectorProductAttribute = new ConnectorProductAttribute
            {
                ConnectorID = TestConnectorID,
                ConnectorProductAttributeID = TestConnectorProductAttributeID,
                DefaultValue         = defaultValue.ToString(),
                IsFilter             = isFilter,
                ProductAttributeID   = TestProductAttributeID,
                ProductAttributeType = defaultValue.GetType().FullName
            };

            ConnectorProductAttributeRepositoryMock
            .Setup(repository => repository.GetSingle(It.IsAny <Int32>(), It.IsAny <Int32>(), It.IsAny <Boolean>()))
            .Returns(connectorProductAttribute);

            ConnectorProductAttributeRepositoryMock
            .Setup(repository => repository.GetMultiple(It.IsAny <Int32?>(), It.IsAny <Int32?>(), It.IsAny <Boolean?>()))
            .Returns(new[]
            {
                connectorProductAttribute
            });

            ConnectorProductAttributeRepositoryMock
            .Setup(repository => repository.Upsert(It.IsAny <ConnectorProductAttribute>()))
            .Returns(RepositoryOperationStatus.Created);

            var propertySettings = new Dictionary <String, Object>
            {
                { "Property 1", true },
                { "Property 2", true },
            };

            ConnectorProductAttributeSettingRepositoryMock
            .Setup(repository => repository.GetMultiple(null, null))
            .Returns(propertySettings.Select(propertySetting => new ConnectorProductAttributeSetting
            {
                ConnectorProductAttributeID = TestConnectorProductAttributeID,
                Code  = propertySetting.Key,
                Type  = propertySetting.Value.GetType().FullName,
                Value = propertySetting.Value.ToString()
            }));

            var propertySettingTemplates = new Dictionary <String, Object>
            {
                { "Property 2", false },
                { "Property 3", false }
            };

            ConnectorProductAttributeSettingTemplateRepositoryMock
            .Setup(repository => repository.GetMultiple(TestConnectorSystemID, null))
            .Returns(propertySettingTemplates.Select(propertySettingTemplate => new ConnectorProductAttributeSettingTemplate
            {
                ConnectorSystemID = TestConnectorSystemID,
                Code  = propertySettingTemplate.Key,
                Type  = propertySettingTemplate.Value.GetType().FullName,
                Value = propertySettingTemplate.Value.ToString()
            }));

            var finalProperties = new Dictionary <String, Object>(propertySettings);

            foreach (var propertySettingTemplate in propertySettingTemplates)
            {
                if (!finalProperties.ContainsKey(propertySettingTemplate.Key))
                {
                    finalProperties.Add(propertySettingTemplate.Key, propertySettingTemplate.Value);
                }
            }

            var connectorProductAttributeSettingList = new List <ConnectorProductAttributeSetting>();

            ConnectorProductAttributeSettingRepositoryMock
            .Setup(repository => repository.CreateOrUpdate(It.IsAny <ConnectorProductAttributeSetting>()))
            .Callback <ConnectorProductAttributeSetting>(setting => connectorProductAttributeSettingList.Add(setting));

            var mapping = new ConnectorProductAttributeMapping(propertySettings)
            {
                Connector = new Connector
                {
                    ConnectorID       = TestConnectorID,
                    ConnectorSystemID = TestConnectorSystemID
                },
                DefaultValue     = defaultValue,
                IsFilter         = isFilter,
                ProductAttribute = new ProductAttributeMetaData
                {
                    AttributeID = TestProductAttributeID
                },
                ProductAttributeType = defaultValue.GetType()
            };

            CreateService().Upsert(mapping);

            ConnectorProductAttributeSettingRepositoryMock.Verify(repository => repository.CreateOrUpdate(It.IsAny <ConnectorProductAttributeSetting>()), Times.AtLeast(finalProperties.Count));

            var connectorProductAttributeSettingDictionary = connectorProductAttributeSettingList.ToDictionary(setting => setting.Code);

            foreach (var finalProperty in finalProperties)
            {
                Assert.True(mapping.Properties.ContainsKey(finalProperty.Key));
                Assert.Equal(mapping.Properties[finalProperty.Key], finalProperty.Value);

                Assert.True(connectorProductAttributeSettingDictionary.ContainsKey(finalProperty.Key));
                Assert.Equal(connectorProductAttributeSettingDictionary[finalProperty.Key].Value, finalProperty.Value.ToString());
            }
        }