예제 #1
0
        public void HasUserStartedTest_UserHasntStartedTest_ReturnsSuccessfulResult()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            int        testAuthorId = 1;
            List <int> questionIds  = SeedQuestions(testAuthorId);
            var        testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);

            var userTestStartDate = utcNow.AddMinutes(-5);

            var scheduledTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                                .IncludeUser(userId)
                                .SetAsCurrentlyAvailable()
                                .Build();

            InsertData(scheduledTest);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var result = service.HasUserStartedTest(scheduledTest.Id, userId);

            //assert
            result.Succeeded.Should().BeFalse();
        }
예제 #2
0
        public async Task HasTestComeToEnd_NoUserHasStartedAvailableTest_ReturnsSuccessResultWithFalse()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            int testAuthorId = 1;
            var questionIds  = SeedQuestions(testAuthorId);
            var testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);

            var scheduledTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                                .SetAsCurrentlyAvailable()
                                .IncludeUser(userId)
                                .Build();

            InsertData(scheduledTest);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var result = await service.HasTestComeToEnd(scheduledTest.Id);

            //assert
            result.Succeeded.Should().BeTrue();
            result.Data.Should().BeFalse();
        }
예제 #3
0
        public async Task CanGetTest_ItsToLateToStartTheTest_ReturnsResultWithError()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            int        testAuthorId = 1;
            List <int> questionIds  = SeedQuestions(testAuthorId);
            var        testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);
            var scheduledTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                                .IncludeUser(userId)
                                .SetAsEnded()
                                .Build();

            InsertData(scheduledTest);
            var userContextMock = new Mock <IUserContext>();

            userContextMock.Setup(x => x.UserId).Returns(userId);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var result = await service.CanGetTest(scheduledTest.Id, userId);

            //assert
            result.Succeeded.Should().BeFalse();
        }
예제 #4
0
        public async Task HasTestWithQuestionStarted_TestWithProvidedQuestionHasntStarted_ReturnsFalse()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            var        usersTakingTest = new int[] { 2, userId, 4, 5 }.AsEnumerable();
            int        testAuthorId = 1;
            List <int> questionIds  = SeedQuestions(testAuthorId);
            var        testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);
            var scheduledTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                                .SetAsUpcoming()
                                .Build();

            InsertData(scheduledTest);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var result = await service.HasTestWithQuestionStarted(questionIds.First());

            //assert
            result.Should().BeFalse();
        }
예제 #5
0
        public void DoesTestContainQuestions_DoesntContainEveryQuestion_ReturnsFalse()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            int testAuthorId = 1;
            var questionIds  = SeedQuestions(testAuthorId);
            var notEquivalentListOfQuestionIds = new List <int>(questionIds)
            {
                20, 21
            };
            var testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);
            var scheduledTest = new ScheduledTestBuilder(testTemplate.Id, utcNow).Build();

            InsertData(scheduledTest);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var doesContainEveryQuestion = service.DoesTestContainQuestions(notEquivalentListOfQuestionIds, scheduledTest.Id);

            //assert
            doesContainEveryQuestion.Should().BeFalse();
        }
예제 #6
0
        public async Task CancelActiveAndUpcomingUserTests_UserHasOneUpcomingAndActiveTest_TwoUserTestRemoved()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            int        testAuthorId = 1;
            List <int> questionIds  = SeedQuestions(testAuthorId);
            var        testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);

            var endedTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                            .IncludeUser(userId)
                            .SetAsEnded()
                            .Build();

            InsertData(endedTest);

            var activeTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                             .IncludeUser(userId)
                             .SetAsCurrentlyAvailable()
                             .Build();

            InsertData(activeTest);

            var upcomingTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                               .IncludeUser(userId)
                               .SetAsUpcoming()
                               .Build();

            InsertData(upcomingTest);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var result = await service.CancelActiveAndUpcomingUserTests(userId);

            //assert
            result.Succeeded.Should().BeTrue();

            var endedTestParticipants = await GetIdOfUsersTakingTest(endedTest.Id);

            endedTestParticipants.Should().HaveCountGreaterThan(1).And.Contain(userId);

            var activeTestParticipants = await GetIdOfUsersTakingTest(activeTest.Id);

            activeTestParticipants.Should().HaveCountGreaterThan(1).And.NotContain(userId);

            var upcomingTestParticipants = await GetIdOfUsersTakingTest(upcomingTest.Id);

            upcomingTestParticipants.Should().HaveCountGreaterThan(1).And.NotContain(userId);
        }
예제 #7
0
        public async Task CancelActiveAndUpcomingUserTests_TestIsScheduledForOneMember_UserTestsRemovedTestsScheduledForOnlyDeletedMemberAreAlsoRemoved()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            int        testAuthorId = 1;
            List <int> questionIds  = SeedQuestions(testAuthorId);
            var        testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);

            var otherUser             = 99;
            var firstTestParticipants = new List <int> {
                userId, otherUser
            };
            var firstTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                            .WithUsers(firstTestParticipants)
                            .SetAsCurrentlyAvailable()
                            .Build();

            InsertData(firstTest);

            var secondTestParticipants = new List <int> {
                userId
            };
            var secondTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                             .WithUsers(secondTestParticipants)
                             .SetAsCurrentlyAvailable()
                             .Build();

            InsertData(secondTest);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var result = await service.CancelActiveAndUpcomingUserTests(userId);

            //assert
            result.Succeeded.Should().BeTrue();
            var firstTestFromDb = dbContext.ScheduledTests.Include(x => x.UserTests).FirstOrDefault(x => x.Id == firstTest.Id);

            firstTestFromDb.Should().NotBeNull();
            firstTest.UserTests.Should().HaveCount(1).And.Contain(x => x.UserId == otherUser);

            dbContext.ScheduledTests.FirstOrDefault(x => x.Id == secondTest.Id).Should().BeNull();
        }
예제 #8
0
        public async Task ShouldNotUpdateQuestionUsedInTestThatHasStarted()
        {
            var authorId      = fixture.UserId;
            var writtenAnswer = new WrittenAnswer("answer", 1f);
            var question      = new Question("Question?", writtenAnswer, authorId);

            await fixture.ExecuteDbContext(db =>
            {
                db.Questions.Add(question);
                return(db.SaveChangesAsync());
            });

            var testTemplate = new TestTemplateBuilder(authorId, new[] { question.Id }).Build();
            await fixture.ExecuteDbContext(db =>
            {
                db.TestTemplates.Add(testTemplate);
                return(db.SaveChangesAsync());
            });

            var scheduledTest = new ScheduledTestBuilder(
                testTemplate.Id,
                IntegrationTestsDateTimeProvider.utcNow)
                                .SetAsCurrentlyAvailable()
                                .WithUsers(new[] { 2, 3, 4 })
                                .Build();

            await fixture.ExecuteDbContext(db =>
            {
                db.ScheduledTests.Add(scheduledTest);
                return(db.SaveChangesAsync());
            });

            var updateQuestionDto = new QuestionWithWrittenAnswerDto
            {
                Answer     = "new answer",
                Question   = "new question",
                Score      = 1f,
                Categories = new List <int>(),
            };

            var json     = fixture.Serialize(updateQuestionDto);
            var response = await fixture.RequestSender.PostAsync($"questions/{question.Id}", json);

            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
        }
예제 #9
0
        public async Task CanGetTest_UserNoLongerHasAccessToTestBecauseTheyExceededTestTimeLimit_ReturnsResultWithError()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            int        testAuthorId = 1;
            List <int> questionIds  = SeedQuestions(testAuthorId);
            var        testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);

            var startDate   = utcNow.AddHours(-1);
            var endDate     = utcNow.AddHours(8);
            var publishDate = utcNow.AddHours(-2);
            var duration    = 15;

            var userTest = new List <UserTest>
            {
                new UserTestBuilder(userId).WithStartDate(startDate.AddMinutes(5)).Build(),
            };

            var scheduledTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                                .IncludeUser(userId)
                                .WithDates(publishDate, startDate, endDate)
                                .WithDuration(duration)
                                .WithUserTests(userTest)
                                .Build();

            InsertData(scheduledTest);
            var userContextMock = new Mock <IUserContext>();

            userContextMock.Setup(x => x.UserId).Returns(userId);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var result = await service.CanGetTest(scheduledTest.Id, userId);

            //assert
            result.Succeeded.Should().BeFalse();
        }
예제 #10
0
        public void MarkTestAsStarted_UserAlreadyHasStartedTest_ReturnResultWithError()
        {
            //arrange
            var utcNow           = new DateTime(2020, 9, 5, 14, 8, 58, 0, DateTimeKind.Utc);
            var dateTimeProdiver = new Mock <IDateTimeProvider>();

            dateTimeProdiver.Setup(x => x.UtcNow).Returns(utcNow);
            int        testAuthorId = 1;
            List <int> questionIds  = SeedQuestions(testAuthorId);
            var        testTemplate = new TestTemplateBuilder(testAuthorId, questionIds).Build();

            InsertData(testTemplate);

            var userTestStartDate = utcNow.AddMinutes(-5);
            var userTests         = new List <UserTest>
            {
                new UserTestBuilder(userId).WithStartDate(userTestStartDate).Build(),
            };

            var scheduledTest = new ScheduledTestBuilder(testTemplate.Id, utcNow)
                                .WithUserTests(userTests)
                                .SetAsCurrentlyAvailable()
                                .Build();

            InsertData(scheduledTest);
            var service = new TestService(dbContext, userContext, dateTimeProdiver.Object);

            //act
            var result = service.MarkTestAsStarted(scheduledTest.Id, userId);

            //assert
            result.Succeeded.Should().BeFalse();
            var userTest = dbContext.UserTests.Single(x => x.ScheduledTestId == scheduledTest.Id && x.UserId == userId);

            userTest.StartDate.Should().Be(userTestStartDate);
        }