Пример #1
0
        public async Task ShouldPopulateDestinationFromSource()
        {
            var dbContext        = GetDb();
            var originalCategory = new ProductCategory {
                CategoryCode = "abc"
            };
            var newCategory = new ProductCategory {
                CategoryCode = "def"
            };
            const string awesomeNewName = "Awesome New Name";

            var destination = new Product {
                ProductCategory = originalCategory
            };

            PersistToDatabase(originalCategory, newCategory);

            var matches = new Dictionary <string, string>
            {
                { "ProductCategory", "cat" },
                { "ProductName", "name" },
            };


            var excelRow = new Dictionary <string, string>
            {
                { "cat", newCategory.CategoryCode },
                { "name", awesomeNewName },
            };

            var overrider = new ProductPropertyOverrider <Product>(dbContext);
            await overrider.UpdateProperties(destination, matches, excelRow, RecordMode.Upsert);

            destination.ProductCategory.CategoryCode.ShouldBe(newCategory.CategoryCode);
            destination.ProductName.ShouldBe(awesomeNewName);
        }
Пример #2
0
        public async Task ShouldImportWithOverrider()
        {
            var dbContext        = GetDb();
            var categoryName     = "Cookies";
            var categoryToSelect = new ProductCategory {
                CategoryCode = "CK", CategoryName = categoryName
            };
            var unselectedCategory = new ProductCategory {
                CategoryCode = "UC", CategoryName = "Unrelated Category"
            };

            PersistToDatabase(categoryToSelect, unselectedCategory);

            var existingProduct = new Product {
                ProductCategoryId = unselectedCategory.Id, ProductName = "Vanilla Wafers"
            };

            PersistToDatabase(existingProduct);


            var overrider = new ProductPropertyOverrider <Product>(dbContext);


            var excelIoWrapper = new FakeExcelIo();

            excelIoWrapper.Rows.Clear();
            var cookieType = "Mint Cookies";

            excelIoWrapper.Rows.Add(new Dictionary <string, string>
            {
                { "xlsCol1", "CK" },
                { "xlsCol2", cookieType },
                { "xlsCol5", "" },
            });
            var updatedCookieName = "Strawberry Wafers";

            excelIoWrapper.Rows.Add(new Dictionary <string, string>
            {
                { "xlsCol1", "CK" },
                { "xlsCol2", updatedCookieName },
                { "xlsCol5", existingProduct.Id.ToString() },
            });


            var importer = new XlsxToTableImporter(dbContext, excelIoWrapper);

            var prod = new Product();
            var importMatchingData = new DataMatchesForImportingOrderData
            {
                FileName = "foo.xlsx",
                Sheet    = "mysheet",
                Selected = new List <XlsToEfColumnPair>
                {
                    XlsToEfColumnPair.Create(() => prod.Id, "xlsCol5"),
                    XlsToEfColumnPair.Create("ProductCategory", "xlsCol1"),
                    XlsToEfColumnPair.Create(() => prod.ProductName, "xlsCol2"),
                },
            };


            Func <int, Expression <Func <Product, bool> > > finderExpression = selectorValue => entity => entity.Id.Equals(selectorValue);
            var result = await importer.ImportColumnData(importMatchingData, finderExpression, overridingMapper : overrider);

            var newItem = GetDb().Set <Product>().Include(x => x.ProductCategory).First(x => x.ProductName == cookieType);

            newItem.ProductCategory.CategoryName.ShouldBe("Cookies");
            newItem.ProductName.ShouldBe(cookieType);

            var updated = GetDb().Set <Product>().Include(x => x.ProductCategory).First(x => x.Id == existingProduct.Id);

            updated.ProductName.ShouldBe(updatedCookieName);
        }