示例#1
0
        public void GetSurveysByTenantFiltersByTenantInPartitionKey()
        {
            var surveyRow = new SurveyRow {
                PartitionKey = "tenant"
            };
            var otherSurveyRow = new SurveyRow {
                PartitionKey = "other tenant"
            };
            var surveyRowsForTheQuery = new[] { surveyRow, otherSurveyRow };
            var mock = new Mock <IAzureTable <SurveyRow> >();

            mock.SetupGet(t => t.Query).Returns(surveyRowsForTheQuery.AsQueryable());
            mock.Setup(t => t.GetRetryPolicyFactoryInstance()).Returns(new DefaultRetryPolicyFactory());
            var store = new SurveyStore(mock.Object, default(IAzureTable <QuestionRow>), null);

            var surveysForTenant = store.GetSurveysByTenant("tenant");

            Assert.AreEqual(1, surveysForTenant.Count());
        }
示例#2
0
        public void DeleteSurveyByTenantAndSlugNameDoesNotDeleteSurveyAndQuestionWhenSurveyDoesNotExist()
        {
            var otherSurveyRow = new SurveyRow {
                RowKey = "other_row_key"
            };
            var surveyRowsForTheQuery = new[] { otherSurveyRow };
            var mockSurveyTable       = new Mock <IAzureTable <SurveyRow> >();

            mockSurveyTable.SetupGet(t => t.Query).Returns(surveyRowsForTheQuery.AsQueryable());
            mockSurveyTable.Setup(t => t.GetRetryPolicyFactoryInstance()).Returns(new DefaultRetryPolicyFactory());
            var mockQuestionTable = new Mock <IAzureTable <QuestionRow> >();

            mockQuestionTable.SetupGet(t => t.Query).Returns((new QuestionRow[] { }).AsQueryable());
            mockQuestionTable.Setup(t => t.GetRetryPolicyFactoryInstance()).Returns(new DefaultRetryPolicyFactory());
            var store = new SurveyStore(mockSurveyTable.Object, mockQuestionTable.Object, null);

            store.DeleteSurveyByTenantAndSlugName("tenant", "slug-name");

            mockSurveyTable.Verify(t => t.Delete(It.IsAny <SurveyRow>()), Times.Never());
            mockQuestionTable.Verify(t => t.Delete(It.IsAny <IEnumerable <QuestionRow> >()), Times.Never());
        }
示例#3
0
        public void GetSurveyByTenantAndSlugNameFiltersByTenantAndSlugNameInRowKey()
        {
            string expectedRowKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", "tenant", "slug-name");
            var    surveyRow      = new SurveyRow {
                RowKey = expectedRowKey
            };
            var otherSurveyRow = new SurveyRow {
                RowKey = "other_row_key"
            };
            var surveyRowsForTheQuery = new[] { surveyRow, otherSurveyRow };
            var mockSurveyTable       = new Mock <IAzureTable <SurveyRow> >();

            mockSurveyTable.SetupGet(t => t.Query).Returns(surveyRowsForTheQuery.AsQueryable()).Verifiable();
            mockSurveyTable.Setup(t => t.GetRetryPolicyFactoryInstance()).Returns(new DefaultRetryPolicyFactory());
            var store = new SurveyStore(mockSurveyTable.Object, default(IAzureTable <QuestionRow>), null);

            var survey = store.GetSurveyByTenantAndSlugName("tenant", "slug-name", false);

            Assert.IsNotNull(survey);
            mockSurveyTable.Verify();
        }
        public async Task GetSurveyByTenantAndSlugNameReturnsWithQuestionPossibleAnswers()
        {
            string expectedKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", "tenant", "slug-name");
            var    surveyRow   = new SurveyRow {
                RowKey = expectedKey
            };
            var mockSurveyTable = new Mock <IAzureTable <SurveyRow> >();

            mockSurveyTable.Setup(t => t.GetByRowKeyAsync(expectedKey)).ReturnsAsync(new[] { surveyRow });
            var questionRow = new QuestionRow {
                PartitionKey = expectedKey, PossibleAnswers = "possible answers", Type = Enum.GetName(typeof(QuestionType), QuestionType.SimpleText)
            };
            var questions         = new[] { questionRow };
            var mockQuestionTable = new Mock <IAzureTable <QuestionRow> >();

            mockQuestionTable.Setup(t => t.GetByPartitionKeyAsync(expectedKey)).ReturnsAsync(questions);
            var store = new SurveyStore(mockSurveyTable.Object, mockQuestionTable.Object, new Mock <IInitializationStatusService>().Object);

            var survey = await store.GetSurveyByTenantAndSlugNameAsync("tenant", "slug-name", true);

            Assert.AreEqual("possible answers", survey.Questions.First().PossibleAnswers);
        }
示例#5
0
        public async Task GetSurveyByTenantAndSlugNameReturnsWithQuestionsFilteredByTenantAndSlugNameInPartitionKey()
        {
            string expectedKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", "tenant", "slug-name");
            var    surveyRow   = new SurveyRow {
                RowKey = expectedKey
            };
            var mockSurveyTable = new Mock <IAzureTable <SurveyRow> >();

            mockSurveyTable.Setup(t => t.GetByRowKeyAsync(expectedKey)).ReturnsAsync(new[] { surveyRow });
            var questionRow = new QuestionRow {
                PartitionKey = expectedKey, Type = Enum.GetName(typeof(QuestionType), QuestionType.SimpleText)
            };
            var questions         = new[] { questionRow };
            var mockQuestionTable = new Mock <IAzureTable <QuestionRow> >();

            mockQuestionTable.Setup(t => t.GetByPartitionKeyAsync(expectedKey)).ReturnsAsync(questions);
            var store = new SurveyStore(mockSurveyTable.Object, mockQuestionTable.Object, null);

            var survey = await store.GetSurveyByTenantAndSlugNameAsync("tenant", "slug-name", true);

            Assert.AreEqual(1, survey.Questions.Count);
        }
示例#6
0
        public void DeleteSurveyByTenantAndSlugNameDeletesSurveyWithTenantAndSlugNameInRowKeyFromSurveyTable()
        {
            string expectedRowKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", "tenant", "slug-name");
            var    surveyRow      = new SurveyRow {
                RowKey = expectedRowKey
            };
            var otherSurveyRow = new SurveyRow {
                RowKey = "other_row_key"
            };
            var surveyRowsForTheQuery = new[] { surveyRow, otherSurveyRow };
            var mockSurveyTable       = new Mock <IAzureTable <SurveyRow> >();

            mockSurveyTable.SetupGet(t => t.Query).Returns(surveyRowsForTheQuery.AsQueryable());
            mockSurveyTable.Setup(t => t.GetRetryPolicyFactoryInstance()).Returns(new DefaultRetryPolicyFactory());
            var mockQuestionTable = new Mock <IAzureTable <QuestionRow> >();

            mockQuestionTable.SetupGet(t => t.Query).Returns((new QuestionRow[] { }).AsQueryable());
            mockQuestionTable.Setup(t => t.GetRetryPolicyFactoryInstance()).Returns(new DefaultRetryPolicyFactory());
            var store = new SurveyStore(mockSurveyTable.Object, mockQuestionTable.Object, null);

            store.DeleteSurveyByTenantAndSlugName("tenant", "slug-name");

            mockSurveyTable.Verify(t => t.Delete(It.Is <SurveyRow>(s => s.RowKey == expectedRowKey)));
        }
        public async Task SaveSurveyAsync(Survey survey)
        {
            if (string.IsNullOrEmpty(survey.SlugName) && string.IsNullOrEmpty(survey.Title))
            {
                throw new ArgumentNullException("survey", "The survey for saving has to have the slug or the title.");
            }

            var slugName = string.IsNullOrEmpty(survey.SlugName) ? GenerateSlug(survey.Title, 100) : survey.SlugName;

            var surveyRow = new SurveyRow
            {
                SlugName     = slugName,
                Title        = survey.Title,
                CreatedOn    = DateTime.UtcNow,
                PartitionKey = survey.TenantId
            };

            surveyRow.RowKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", survey.TenantId, surveyRow.SlugName);

            var questionRows = new List <QuestionRow>(survey.Questions.Count);

            for (int i = 0; i < survey.Questions.Count; i++)
            {
                var question    = survey.Questions[i];
                var questionRow = new QuestionRow
                {
                    Text            = question.Text,
                    Type            = Enum.GetName(typeof(QuestionType), question.Type),
                    PossibleAnswers = question.PossibleAnswers
                };

                questionRow.PartitionKey = surveyRow.RowKey;
                questionRow.RowKey       = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", DateTime.UtcNow.GetFormatedTicks(), i.ToString("D3"));

                questionRows.Add(questionRow);
            }

            //// First add the questions
            await this.questionTable.AddAsync(questionRows);

            try
            {
                //// Then add the survey
                //// If this fails, the questions may end up orphan but data will be consistent for the user
                await this.surveyTable.AddAsync(surveyRow);

                if (this.CacheEnabled)
                {
                    await TenantCacheHelper.AddToCacheAsync(survey.TenantId, slugName, survey).ConfigureAwait(false);
                }
            }

            //TODO: update this catch for Azure Table client SDK
            catch (DataServiceRequestException ex)
            {
                TraceHelper.TraceError(ex.TraceInformation());

                var dataServiceClientException = ex.InnerException as DataServiceClientException;
                if (dataServiceClientException != null)
                {
                    if (dataServiceClientException.StatusCode == 409)
                    {
                        await this.questionTable.DeleteAsync(questionRows);

                        throw;
                    }
                }

                throw;
            }
        }
示例#8
0
        public void SaveSurvey(Survey survey)
        {
            if (string.IsNullOrEmpty(survey.SlugName) && string.IsNullOrEmpty(survey.Title))
            {
                throw new ArgumentNullException("survey", "The survey for saving has to have the slug or the title.");
            }

            var slugName = string.IsNullOrEmpty(survey.SlugName) ? GenerateSlug(survey.Title, 100) : survey.SlugName;

            var surveyRow = new SurveyRow
            {
                SlugName          = slugName,
                Title             = survey.Title,
                CreatedOn         = DateTime.UtcNow,
                PartitionKey      = survey.Tenant,
                UserDefinedFields = survey.UserDefinedFields
            };

            surveyRow.RowKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", survey.Tenant, surveyRow.SlugName);

            var questionRows = new List <QuestionRow>(survey.Questions.Count);

            for (int i = 0; i < survey.Questions.Count; i++)
            {
                var question    = survey.Questions[i];
                var questionRow = new QuestionRow
                {
                    Text            = question.Text,
                    Type            = Enum.GetName(typeof(QuestionType), question.Type),
                    PossibleAnswers = question.PossibleAnswers
                };

                questionRow.PartitionKey = surveyRow.RowKey;
                questionRow.RowKey       = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", DateTime.UtcNow.GetFormatedTicks(), i.ToString("D3"));

                questionRows.Add(questionRow);
            }

            //// First add the questions
            this.questionTable.Add(questionRows);

            try
            {
                //// Then create the container for the answers
                this.surveyAnswerContainerFactory.Create(surveyRow.PartitionKey, surveyRow.SlugName).EnsureExist();

                //// And finally, add the survey
                //// If this fails, the questions may end up orphan but data will be consistent for the user
                this.surveyTable.Add(surveyRow);

                if (this.CacheEnabled)
                {
                    TenantCacheHelper.AddToCache(survey.Tenant, slugName, survey);
                }
            }
            catch (DataServiceRequestException ex)
            {
                TraceHelper.TraceError(ex.TraceInformation());

                var dataServiceClientException = ex.InnerException as DataServiceClientException;
                if (dataServiceClientException != null)
                {
                    if (dataServiceClientException.StatusCode == 409)
                    {
                        this.questionTable.Delete(questionRows);
                        throw;
                    }
                }

                throw;
            }
        }
示例#9
0
 public async Task <SurveyRow> GetSurveyRow(SurveyRow surveyRow)
 {
     return(await KoohTajrobeDbContext.Set <SurveyRow>()
            .FirstOrDefaultAsync(x => x.Id == surveyRow.Id));
 }
示例#10
0
 public async Task InsertSurveyRow(SurveyRow surveyRow)
 {
     await KoohTajrobeDbContext.Set <SurveyRow>().AddAsync(surveyRow);
 }
示例#11
0
 public async Task <List <SurveyRowDetail> > GetSurveyRowDetailBySurveyRow(SurveyRow surveyRow)
 {
     throw new NotImplementedException();
 }