public void Find_ShouldReturnAllDrawsOfAGameWhenThereAreNoDateLimits() { //Arrange var someGame = new LotteryGameBuilder().WithRandomDraws(5, 10).Build(); var someOtherGame = new LotteryGameBuilder().WithRandomDraws(5, 10).Build(); using (var context = CreateDbContext()) { context.Add(someGame); context.Add(someOtherGame); context.SaveChanges(); } using (var context = CreateDbContext()) { var repo = new DrawRepository(context); //Act var draws = repo.Find(someGame.Id, null, null); //Assert Assert.That(draws, Is.Not.Null, () => "No draws are returned."); Assert.That(draws.Count, Is.Not.GreaterThan(someGame.Draws.Count), () => "Too much draws are returned."); Assert.That(draws.Count, Is.EqualTo(someGame.Draws.Count), () => "All the draws of a game should be returned."); Assert.That(draws, Has.All.Matches((Draw draw) => draw.LotteryGameId == someGame.Id), () => "Only draws of the lottery game with an 'Id' that matches the 'lotterGameId' parameter, should be returned."); } }
public void Add_ShouldAddADrawToTheDatabase() { //Arrange var someGame = new LotteryGameBuilder().Build(); using (var context = CreateDbContext()) { context.Add(someGame); context.SaveChanges(); } var newDraw = new DrawBuilder().WithLotteryGameId(someGame.Id).WithRandomDrawNumbers(1, 1).Build(); using (var context = CreateDbContext()) { var repo = new DrawRepository(context); //Act repo.Add(newDraw); } using (var context = CreateDbContext()) { //Assert var savedDraw = context.Set <Draw>().FirstOrDefault(draw => draw.Id == newDraw.Id); Assert.That(savedDraw, Is.Not.Null, () => "Cannot find the added draw in the database."); } }
public ActionResult Update(HttpPostedFileBase file, HttpPostedFileBase image, int id, string title, string genre, int rating, string language, string description = "", string author_name = "", string country = "", string publisher_name = "", float price = 0) { if (file == null || image == null || author_name == "" || publisher_name == "" || genre == "" || language == "" || title == "") { return(RedirectToAction("Update", new { id = id })); } if (!file.FileName.Contains(".docx") && !file.FileName.Contains(".doc") && !file.FileName.Contains(".pdf") && !file.FileName.Contains(".mobi") && !file.FileName.Contains(".rtf")) { ViewBag.Alert = "Wrong file format (.pdf, .doc, .docx, .rtf, .mobi)"; return(RedirectToAction("Update", new { id = id })); } BookRepository br = new BookRepository(); Book model = new Book() { Id = id, Title = title, Rating = rating, Genre = genre, Price = price, Description = description, Language = language, Author_Name_fk = author_name, Publisher_Name_fk = publisher_name }; if (file != null) { string uniqueName = Guid.NewGuid().ToString() + file.FileName; string fileName = HttpContext.Server.MapPath("~/Static/Files/") + uniqueName; file.SaveAs(fileName); model.fileUrl = uniqueName; } string converted_path = ""; string imgName = ""; if (image != null) { DrawRepository draw = new DrawRepository(); string uniqueName = Guid.NewGuid().ToString() + image.FileName; imgName = HttpContext.Server.MapPath("~/Static/Images/") + uniqueName; image.SaveAs(imgName); uniqueName = uniqueName.Replace(".jpg", ".png"); System.Drawing.Image converted = System.Drawing.Image.FromFile(imgName); converted_path = imgName; converted_path = converted_path.Replace(".jpg", ".png"); converted.Save(converted_path, System.Drawing.Imaging.ImageFormat.Png); draw.Resize(converted_path, 1400, 2046); model.ImgUrl = uniqueName.Replace(".png", "_" + 1400 + "x" + 2046 + ".png"); } br.UpdateBook(model, model.Id); return(RedirectToAction("Book", new { id = model.Id })); }
public void Find_ShouldIncludeTheDrawNumbersOfTheReturnedDraws() { //Arrange var someGame = new LotteryGameBuilder().WithRandomDraws(1, 1).Build(); using (var context = CreateDbContext()) { context.Add(someGame); context.SaveChanges(); } using (var context = CreateDbContext()) { var repo = new DrawRepository(context); //Act var draws = repo.Find(someGame.Id, null, null); //Assert Assert.That(draws, Is.Not.Null, () => "No draws are returned."); var firstDraw = draws.FirstOrDefault(); Assert.That(firstDraw, Is.Not.Null, () => "No draws are returned."); Assert.That(firstDraw.DrawNumbers, Is.Not.Null.And.Not.Empty, () => "The draw numbers of the draws are not returned from the database."); } }
public ActionResult Edit(HttpPostedFileBase image, Author author, string country) { author.Country = country; AuthorRepository ar = new AuthorRepository(); if (!User.Identity.IsAuthenticated || author == null || country == null) { return(RedirectToAction("Info", new { author = author })); } string converted_path = ""; string imgName = ""; if (image != null) { DrawRepository draw = new DrawRepository(); string uniqueName = Guid.NewGuid().ToString() + image.FileName; imgName = HttpContext.Server.MapPath("~/Static/Images/Authors/") + uniqueName; image.SaveAs(imgName); uniqueName = uniqueName.Replace(".jpg", ".png"); System.Drawing.Image converted = System.Drawing.Image.FromFile(imgName); converted_path = imgName; converted_path = converted_path.Replace(".jpg", ".png"); converted.Save(converted_path, System.Drawing.Imaging.ImageFormat.Png); draw.Resize(converted_path, 1400, 2046); author.ImgUrl = uniqueName.Replace(".png", "_" + 1400 + "x" + 2046 + ".png"); } ar.UpdateAuthor(author); return(RedirectToAction("Info", new { author = author.Author_Name })); }
public void Setup() { _connectionFactoryMock = new Mock <IConnectionFactory>(); _connection = Cc(); _connectionFactoryMock.Setup(factory => factory.CreateSqlConnection()).Returns(_connection); _repository = new DrawRepository(_connectionFactoryMock.Object); _random = new Random(); }
protected override void OnStartup(StartupEventArgs e) { var connectionFactory = new ConnectionFactory(); var lotteryGameRepository = new LotteryGameRepository(connectionFactory); var drawRepository = new DrawRepository(connectionFactory); var drawService = new DrawService(drawRepository); var lotteryWindow = new LotteryWindow(lotteryGameRepository, drawRepository, drawService); lotteryWindow.Show(); }
protected override void OnStartup(StartupEventArgs e) { var context = new LotteryContext(); context.CreateOrUpdateDatabase(); var lotteryGameRepository = new LotteryGameRepository(context); var drawRepository = new DrawRepository(context); var drawService = new DrawService(drawRepository); var lotteryWindow = new LotteryWindow(lotteryGameRepository, drawRepository, drawService); lotteryWindow.Show(); }
public void Add_ShouldThrowArgumentExceptionWhenTheDrawContainsNoDrawNumbers() { var newDraw = new DrawBuilder().Build(); using (var context = CreateDbContext()) { var repo = new DrawRepository(context); newDraw.DrawNumbers = null; Assert.That(() => repo.Add(newDraw), Throws.ArgumentException, () => "Should throw an 'ArgumentException' when 'DrawNumbers' is null."); newDraw.DrawNumbers = new List <DrawNumber>(); Assert.That(() => repo.Add(newDraw), Throws.ArgumentException, () => "Should throw an 'ArgumentException' when 'DrawNumbers' is empty."); } }
private void TestFindForDateRange(int gameId, DateTime?fromDate, DateTime?untilDate, IList <Draw> expectedDraws) { using (var context = CreateDbContext()) { var repo = new DrawRepository(context); //Act var draws = repo.Find(gameId, fromDate, untilDate); //Assert Assert.That(draws, Is.Not.Empty, () => "No draws are returned."); Assert.That(draws, Has.Count.LessThanOrEqualTo(expectedDraws.Count), () => "One or more draws that are out of range are returned."); Assert.That(draws, Has.Count.EqualTo(expectedDraws.Count), () => "Not all draws that match the date range are returned. " + "Make sure you also include the draws exactly on the from date or until date."); } }
public void Get_NoDrawFound_ThrowsException() { // Arrange int expectedDrawnumber = 123456; using (var context = new BitLotteryContext(Options)) { DrawRepository drawRepository = new DrawRepository(context); try { // Act drawRepository.Get(expectedDrawnumber); } catch (Exception exception) { // Assert exception.Message.Should().Be("Drawnumber: 123456 not found"); throw; } } }
public object Add(HttpPostedFileBase file, HttpPostedFileBase image, string title, string genre, string language, int rating, string description, string author_name, string country, string publisher_name, float price = 0) { if (!ModelState.IsValid || file == null) { ViewBag.Alert = "Not valid"; return(RedirectToAction("Add")); } if (!file.FileName.Contains(".docx") && !file.FileName.Contains(".doc") && !file.FileName.Contains(".pdf") && !file.FileName.Contains(".mobi") && !file.FileName.Contains(".rtf")) { ViewBag.Alert = "Wrong file format (.pdf, .doc, .docx, .rtf, .mobi)"; return(RedirectToAction("Add")); } description = description.Trim(); Author author = new Author { Author_Name = author_name, Country = country }; Publisher publisher = new Publisher { Publisher_Name = publisher_name }; Book book = new Book { Title = title, Rating = rating, Genre = genre, Price = price, Description = description, Language = language, Author = author, Author_Name_fk = author_name, Publisher_Name_fk = publisher_name, Publisher = publisher }; if (file != null) { string uniqueName = Guid.NewGuid().ToString() + file.FileName; string fileName = HttpContext.Server.MapPath("~/Static/Files/") + uniqueName; file.SaveAs(fileName); book.fileUrl = uniqueName; } string converted_path = ""; string imgName = ""; if (image != null) { DrawRepository draw = new DrawRepository(); string uniqueName = Guid.NewGuid().ToString() + image.FileName; imgName = HttpContext.Server.MapPath("~/Static/Images/") + uniqueName; image.SaveAs(imgName); uniqueName = uniqueName.Replace(".jpg", ".png"); System.Drawing.Image converted = System.Drawing.Image.FromFile(imgName); converted_path = imgName; converted_path = converted_path.Replace(".jpg", ".png"); converted.Save(converted_path, System.Drawing.Imaging.ImageFormat.Png); draw.Resize(converted_path, 1400, 2046); book.ImgUrl = uniqueName.Replace(".png", "_" + 1400 + "x" + 2046 + ".png"); } BookRepository bookRepository = new BookRepository(); try { string result = bookRepository.AddBook(book); if (result == "Added") { return(RedirectToAction("Products", "Home")); } else { return("Book exists already"); } } catch (Exception) { return(RedirectToAction("Add")); } }
public DrawController(DrawRepository repository) { _repository = repository; }