public void ShouldReturnErrorMessageIfSurveyNotFound() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentSurveys = context.Surveys.Count(); var surveyDto = new SurveyDto { Id = -1 }; var service = new RemoveSurveyService(context); var result = service.RemoveSurvey(surveyDto); result.ShouldNotBeNull(); result.First().ErrorMessage.ShouldEqual($"A survey with the ID of {surveyDto.Id} was not found."); context.SaveChanges(); context.Surveys.Count().ShouldEqual(currentSurveys); } }
public async Task ShouldReturnBadObjectResult() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var controller = new TestController(); var errorMessage = "Could not retrieve surveys."; var mockService = new Mock <IListSurveysService>(); mockService.Setup(m => m.GetSurveys()).Returns((Task.FromResult((List <Survey>)null))); var result = await controller.GetListOfSurveys(mockService.Object); result.ShouldNotBeNull(); result.ShouldBeType <BadRequestObjectResult>(); var r = result as BadRequestObjectResult; r.Value.ShouldEqual(errorMessage); } }
public IntervieweesPreloader(SurveyDbContext dbContext) { lock (lockObject) { companyModelInterviewees ??= dbContext .Interviewees .AsNoTracking() .Include(i => i.CompanyAnswers) .ThenInclude(ca => ca.Company) .AsEnumerable() .DistinctBy(i => i.Id) .ToList(); metaInterviewees ??= dbContext .Interviewees .AsNoTracking() .Include(i => i.Languages) .ThenInclude(il => il.Language) .Include(i => i.CommunitySources) .ThenInclude(cs => cs.CommunitySource) .Include(i => i.MotivationFactors) .ThenInclude(mf => mf.MotivationFactor) .AsEnumerable() .DistinctBy(i => i.Id) .ToList(); } }
public void TestSaveSurveyNoName() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedSurveys = context.CompletedSurveys.Count(); var completedQuestionsDto = EfTestData.CreateCompletedQuestionsDto(); var completedSurveyDto = new CompletedSurveyDto { CaseNo = 999, Questions = completedQuestionsDto }; var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedSurvey(completedSurveyDto); context.SaveChanges(); result.ShouldNotBeNull(); service.Errors.Count.ShouldEqual(1); service.Errors.First().ErrorMessage.ShouldEqual("A survey name has not been supplied."); context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys); } }
public void TestSaveSurveyNoQuestions() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedSurveys = context.CompletedSurveys.Count(); var completedQuestionsDto = EfTestData.CreateCompletedQuestionsDto(); var completedSurveyDto = new CompletedSurveyDto { Name = "Test Completed Survey", CaseNo = 999 }; var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedSurvey(completedSurveyDto); context.SaveChanges(); result.ShouldNotBeNull(); result.First().ErrorMessage.ShouldEqual("No Questions have been submitted with this completed survey."); context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys); } }
public void ShouldReturnErrorMessageWhenQuestionGroupNotFound() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var questionGroupId = -1; var currentQuestions = context.Questions.Count(); var questionDtos = new List <QuestionDto> { new QuestionDto { Text = "How well have you been sleeping?", Type = 1 } }; var surveyDto = new SurveyDto { Id = 1, QuestionGroupId = questionGroupId, QuestionsDtos = questionDtos }; var service = new AddQuestionService(context, new MapQuestionDtoToQuestionsService()); var result = service.AddQuestion(surveyDto); result.ShouldNotBeNull(); result.First().ErrorMessage.ShouldEqual($"Could not find QuestionGroup with an Id of {questionGroupId}"); context.SaveChanges(); context.Questions.Count().ShouldEqual(currentQuestions); } }
public void TestSaveSurveyOk() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedSurveys = context.CompletedSurveys.Count(); var completedQuestionsDto = EfTestData.CreateCompletedQuestionsDto(); var completedSurveyDto = new CompletedSurveyDto { Name = "Test Completed Survey", CaseNo = 999, Questions = completedQuestionsDto }; var service = new SaveCompletedSurveysService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedSurvey(completedSurveyDto); result.ShouldBeNull(); context.SaveChanges(); service.Errors.Count.ShouldEqual(0); context.CompletedSurveys.Count().ShouldEqual(currentCompletedSurveys + 1); } }
public void ShouldReturnErrorMessageWhenQuestionTextNotSpecified() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentQuestions = context.Questions.Count(); var questionDtos = new List <QuestionDto> { new QuestionDto { Type = 1 } }; var surveyDto = new SurveyDto { Id = 1, QuestionGroupId = 1, QuestionsDtos = questionDtos }; var service = new AddQuestionService(context, new MapQuestionDtoToQuestionsService()); var result = service.AddQuestion(surveyDto); result.ShouldNotBeNull(); result.First().ErrorMessage.ShouldEqual("A question has been submitted without any text."); context.SaveChanges(); context.Questions.Count().ShouldEqual(currentQuestions); } }
public void ShouldAddAQuestion() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentQuestions = context.Questions.Count(); var questionDtos = new List <QuestionDto> { new QuestionDto { Text = "How well have you been sleeping?", Type = 1 } }; var surveyDto = new SurveyDto { Id = 1, QuestionGroupId = 1, QuestionsDtos = questionDtos }; var service = new AddQuestionService(context, new MapQuestionDtoToQuestionsService()); var result = service.AddQuestion(surveyDto); result.ShouldBeNull(); context.SaveChanges(); context.Questions.Count().ShouldEqual(currentQuestions + 1); } }
public ActionResult Register(LoginAndRegisterModel register) { if (ModelState.IsValid && register.Nick.Length >= 8) { using (SurveyDbContext entities = new SurveyDbContext()) { if (entities.Users.Any(X => X.Nick == register.Nick)) { return(View()); } else { User surveyTaker = new User(); Random random = new Random(); surveyTaker.UserId = random.Next(); surveyTaker.Nick = register.Nick; surveyTaker.Password = register.Pass; entities.Users.Add(surveyTaker); entities.SaveChanges(); entities.Dispose(); return(RedirectToAction("Login", "Home")); } } } else { return(View()); } }
public void ShouldReturnAnErrorMessageIfNewNameNotProvided() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentName = context.Surveys.First(m => m.SurveyId == 1).Name; var surveyDto = new SurveyDto { Id = 1, NewName = "" }; var service = new RenameSurveyService(context); var result = service.RenameSurvey(surveyDto); result.ShouldNotBeNull(); result.First().ErrorMessage.ShouldEqual("No new name has been provided for this survey."); context.SaveChanges(); context.Surveys.First(m => m.SurveyId == surveyDto.Id).Name.ShouldEqual(currentName); } }
public void ShouldAddQuestionGroup() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentQuestionGroups = context.QuestionGroups.Count(); var questions = EfTestData.CreateQuestionsDtos(); var questionGroupsDto = new List <QuestionGroupDto> { new QuestionGroupDto { Name = "Test QuestionGroup Dto", Questions = questions } }; var surveyDto = new SurveyDto { Id = 1, QuestionGroupsDtos = questionGroupsDto }; var service = new AddQuestionGroupService(context, new MapQuestionsToGroupService()); var result = service.AddQuestionGroup(surveyDto); result.ShouldBeNull(); context.SaveChanges(); context.QuestionGroups.Count().ShouldEqual(currentQuestionGroups + 1); } }
public void ShouldReturnErrorMessageWhenSurveyNotFound() { var options = SqliteInMemory.CreateOptions<SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentCompletedQuestions = context.CompletedQuestions.Count(); var completedSurveyId = -1; var completedSurveyDto = new CompletedSurveyDto { CompletedSurveyId = completedSurveyId, Name = "Test Completed Survey", CaseNo = 999, Questions = completedQuestionsDto }; var service = new SaveCompletedQuestionToCompletedSurveyService(context, new MapCompletedQuestionsFromDtoService()); var result = service.SaveCompletedQuestion(completedSurveyDto); result.ShouldNotBeNull(); context.SaveChanges(); result.First().ErrorMessage.ShouldEqual($"Could not find Survey with an Id of {completedSurveyId}"); context.CompletedQuestions.Count().ShouldEqual(currentCompletedQuestions); } }
private void Open(Guid? oid = null) { Processing = true; Unwire(); IsDirty = false; _shouldUpdateSurveyTemplateQuestions = false; Question theQuestion = null; using (var db = new SurveyDbContext()) { Expression<Func<Question, bool>> findBy; if (oid.HasValue) findBy = (subject) => subject.Id.Equals(oid.Value); else findBy = (subject) => subject.QuestionNo == QuestionNo; Expression<Func<Question, bool>> whereClause = findBy.CombineWithAnd(_whereClause); theQuestion = db.Questions .Where(whereClause) .FirstOrDefault(); } Question = theQuestion; if (Question == null) { Processing = false; RefreshAll(); return; } QuestionNo = Question.QuestionNo; Processing = false; RefreshAll(); }
public void UpdateDatabase(SurveyDbContext surveyEntities) { if (String.IsNullOrEmpty(Soru) || Choices.Count(x => !String.IsNullOrEmpty(x.Yazı)) < 2) { surveyEntities.Choices.RemoveRange(surveyEntities.Choices.Where(x => x.QuestionId == Questionİd)); surveyEntities.Answers.RemoveRange(surveyEntities.Answers.Where(x => x.QuestionId == Questionİd)); surveyEntities.Questions.Remove(surveyEntities.Questions.First(X => X.QuestionId == Questionİd)); } else { foreach (var item2 in Choices) { if (String.IsNullOrEmpty(item2.Yazı)) { surveyEntities.Choices.Remove(surveyEntities.Choices.Single(x => x.ChoiceId == item2.ChoiceId)); } else { surveyEntities.Choices.Single(x => x.ChoiceId == item2.ChoiceId).Yazı = item2.Yazı; } } if (Soru.Length > 5) { surveyEntities.Questions.Single(x => x.QuestionId == Questionİd).Soru = Soru; } } }
public ActionResult Index() { EnteringTestModel enteringTestModel = new EnteringTestModel(); int surveyTaker = (int)Session["CurrentUser"]; enteringTestModel.TestThatUserCanTake = new List <ManageTestModel>(); SurveyDbContext surveyEntities = new SurveyDbContext(); foreach (var item in surveyEntities.Tests.Where(x => x.Open == true).Include(x => x.Questions.Select(p => p.Answers))) { bool eklenecekmi = true; foreach (var item2 in item.Questions) { if (item2.Answers.Any(p => p.UserId == surveyTaker)) { eklenecekmi = false; } } if (eklenecekmi == true) { enteringTestModel.TestThatUserCanTake.Add(new ManageTestModel() { id = item.TestId, Title = item.Title }); } } return(View(enteringTestModel)); }
public ActionResult Taking(ModelTest test) { if (test.Questions.All(x => x.Seçim != null)) { SurveyDbContext surveyEntities = new SurveyDbContext(); int surveytakerid = (int)Session["CurrentUser"]; int aı = surveyEntities.Answers.Count() + 1; int i = 0; foreach (var item in test.Questions) { Answer answer = new Answer(); answer.AnswerId = aı; while (surveyEntities.Answers.Any(x => x.AnswerId == answer.AnswerId)) { answer.AnswerId = new Random().Next(); } answer.QuestionId = item.Question.QuestionId; answer.UserId = surveytakerid; answer.Seçim = item.Seçim; surveyEntities.Answers.Add(answer); aı++; i++; } surveyEntities.SaveChanges(); return(RedirectToAction("Index", "Home")); } else { TempData["Error"] = "Tüm Sorulara Cevap Vermelisiniz"; return(RedirectToAction("Taking", "TakingSurvey", new { id = test.id })); } }
public StatusGenericHandler RenameSurvey(string newName, SurveyDbContext context = null) { var status = new StatusGenericHandler(); if (string.IsNullOrWhiteSpace(newName)) { status.AddError("A new name has not been provided"); return(status); } if (context == null) { status.AddError("You must provide a context if you want to remove this Survey.", nameof(context)); return(status); } var nameAlreadyTaken = context.Surveys.Any(m => m.Name == newName); if (nameAlreadyTaken) { status.AddError("A survey with the name you have provided already exists.", nameof(context)); return(status); } Name = newName; return(status); }
public StatusGenericHandler AddQuestion(string text, QuestionType type, SurveyDbContext context = null) { var status = new StatusGenericHandler(); var question = new Question(text, type, this); if (_questions != null) { _questions.Add(question); } else if (context == null) { status.AddError("You must provide a context if you want to remove this Survey.", nameof(context)); return(status); } else if (context.Entry(this).IsKeySet) { context.Add(new Question(text, type, this)); } else { status.AddError("Could not add a new survey.", nameof(context)); return(status); } return(status); }
public ActionResult Edit(int id) { Session["id"] = id; CreateEditTestModel createEditTestModel = new CreateEditTestModel(); createEditTestModel.CreatingQuestionmodels = new List <CreatingTestQuestionmodel>(); createEditTestModel.EditQuestionModels = new List <EditQuestionModel>(); SurveyDbContext surveyEntities = new SurveyDbContext(); createEditTestModel.Title = surveyEntities.Tests.Single(x => x.TestId == id).Title; foreach (var item in surveyEntities.Questions.Where(x => x.TestId == id).Include(x => x.Choices)) { List <Choice> vs = new List <Choice>(); foreach (var item1 in item.Choices) { Choice choice = item1; vs.Add(choice); } EditQuestionModel editQuestionModel = new EditQuestionModel { Questionİd = item.QuestionId, Choices = vs, Soru = item.Soru }; createEditTestModel.EditQuestionModels.Add(editQuestionModel); } for (int i = 0; i < (10 - createEditTestModel.EditQuestionModels.Count); i++) { CreatingTestQuestionmodel model = new CreatingTestQuestionmodel(); model.Choices = new List <string>(); for (int j = 0; j < 5; j++) { model.Choices.Add(""); } createEditTestModel.CreatingQuestionmodels.Add(model); } return(View("CreateTest", createEditTestModel)); }
public void ShouldReturnAnErrorMessageWhenNoNameProvided() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentSurveys = context.Surveys.Count(); var questionGroups = EfTestData.CreateQuestionGroupDtos(); var surveyDto = new SurveyDto { QuestionGroupsDtos = questionGroups, }; var service = new AddSurveyService(context, new MapQuestionsToGroupService()); var result = service.AddSurvey(surveyDto); result.ShouldNotBeNull(); context.SaveChanges(); result.First().ErrorMessage.ShouldEqual("A name has not been provided for this survey."); context.Surveys.Count().ShouldEqual(currentSurveys); } }
public StatusGenericHandler AddQuestion(CompletedQuestion question, SurveyDbContext context = null) { var status = new StatusGenericHandler(); if (string.IsNullOrWhiteSpace(question.Answer)) { status.AddError("An answer is needed when submitting a question.", nameof(question.Answer)); return(status); } if (_completedQuestions != null) { var completedQuestion = new CompletedQuestion(question.Question, question.Answer, this); _completedQuestions.Add(completedQuestion); } else if (context == null) { status.AddError("You must provide a context if the CompletedQuestions collection isn't valid.", nameof(context)); return(status); } else if (context.Entry(this).IsKeySet) { context.Add(new CompletedQuestion(question.Question, question.Answer, this)); } else { status.AddError("Could not add a new CompletedQuestion."); return(status); } return(status); }
public void ShouldReturnAnErrorMessageWhenGroupNoNameProvided() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var currentQuestionGroups = context.QuestionGroups.Count(); var questions = EfTestData.CreateQuestionsDtos(); var questionGroupsDto = new List <QuestionGroupDto> { new QuestionGroupDto { Questions = questions } }; var surveyDto = new SurveyDto { Id = 1, QuestionGroupsDtos = questionGroupsDto }; var service = new AddQuestionGroupService(context, new MapQuestionsToGroupService()); var result = service.AddQuestionGroup(surveyDto); result.ShouldNotBeNull(); context.SaveChanges(); result.First().ErrorMessage.ShouldEqual("A name is needed when creating a new question group."); context.QuestionGroups.Count().ShouldEqual(currentQuestionGroups); } }
public bool AddVote(VoteContract vote) { try { using (var db = new SurveyDbContext()) { if (!db.Vote.Any(a => a.IdVote == vote.IdVote)) { db.Vote.Add(new Data.Entities.Vote { IdVote = vote.IdVote, IdAnswer = vote.IdAnswer }); if (vote.SenderId == App.AppId) { db.MyVotes.Add(new Data.Entities.MyVotes { IdAnswer = vote.IdAnswer, IdSurvey = vote.IdSurvey }); } db.SaveChanges(); return(true); } return(false); } } catch (Exception e) { Logger.Log(e); return(false); } }
public bool AddSurvey(SurveyContract survey) { try { using (var db = new SurveyDbContext()) { if (!db.Survey.Any(a => a.IdSurvey == survey.IdSurvey)) { db.Survey.Add(new Data.Entities.Survey { IdSurvey = survey.IdSurvey, Description = survey.Description, Name = survey.Name, StartDateUTC = DateTime.Now.ToUniversalTime(), EndDateUTC = survey.EndDateUTC }); var answers = survey.Answers.Select(s => new Data.Entities.Answer { IdAnswer = s.IdAnswer, IdSurvey = survey.IdSurvey, Text = s.Text }); db.Answer.AddRange(answers); db.SaveChanges(); return(true); } return(false); } } catch (Exception e) { Logger.Log(e); return(false); } }
public async Task ShouldReturnOkObjectResult() { var options = SqliteInMemory.CreateOptions <SurveyDbContext>(); using (var context = new SurveyDbContext(options)) { context.Database.EnsureCreated(); context.SeedDataBaseWithSurveys(); var controller = new TestController(); var surveys = EfTestData.CreateSurveys(); var mockService = new Mock <IListSurveysService>(); mockService.Setup(m => m.GetSurveys()).Returns((Task.FromResult((surveys)))); var result = await controller.GetListOfSurveys(mockService.Object); result.ShouldNotBeNull(); result.ShouldEqual(result as OkObjectResult); var r = result as OkObjectResult; r.Value.ShouldEqual(surveys); } }
public static void SeedDataBaseWithSurveys(this SurveyDbContext context) { context.Surveys.AddRange(CreateSurveys()); context.CompletedSurveys.AddRange(CreateCompletedSurveys()); context.QuestionTypes.AddRange(CreateQuestionTypes()); context.SaveChanges(); }
public DatabaseFixture() { var options = new DbContextOptionsBuilder <SurveyDbContext>() .UseInMemoryDatabase("IntegrationTests") .Options; DbContext = new SurveyDbContext(options); }
public IEnumerable <SurveyListItem> GetSurveys() { using (var db = new SurveyDbContext()) { var surveys = db.Survey.AsNoTracking().Select(s => new SurveyListItem { IdSurvey = s.IdSurvey, Name = s.Name }).ToArray(); return(surveys); } }
public SaveCompletedSurveysService(SurveyDbContext context, IMapCompletedQuestionsFromDtoService mapper) { _context = context; var saveCompletedSurveyDbAccess = new SaveCompletedSurveyDbAccess(_context); _saveCompletedSurveyAction = new SaveCompletedSurveyAction(saveCompletedSurveyDbAccess); _runner = new RunnerWriteDb <SaveCompletedSurveyDto, CompletedSurvey>(_saveCompletedSurveyAction, _context); _mapper = mapper; }