public async Task UpdateCustomAttribute_Should_Update_Custom_Attribute_With_Specified_Values() { var attribute = CustomAttribute.Create("attribute", "number"); var repositoryMock = new Mock <Repository.IRepository>(); repositoryMock.Setup(r => r.GetByKeyAsync <CustomAttribute>(It.IsAny <Guid>())) .Returns(Task.FromResult(attribute)); Repository.IRepository repository = repositoryMock.Object; Core.Infrastructure.IEventBus eventBus = new Mock <Core.Infrastructure.IEventBus>().Object; Guid attributeId = attribute.Id; string name = "Size"; string type = "string"; string description = "size description"; string unitOfMeasure = "unit"; IEnumerable <object> values = new[] { "XS", "S", "M", "L", "XL" }; var commands = new CustomAttributeCommands(repository, eventBus); await commands.UpdateCustomAttribute(attributeId, name, type, description, unitOfMeasure, values); Assert.Equal(name, attribute.Name); Assert.Equal(type, attribute.DataType); Assert.Equal(description, attribute.Description); Assert.Equal(unitOfMeasure, attribute.UnitOfMeasure); Assert.Equal(values, attribute.Values); }
/// <summary> /// Implementation of <see cref="ICustomAttributesCommands.CreateNewCustomAttribute(string, string, string, string, IEnumerable{object})"/> /// </summary> /// <param name="name">The custom attribute's name</param> /// <param name="type">The data type of the custom attribute</param> /// <param name="description">The custom attribute's description</param> /// <param name="unitOfMeasure">The unit of measure of the custom attribute</param> /// <param name="values">The available values for the custom attribute</param> /// <returns>The custom attribute id</returns> public async Task <Guid> CreateNewCustomAttribute(string name, string type, string description, string unitOfMeasure, IEnumerable <object> values) { try { var attribute = CustomAttribute.Create(name, type); if (!string.IsNullOrWhiteSpace(description)) { attribute.ChangeDescription(description); } if (!string.IsNullOrWhiteSpace(unitOfMeasure)) { attribute.SetUnitOfMeasure(unitOfMeasure); } if (values != null && values.Count() > 0) { values.ToList().ForEach(v => attribute.AddValue(v)); } Repository.Add(attribute); await Repository.SaveChangesAsync(); var @event = new CustomAttributeCreatedEvent(attribute.Id, attribute.Name, attribute.DataType); EventBus.RaiseEvent(@event); return(attribute.Id); } catch { throw; } }
public void CustomAttributeFactory_Should_Throw_ArgumentNullException_If_Type_IsEmpty(string value) { var ex = Assert.Throws <ArgumentNullException>(() => CustomAttribute.Create( "Attribute", value )); Assert.Equal("type", ex.ParamName); }
public void SetUnitOfMeasure_Should_Throw_ArgumentNullException_If_UnitOfMeasure_IsEmpty(string value) { var attribute = CustomAttribute.Create( "Attribute", "string" ); var ex = Assert.Throws <ArgumentNullException>(() => attribute.SetUnitOfMeasure(value)); Assert.Equal("unitOfMeasure", ex.ParamName); }
public void ChangeDataType_Should_Throw_ArgumentNullException_If_DataType_IsEmpty(string value) { var attribute = CustomAttribute.Create( "Attribute", "string" ); var ex = Assert.Throws <ArgumentNullException>(() => attribute.ChangeDataType(value)); Assert.Equal("dataType", ex.ParamName); }
public void Restore_Should_Throw_InvalidOperationException_If_Attribute_IsNotDeleted() { var attribute = CustomAttribute.Create( "Attribute", "string" ); var ex = Assert.Throws <InvalidOperationException>(() => attribute.Restore()); Assert.Equal("The attribute is not deleted", ex.Message); }
public void RemoveValue_Should_Throw_ArgumentNullException_If_Value_IsNull() { var attribute = CustomAttribute.Create( "Attribute", "string" ); var ex = Assert.Throws <ArgumentNullException>(() => attribute.RemoveValue(null)); Assert.Equal("value", ex.ParamName); }
public void ChangeDescription_Should_Clear_If_Description_IsEmpty(string value) { var attribute = CustomAttribute.Create( "Attribute", "string" ); attribute.ChangeDescription(value); Assert.True(string.IsNullOrWhiteSpace(attribute.Description)); }
public void RemoveValue_Should_Throw_InvalidOperationException_If_Values_IsNull() { var attribute = CustomAttribute.Create( "Attribute", "string" ); string value = "My value"; var ex = Assert.Throws <InvalidOperationException>(() => attribute.RemoveValue(value)); Assert.Equal("Cannot remove item from empty list", ex.Message); }
public void RemoveValue_Should_Throw_ArgumentException_If_Value_Is_Not_In_Collection() { var attribute = CustomAttribute.Create( "Attribute", "string" ); attribute.AddValue("first value"); string value = "My value"; var ex = Assert.Throws <ArgumentException>(() => attribute.RemoveValue(value)); Assert.Equal("value", ex.ParamName); }
public void DeleteAttribute_Should_Throw_InvalidOperationException_If_Attribute_DoesNot_Exist() { var product = Product.Create( "ean", "sku", "product", "my-product" ); var attribute = CustomAttribute.Create("attribute", "string"); var ex = Assert.Throws <InvalidOperationException>(() => product.DeleteAttribute(attribute)); Assert.Equal("Attribute not found", ex.Message); }
public void DeleteAttribute_Should_Remove_Attribute() { var product = Product.Create( "ean", "sku", "product", "my-product" ); product.AddAttribute(CustomAttribute.Create("attribute", "number"), "value"); var attribute = product.Attributes.First(); product.DeleteAttribute(attribute.Id); Assert.Equal(0, product.Attributes.Count(a => a.Id == attribute.Id)); }
public void AddValue_Should_Increment_Values_Number() { var attribute = CustomAttribute.Create( "Attribute", "string" ); int valuesCount = attribute.Values == null ? 0 : attribute.Values.Count(); string value = "My value"; attribute.AddValue(value); Assert.Equal(valuesCount + 1, attribute.Values.Count()); }
public void Active_Should_Return_Only_Attributes_Not_Deleted() { var a1 = CustomAttribute.Create("a1", "t1"); var a2 = CustomAttribute.Create("a2", "t2"); var a3 = CustomAttribute.Create("a3", "t3"); a3.Delete(); IQueryable <CustomAttribute> attributes = new CustomAttribute[] { a1, a2, a3 }.AsQueryable(); var activeAttributes = CustomAttributeExtensions.Active(attributes).ToArray(); Assert.True(activeAttributes.All(a => !a.Deleted)); }
public async Task DeleteCustomAttribute_Should_Mark_CustomAttribute_As_Deleted() { var attribute = CustomAttribute.Create("attribute", "number"); var repositoryMock = new Mock <Repository.IRepository>(); repositoryMock.Setup(r => r.GetByKeyAsync <CustomAttribute>(It.IsAny <Guid>())) .Returns(Task.FromResult(attribute)); Repository.IRepository repository = repositoryMock.Object; Core.Infrastructure.IEventBus eventBus = new Mock <Core.Infrastructure.IEventBus>().Object; Guid attributeId = attribute.Id; var commands = new CustomAttributeCommands(repository, eventBus); await commands.DeleteCustomAttribute(attributeId); Assert.True(attribute.Deleted); }
public void AddAttribute_Should_Get_Deserialized_Value() { var product = Product.Create( "ean", "sku", "product", "my-product" ); var value = DateTime.Now; product.AddAttribute(CustomAttribute.Create("attribute", "datetime"), value); var attribute = product.Attributes.FirstOrDefault(a => a.Attribute.DataType == "datetime"); Assert.NotNull(attribute); Assert.Equal(value, attribute.Value); }
public void ByProduct_Should_Throw_ArgumentException_If_ProductId_Is_Empty() { var product = Product.Create("ean", "sku", "name", "url"); IQueryable <ProductAttribute> attributes = new ProductAttribute[] { new ProductAttribute { Id = Guid.NewGuid(), Value = 123, Attribute = CustomAttribute.Create("a1", "t1"), Product = product }, new ProductAttribute { Id = Guid.NewGuid(), Value = 123, Attribute = CustomAttribute.Create("a2", "t2"), Product = product }, new ProductAttribute { Id = Guid.NewGuid(), Value = 123, Attribute = CustomAttribute.Create("a3", "t3"), Product = product } }.AsQueryable(); Guid productId = Guid.Empty; var ex = Assert.Throws <ArgumentException>(() => ProductAttributeExtensions.ByProduct(attributes, productId)); Assert.Equal(nameof(productId), ex.ParamName); }
public void ByProduct_Should_Return_Product_Attributes_With_The_Specified_Product() { var product = Product.Create("ean", "sku", "name", "url"); IQueryable <ProductAttribute> attributes = new ProductAttribute[] { new ProductAttribute { Id = Guid.NewGuid(), Value = 123, Attribute = CustomAttribute.Create("a1", "t1"), Product = product }, new ProductAttribute { Id = Guid.NewGuid(), Value = 123, Attribute = CustomAttribute.Create("a2", "t2"), Product = product }, new ProductAttribute { Id = Guid.NewGuid(), Value = 123, Attribute = CustomAttribute.Create("a3", "t3"), Product = Product.Create("ean1", "sku1", "name1", "url1") } }.AsQueryable(); Guid productId = product.Id; var attributesByProduct = ProductAttributeExtensions.ByProduct(attributes, productId).ToArray(); Assert.True(attributesByProduct.All(a => a.Product.Id == productId)); }
protected virtual void PrepareData() { var category = Category.Create("CAT01", "Category1", "category1"); var child = Category.Create("CHILD01", "Child1", "child1"); category.AddChild(child); Context.Categories.Add(category); var brand = Brand.Create("MyBrand", "mybrand"); Context.Brands.Add(brand); var customAttribute = CustomAttribute.Create("color", "string"); Context.CustomAttributes.Add(customAttribute); var product = Product.Create("EAN", "SKU", "First Product", "first-product"); product.EnableTierPrices(); product.AddTierPrice(1, 10, new Currency { Amount = 20, Code = "EUR" }); product.AddReview("Alberto", 10); product.AddImage("/path/to/image.jpg", "MyImage", "image", true, DateTime.Now); product.AddAttribute(customAttribute, "#fff"); product.AddMainCategory(category); product.SetVendor(brand); Context.Products.Add(product); Context.SaveChanges(); }