public void TestShopSaleReadProperly() { //SETUP var options = SqliteInMemory.CreateOptions <CompanyDbContext>(); using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider("accessKey*"))) { context.Database.EnsureCreated(); var company = Company.AddTenantToDatabaseWithSaveChanges("TestCompany", PaidForModules.None, context); var shop = RetailOutlet.AddTenantToDatabaseWithSaveChanges("TestShop", company, context); var shopStock = new ShopStock { Name = "dress", RetailPrice = 12, NumInStock = 2, Shop = shop }; context.Add(shopStock); context.SaveChanges(); //ATTEMPT var status = ShopSale.CreateSellAndUpdateStock(1, shopStock.ShopStockId, context); status.IsValid.ShouldBeTrue(status.GetAllErrors()); context.Add(status.Result); context.SaveChanges(); //VERIFY var salesNotFiltered = context.ShopSales.IgnoreQueryFilters() .Include(x => x.StockItem).ThenInclude(x => x.Shop) .ToList(); salesNotFiltered.Count.ShouldEqual(1); salesNotFiltered.First().StockItem.ShouldNotBeNull(); salesNotFiltered.First().StockItem.Shop.ShouldNotBeNull(); } }
public void TestQueryFilterWorksOnShopStock() { //SETUP var options = SqliteInMemory.CreateOptions <CompanyDbContext>(); using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider("accessKey*"))) { context.Database.EnsureCreated(); var company = Company.AddTenantToDatabaseWithSaveChanges("TestCompany", PaidForModules.None, context); var shop = RetailOutlet.AddTenantToDatabaseWithSaveChanges("TestShop", company, context); var stock = new ShopStock { Name = "dress", NumInStock = 5, Shop = shop }; context.Add(stock); context.SaveChanges(); var utData = context.SetupSingleDtoAndEntities <SellItemDto>(); var service = new CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var dto = new SellItemDto { TenantItemId = shop.TenantItemId, ShopStockId = stock.ShopStockId, NumBought = 1 }; var shopSale = service.CreateAndSave(dto); //VERIFY service.IsValid.ShouldBeTrue(service.GetAllErrors()); context.ShopSales.Count().ShouldEqual(1); context.ShopStocks.Single().NumInStock.ShouldEqual(4); } }
public void TestQueryFilterWorksOnShopStock() { //SETUP var options = SqliteInMemory.CreateOptions <CompanyDbContext>(); using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider("accessKey*"))) { context.Database.EnsureCreated(); var company = Company.AddTenantToDatabaseWithSaveChanges("TestCompany", PaidForModules.None, context); var shop = RetailOutlet.AddTenantToDatabaseWithSaveChanges("TestShop", company, context); context.Add(new ShopStock { Name = "dress", Shop = shop }); context.SaveChanges(); } using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider("DIFF-accessKey*"))) { //ATTEMPT var stocksFiltered = context.ShopStocks.ToList(); var stocksNotFiltered = context.ShopStocks.IgnoreQueryFilters().ToList(); //VERIFY stocksFiltered.Count.ShouldEqual(0); stocksNotFiltered.Count.ShouldEqual(1); stocksNotFiltered.First().DataKey.ShouldEqual("accessKey*"); } }
public void TestDataKeyNotSetIfProvidedKeyIsNull() { //SETUP var options = SqliteInMemory.CreateOptions <CompanyDbContext>(); using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider(null))) { context.Database.EnsureCreated(); var company = Company.AddTenantToDatabaseWithSaveChanges("TestCompany", PaidForModules.None, context); var shop = RetailOutlet.AddTenantToDatabaseWithSaveChanges("TestShop", company, context); var stock = new ShopStock { Name = "dress", Shop = shop }; stock.SetShopLevelDataKey("accessKey*"); context.Add(stock); context.SaveChanges(); } using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider("accessKey*"))) { //ATTEMPT var stocksFiltered = context.ShopStocks.ToList(); //VERIFY stocksFiltered.Count.ShouldEqual(1); } }
public void TestCreateCompanyAndChild() { //SETUP var options = SqliteInMemory.CreateOptions <CompanyDbContext>(); using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider(null))) { context.Database.EnsureCreated(); var company = Company.AddTenantToDatabaseWithSaveChanges("TestCompany", PaidForModules.None, context); //ATTEMPT var shop = RetailOutlet.AddTenantToDatabaseWithSaveChanges("TestShop", company, context); //VERIFY shop.DataKey.ShouldNotBeNull(); } }
public void TestAddTenantRetailOutletOk() { //SETUP var options = SqliteInMemory.CreateOptions <CompanyDbContext>(); using (var context = new CompanyDbContext(options, new FakeGetClaimsProvider("accessKey"))) { context.Database.EnsureCreated(); var rootCompanies = context.AddCompanyAndChildrenInDatabase(); // -- West Coast --|--- San Fran --- var sanFran = rootCompanies.First().Children.Single().Children.First(); //ATTEMPT RetailOutlet.AddTenantToDatabaseWithSaveChanges("New shop", sanFran, context); //VERIFY var display = context.Tenants.IgnoreQueryFilters().Select(x => x.ToString()).ToList(); foreach (var line in display) { _output.WriteLine($"\"{line}\","); } display.ShouldEqual(new List <string> { "Company: Name = 4U Inc., DataKey = 1|", "SubGroup: Name = West Coast, DataKey = 1|2|", "SubGroup: Name = San Fran, DataKey = 1|2|3|", "RetailOutlet: Name = SF Dress4U, DataKey = 1|2|3|4*", "RetailOutlet: Name = SF Tie4U, DataKey = 1|2|3|5*", "RetailOutlet: Name = SF Shirt4U, DataKey = 1|2|3|6*", "SubGroup: Name = LA, DataKey = 1|2|7|", "RetailOutlet: Name = LA Dress4U, DataKey = 1|2|7|8*", "RetailOutlet: Name = LA Tie4U, DataKey = 1|2|7|9*", "RetailOutlet: Name = LA Shirt4U, DataKey = 1|2|7|a*", "RetailOutlet: Name = New shop, DataKey = 1|2|3|b*", }); } }
public static List <Company> AddCompanyAndChildrenInDatabase(this CompanyDbContext context, params string[] companyDefinitions) { if (!companyDefinitions.Any()) { companyDefinitions = new[] { "4U Inc.|West Coast|San Fran|SF Dress4U, SF Tie4U, SF Shirt4U", "4U Inc.|West Coast|LA|LA Dress4U, LA Tie4U, LA Shirt4U" } } ; var companyDict = new Dictionary <string, Company>(); var subGroupsDict = new Dictionary <int, List <SubGroup> >(); foreach (var companyDefinition in companyDefinitions) { var hierarchyNames = companyDefinition.Split('|'); if (!companyDict.ContainsKey(hierarchyNames[0])) { companyDict[hierarchyNames[0]] = Company.AddTenantToDatabaseWithSaveChanges( hierarchyNames[0], PaidForModules.None, context); subGroupsDict.Clear(); } TenantBase parent = companyDict[hierarchyNames[0]]; for (int i = 1; i < hierarchyNames.Length; i++) { if (!subGroupsDict.ContainsKey(i)) { subGroupsDict[i] = new List <SubGroup>(); } if (i + 1 == hierarchyNames.Length) { //End, which are shops var shopNames = hierarchyNames[i].Split(',').Select(x => x.Trim()); foreach (var shopName in shopNames) { RetailOutlet.AddTenantToDatabaseWithSaveChanges(shopName, parent, context); } } else { //Groups SubGroup subGroup = null; if (subGroupsDict[i].Any(x => x.Name == hierarchyNames[i])) { subGroup = subGroupsDict[i].Single(x => x.Name == hierarchyNames[i]); } else { subGroup = SubGroup.AddTenantToDatabaseWithSaveChanges(hierarchyNames[i], parent, context); subGroupsDict[i].Add(subGroup); } parent = subGroup; } } } return(new List <Company>(companyDict.Values)); } }