Esempio n. 1
0
        public async Task GetTextEnTest()
        {
            TestingContext testingContext = new TestingContext();

            testingContext.AddAdminPrincipalMock();
            testingContext.AddInMemoryDb();
            testingContext.AddMockLogService();
            testingContext.AddEnglishCultureServiceMock();

            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();

            dbContext.Texts.Add(new Text()
            {
                Key       = "TestKey",
                ContentDe = "ContentDe  {0} {1} {2}",
                ContentEn = "ContentEn {0} {1} {2}",
            });
            await dbContext.SaveChangesAsync();

            ITextService textService = testingContext.GetService <TextService>();

            //Act
            string text = await textService.GetTextAsync("TestKey", "pl1", "pl2", "pl3");

            //Assert
            Assert.Equal("ContentEn pl1 pl2 pl3", text);
        }
Esempio n. 2
0
        public async void TestExport()
        {
            //Prepare
            TestingContext context = new TestingContext();

            context.AddAdminPrincipalMock();
            context.AddBinaryServiceMock();
            context.AddInMemoryDb();
            context.AddMockLogService();
            context.AddGermanCultureServiceMock();
            context.AddQuestionService();

            IExportService exportService = context.GetService <ExportService>();

            //Act
            Stream stream = await exportService.Export();

            //Assert
            ZipArchive      arch           = new ZipArchive(stream);
            ZipArchiveEntry entry          = arch.GetEntry("questions.json");
            Stream          zipEntryStream = entry.Open();

            using (var reader = new StreamReader(zipEntryStream, Encoding.UTF8))
            {
                string value = reader.ReadToEnd();
                IList <QuestionDto> questions = JsonConvert.DeserializeObject <IList <QuestionDto> >(value);
                Assert.True(questions.Count > 0);
            }
            Assert.True(arch.Entries.Count > 0);
        }
Esempio n. 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);
        }
Esempio n. 4
0
 private void SetUpTestingContext(TestingContext testingContext)
 {
     testingContext.AddPrincipalMock();
     testingContext.AddBinaryServiceMock();
     testingContext.AddMockLogService();
     testingContext.AddInMemoryDb();
 }
Esempio n. 5
0
        public async Task TestTag()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            testingContext.AddAdminPrincipalMock();
            testingContext.AddMockLogService();
            testingContext.AddRealDb();


            ApplicationDbContext context = testingContext.GetSimple <ApplicationDbContext>();

            Tag tag = new Tag
            {
                Name        = "Test",
                TagType     = TagType.Standard,
                Description = "Desc",
                ShortDescDe = "ShortDescDe",
                ShortDescEn = "ShortDescEn",
            };

            context.Tags.Add(tag);

            //Act
            await context.SaveChangesAsync();

            //Assert
            Assert.True(tag.Id > 0);

            //Cleanup
            context.Remove(tag);
            context.SaveChanges();
        }
Esempio n. 6
0
 private void SetupContext(TestingContext context)
 {
     context.AddPrincipalMock();
     context.AddBinaryServiceMock();
     context.AddInMemoryDb();
     context.AddMockLogService();
     context.AddCacheService();
 }
Esempio n. 7
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>();
 }
Esempio n. 8
0
        public async void TestPaging()
        {
            TestingContext testingContext = new TestingContext();

            testingContext.AddAdminPrincipalMock();
            testingContext.AddInMemoryDb();
            testingContext.AddMockLogService();
            testingContext.AddEnglishCultureServiceMock();

            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();

            for (int i = 0; i < 13; i++)
            {
                dbContext.Texts.Add(new Text()
                {
                    Key       = $"TestKey{i}",
                    ContentDe = $"ContentDe",
                    ContentEn = $"ContentEn",
                });
            }

            await dbContext.SaveChangesAsync();

            ITextService textService = testingContext.GetService <TextService>();

            //Act
            SearchTextDto dto = new SearchTextDto();

            dto.Page = 0;
            PagedResultDto <TextDto> res = await textService.GetTextsAsync(dto);

            //Assert
            Assert.Equal(13, res.Count);
            Assert.Equal(5, res.Pagesize);
            Assert.Equal(5, res.Data.Count);
            Assert.Equal(3, res.Numpages);

            //Act
            dto      = new SearchTextDto();
            dto.Page = 2;
            res      = await textService.GetTextsAsync(dto);

            //Assert
            Assert.Equal(13, res.Count);
            Assert.Equal(5, res.Pagesize);
            Assert.Equal(3, res.Data.Count);
            Assert.Equal(3, res.Numpages);
        }
Esempio n. 9
0
        public async Task SaveBinaryTest()
        {
            TestingContext context = new TestingContext();

            context.AddAdminPrincipalMock();
            context.AddMockLogService();
            context.AddRealDb();

            IBinaryService service = context.GetService <BinaryService>();

            string testWord = "Hello World";
            var    bytes    = System.Text.Encoding.UTF8.GetBytes(testWord);

            MemoryStream stream = new MemoryStream();

            stream.Write(bytes, 0, bytes.Length);
            stream.Flush();
            stream.Seek(0, SeekOrigin.Begin);



            BinaryDto binaryDto = new BinaryDto
            {
                ContentDisposition = "ContentDisposition",
                ContentType        = "ContentType",
                FileName           = "FileName",
                Name   = "Name",
                Length = 2334,
            };

            int id = await service.AddBinaryAsync(binaryDto);

            //Act
            await service.SaveAsync(id, stream);

            //Assert
            Stream streamToAssert = await service.GetBinaryAsync(id);

            StreamReader reader = new StreamReader(streamToAssert);
            string       text   = reader.ReadToEnd();

            Assert.Equal(testWord, text);

            //Cleanup
            await service.DeleteBinaryAsync(id);
        }
Esempio n. 10
0
        public async Task TestQuestion()
        {
            //Prepare
            TestingContext testingContext = new TestingContext();

            testingContext.AddAdminPrincipalMock();
            testingContext.AddMockLogService();
            testingContext.AddRealDb();


            ApplicationDbContext context = testingContext.GetSimple <ApplicationDbContext>();


            Container container = new Container();

            context.Containers.Add(container);

            Question question1 = new Question();

            question1.Container = container;
            question1.Title     = "hello";
            question1.Text      = "Hello Text";
            question1.Status    = QuestionStatus.Created;
            question1.User      = await GetTestUserAsync(context);

            question1.Language     = Language.De;
            question1.QuestionType = QuestionType.SingleChoice;
            context.Questions.Add(question1);


            //Act
            context.SaveChanges();

            //Assert
            Question found = await context.FindAsync <Question>(question1.Id);

            Assert.NotNull(found);
            Assert.Equal(QuestionType.SingleChoice, found.QuestionType);

            //Cleanup
            context.Remove(question1);
            context.Remove(container);
            context.SaveChanges();
        }
Esempio n. 11
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);
        }