public void SetUp() { mDbContext = new TestAuctioneerDbContext(Effort.DbConnectionFactory.CreateTransient()); AddTestData(mDbContext); mQueryable = mDbContext.Auctions; }
public AuctionExpireCheckBackgroundTask(AuctioneerDbContext context, IUserNotifier userNotifier) { Contract.Requires(context != null); Contract.Requires(userNotifier != null); mContext = context; mUserNotifier = userNotifier; }
public static void Add(AuctioneerDbContext context) { context.Set<BackgroundTasks.BackgroundTasksData>().Add(new BackgroundTasks.BackgroundTasksData { AuctionExpiryCheckLastRun = DateTime.Now }); context.SaveChanges(); }
public void SetUp() { mDbContext = new TestAuctioneerDbContext(Effort.DbConnectionFactory.CreateTransient()); AddTestData(mDbContext); mUserNotifierMock = A.Fake<IUserNotifier>(); var userService = new UserService(mDbContext, mUserNotifierMock); mTestedService = new AuctionService(mDbContext, mUserNotifierMock, userService, "Ignored", "Ignored"); }
private void AddTestData(AuctioneerDbContext context) { context.Categories.Add(new TestCategory { Id = 1, Name = "Computers", Left = 1, Right = 22, SubCategories = new TestCategory[] { new TestCategory { Id = 2, Name = "Desktop computers", Left = 2, Right = 3 }, new TestCategory { Id = 3, Name = "Mobile computers", Left = 4, Right = 5 }, new TestCategory { Id = 4, Name = "Components", Left = 6, Right = 21, SubCategories = new TestCategory[] { new TestCategory { Id = 5, Name = "Hard drives", Left = 7, Right = 8 }, new TestCategory { Id = 6, Name = "Graphics cards", Left = 9, Right = 10 }, new TestCategory { Id = 7, Name = "Motherboards", Left = 11, Right = 12 }, new TestCategory { Id = 8, Name = "Processors", Left = 13, Right = 14 }, new TestCategory { Id = 9, Name = "RAM memory", Left = 15, Right = 16 }, new TestCategory { Id = 15, Name = "Power supplies", Left = 17, Right = 18 }, new TestCategory { Id = 16, Name = "Cases", Left = 19, Right = 20 }, } } } }); context.Categories.Add(new TestCategory { Id = 12, Name = "Software", Left = 23, Right = 32, SubCategories = new TestCategory[] { new TestCategory { Id = 13, Name = "Operating systems", Left = 24, Right = 25 }, new TestCategory { Id = 14, Name = "Office", Left = 26, Right = 27 }, new TestCategory { Id = 10, Name = "Security", Left = 28, Right = 29 }, new TestCategory { Id = 11, Name = "Games", Left = 30, Right = 31 }, } }); context.SaveChanges(); context.Users.Add(new TestUser { Id = "1" }); AddAuctionsToCategory(context, 1, 5); AddAuctionsToCategory(context, 2, 3, AuctionStatus.Expired); AddAuctionsToCategory(context, 3, 2); AddAuctionsToCategory(context, 7, 3); AddAuctionsToCategory(context, 8, 7); AddAuctionsToCategory(context, 12, 3); AddAuctionsToCategory(context, 14, 2, AuctionStatus.Sold); AddAuctionsToCategory(context, 11, 10); context.SaveChanges(); }
public static void Add(AuctioneerDbContext context) { var currencies = new Currency[] { new Currency("$", CurrencySymbolPosition.BeforeAmount), new Currency("€", CurrencySymbolPosition.AfterAmountWithSpace), new Currency("zł", CurrencySymbolPosition.AfterAmountWithSpace), new Currency("£", CurrencySymbolPosition.BeforeAmount), }; context.Currencies.AddRange(currencies); context.SaveChanges(); }
public void SetUp() { mDbContext = new TestAuctioneerDbContext(Effort.DbConnectionFactory.CreateTransient()); mDbContext.Categories.Add(new TestCategory { Id = 1 }); mDbContext.Users.Add(new TestUser { Id = "1" }); mDbContext.Users.Add(new TestUser { Id = "2" }); mDbContext.Users.Add(new TestUser { Id = "3" }); mDbContext.SaveChanges(); mUserNotifierMock = A.Fake<IUserNotifier>(); var userService = new UserService(mDbContext, mUserNotifierMock); mTestedService = new AuctionService(mDbContext, mUserNotifierMock, userService, "Ignored", "Ignored"); }
public AuctionService(AuctioneerDbContext context, IUserNotifier userNotifier, IUserService userService, string photoDirectoryPath, string thumbnailDirectoryPath) { Contract.Requires(context != null); Contract.Requires(userNotifier != null); Contract.Requires(userService != null); Contract.Requires(!String.IsNullOrWhiteSpace(photoDirectoryPath)); Contract.Requires(!String.IsNullOrWhiteSpace(thumbnailDirectoryPath)); mContext = context; mUserNotifier = userNotifier; mUserService = userService; mAuctionPhotoDirectoryPath = photoDirectoryPath; mAuctionThumbnailDirectoryPath = thumbnailDirectoryPath; }
private void AddTestData(AuctioneerDbContext context) { context.Categories.Add(new TestCategory { Id = 1 }); context.Users.Add(new TestUser { Id = "1" }); context.SaveChanges(); context.Auctions.Add(new TestAuction { Id = 1, BuyoutPrice = new Money(10, new TestCurrency()), MinimumPrice = new Money( 5, new TestCurrency()), }); context.Auctions.Add(new TestAuction { Id = 2, MinimumPrice = new Money(1, new TestCurrency()), Offers = new Collection<BuyOffer> { new TestBuyOffer { Amount = 5 }, new TestBuyOffer { Amount = 8 }, } }); context.Auctions.Add(new TestAuction { Id = 3, BuyoutPrice = new Money(6, new TestCurrency()), }); context.Auctions.Add(new TestAuction { Id = 4, BuyoutPrice = new Money(50, new TestCurrency()), MinimumPrice = new Money(2, new TestCurrency()), Offers = new Collection<BuyOffer> { new TestBuyOffer { Amount = 3 }, new TestBuyOffer { Amount = 4 }, } }); context.SaveChanges(); }
private static UserManager<User> CreateUserManagerWithoutValidation(AuctioneerDbContext context) { var userManager = new UserManager<User>(new UserStore<User>(context)); userManager.PasswordValidator = new PasswordValidator { RequiredLength = 0, RequireDigit = false, RequireLowercase = false, RequireNonLetterOrDigit = false, RequireUppercase = false }; userManager.UserValidator = new UserValidator<User>(userManager) { AllowOnlyAlphanumericUserNames = true, RequireUniqueEmail = false }; return userManager; }
private void AddAuctionsToCategory(AuctioneerDbContext context, int categoryId, int auctionCount, AuctionStatus status = AuctionStatus.Active) { for(int i = 0; i < auctionCount; ++i) { var auction = new TestAuction { CategoryId = categoryId, EndDate = (status != AuctionStatus.Expired) ? DateTime.Now.Add(TimeSpan.FromDays(1)) : DateTime.Now.Subtract(TimeSpan.FromDays(1)), BuyoutPrice = new Money(10, new TestCurrency()) }; if(status == AuctionStatus.Sold) { auction.Offers.Add(new TestBuyOffer { Amount = 10 } ); } context.Auctions.Add(auction); } }
public static void Add(AuctioneerDbContext context) { var rndGenerator = new Random(Seed: 2934228); var userManager = CreateUserManagerWithoutValidation(context); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); var firstNames = new string[] { "Alexa", "Amanda", "Olivia", "Jacob", "William", "Michael", "John" }; var lastNames = new string[] { "Smith", "Johnson", "Williams", "Brown", "Miller", "King", "Kelly", "Foster" }; roleManager.Create(new IdentityRole { Name = "Admin" }); for(int i = 0; i < 50; ++i) { var user = new User(); user.FirstName = firstNames[rndGenerator.Next(firstNames.Length)]; user.LastName = lastNames[rndGenerator.Next(lastNames.Length)]; user.Address = "ul. Wawelska 84/32\n45-345 Warszawa"; user.Email = user.FirstName + user.LastName + "@mail.abc"; user.EmailConfirmed = true; user.UserName = String.Format("{0}_{1}", user.FirstName, user.LastName).ToLower(); userManager.Create(user, "Password"); } var admin = new User { UserName = "******", FirstName = "Mr. Admin", LastName = "Admin", Address = "Administrator Panel", Email = "*****@*****.**", EmailConfirmed = true, }; userManager.Create(admin, password: "******"); userManager.AddToRole(admin.Id, "Admin"); }
private void AddTestData(AuctioneerDbContext context) { context.Categories.AddRange(new TestCategory[] { new TestCategory { Id = 1, Left = 1, Right = 10, SubCategories = new TestCategory[] { new TestCategory { Id = 2, Left = 2, Right = 3 }, new TestCategory { Id = 3, Left = 4, Right = 9, SubCategories = new TestCategory[] { new TestCategory { Id = 4, Left = 5, Right = 6 }, new TestCategory { Id = 5, Left = 7, Right = 8 }, } }, } }, new TestCategory { Id = 6, Left = 11, Right = 12 }, }); context.Users.Add(new TestUser { Id = "1" }); context.Users.Add(new TestUser { Id = "2" }); context.Users.Add(new TestUser { Id = "3" }); context.Roles.Add(new IdentityRole { Name = "Admin" }); context.SaveChanges(); context.Users.Find("3").Roles.Add(new IdentityUserRole { RoleId = context.Roles.Single().Id, UserId = "3" }); // TODO the tests are hard to update due to dependencies between test data, how to solve this? // The best would be independent data tailored just for single test, but there is a lot of data to setup // which will make the class with tests very long. context.Auctions.Add(new TestAuction { Id = 1, Title = "1", CategoryId = 2, CreationDate = new DateTime(2015, 3, 12), EndDate = DateTime.Now.Subtract(TimeSpan.FromDays(2)), SellerId = "1" }); context.Auctions.Add(new TestAuction { Id = 2, Title = "2", CategoryId = 6, CreationDate = new DateTime(2013, 5, 25), EndDate = DateTime.Now.Add(TimeSpan.FromDays(2)), SellerId = "1" }); context.Auctions.Add(new TestAuction { Id = 3, Title = "3", CategoryId = 6, CreationDate = new DateTime(2013, 6, 11), EndDate = DateTime.Now.Add(TimeSpan.FromDays(2)), SellerId = "2" }); context.Auctions.Add(new TestAuction { Id = 4, Title = "4", CategoryId = 2, CreationDate = new DateTime(2014, 9, 16), EndDate = DateTime.Now.Add(TimeSpan.FromDays(1)), SellerId = "1", BuyoutPrice = new Money(100, new Currency("$", CurrencySymbolPosition.BeforeAmount)), Offers = new Collection<BuyOffer> { new TestBuyOffer { UserId = "2", Amount = 100 } } }); context.Auctions.Add(new TestAuction { Id = 5, Title = "5", CategoryId = 2, CreationDate = new DateTime(2013, 9, 5), EndDate = DateTime.Now.Add(TimeSpan.FromDays(2)), SellerId = "1" }); context.Auctions.Add(new TestAuction { Id = 6, Title = "6", CategoryId = 2, CreationDate = new DateTime(2012, 1, 16), EndDate = DateTime.Now.Subtract(TimeSpan.FromDays(10)), SellerId = "1" }); context.Auctions.Add(new TestAuction { Id = 7, Title = "7", CategoryId = 2, CreationDate = new DateTime(2014, 12, 22), EndDate = DateTime.Now.Add(TimeSpan.FromDays(10)), SellerId = "1" }); context.Auctions.Add(new TestAuction { Id = 8, Title = "8", CategoryId = 2, CreationDate = new DateTime(2013, 1, 12), EndDate = DateTime.Now.Add(TimeSpan.FromMinutes(1)), SellerId = "1" }); context.Auctions.Add(new TestAuction { Id = 9, Title = "9", CategoryId = 3, CreationDate = new DateTime(2015, 2, 1), EndDate = DateTime.Now.Add(TimeSpan.FromDays(1)), SellerId = "2" }); context.Auctions.Add(new TestAuction { Id = 10, Title = "10", CategoryId = 5, CreationDate = new DateTime(2014, 4, 30), EndDate = DateTime.Now.Add(TimeSpan.FromDays(1)), SellerId = "1" }); context.Auctions.Add(new TestAuction { Id = 11, Title = "11", CategoryId = 3, CreationDate = new DateTime(2013, 2, 1), EndDate = DateTime.Now.Subtract(TimeSpan.FromDays(1)), SellerId = "2" }); context.Auctions.Add(new TestAuction { Id = 12, Title = "12", CategoryId = 3, CreationDate = new DateTime(2013, 2, 1), EndDate = DateTime.Now.Subtract(TimeSpan.FromDays(1)), SellerId = "1" }); context.SaveChanges(); }
private void AddTestData(AuctioneerDbContext context) { context.Categories.Add(new TestCategory { Id = 1 }); context.Users.Add(new TestUser { Id = "1" }); context.SaveChanges(); mExpiredAuction = new TestAuction { EndDate = DateTime.Now.Subtract(TimeSpan.FromDays(2)) }; mAuctionSoldByBidding = new TestAuction { EndDate = DateTime.Now.Subtract(TimeSpan.FromDays(3)), Offers = new BuyOffer[] { new TestBuyOffer { Amount = 10 } } }; mAuctionSoldByBuyout = new TestAuction { BuyoutPrice = new Money(100, new Currency("$", CurrencySymbolPosition.BeforeAmount)), EndDate = DateTime.Now.AddDays(3), Offers = new BuyOffer[] { new TestBuyOffer { Amount = 50 }, new TestBuyOffer { Amount = 100 } } }; mActiveActionWithoutOffers = new TestAuction { EndDate = DateTime.Now.AddDays(3) }; mActiveActionWithOffers = new TestAuction { EndDate = DateTime.Now.AddDays(3), Offers = new BuyOffer[] { new TestBuyOffer { Amount = 50 }, new TestBuyOffer { Amount = 30 } } }; context.Auctions.Add(mExpiredAuction); context.Auctions.Add(mAuctionSoldByBidding); context.Auctions.Add(mAuctionSoldByBuyout); context.Auctions.Add(mActiveActionWithOffers); context.Auctions.Add(mActiveActionWithoutOffers); context.SaveChanges(); }
private static void InsertAuctions(AuctioneerDbContext context, IList<Auction> auctions) { // It's the fastest way to correctly insert a lot of auctions. // Plain SQL Insert took more than 4 minutes while this method takes only 16 seconds. // EntityFramework.AddRange() was way too slow and BulkInsert requires foreign key property on entity to // relate the entities properly (which I don't want to add because it is not needed). var minPrices = auctions.Select(x => x.MinimumPrice).Where(x => x != null).ToList(); var buyoutPrices = auctions.Select(x => x.BuyoutPrice).Where(x => x != null).ToList(); InsertMoneys(context, minPrices.Concat(buyoutPrices)); var table = new DataTable("Auctions"); table.Columns.Add("Id", typeof(int)); table.Columns.Add("Title", typeof(string)); table.Columns.Add("Description", typeof(string)); table.Columns.Add("CreationDate", typeof(DateTime)); table.Columns.Add("EndDate", typeof(DateTime)); table.Columns.Add("PhotoCount", typeof(int)); table.Columns.Add("CategoryId", typeof(int)); table.Columns.Add("SellerId", typeof(string)); table.Columns.Add("MinimumPrice_Id", typeof(int)); table.Columns.Add("BuyoutPrice_Id", typeof(int)); int minPriceId = 1; int buyoutPriceId = minPrices.Count + 1; foreach(var auction in auctions) { var row = table.NewRow(); row[1] = auction.Title; row[2] = auction.Description; row[3] = auction.CreationDate; row[4] = auction.EndDate; row[5] = auction.PhotoCount; row[6] = auction.CategoryId; row[7] = auction.SellerId; if(auction.MinimumPrice != null) { row[8] = minPriceId++; } if(auction.BuyoutPrice != null) { row[9] = buyoutPriceId++; } table.Rows.Add(row); } BulkInsert(context, table); InsertBuyOffers(context, auctions.SelectMany(x => x.Offers)); }
private static void BulkInsert(AuctioneerDbContext context, DataTable data) { var bulkInsert = new SqlBulkCopy(context.Database.Connection.ConnectionString, SqlBulkCopyOptions.TableLock) { DestinationTableName = data.TableName }; foreach(var column in data.Columns.Cast<DataColumn>()) { bulkInsert.ColumnMappings.Add(column.ColumnName, column.ColumnName); } bulkInsert.WriteToServer(data); }
public static void Add(AuctioneerDbContext context) { var rndGenerator = new Random(Seed: 746293114); var imageInitializer = new AuctionImageInitializer(); var categoryCount = context.Categories.Count(); var userIds = context.Users.Select(x => x.Id).ToList(); var currencies = context.Currencies.ToList(); var auctions = new Auction[100000]; for(int i = 0; i < auctions.Length; ++i) { var creationDate = new DateTime ( day : rndGenerator.Next(1, 29), month : rndGenerator.Next(1, 13), year : rndGenerator.Next(2010, 2016), hour : rndGenerator.Next(0, 24), minute : rndGenerator.Next(0, 60), second : rndGenerator.Next(0, 60) ); if(creationDate > DateTime.Now) creationDate = DateTime.Now; auctions[i] = new Auction { CategoryId = rndGenerator.Next(categoryCount) + 1, Title = "The auction #" + (i + 1), Description = "The description of auction number " + (i + 1), SellerId = userIds[rndGenerator.Next(userIds.Count)], CreationDate = creationDate, EndDate = creationDate.AddDays(rndGenerator.NextDouble() * 14), PhotoCount = 0 }; bool hasBuyoutPrice = (rndGenerator.Next(100) + 1) < 90; if(hasBuyoutPrice) { auctions[i].BuyoutPrice = new Money(rndGenerator.Next(1, 1000), currencies[rndGenerator.Next(currencies.Count)]); } bool isBiddingEnabled = !hasBuyoutPrice || ((rndGenerator.Next(100) + 1) < 30 && (auctions[i].BuyoutPrice.Amount >= 10.0m)); if(isBiddingEnabled) { auctions[i].MinimumPrice = new Money(rndGenerator.Next(1, 1000), currencies[rndGenerator.Next(currencies.Count)]); } if(isBiddingEnabled && hasBuyoutPrice) { var fixedAmount = Math.Min(auctions[i].MinimumPrice.Amount, auctions[i].BuyoutPrice.Amount * 0.5m); auctions[i].MinimumPrice = new Money(fixedAmount, auctions[i].BuyoutPrice.Currency); } bool hasBidOffer = (rndGenerator.Next(100) + 1) < 90; if(isBiddingEnabled && hasBidOffer) { var offerCountProbability = new int[100]; for(int x = 0; x < 30; ++x) offerCountProbability[x] = 1; for(int x = 30; x < 60; ++x) offerCountProbability[x] = 2; for(int x = 60; x < 80; ++x) offerCountProbability[x] = 3; for(int x = 80; x < 95; ++x) offerCountProbability[x] = 4; for(int x = 95; x < 100; ++x) offerCountProbability[x] = 5; var offerCount = offerCountProbability[rndGenerator.Next(offerCountProbability.Length)]; var previousOfferDate = auctions[i].CreationDate.AddDays(1); var previousOfferAmount = 0.0m; for(int x = 0; x < offerCount; ++x) { var minimumPossibleOffer = Math.Max(Math.Ceiling(auctions[i].MinimumPrice.Amount), previousOfferAmount + 1); var maximumPossibleOffer = Math.Floor(hasBuyoutPrice ? auctions[i].BuyoutPrice.Amount * 0.9m : 1000.0m); if(minimumPossibleOffer >= maximumPossibleOffer) break; var offer = new BuyOffer { AuctionId = i + 1, UserId = userIds[rndGenerator.Next(userIds.Count)], Date = previousOfferDate.AddHours(rndGenerator.Next(1, 24)), Amount = rndGenerator.Next((int)minimumPossibleOffer, (int)maximumPossibleOffer) }; previousOfferDate = offer.Date; previousOfferAmount = offer.Amount; auctions[i].Offers.Add(offer); } } bool hasBeenBoughtOut = (rndGenerator.Next(100) + 1) < 75; if(hasBeenBoughtOut && hasBuyoutPrice) { var lastOfferDate = (auctions[i].Offers.Any()) ? auctions[i].Offers.Last().Date : auctions[i].CreationDate; var buyoutOffer = new BuyOffer { AuctionId = i + 1, UserId = userIds[rndGenerator.Next(userIds.Count)], Date = lastOfferDate.AddHours(rndGenerator.Next(1, 24)), Amount = auctions[i].BuyoutPrice.Amount }; auctions[i].Offers.Add(buyoutOffer); } if(auctions[i].Status == AuctionStatus.Active) { imageInitializer.CopyRandomThumbnailForAuction(i + 1); imageInitializer.CopyRandomPhotosForAuction(i + 1); auctions[i].PhotoCount = imageInitializer.GetAuctionPhotoCount(i + 1); } } InsertAuctions(context, auctions); }
public CategoryService(AuctioneerDbContext context) { Contract.Requires(context != null); mContext = context; }
private static void InsertBuyOffers(AuctioneerDbContext context, IEnumerable<BuyOffer> offers) { context.BulkInsert(offers); }
private static void InsertMoneys(AuctioneerDbContext context, IEnumerable<Money> moneys) { var table = new DataTable("Moneys"); table.Columns.Add("Id", typeof(int)); table.Columns.Add("Amount", typeof(decimal)); table.Columns.Add("Currency_Symbol", typeof(string)); foreach(var money in moneys) { var row = table.NewRow(); row[1] = money.Amount; row[2] = money.Currency.Symbol; table.Rows.Add(row); } BulkInsert(context, table); }
public static void Add(AuctioneerDbContext context) { var categories = new Category[] { new Category { Name = "Computers", SubCategories = new Category[] { new Category { Name = "Desktop computers" }, new Category { Name = "Mobile computers", SubCategories = new Category[] { new Category { Name = "Tablets" }, new Category { Name = "Netbooks" }, new Category { Name = "Notebooks" }, } }, new Category { Name = "Components", SubCategories = new Category[] { new Category { Name = "Hard drives" }, new Category { Name = "Graphics cards" }, new Category { Name = "Motherboards" }, new Category { Name = "Processors" }, new Category { Name = "RAM memory" }, new Category { Name = "Power supplies" }, new Category { Name = "Cases" }, } } } }, new Category { Name = "Sport", SubCategories = new Category[] { new Category { Name = "Cycling", SubCategories = new Category[] { new Category { Name = "Bicycles" }, new Category { Name = "Accessories" }, new Category { Name = "Clothing" } } }, new Category { Name = "Team sports", SubCategories = new Category[] { new Category { Name = "Baseball" }, new Category { Name = "Soccer" }, new Category { Name = "Hockey" }, } }, new Category { Name = "Weightlifting" } } }, new Category { Name = "Software", SubCategories = new Category[] { new Category { Name = "Operating systems", SubCategories = new Category[] { new Category { Name = "Microsoft Windows" }, new Category { Name = "Apple OS X" }, new Category { Name = "Linux" }, new Category { Name = "Other" }, } }, new Category { Name = "Office" }, new Category { Name = "Security" }, new Category { Name = "Games", SubCategories = new Category[] { new Category { Name = "XBox One" }, new Category { Name = "Xbox 360" }, new Category { Name = "PlayStation 4" }, new Category { Name = "PlayStation 3" }, new Category { Name = "PC" }, new Category { Name = "Other" }, } }, new Category { Name = "Programming software" }, new Category { Name = "Other" }, } } }; InitializeNestedSetProperties(categories); context.Categories.AddRange(categories); context.SaveChanges(); }