Example #1
0
        public async Task TestGetVersions()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);
            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            testingContext.AddPrincipalMock(question.User.UserId, Roles.User);
            testingContext.AddBusinessSecurityService();

            IQuestionService questionService = testingContext.GetService <QuestionService>();

            testingContext.DependencyMap[typeof(IQuestionService)] = questionService;
            IVersionService versionService = testingContext.GetService <VersionService>();


            int newId = await versionService.VersionQuestionAsync(question.Id);

            await versionService.AcceptQuestionAsync(newId);

            VersionInfoParametersDto versionInfoParametersDto = new VersionInfoParametersDto()
            {
                Page       = 0,
                IdQuestion = newId,
            };

            //Act
            PagedResultDto <VersionInfoDto> result = await versionService.GetVersionsAsync(versionInfoParametersDto);

            //Assert
            Assert.Equal(2, result.Count);
        }
Example #2
0
        public async Task UpdateQuestionOk()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            InitContext(testingContext);
            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            testingContext.AddPrincipalMock(question.User.UserId, Roles.User);
            testingContext.AddBusinessSecurityService();
            IQuestionService questionService = testingContext.GetService <QuestionService>();

            QuestionDto dto = await questionService.GetQuestionAsync(question.Id);

            dto.Title = "Another Title";

            //Act
            await questionService.UpdateQuestionAsync(dto);

            //Assert
            QuestionDto dtoResult = await questionService.GetQuestionAsync(question.Id);

            Assert.Equal("Another Title", dtoResult.Title);
        }
Example #3
0
        public async Task TestNotEnoughQuestion()
        {
            //Prepare
            TestingContext context = new TestingContext();

            context.AddPrincipalMock();
            context.AddBinaryServiceMock();
            context.AddInMemoryDb();
            context.AddMockLogService();
            context.AddGermanCultureServiceMock();
            context.AddQuestionService();
            CourseService courseService = context.GetService <CourseService>();

            context.DependencyMap[typeof(ICourseService)] = courseService;

            ApplicationDbContext dbContext = context.GetSimple <ApplicationDbContext>();
            Course course = dbContext.Courses.First();

            course.NumQuestionsToAsk = 100;

            ICourseInstanceService courseInstanceService = context.GetService <CourseInstanceService>();

            //Act
            var courseInstance = await courseInstanceService.StartCourseAsync(course.Id);

            //Assert
            Assert.Equal(10, courseInstance.NumQuestionsTotal);
        }
Example #4
0
 private void SetUpTestingContext(TestingContext testingContext)
 {
     testingContext.AddPrincipalMock();
     testingContext.AddBinaryServiceMock();
     testingContext.AddMockLogService();
     testingContext.AddInMemoryDb();
 }
Example #5
0
        public async Task TestCopyQuestion()
        {
            //Prepare
            TestingContext context = new TestingContext();

            InitContext(context);
            ApplicationDbContext dbContext = context.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            context.AddPrincipalMock(question.User.UserId, Roles.User);

            IQuestionService questionService = context.GetService <QuestionService>();

            context.DependencyMap[typeof(IQuestionService)] = questionService;
            IVersionService versionService = context.GetService <VersionService>();

            //Act
            int newId = await versionService.CopyQuestionAsync(question.Id);

            //Assert
            Assert.True(question.Id != newId);
            Assert.True(newId > 0);
            QuestionDto newQuestion = await questionService.GetQuestionAsync(newId);

            Assert.Equal(question.Options.Count, newQuestion.Options.Count);
            Assert.Equal(question.QuestionType, newQuestion.QuestionType);
            for (int i = 0; i < question.Options.Count; i++)
            {
                QuestionOption    optionSource      = question.Options[i];
                QuestionOptionDto optionDestination = newQuestion.Options[i];

                Assert.NotEqual(optionSource.Id, optionDestination.Id);
                Assert.True(optionDestination.Id > 0);
            }
        }
Example #6
0
 private void SetupContext(TestingContext context)
 {
     context.AddPrincipalMock();
     context.AddBinaryServiceMock();
     context.AddInMemoryDb();
     context.AddLogServiceMock();
     context.AddCacheService();
 }
Example #7
0
 private void SetUpTestingContext(TestingContext testingContext)
 {
     testingContext.AddPrincipalMock();
     testingContext.AddBinaryServiceMock();
     testingContext.AddLogServiceMock();
     testingContext.AddInMemoryDb();
     testingContext.AddUserService();
     testingContext.AddBusinessSecurityService();
 }
Example #8
0
 private void InitContext(TestingContext context)
 {
     context.AddPrincipalMock();
     context.AddInMemoryDb();
     context.AddMockLogService();
     context.AddGermanCultureServiceMock();
     context.AddBinaryServiceMock();
     context.AddQuestionService();
     context.DependencyMap[typeof(IBinaryService)] = context.GetService <BinaryService>();
 }
 private void InitTestingContext(TestingContext testingContext)
 {
     testingContext.AddPrincipalMock();
     testingContext.AddBinaryServiceMock();
     testingContext.AddInMemoryDb();
     testingContext.AddUserService();
     testingContext.AddBusinessSecurityService();
     testingContext.AddLogServiceMock();
     testingContext.AddGermanCultureServiceMock();
     testingContext.AddQuestionService();
 }
Example #10
0
 private void InitContext(TestingContext testingContext)
 {
     testingContext.AddPrincipalMock();
     testingContext.AddInMemoryDb();
     testingContext.AddUserService();
     testingContext.AddBusinessSecurityService();
     testingContext.AddLogServiceMock();
     testingContext.AddGermanCultureServiceMock();
     testingContext.AddBinaryServiceMock();
     testingContext.AddQuestionService();
     testingContext.DependencyMap[typeof(IBinaryService)] = testingContext.GetService <BinaryService>();
 }
Example #11
0
        public async Task TestVersionQuestion()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            SetUpTestingContext(testingContext);
            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            testingContext.AddPrincipalMock(question.User.UserId, Roles.User);
            testingContext.AddBusinessSecurityService();

            IQuestionService questionService = testingContext.GetService <QuestionService>();

            testingContext.DependencyMap[typeof(IQuestionService)] = questionService;

            Mock <INotificationService> notificationMock = new Mock <INotificationService>(MockBehavior.Loose);

            notificationMock.Setup(x => x.SendNotificationAboutQuestionAsync(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.CompletedTask);
            testingContext.DependencyMap[typeof(INotificationService)] = notificationMock.Object;

            IVersionService versionService = testingContext.GetService <VersionService>();

            //Act
            int newId = await versionService.VersionQuestionAsync(question.Id);

            //Assert
            Assert.True(question.Id != newId);
            Assert.True(newId > 0);

            await versionService.AcceptQuestionAsync(newId);

            //Assert
            QuestionDto oldDto = await questionService.GetQuestionAsync(question.Id);

            QuestionDto newDto = await questionService.GetQuestionAsync(newId);

            Assert.Contains(oldDto.Tags, x => x.Name == "Deprecated");
            Assert.Equal(QuestionStatus.Released, (QuestionStatus)newDto.Status);
            Assert.Equal(QuestionStatus.Obsolete, (QuestionStatus)oldDto.Status);
            question = await dbContext.FindAsync <Question>(newId);

            Assert.NotNull(question.ApprovalDate);
            notificationMock.Verify(x => x.SendNotificationAboutQuestionAsync(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>()), Times.Once());
        }
Example #12
0
        private void SetUpTestingContext(TestingContext testingContext)
        {
            testingContext.AddPrincipalMock();
            testingContext.AddBinaryServiceMock();
            testingContext.AddLogServiceMock();
            testingContext.AddCranAppSettings();
            testingContext.AddInfoTextServiceMock();
            testingContext.AddInMemoryDb();
            testingContext.AddUserService();

            Mock <IWebPushClient> webPushClientMock = new Mock <IWebPushClient>(MockBehavior.Loose);

            webPushClientMock.Setup(x => x.SendNotificationAsync(It.IsAny <PushSubscription>(),
                                                                 It.IsAny <string>(), It.IsAny <VapidDetails>()))
            .Returns(Task.CompletedTask);

            testingContext.DependencyMap[typeof(IWebPushClient)] = webPushClientMock.Object;
        }
Example #13
0
        private void SetUpTestingContext(TestingContext testingContext)
        {
            testingContext.AddPrincipalMock();
            testingContext.AddInMemoryDb();
            testingContext.AddUserService();
            testingContext.AddBusinessSecurityService();
            testingContext.AddLogServiceMock();
            testingContext.AddGermanCultureServiceMock();
            testingContext.AddBinaryServiceMock();
            testingContext.AddInfoTextServiceMock();
            testingContext.AddCacheService();
            testingContext.DependencyMap[typeof(ICommentsService)] = testingContext.GetService <CommentsService>();
            testingContext.DependencyMap[typeof(ITagService)]      = testingContext.GetService <TagService>();



            Mock <INotificationService> notificationMock = new Mock <INotificationService>(MockBehavior.Loose);

            notificationMock.Setup(x => x.SendNotificationAboutQuestionAsync(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.CompletedTask);

            testingContext.DependencyMap[typeof(INotificationService)] = notificationMock.Object;
        }
Example #14
0
        public async Task TestVersionQuestion()
        {
            //Prepare
            TestingContext context = new TestingContext();

            InitContext(context);
            ApplicationDbContext dbContext = context.GetSimple <ApplicationDbContext>();
            Question             question  = dbContext.Questions.First();

            context.AddPrincipalMock(question.User.UserId, Roles.User);

            IQuestionService questionService = context.GetService <QuestionService>();

            context.DependencyMap[typeof(IQuestionService)] = questionService;
            IVersionService versionService = context.GetService <VersionService>();

            //Act
            int newId = await versionService.VersionQuestionAsync(question.Id);

            //Assert
            Assert.True(question.Id != newId);
            Assert.True(newId > 0);

            await versionService.AcceptQuestionAsync(newId);

            //Assert
            QuestionDto oldDto = await questionService.GetQuestionAsync(question.Id);

            QuestionDto newDto = await questionService.GetQuestionAsync(newId);

            Assert.Contains(oldDto.Tags, x => x.Name == "Deprecated");
            Assert.Equal(QuestionStatus.Released, (QuestionStatus)newDto.Status);
            Assert.Equal(QuestionStatus.Obsolete, (QuestionStatus)oldDto.Status);
            question = await dbContext.FindAsync <Question>(newId);

            Assert.NotNull(question.ApprovalDate);
        }
Example #15
0
        public async Task TestStartTest()
        {
            TestingContext context = new TestingContext();

            context.AddPrincipalMock();
            context.AddBinaryServiceMock();
            context.AddInMemoryDb();
            context.AddMockLogService();
            context.AddGermanCultureServiceMock();
            context.AddQuestionService();
            ICourseService courseService = context.GetService <CourseService>();

            context.DependencyMap[typeof(ICourseService)] = courseService;

            ICourseInstanceService courseInstanceService = context.GetService <CourseInstanceService>();


            var courses = await courseService.GetCoursesAsync(0);

            int courseId = courses.Data.Select(x => x.Id).First();

            //Act
            CourseInstanceDto result = await courseInstanceService.StartCourseAsync(courseId);

            Assert.True(result.IdCourseInstance > 0);
            Assert.True(result.IdCourse == courseId);
            Assert.True(result.IdCourseInstanceQuestion > 0);
            Assert.True(result.NumQuestionsAlreadyAsked == 0);
            Assert.True(result.NumQuestionsTotal > 0);

            CourseInstanceDto result2 = await courseInstanceService.NextQuestion(result.IdCourseInstance);

            Assert.True(result2.IdCourse == courseId);
            Assert.True(result2.IdCourseInstanceQuestion > 0);
            Assert.True(result2.NumQuestionsAlreadyAsked == 1);
            Assert.True(result2.NumQuestionsTotal > 0);

            CourseInstanceDto result3 = await courseInstanceService.NextQuestion(result.IdCourseInstance);

            Assert.True(result3.IdCourse == courseId);
            Assert.True(result3.IdCourseInstanceQuestion > 0);
            Assert.True(result3.NumQuestionsAlreadyAsked == 2);
            Assert.True(result3.NumQuestionsTotal > 0);

            QuestionToAskDto result4 = await courseInstanceService.GetQuestionToAskAsync(result.IdCourseInstanceQuestion);

            Assert.Equal(QuestionType.MultipleChoice, result4.QuestionType);

            QuestionAnswerDto answer = new QuestionAnswerDto();

            answer.IdCourseInstanceQuestion = result.IdCourseInstanceQuestion;
            answer.Answers.Add(false);
            answer.Answers.Add(true);
            answer.Answers.Add(false);
            answer.Answers.Add(true);

            QuestionDto result5 = await courseInstanceService.AnswerQuestionAndGetSolutionAsync(answer);

            Assert.True(result5.Explanation != null);
            Assert.Equal(4, result5.Options.Count);
            Assert.True(result5.Options[1].IsTrue);
            Assert.True(result5.Options[3].IsTrue);


            CourseInstanceDto result6 = await courseInstanceService.AnswerQuestionAndGetNextQuestionAsync(answer);

            Assert.True(result6.AnsweredCorrectly);
            Assert.True(result6.Done);
            Assert.Equal(3, result6.NumQuestionsAlreadyAsked);
            Assert.Equal(3, result6.NumQuestionsTotal);
        }