Exemple #1
0
        protected void Setup()
        {
            _warehouseService               = Resolve <IWarehouseService>();
            _warehouseInventoryService      = Resolve <IWarehouseInventoryService>();
            _productService                 = Resolve <IProductService>();
            _orderAccountant                = Resolve <IOrderAccountant>();
            _productAttributeService        = Resolve <IProductAttributeService>();
            _availableAttributeService      = Resolve <IAvailableAttributeService>();
            _availableAttributeValueService = Resolve <IAvailableAttributeValueService>();
            _productVariantService          = Resolve <IProductVariantService>();
            _addressService                 = Resolve <IAddressService>();

            var address = new Address()
            {
                CountryId   = 1,
                AddressType = AddressType.Home,
                Name        = "abc"
            };

            _addressService.Insert(address);
            _w1 = new Warehouse()
            {
                AddressId = address.Id
            };
            _w2 = new Warehouse()
            {
                AddressId = address.Id
            };
            _warehouseService.Insert(_w1);
            _warehouseService.Insert(_w2);

            _sizeAvailableAttribute = new AvailableAttribute()
            {
                Name = "Size"
            };
            _colorAvailableAttribute = new AvailableAttribute()
            {
                Name = "Color"
            };
            var availableAttributes = new[]
            {
                _sizeAvailableAttribute, _colorAvailableAttribute
            };

            _availableAttributeService.Insert(availableAttributes);

            var attributeValues = new[]
            {
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = _sizeAvailableAttribute.Id,
                    Value = "S"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = _sizeAvailableAttribute.Id,
                    Value = "M"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = _sizeAvailableAttribute.Id,
                    Value = "L"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = _colorAvailableAttribute.Id,
                    Value = "Red"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = _colorAvailableAttribute.Id,
                    Value = "Green"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = _colorAvailableAttribute.Id,
                    Value = "Blue"
                },
            };

            _availableAttributeValueService.Insert(attributeValues);

            _p1 = new Product()
            {
                Name = "Microsoft Surface Pro",
                CanOrderWhenOutOfStock = true,
                ChargeTaxes            = true,
                ComparePrice           = 100,
                Price                 = 80,
                CreatedOn             = DateTime.UtcNow,
                UpdatedOn             = DateTime.UtcNow,
                IsShippable           = true,
                IsFeatured            = true,
                IsVisibleIndividually = true,
                DisplayOrder          = 1,
                Mpn            = "1",
                Gtin           = "2",
                Sku            = "SUR12",
                IsDownloadable = false,
                Description    =
                    "Surface Pro delivers even more speed and performance thanks to a powerful Intel® Core™ processor — with up to 50% more battery life1 than Surface Pro 4 and 2.5x more performance than Surface Pro 3.",
                Summary =
                    "Surface Pro delivers even more speed and performance thanks to a powerful Intel® Core™ processor — with up to 50% more battery life1 than Surface Pro 4 and 2.5x more performance than Surface Pro 3.",
                Published      = true,
                Deleted        = false,
                TrackInventory = true,
            };

            _p2 = new Product()
            {
                Name = "Microsoft Surface Pro",
                CanOrderWhenOutOfStock = true,
                ChargeTaxes            = true,
                ComparePrice           = 100,
                Price                 = 80,
                CreatedOn             = DateTime.UtcNow,
                UpdatedOn             = DateTime.UtcNow,
                IsShippable           = true,
                IsFeatured            = true,
                IsVisibleIndividually = true,
                DisplayOrder          = 1,
                Mpn            = "1",
                Gtin           = "2",
                Sku            = "SUR12",
                IsDownloadable = false,
                Description    =
                    "Surface Pro delivers even more speed and performance thanks to a powerful Intel® Core™ processor — with up to 50% more battery life1 than Surface Pro 4 and 2.5x more performance than Surface Pro 3.",
                Summary =
                    "Surface Pro delivers even more speed and performance thanks to a powerful Intel® Core™ processor — with up to 50% more battery life1 than Surface Pro 4 and 2.5x more performance than Surface Pro 3.",
                Published      = true,
                Deleted        = false,
                TrackInventory = true,
            };

            _productService.Insert(_p1);
            _productService.Insert(_p2);

            //link product attributes
            var productAttributeValuesSize = new List <ProductAttributeValue>()
            {
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[0].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[1].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[2].Id
                },
            };

            var productAttributeSize = new ProductAttribute()
            {
                AvailableAttributeId   = availableAttributes[0].Id,
                ProductId              = _p1.Id,
                ProductAttributeValues = productAttributeValuesSize
            };

            _productAttributeService.Insert(productAttributeSize);

            var productAttributeValuesColor = new List <ProductAttributeValue>()
            {
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[3].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[4].Id
                },
            };

            var productAttributeColor = new ProductAttribute()
            {
                AvailableAttributeId   = availableAttributes[1].Id,
                ProductId              = _p1.Id,
                ProductAttributeValues = productAttributeValuesColor
            };

            _productAttributeService.Insert(productAttributeColor);


            //generate variations
            // s + red
            var sRedVariant = _productVariantService.AddVariant(_p1, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[0].Id,
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[0].Id
                    }
                },
                TrackInventory = true
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = _p1.Id,
                ProductVariantId = sRedVariant.Id,
                TotalQuantity    = 5,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });
            // s + green
            var sGreenVariant = _productVariantService.AddVariant(_p1, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[0].Id
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[1].Id
                    }
                },
                TrackInventory = false
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = _p1.Id,
                ProductVariantId = sGreenVariant.Id,
                TotalQuantity    = 5,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });

            // m + red
            var mRedVariant = _productVariantService.AddVariant(_p1, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[1].Id
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[0].Id
                    }
                },
                TrackInventory = true
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = _p1.Id,
                ProductVariantId = mRedVariant.Id,
                TotalQuantity    = 0,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = _p2.Id,
                WarehouseId      = _w1.Id,
                ReservedQuantity = 1,
                TotalQuantity    = 1
            });
            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = _p2.Id,
                WarehouseId      = _w2.Id,
                ReservedQuantity = 1,
                TotalQuantity    = 2
            });

            _o1 = new Order()
            {
                Id         = 1,
                OrderItems = new List <OrderItem>()
                {
                    new OrderItem()
                    {
                        OrderId          = 1,
                        ProductId        = _p1.Id,
                        ProductVariantId = sRedVariant.Id,
                        Quantity         = 2,
                        Id = 1
                    },
                    new OrderItem()
                    {
                        OrderId          = 1,
                        ProductId        = _p1.Id,
                        ProductVariantId = sGreenVariant.Id,
                        Quantity         = 1,
                        Id = 2
                    }
                }
            };

            _o2 = new Order()
            {
                Id         = 2,
                OrderItems = new List <OrderItem>()
                {
                    new OrderItem()
                    {
                        OrderId   = 2,
                        ProductId = _p2.Id,
                        Quantity  = 1,
                        Id        = 3
                    }
                }
            };

            _o3 = new Order()
            {
                Id         = 3,
                OrderItems = new List <OrderItem>()
                {
                    new OrderItem()
                    {
                        OrderId   = 2,
                        ProductId = _p2.Id,
                        Quantity  = 3,
                        Id        = 4
                    }
                }
            };

            _o4 = new Order()
            {
                Id         = 4,
                OrderItems = new List <OrderItem>()
                {
                    new OrderItem()
                    {
                        OrderId          = 1,
                        ProductId        = _p1.Id,
                        ProductVariantId = sRedVariant.Id,
                        Quantity         = 2,
                        Id = 5
                    },
                    new OrderItem()
                    {
                        OrderId          = 1,
                        ProductId        = _p1.Id,
                        ProductVariantId = sGreenVariant.Id,
                        Quantity         = 1,
                        Id = 6
                    },
                    new OrderItem()
                    {
                        OrderId   = 2,
                        ProductId = _p2.Id,
                        Quantity  = 1,
                        Id        = 7
                    }
                }
            };
        }
        public void Get_Product_By_Id_Succeeds()
        {
            var manufacturer = new Manufacturer()
            {
                Name = "RoastedBytes"
            };

            _manufacturerService.Insert(manufacturer);

            var vendor1 = new Vendor()
            {
                Name = "Vendor One"
            };
            var vendor2 = new Vendor()
            {
                Name = "Vendor One"
            };

            _vendorService.Insert(new[] { vendor1, vendor2 });

            var availableAttributes = new[]
            {
                new AvailableAttribute()
                {
                    Name = "Size"
                },
                new AvailableAttribute()
                {
                    Name = "Color"
                }
            };

            _availableAttributeService.Insert(availableAttributes);

            var attributeValues = new[]
            {
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 1,
                    Value = "S"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 1,
                    Value = "M"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 1,
                    Value = "L"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 2,
                    Value = "Red"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 2,
                    Value = "Green"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 2,
                    Value = "Blue"
                },
            };

            _availableAttributeValueService.Insert(attributeValues);

            var product = new Product()
            {
                Name = "Microsoft Surface Pro",
                CanOrderWhenOutOfStock = true,
                ChargeTaxes            = true,
                ComparePrice           = 100,
                Price                 = 80,
                CreatedOn             = DateTime.UtcNow,
                UpdatedOn             = DateTime.UtcNow,
                IsShippable           = true,
                IsFeatured            = true,
                IsVisibleIndividually = true,
                DisplayOrder          = 1,
                Mpn            = "1",
                Gtin           = "2",
                Sku            = "SUR12",
                IsDownloadable = false,
                Description    =
                    "Surface Pro delivers even more speed and performance thanks to a powerful Intel® Core™ processor — with up to 50% more battery life1 than Surface Pro 4 and 2.5x more performance than Surface Pro 3.",
                Summary =
                    "Surface Pro delivers even more speed and performance thanks to a powerful Intel® Core™ processor — with up to 50% more battery life1 than Surface Pro 4 and 2.5x more performance than Surface Pro 3.",
                Published      = true,
                Deleted        = false,
                TrackInventory = true,
                ManufacturerId = manufacturer.Id
            };

            _productService.Insert(product);

            //link product attributes
            var productAttributeValuesSize = new List <ProductAttributeValue>()
            {
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[0].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[1].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[2].Id
                },
            };

            var productAttributeSize = new ProductAttribute()
            {
                AvailableAttributeId   = availableAttributes[0].Id,
                ProductId              = product.Id,
                ProductAttributeValues = productAttributeValuesSize
            };

            _productAttributeService.Insert(productAttributeSize);

            var productAttributeValuesColor = new List <ProductAttributeValue>()
            {
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[3].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[4].Id
                },
            };

            var productAttributeColor = new ProductAttribute()
            {
                AvailableAttributeId   = availableAttributes[1].Id,
                ProductId              = product.Id,
                ProductAttributeValues = productAttributeValuesColor
            };

            _productAttributeService.Insert(productAttributeColor);


            //generate variations
            // s + red
            var sRedVariant = _productVariantService.AddVariant(product, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[0].Id,
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[0].Id
                    }
                },
                TrackInventory = true
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = product.Id,
                ProductVariantId = sRedVariant.Id,
                TotalQuantity    = 5,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });

            // s + green
            var sGreenVariant = _productVariantService.AddVariant(product, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[0].Id
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[1].Id
                    }
                },
                TrackInventory = false
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = product.Id,
                ProductVariantId = sGreenVariant.Id,
                TotalQuantity    = 5,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });

            // m + red
            var mRedVariant = _productVariantService.AddVariant(product, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[1].Id
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[0].Id
                    }
                },
                TrackInventory = true
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = product.Id,
                ProductVariantId = mRedVariant.Id,
                TotalQuantity    = 0,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });

            //link vendors
            _vendorService.AddVendorProduct(vendor1.Id, product.Id);
            _vendorService.AddVendorProduct(vendor2.Id, product.Id);

            //retrive the product
            var p = _productService.Get(product.Id);

            //same product?
            Assert.AreEqual(product.Id, p.Id);

            //attributes correct?
            Assert.AreEqual(2, p.ProductAttributes.Count);

            //attribute values correct
            Assert.AreEqual(3, p.ProductAttributes[0].ProductAttributeValues.Count);
            Assert.AreEqual(2, p.ProductAttributes[1].ProductAttributeValues.Count);

            //manufacturer right
            Assert.AreEqual(manufacturer.Id, p.Manufacturer.Id);

            //vendors right?
            Assert.AreEqual(2, p.Vendors.Count);

            //get variants
            var variants = _productVariantService.GetByProductId(product.Id);

            Assert.AreEqual(3, variants.Count);

            //single variants
            var singleVariant = _productVariantService.GetByAttributeValueIds(new List <int>()
            {
                productAttributeValuesSize[0].Id,
                productAttributeValuesColor[0].Id
            });

            Assert.AreEqual(sRedVariant.Id, singleVariant.Id);

            //single variants
            singleVariant = _productVariantService.GetByAttributeValueIds(new List <int>()
            {
                productAttributeValuesSize[0].Id,
                productAttributeValuesColor[1].Id
            });
            Assert.AreEqual(sGreenVariant.Id, singleVariant.Id);

            singleVariant = _productVariantService.GetByAttributeValueIds(new List <int>()
            {
                productAttributeValuesSize[1].Id,
                productAttributeValuesColor[0].Id
            });
            Assert.AreEqual(mRedVariant.Id, singleVariant.Id);

            //verify stocks
            var stockStatus = _productAccountant.GetStockStatus(product, new List <int>()
            {
                productAttributeValuesSize[0].Id,
                productAttributeValuesColor[0].Id
            }, out ProductVariant _);

            Assert.AreEqual(StockStatus.InStock, stockStatus);

            stockStatus = _productAccountant.GetStockStatus(product, new List <int>()
            {
                productAttributeValuesSize[0].Id,
                productAttributeValuesColor[1].Id
            }, out ProductVariant _);
            Assert.AreEqual(StockStatus.InStock, stockStatus);

            stockStatus = _productAccountant.GetStockStatus(product, new List <int>()
            {
                productAttributeValuesSize[1].Id,
                productAttributeValuesColor[0].Id
            }, out ProductVariant _);
            Assert.AreEqual(StockStatus.OutOfStock, stockStatus);

            stockStatus = _productAccountant.GetStockStatus(product, new List <int>()
            {
                productAttributeValuesSize[1].Id,
                productAttributeValuesColor[1].Id
            }, out ProductVariant _);
            Assert.AreEqual(StockStatus.Unavailable, stockStatus);

            //remove a product attribute value to see how variants are affected
            _productVariantService.DeleteVariantsByProductAttributeValueId(productAttributeValuesSize[0].Id);

            //get the variants again
            variants = _productVariantService.GetByProductId(product.Id);
            Assert.AreEqual(1, variants.Count);
            Assert.AreEqual(mRedVariant.Id, variants[0].Id);
        }