public void Fetch() { var data = new List <PollSearchResultsData> { EntityCreator.Create <PollSearchResultsData>() }; var pollSearchResultsFactory = new Mock <IObjectFactory <ReadOnlySwitchList <IPollSearchResult> > >(MockBehavior.Strict); pollSearchResultsFactory.Setup(_ => _.FetchChild()).Returns(DataPortal.FetchChild <ReadOnlySwitchList <IPollSearchResult> >()); var pollSearchResultFactory = new Mock <IObjectFactory <IPollSearchResult> >(MockBehavior.Strict); pollSearchResultFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollSearchResult>(_[0] as PollSearchResultsData)); var builder = new ContainerBuilder(); builder.RegisterInstance(Mock.Of <IEntities>()).As <IEntities>(); builder.Register <IObjectFactory <ReadOnlySwitchList <IPollSearchResult> > >(_ => pollSearchResultsFactory.Object); builder.Register <IObjectFactory <IPollSearchResult> >(_ => pollSearchResultFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var result = DataPortal.FetchChild <PollSearchResultsByCategory>(data); Assert.AreEqual(data[0].Category, result.Category, nameof(result.Category)); Assert.AreEqual(1, result.SearchResults.Count, nameof(result.SearchResults)); } pollSearchResultsFactory.VerifyAll(); pollSearchResultFactory.VerifyAll(); }
public void Fetch() { var entity = EntityCreator.Create <MVPollOption>(); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.Dispose()); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var pollOption = DataPortal.FetchChild <PollOption>(entity); Assert.AreEqual(entity.OptionPosition, pollOption.OptionPosition, nameof(pollOption.OptionPosition)); Assert.AreEqual(entity.OptionText, pollOption.OptionText, nameof(pollOption.OptionText)); Assert.AreEqual(entity.PollID, pollOption.PollID, nameof(pollOption.PollID)); Assert.AreEqual(entity.PollOptionID, pollOption.PollOptionID, nameof(pollOption.PollOptionID)); } entities.VerifyAll(); }
public void Fetch() { var entity = EntityCreator.Create <MVCategory>(); var context = new Mock <IEntities>(MockBehavior.Strict); context.Setup(_ => _.MVCategories).Returns(new InMemoryDbSet <MVCategory> { entity }); context.Setup(_ => _.Dispose()); var categoryFactory = new Mock <IObjectFactory <ICategory> >(MockBehavior.Strict); categoryFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <Category>(_[0] as MVCategory)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => context.Object); builder.Register <IObjectFactory <ICategory> >(_ => categoryFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var categories = DataPortal.Fetch <CategoryCollection>(); Assert.AreEqual(1, categories.Count, nameof(categories.Count)); } context.VerifyAll(); categoryFactory.VerifyAll(); }
internal static T Create <T>(Action <T> modifier) where T : new() { var entity = EntityCreator.Create <T>(); modifier(entity); return(entity); }
public void Fetch() { var entity = EntityCreator.Create <MVPoll>(_ => _.PollDeletedFlag = false); entity.MVPollOptions = new List <MVPollOption> { EntityCreator.Create <MVPollOption>() }; var pollOptions = new Mock <IObjectFactory <BusinessList <IPollOption> > >(MockBehavior.Strict); pollOptions.Setup(_ => _.FetchChild()).Returns(new BusinessList <IPollOption>()); var pollOption = new Mock <IObjectFactory <IPollOption> >(MockBehavior.Strict); pollOption.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollOption>(_[0] as MVPollOption)); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPolls).Returns(new InMemoryDbSet <MVPoll> { entity }); entities.Setup(_ => _.Dispose()); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <BusinessList <IPollOption> > >(_ => pollOptions.Object); builder.Register <IObjectFactory <IPollOption> >(_ => pollOption.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var poll = new DataPortal <Poll>().Fetch(entity.PollID); Assert.AreEqual(entity.PollAdminRemovedFlag, poll.PollAdminRemovedFlag, nameof(poll.PollAdminRemovedFlag)); Assert.AreEqual(entity.PollCategoryID, poll.PollCategoryID, nameof(poll.PollCategoryID)); Assert.AreEqual(entity.PollDateRemoved, poll.PollDateRemoved, nameof(poll.PollDateRemoved)); Assert.AreEqual(entity.PollDeletedDate, poll.PollDeletedDate, nameof(poll.PollDeletedDate)); Assert.AreEqual(entity.PollDeletedFlag, poll.PollDeletedFlag, nameof(poll.PollDeletedFlag)); Assert.AreEqual(entity.PollDescription, poll.PollDescription, nameof(poll.PollDescription)); Assert.AreEqual(entity.PollEndDate, poll.PollEndDate, nameof(poll.PollEndDate)); Assert.AreEqual(entity.PollID, poll.PollID, nameof(poll.PollID)); Assert.AreEqual(entity.PollImageLink, poll.PollImageLink, nameof(poll.PollImageLink)); Assert.AreEqual(entity.PollMaxAnswers, poll.PollMaxAnswers, nameof(poll.PollMaxAnswers)); Assert.AreEqual(entity.PollMinAnswers, poll.PollMinAnswers, nameof(poll.PollMinAnswers)); Assert.AreEqual(entity.PollQuestion, poll.PollQuestion, nameof(poll.PollQuestion)); Assert.AreEqual(entity.PollStartDate, poll.PollStartDate, nameof(poll.PollStartDate)); Assert.AreEqual(entity.UserID, poll.UserID, nameof(poll.UserID)); Assert.AreEqual(1, poll.PollOptions.Count, nameof(poll.PollOptions)); } entities.VerifyAll(); pollOptions.VerifyAll(); pollOption.VerifyAll(); }
public void FetchNotOwnedByUser() { var generator = new RandomObjectGenerator(); var pollId = generator.Generate <int>(); var userId = generator.Generate <int>(); var pollDataResults = Mock.Of <IPollDataResults>(); var pollDataResultsFactory = new Mock <IObjectFactory <IPollDataResults> >(MockBehavior.Strict); pollDataResultsFactory.Setup(_ => _.FetchChild(pollId)).Returns(pollDataResults); var pollComments = Mock.Of <IPollComments>(); var pollCommentsFactory = new Mock <IObjectFactory <IPollComments> >(MockBehavior.Strict); pollCommentsFactory.Setup(_ => _.FetchChild(pollId)).Returns(pollComments); var poll = EntityCreator.Create <MVPoll>(_ => { _.PollID = pollId; _.UserID = userId + 1; _.PollDeletedFlag = null; _.PollStartDate = DateTime.UtcNow.AddDays(-1); _.PollEndDate = DateTime.UtcNow.AddDays(1); }); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPolls).Returns(new InMemoryDbSet <MVPoll> { poll }); entities.Setup(_ => _.Dispose()); var builder = new ContainerBuilder(); builder.Register <IObjectFactory <IPollDataResults> >(_ => pollDataResultsFactory.Object); builder.Register <IObjectFactory <IPollComments> >(_ => pollCommentsFactory.Object); builder.Register <IEntities>(_ => entities.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var pollResults = DataPortal.Fetch <PollResults>(new PollResultsCriteria(userId, pollId)); Assert.AreEqual(pollId, pollResults.PollID, nameof(pollResults.PollID)); Assert.AreEqual(poll.PollImageLink, pollResults.PollImageLink, nameof(pollResults.PollImageLink)); Assert.AreSame(pollDataResults, pollResults.PollDataResults, nameof(pollResults.PollDataResults)); Assert.AreSame(pollComments, pollResults.PollComments, nameof(pollResults.PollComments)); Assert.IsFalse(pollResults.IsPollOwnedByUser, nameof(pollResults.IsPollOwnedByUser)); Assert.IsTrue(pollResults.IsActive, nameof(pollResults.IsActive)); } pollDataResultsFactory.VerifyAll(); pollCommentsFactory.VerifyAll(); entities.VerifyAll(); }
public void Fetch() { var entity = EntityCreator.Create <MVCategory>(); var container = new ContainerBuilder(); container.RegisterInstance(Mock.Of <IEntities>()).As <IEntities>(); using (new ObjectActivator(container.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var category = DataPortal.FetchChild <Category>(entity); Assert.AreEqual(entity.CategoryID, category.ID, nameof(category.ID)); Assert.AreEqual(entity.CategoryName, category.Name, nameof(category.Name)); } }
public void Fetch() { var data = EntityCreator.Create <PollData>(); var container = new ContainerBuilder(); container.RegisterInstance(Mock.Of <IEntities>()).As <IEntities>(); using (new ObjectActivator(container.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var result = DataPortal.FetchChild <PollDataResult>(data); Assert.AreEqual(data.PollOptionID, result.PollOptionID, nameof(result.PollOptionID)); Assert.AreEqual(data.ResponseCount, result.ResponseCount, nameof(result.ResponseCount)); } }
public void Fetch() { var container = new ContainerBuilder(); container.RegisterInstance(Mock.Of <IEntities>()).As <IEntities>(); var data = EntityCreator.Create <PollSearchResultsData>(); using (new ObjectActivator(container.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var result = DataPortal.FetchChild <PollSearchResult>(data); Assert.AreEqual(data.Id, result.Id, nameof(result.Id)); Assert.AreEqual(data.ImageLink, result.ImageLink, nameof(result.ImageLink)); Assert.AreEqual(data.Question, result.Question, nameof(result.Question)); } }
public void CreateWhenUserHasSubmittedAnswersBefore() { var generator = new RandomObjectGenerator(); var userId = generator.Generate <int>(); var pollId = generator.Generate <int>(); var entity = EntityCreator.Create <MVPollSubmission>(_ => { _.UserID = userId; _.PollID = pollId; }); var entities = new Mock <IEntities>(); entities.Setup(_ => _.MVPollSubmissions) .Returns(new InMemoryDbSet <MVPollSubmission> { entity }); entities.Setup(_ => _.Dispose()); var pollFactory = new Mock <IObjectFactory <IPoll> >(); pollFactory.Setup(_ => _.Fetch(pollId)).Returns <int>(_ => DataPortal.Fetch <Poll>(_)); var pollSubmissionResponsesFactory = new Mock <IObjectFactory <IPollSubmissionResponseCollection> >(); pollSubmissionResponsesFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponseCollection>(_[0] as BusinessList <IPollOption>)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <IPoll> >(_ => pollFactory.Object); builder.Register <IObjectFactory <IPollSubmissionResponseCollection> >(_ => pollSubmissionResponsesFactory.Object); using (new ObjectActivator(builder.Build(), Mock.Of <ICallContext>()) .Bind(() => ApplicationContext.DataPortalActivator)) { var criteria = new PollSubmissionCriteria(pollId, userId); new DataPortal <PollSubmission>().Create(criteria); } }
public void Fetch() { var generator = new RandomObjectGenerator(); var profileId = generator.Generate <string>(); var userId = generator.Generate <int>(); var userName = generator.Generate <string>(); var entity = EntityCreator.Create <MVUser>(_ => { _.ProfileID = profileId; _.UserID = userId; _.UserName = userName; _.MVUserRole = EntityCreator.Create <MVUserRole>(); }); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVUsers) .Returns(new InMemoryDbSet <MVUser> { entity }); entities.Setup(_ => _.Dispose()); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var identity = DataPortal.Fetch <UserIdentity>(profileId); Assert.AreEqual(profileId, identity.ProfileID, nameof(identity.ProfileID)); Assert.AreEqual(userId, identity.UserID, nameof(identity.UserID)); Assert.AreEqual(userName, identity.UserName, nameof(identity.UserName)); Assert.IsTrue(identity.IsAuthenticated, nameof(identity.IsAuthenticated)); } entities.VerifyAll(); }
public void Update() { var generator = new RandomObjectGenerator(); var newOptionPosition = generator.Generate <short>(); var newOptionText = generator.Generate <string>(); var entity = EntityCreator.Create <MVPollOption>(); var poll = Mock.Of <IPoll>(); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPollOptions) .Returns(new InMemoryDbSet <MVPollOption> { entity }); entities.Setup(_ => _.SetState(It.IsAny <MVPollOption>(), EntityState.Modified)); entities.Setup(_ => _.SaveChanges()).Returns(1); entities.Setup(_ => _.Dispose()); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var pollOption = DataPortal.FetchChild <PollOption>(entity); pollOption.OptionPosition = newOptionPosition; pollOption.OptionText = newOptionText; DataPortal.UpdateChild(pollOption, poll); Assert.AreEqual(newOptionPosition, pollOption.OptionPosition, nameof(pollOption.OptionPosition)); Assert.AreEqual(newOptionText, pollOption.OptionText, nameof(pollOption.OptionText)); } entities.VerifyAll(); }
public void ExecuteWhenUserHasSubmittedAnswersForThePoll() { var submission = EntityCreator.Create <MVPollSubmission>(); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPollSubmissions).Returns( new InMemoryDbSet <MVPollSubmission> { submission }); entities.Setup(_ => _.Dispose()); var factory = Mock.Of <IObjectFactory <IPollSubmission> >(); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <IPollSubmission> >(_ => factory); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var command = DataPortal.Execute <PollSubmissionCommand>( new PollSubmissionCommand { PollID = submission.PollID, UserID = submission.UserID }); Assert.AreEqual(submission.PollID, command.PollID, nameof(command.PollID)); Assert.AreEqual(submission.UserID, command.UserID, nameof(command.UserID)); Assert.IsNull(command.Submission, nameof(command.Submission)); } entities.VerifyAll(); }
public void Fetch() { var generator = new RandomObjectGenerator(); var userName = generator.Generate <string>(); var pollComment = new PollCommentData { Comment = EntityCreator.Create <MVPollComment>(), UserName = userName }; var childPollComment = new PollCommentData { Comment = EntityCreator.Create <MVPollComment>(_ => { _.ParentCommentID = pollComment.Comment.PollCommentID; _.PollID = pollComment.Comment.PollID; _.UserID = pollComment.Comment.UserID; }), UserName = userName }; var comments = new List <PollCommentData> { pollComment, childPollComment }; var newPollComment = Mock.Of <IPollComment>(); var pollCommentFactoryCount = 0; var pollCommentFactory = new Mock <IObjectFactory <IPollComment> >(MockBehavior.Strict); pollCommentFactory.Setup(_ => _.FetchChild(childPollComment, comments)) .Callback <object[]>(_ => pollCommentFactoryCount++) .Returns(newPollComment); var pollComments = new Mock <IPollCommentCollection>(MockBehavior.Strict); pollComments.Setup(_ => _.Add(newPollComment)); pollComments.Setup(_ => _.SetParent(It.IsAny <PollComment>())); pollComments.SetupGet(_ => _.EditLevel).Returns(0); var pollCommentsFactoryCount = 0; var pollCommentsFactory = new Mock <IObjectFactory <IPollCommentCollection> >(MockBehavior.Strict); pollCommentsFactory.Setup(_ => _.FetchChild()) .Callback(() => pollCommentsFactoryCount++) .Returns(pollComments.Object); var builder = new ContainerBuilder(); builder.Register <IObjectFactory <IPollComment> >(_ => pollCommentFactory.Object); builder.Register <IObjectFactory <IPollCommentCollection> >(_ => pollCommentsFactory.Object); builder.Register <IEntities>(_ => Mock.Of <IEntities>()); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var result = DataPortal.FetchChild <PollComment>(pollComment, comments); Assert.AreEqual(pollComment.Comment.CommentDate, result.CommentDate); Assert.AreEqual(pollComment.Comment.CommentText, result.CommentText); Assert.AreEqual(pollComment.Comment.PollCommentID, result.PollCommentID); Assert.AreEqual(pollComment.Comment.UserID, result.UserID); Assert.AreEqual(pollComment.UserName, result.UserName); Assert.AreSame(pollComments.Object, result.Comments); } pollCommentsFactory.VerifyAll(); pollComments.VerifyAll(); pollCommentFactory.VerifyAll(); }
public void Update() { var now = DateTime.UtcNow; var entity = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollMinAnswers = 1; _.PollMaxAnswers = 2; _.PollStartDate = now; _.PollEndDate = now.AddDays(2); }); entity.MVPollOptions = new List <MVPollOption> { EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>() }; var generator = new RandomObjectGenerator(); var newPollAdminRemoveFlag = generator.Generate <bool>(); var newPollCategoryId = generator.Generate <int>(); var newPollDateRemoved = generator.Generate <DateTime>(); var newPollDeletedDate = generator.Generate <DateTime>(); var newPollDeletedFlag = generator.Generate <bool>(); var newPollDescription = generator.Generate <string>(); var newPollEndDate = entity.PollEndDate.AddDays(1); var newPollImageLink = generator.Generate <string>(); short newPollMaxAnswers = 3; short newPollMinAnswers = 2; var newPollQuestion = generator.Generate <string>(); var newPollStartDate = entity.PollStartDate.AddDays(1); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPolls).Returns(new InMemoryDbSet <MVPoll> { entity }); entities.Setup(_ => _.SetState(It.IsAny <MVPoll>(), EntityState.Modified)); entities.Setup(_ => _.SaveChanges()).Returns(1); entities.Setup(_ => _.Dispose()); var pollOptions = new Mock <IObjectFactory <BusinessList <IPollOption> > >(); pollOptions.Setup(_ => _.FetchChild()).Returns(new BusinessList <IPollOption>()); var pollOption = new Mock <IObjectFactory <IPollOption> >(); pollOption.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollOption>(_[0] as MVPollOption)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <BusinessList <IPollOption> > >(_ => pollOptions.Object); builder.Register <IObjectFactory <IPollOption> >(_ => pollOption.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var poll = new DataPortal <Poll>().Fetch(entity.PollID); poll.PollAdminRemovedFlag = newPollAdminRemoveFlag; poll.PollCategoryID = newPollCategoryId; poll.PollDateRemoved = newPollDateRemoved; poll.PollDeletedDate = newPollDeletedDate; poll.PollDeletedFlag = newPollDeletedFlag; poll.PollDescription = newPollDescription; poll.PollEndDate = newPollEndDate; poll.PollImageLink = newPollImageLink; poll.PollMaxAnswers = newPollMaxAnswers; poll.PollMinAnswers = newPollMinAnswers; poll.PollQuestion = newPollQuestion; poll.PollStartDate = newPollStartDate; poll = poll.Save(); Assert.AreEqual(newPollAdminRemoveFlag, poll.PollAdminRemovedFlag, nameof(poll.PollAdminRemovedFlag)); Assert.AreEqual(newPollCategoryId, poll.PollCategoryID, nameof(poll.PollCategoryID)); Assert.AreEqual(newPollDateRemoved, poll.PollDateRemoved, nameof(poll.PollDateRemoved)); Assert.AreEqual(newPollDeletedDate, poll.PollDeletedDate, nameof(poll.PollDeletedDate)); Assert.AreEqual(newPollDeletedFlag, poll.PollDeletedFlag, nameof(poll.PollDeletedFlag)); Assert.AreEqual(newPollDescription, poll.PollDescription, nameof(poll.PollDescription)); Assert.AreEqual(newPollEndDate.ToUniversalTime(), poll.PollEndDate, nameof(poll.PollEndDate)); Assert.AreEqual(newPollImageLink, poll.PollImageLink, nameof(poll.PollImageLink)); Assert.AreEqual(newPollMaxAnswers, poll.PollMaxAnswers, nameof(poll.PollMaxAnswers)); Assert.AreEqual(newPollMinAnswers, poll.PollMinAnswers, nameof(poll.PollMinAnswers)); Assert.AreEqual(newPollQuestion, poll.PollQuestion, nameof(poll.PollQuestion)); Assert.AreEqual(newPollStartDate.ToUniversalTime(), poll.PollStartDate, nameof(poll.PollStartDate)); } entities.VerifyAll(); }
public void Insert() { var now = DateTime.UtcNow; var generator = new RandomObjectGenerator(); var userId = generator.Generate <int>(); var poll = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollStartDate = now.AddDays(-2); _.PollEndDate = now.AddDays(2); _.PollMinAnswers = 2; _.PollMaxAnswers = 3; }); poll.MVPollOptions = new List <MVPollOption> { EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>() }; var category = EntityCreator.Create <MVCategory>(_ => _.CategoryID = poll.PollCategoryID); var polls = new InMemoryDbSet <MVPoll> { poll }; var submissions = new InMemoryDbSet <MVPollSubmission>(); var responses = new InMemoryDbSet <MVPollResponse>(); var saveChangesCount = 0; var submissionId = generator.Generate <int>(); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPolls).Returns(polls); entities.Setup(_ => _.MVPollSubmissions).Returns(submissions); entities.Setup(_ => _.MVPollResponses).Returns(responses); entities.Setup(_ => _.MVCategories) .Returns(new InMemoryDbSet <MVCategory> { category }); entities.Setup(_ => _.SaveChanges()).Callback(() => { if (saveChangesCount == 0) { submissions.Local[0].PollSubmissionID = submissionId; } saveChangesCount++; }).Returns(1); entities.Setup(_ => _.Dispose()); var pollOptions = new Mock <IObjectFactory <BusinessList <IPollOption> > >(); pollOptions.Setup(_ => _.FetchChild()).Returns(new BusinessList <IPollOption>()); var pollOption = new Mock <IObjectFactory <IPollOption> >(); pollOption.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollOption>(_[0] as MVPollOption)); var pollFactory = new Mock <IObjectFactory <IPoll> >(); pollFactory.Setup(_ => _.Fetch(poll.PollID)).Returns <int>(_ => DataPortal.Fetch <Poll>(_)); var pollSubmissionResponsesFactory = new Mock <IObjectFactory <IPollSubmissionResponseCollection> >(); pollSubmissionResponsesFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponseCollection>(_[0] as BusinessList <IPollOption>)); var pollSubmissionResponseFactory = new Mock <IObjectFactory <IPollSubmissionResponse> >(); pollSubmissionResponseFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponse>(_[0] as IPollOption)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <BusinessList <IPollOption> > >(_ => pollOptions.Object); builder.Register <IObjectFactory <IPollOption> >(_ => pollOption.Object); builder.Register <IObjectFactory <IPoll> >(_ => pollFactory.Object); builder.Register <IObjectFactory <IPollSubmissionResponseCollection> >(_ => pollSubmissionResponsesFactory.Object); builder.Register <IObjectFactory <IPollSubmissionResponse> >(_ => pollSubmissionResponseFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var criteria = new PollSubmissionCriteria(poll.PollID, userId); var submission = new DataPortal <PollSubmission>().Create(criteria); submission.Responses[1].IsOptionSelected = true; submission.Responses[3].IsOptionSelected = true; submission = submission.Save(); Assert.AreEqual(1, submissions.Local.Count, nameof(submissions.Local.Count)); Assert.AreEqual(submissionId, submission.PollSubmissionID, nameof(submission.PollSubmissionID)); Assert.AreEqual(4, responses.Local.Count, nameof(responses.Local.Count)); Assert.IsFalse(responses.Local[0].OptionSelected, nameof(MVPollResponse.OptionSelected)); Assert.IsTrue(responses.Local[1].OptionSelected, nameof(MVPollResponse.OptionSelected)); Assert.IsFalse(responses.Local[2].OptionSelected, nameof(MVPollResponse.OptionSelected)); Assert.IsTrue(responses.Local[3].OptionSelected, nameof(MVPollResponse.OptionSelected)); } entities.VerifyAll(); }
public void CreateWhenPollIsDeleted() { var now = DateTime.UtcNow; var generator = new RandomObjectGenerator(); var userId = generator.Generate <int>(); var poll = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = true; _.PollStartDate = now.AddDays(-2); _.PollEndDate = now.AddDays(2); _.PollMinAnswers = 2; _.PollMaxAnswers = 3; }); poll.MVPollOptions = new List <MVPollOption> { EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>() }; var category = EntityCreator.Create <MVCategory>(_ => _.CategoryID = poll.PollCategoryID); var pollFactory = new Mock <IObjectFactory <IPoll> >(); pollFactory.Setup(_ => _.Fetch(poll.PollID)).Returns <int>(_ => DataPortal.Fetch <Poll>(_)); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPolls) .Returns(new InMemoryDbSet <MVPoll> { poll }); entities.Setup(_ => _.MVPollSubmissions) .Returns(new InMemoryDbSet <MVPollSubmission>()); entities.Setup(_ => _.MVCategories) .Returns(new InMemoryDbSet <MVCategory> { category }); entities.Setup(_ => _.Dispose()); var pollOptions = new Mock <IObjectFactory <BusinessList <IPollOption> > >(); pollOptions.Setup(_ => _.FetchChild()).Returns(new BusinessList <IPollOption>()); var pollOption = new Mock <IObjectFactory <IPollOption> >(); pollOption.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollOption>(_[0] as MVPollOption)); var pollSubmissionResponsesFactory = new Mock <IObjectFactory <IPollSubmissionResponseCollection> >(); pollSubmissionResponsesFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponseCollection>(_[0] as BusinessList <IPollOption>)); var pollSubmissionResponseFactory = new Mock <IObjectFactory <IPollSubmissionResponse> >(); pollSubmissionResponseFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponse>(_[0] as IPollOption)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <BusinessList <IPollOption> > >(_ => pollOptions.Object); builder.Register <IObjectFactory <IPollOption> >(_ => pollOption.Object); builder.Register <IObjectFactory <IPoll> >(_ => pollFactory.Object); builder.Register <IObjectFactory <IPollSubmissionResponseCollection> >(_ => pollSubmissionResponsesFactory.Object); builder.Register <IObjectFactory <IPollSubmissionResponse> >(_ => pollSubmissionResponseFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var submission = new DataPortal <PollSubmission>().Create( new PollSubmissionCriteria(poll.PollID, userId)); submission.BrokenRulesCollection.AssertRuleCount(PollSubmission.PollDeletedFlagProperty, 1); submission.BrokenRulesCollection.AssertBusinessRuleExists <PollSubmissionPollDeletedFlagRule>( PollSubmission.PollDeletedFlagProperty, true); } entities.VerifyAll(); }
public void Create() { var now = DateTime.UtcNow; var generator = new RandomObjectGenerator(); var userId = generator.Generate <int>(); var pollEntity = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollStartDate = now.AddDays(-2); _.PollEndDate = now.AddDays(2); _.PollMinAnswers = 2; _.PollMaxAnswers = 3; }); pollEntity.MVPollOptions = new List <MVPollOption> { EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>() }; var category = EntityCreator.Create <MVCategory>(_ => _.CategoryID = pollEntity.PollCategoryID); Poll poll = null; var pollFactory = new Mock <IObjectFactory <IPoll> >(MockBehavior.Strict); pollFactory.Setup(_ => _.Fetch(pollEntity.PollID)) .Returns <int>(_ => { poll = DataPortal.Fetch <Poll>(_); return(poll); }); var pollSubmissionResponsesFactory = new Mock <IObjectFactory <IPollSubmissionResponseCollection> >(MockBehavior.Strict); pollSubmissionResponsesFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>())) .Callback <object[]>(_ => Assert.AreSame(poll.PollOptions, _[0])) .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponseCollection>(_[0] as BusinessList <IPollOption>)); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPolls) .Returns(new InMemoryDbSet <MVPoll> { pollEntity }); entities.Setup(_ => _.MVPollSubmissions) .Returns(new InMemoryDbSet <MVPollSubmission>()); entities.Setup(_ => _.MVCategories) .Returns(new InMemoryDbSet <MVCategory> { category }); entities.Setup(_ => _.Dispose()); var pollOptions = new Mock <IObjectFactory <BusinessList <IPollOption> > >(); pollOptions.Setup(_ => _.FetchChild()).Returns(new BusinessList <IPollOption>()); var pollOption = new Mock <IObjectFactory <IPollOption> >(); pollOption.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollOption>(_[0] as MVPollOption)); var pollSubmissionResponseFactory = new Mock <IObjectFactory <IPollSubmissionResponse> >(); pollSubmissionResponseFactory.Setup(_ => _.CreateChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.CreateChild <PollSubmissionResponse>(_[0] as IPollOption)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <IPoll> >(_ => pollFactory.Object); builder.Register <IObjectFactory <BusinessList <IPollOption> > >(_ => pollOptions.Object); builder.Register <IObjectFactory <IPollOption> >(_ => pollOption.Object); builder.Register <IObjectFactory <IPollSubmissionResponseCollection> >(_ => pollSubmissionResponsesFactory.Object); builder.Register <IObjectFactory <IPollSubmissionResponse> >(_ => pollSubmissionResponseFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var criteria = new PollSubmissionCriteria(pollEntity.PollID, userId); var submission = DataPortal.Create <PollSubmission>(criteria); Assert.AreEqual(pollEntity.PollID, submission.PollID, nameof(submission.PollID)); Assert.AreEqual(pollEntity.PollImageLink, submission.PollImageLink, nameof(submission.PollImageLink)); Assert.AreEqual(userId, submission.UserID, nameof(submission.UserID)); Assert.AreEqual(pollEntity.PollQuestion, submission.PollQuestion, nameof(submission.PollQuestion)); Assert.AreEqual(category.CategoryName, submission.CategoryName, nameof(submission.CategoryName)); Assert.AreEqual(pollEntity.PollDescription, submission.PollDescription, nameof(submission.PollDescription)); Assert.AreEqual(pollEntity.PollMaxAnswers.Value, submission.PollMaxAnswers, nameof(submission.PollMaxAnswers)); Assert.AreEqual(pollEntity.PollMinAnswers.Value, submission.PollMinAnswers, nameof(submission.PollMinAnswers)); Assert.AreEqual(4, submission.Responses.Count, nameof(submission.Responses)); submission.BrokenRulesCollection.AssertRuleCount(1); submission.BrokenRulesCollection.AssertRuleCount(PollSubmission.ResponsesProperty, 1); submission.BrokenRulesCollection.AssertBusinessRuleExists <MinimumAndMaximumPollSubmissionResponsesRule>( PollSubmission.ResponsesProperty, true); } entities.VerifyAll(); pollFactory.VerifyAll(); pollSubmissionResponsesFactory.VerifyAll(); }
public void Fetch() { var generator = new RandomObjectGenerator(); var pollId = generator.Generate <int>(); var userEntity = EntityCreator.Create <MVUser>(); var pollCommentEntity = EntityCreator.Create <MVPollComment>(_ => { _.ParentCommentID = null; _.PollID = pollId; _.UserID = userEntity.UserID; }); var childPollCommentEntity = EntityCreator.Create <MVPollComment>(_ => { _.ParentCommentID = generator.Generate <int>(); _.PollID = pollId; _.UserID = userEntity.UserID; }); var pollCommentFactoryCount = 0; var pollComment = Mock.Of <IPollComment>(); var pollCommentFactory = new Mock <IObjectFactory <IPollComment> >(MockBehavior.Strict); pollCommentFactory.Setup(_ => _.FetchChild( It.IsAny <PollCommentData>(), It.IsAny <List <PollCommentData> >())) .Callback(() => pollCommentFactoryCount++) .Returns(pollComment); var addCount = 0; var pollCommentCollection = new Mock <IPollCommentCollection>(MockBehavior.Strict); pollCommentCollection.Setup(_ => _.Add(pollComment)).Callback <IPollComment>(_ => addCount++); pollCommentCollection.Setup(_ => _.SetParent(It.IsAny <PollComments>())); pollCommentCollection.SetupGet(_ => _.EditLevel).Returns(0); var pollCommentsFactory = new Mock <IObjectFactory <IPollCommentCollection> >(MockBehavior.Strict); pollCommentsFactory.Setup(_ => _.FetchChild()).Returns(pollCommentCollection.Object); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPollComments).Returns( new InMemoryDbSet <MVPollComment> { pollCommentEntity, childPollCommentEntity }); entities.Setup(_ => _.MVUsers).Returns( new InMemoryDbSet <MVUser> { userEntity }); entities.Setup(_ => _.Dispose()); var builder = new ContainerBuilder(); builder.Register <IObjectFactory <IPollComment> >(_ => pollCommentFactory.Object); builder.Register <IObjectFactory <IPollCommentCollection> >(_ => pollCommentsFactory.Object); builder.Register <IEntities>(_ => entities.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var results = DataPortal.FetchChild <PollComments>(pollId); Assert.AreSame(pollCommentCollection.Object, results.Comments); Assert.AreEqual(1, pollCommentFactoryCount); Assert.AreEqual(1, addCount); } entities.VerifyAll(); pollCommentsFactory.VerifyAll(); pollCommentCollection.VerifyAll(); pollCommentFactory.VerifyAll(); }
public void Fetch() { var generator = new RandomObjectGenerator(); var pollID = generator.Generate <int>(); var response1 = EntityCreator.Create <MVPollResponse>(_ => { _.PollID = pollID; _.PollOptionID = 1; _.OptionSelected = true; }); var response2 = EntityCreator.Create <MVPollResponse>(_ => { _.PollID = pollID; _.PollOptionID = 2; _.OptionSelected = true; }); var response3 = EntityCreator.Create <MVPollResponse>(_ => { _.PollID = pollID; _.PollOptionID = 3; _.OptionSelected = false; }); var response4 = EntityCreator.Create <MVPollResponse>(_ => { _.PollID = pollID; _.PollOptionID = 1; _.OptionSelected = false; }); var response5 = EntityCreator.Create <MVPollResponse>(_ => { _.PollID = pollID; _.PollOptionID = 2; _.OptionSelected = true; }); var response6 = EntityCreator.Create <MVPollResponse>(_ => { _.PollID = pollID; _.PollOptionID = 1; _.OptionSelected = false; }); var option1 = EntityCreator.Create <MVPollOption>(_ => { _.PollOptionID = 1; _.PollID = pollID; }); var option2 = EntityCreator.Create <MVPollOption>(_ => { _.PollOptionID = 2; _.PollID = pollID; }); var option3 = EntityCreator.Create <MVPollOption>(_ => { _.PollOptionID = 3; _.PollID = pollID; }); var poll = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollID = pollID; }); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPollResponses).Returns(new InMemoryDbSet <MVPollResponse> { response1, response2, response3, response4, response5, response6 }); entities.Setup(_ => _.MVPollOptions).Returns(new InMemoryDbSet <MVPollOption> { option1, option2, option3 }); entities.Setup(_ => _.MVPolls).Returns(new InMemoryDbSet <MVPoll> { poll }); entities.Setup(_ => _.Dispose()); var pollDataResultsFactory = new Mock <IObjectFactory <ReadOnlySwitchList <IPollDataResult> > >(MockBehavior.Strict); pollDataResultsFactory.Setup(_ => _.FetchChild()).Returns(DataPortal.FetchChild <ReadOnlySwitchList <IPollDataResult> >()); var pollDataResultFactory = new Mock <IObjectFactory <IPollDataResult> >(MockBehavior.Strict); pollDataResultFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollDataResult>(_[0] as PollData)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <ReadOnlySwitchList <IPollDataResult> > >(_ => pollDataResultsFactory.Object); builder.Register <IObjectFactory <IPollDataResult> >(_ => pollDataResultFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var results = DataPortal.FetchChild <PollDataResults>(pollID); Assert.AreEqual(pollID, results.PollID, nameof(results.PollID)); Assert.AreEqual(poll.PollQuestion, results.Question, nameof(results.Question)); Assert.AreEqual(3, results.Results.Count, nameof(results.Results)); Assert.AreEqual(1, results.Results.First(_ => _.PollOptionID == 1).ResponseCount); Assert.AreEqual(2, results.Results.First(_ => _.PollOptionID == 2).ResponseCount); Assert.AreEqual(0, results.Results.First(_ => _.PollOptionID == 3).ResponseCount); } entities.VerifyAll(); pollDataResultsFactory.VerifyAll(); pollDataResultFactory.VerifyAll(); }
public void FetchByUserForPollsThatAreNotActive() { var now = DateTime.UtcNow; var category1 = new MVCategory { CategoryID = 1, CategoryName = "1" }; var category2 = new MVCategory { CategoryID = 2, CategoryName = "2" }; var poll1 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollStartDate = now.AddDays(-4); _.PollEndDate = DateTime.UtcNow.AddDays(-2); _.PollCategoryID = 1; _.UserID = 1; }); var poll2 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollStartDate = now.AddDays(-2); _.PollEndDate = DateTime.UtcNow.AddDays(2); _.UserID = 1; }); var poll3 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollStartDate = now.AddDays(-4); _.PollEndDate = DateTime.UtcNow.AddDays(-2); _.PollCategoryID = 2; _.UserID = 1; }); var poll4 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = true; _.PollStartDate = now.AddDays(-4); _.PollEndDate = DateTime.UtcNow.AddDays(-2); _.PollCategoryID = 2; _.UserID = 1; }); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVCategories).Returns(new InMemoryDbSet <MVCategory> { category1, category2 }); entities.Setup(_ => _.MVPolls).Returns(new InMemoryDbSet <MVPoll> { poll1, poll2, poll3, poll4 }); entities.Setup(_ => _.MVPollSubmissions).Returns(new InMemoryDbSet <MVPollSubmission>()); entities.Setup(_ => _.Dispose()); var pollSearchResultsByCategoryFactory = new Mock <IObjectFactory <ReadOnlySwitchList <IPollSearchResultsByCategory> > >(); pollSearchResultsByCategoryFactory.Setup(_ => _.FetchChild()).Returns(DataPortal.FetchChild <ReadOnlySwitchList <IPollSearchResultsByCategory> >()); var pollSearchResultByCategoryFactory = new Mock <IObjectFactory <IPollSearchResultsByCategory> >(); pollSearchResultByCategoryFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollSearchResultsByCategory>(_[0] as List <PollSearchResultsData>)); var pollSearchResultsFactory = new Mock <IObjectFactory <ReadOnlySwitchList <IPollSearchResult> > >(); pollSearchResultsFactory.Setup(_ => _.FetchChild()).Returns(() => DataPortal.FetchChild <ReadOnlySwitchList <IPollSearchResult> >()); var pollSearchResultFactory = new Mock <IObjectFactory <IPollSearchResult> >(); pollSearchResultFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollSearchResult>(_[0] as PollSearchResultsData)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <ISearchWhereClause>(_ => Mock.Of <ISearchWhereClause>()); builder.Register <IObjectFactory <ReadOnlySwitchList <IPollSearchResultsByCategory> > >(_ => pollSearchResultsByCategoryFactory.Object); builder.Register <IObjectFactory <IPollSearchResultsByCategory> >(_ => pollSearchResultByCategoryFactory.Object); builder.Register <IObjectFactory <ReadOnlySwitchList <IPollSearchResult> > >(_ => pollSearchResultsFactory.Object); builder.Register <IObjectFactory <IPollSearchResult> >(_ => pollSearchResultFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var result = DataPortal.Fetch <PollSearchResults>( new PollSearchResultsByUserCriteria(1, false)); Assert.AreEqual(2, result.SearchResultsByCategory.Count, nameof(result.SearchResultsByCategory)); } entities.VerifyAll(); }
public void FetchByPollQuestion() { var now = DateTime.UtcNow; var category1 = new MVCategory { CategoryID = 1, CategoryName = "1" }; var poll1 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollCategoryID = category1.CategoryID; _.PollQuestion = "12345"; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-2); }); var poll2 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollCategoryID = category1.CategoryID; _.PollQuestion = "AbCdE"; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-2); }); var poll3 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = true; _.PollCategoryID = category1.CategoryID; _.PollQuestion = "DeFgH"; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-2); }); var submission1 = EntityCreator.Create <MVPollSubmission>(_ => { _.PollID = poll2.PollID; }); var submission2 = EntityCreator.Create <MVPollSubmission>(_ => { _.PollID = poll2.PollID; }); var submission3 = EntityCreator.Create <MVPollSubmission>(_ => { _.PollID = poll1.PollID; }); var submission4 = EntityCreator.Create <MVPollSubmission>(_ => { _.PollID = poll2.PollID; }); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVCategories).Returns(new InMemoryDbSet <MVCategory> { category1 }); entities.Setup(_ => _.MVPolls).Returns(new InMemoryDbSet <MVPoll> { poll1, poll2, poll3 }); entities.Setup(_ => _.MVPollSubmissions).Returns(new InMemoryDbSet <MVPollSubmission> { submission1, submission2, submission3, submission4 }); entities.Setup(_ => _.Dispose()); var searchWhereClause = new Mock <ISearchWhereClause>(MockBehavior.Strict); searchWhereClause.Setup(_ => _.WhereClause(It.IsAny <DateTime>(), "%bcd%")).Returns(_ => _.PollQuestion == "AbCdE"); var pollSearchResultsByCategoryFactory = new Mock <IObjectFactory <ReadOnlySwitchList <IPollSearchResultsByCategory> > >(); pollSearchResultsByCategoryFactory.Setup(_ => _.FetchChild()).Returns(DataPortal.FetchChild <ReadOnlySwitchList <IPollSearchResultsByCategory> >()); var pollSearchResultByCategoryFactory = new Mock <IObjectFactory <IPollSearchResultsByCategory> >(); pollSearchResultByCategoryFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollSearchResultsByCategory>(_[0] as List <PollSearchResultsData>)); var pollSearchResultsFactory = new Mock <IObjectFactory <ReadOnlySwitchList <IPollSearchResult> > >(); pollSearchResultsFactory.Setup(_ => _.FetchChild()).Returns(() => DataPortal.FetchChild <ReadOnlySwitchList <IPollSearchResult> >()); var pollSearchResultFactory = new Mock <IObjectFactory <IPollSearchResult> >(); pollSearchResultFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollSearchResult>(_[0] as PollSearchResultsData)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <ISearchWhereClause>(_ => searchWhereClause.Object); builder.Register <IObjectFactory <ReadOnlySwitchList <IPollSearchResultsByCategory> > >(_ => pollSearchResultsByCategoryFactory.Object); builder.Register <IObjectFactory <IPollSearchResultsByCategory> >(_ => pollSearchResultByCategoryFactory.Object); builder.Register <IObjectFactory <ReadOnlySwitchList <IPollSearchResult> > >(_ => pollSearchResultsFactory.Object); builder.Register <IObjectFactory <IPollSearchResult> >(_ => pollSearchResultFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var result = DataPortal.Fetch <PollSearchResults>("bCd"); Assert.AreEqual(1, result.SearchResultsByCategory.Count, nameof(result.SearchResultsByCategory)); Assert.AreEqual(1, result.SearchResultsByCategory[0].SearchResults.Count, nameof(ICollection.Count)); Assert.AreEqual(poll2.PollQuestion, result.SearchResultsByCategory[0].SearchResults[0].Question, nameof(IPollSearchResult.Question)); } searchWhereClause.VerifyAll(); entities.VerifyAll(); }
public void FetchByNewest() { var now = DateTime.UtcNow; var category1 = new MVCategory { CategoryID = 1, CategoryName = "b" }; var category2 = new MVCategory { CategoryID = 2, CategoryName = "a" }; var poll1 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-2); _.PollCategoryID = 1; }); var poll2 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-2); _.PollCategoryID = 2; }); var poll3 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-4); _.PollCategoryID = 1; }); var poll4 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-4); _.PollCategoryID = 2; }); var poll5 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-1); _.PollCategoryID = 1; }); var poll6 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-1); _.PollCategoryID = 2; }); var poll7 = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = true; _.PollEndDate = now.AddDays(2); _.PollStartDate = now.AddDays(-1); _.PollCategoryID = 2; }); var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVCategories).Returns(new InMemoryDbSet <MVCategory> { category1, category2 }); entities.Setup(_ => _.MVPolls).Returns(new InMemoryDbSet <MVPoll> { poll1, poll2, poll3, poll4, poll5, poll6, poll7 }); entities.Setup(_ => _.MVPollSubmissions).Returns(new InMemoryDbSet <MVPollSubmission>()); entities.Setup(_ => _.Dispose()); var pollSearchResultsByCategoryFactory = new Mock <IObjectFactory <ReadOnlySwitchList <IPollSearchResultsByCategory> > >(); pollSearchResultsByCategoryFactory.Setup(_ => _.FetchChild()).Returns(DataPortal.FetchChild <ReadOnlySwitchList <IPollSearchResultsByCategory> >()); var pollSearchResultByCategoryFactory = new Mock <IObjectFactory <IPollSearchResultsByCategory> >(); pollSearchResultByCategoryFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollSearchResultsByCategory>(_[0] as List <PollSearchResultsData>)); var pollSearchResultsFactory = new Mock <IObjectFactory <ReadOnlySwitchList <IPollSearchResult> > >(); pollSearchResultsFactory.Setup(_ => _.FetchChild()).Returns(() => DataPortal.FetchChild <ReadOnlySwitchList <IPollSearchResult> >()); var pollSearchResultFactory = new Mock <IObjectFactory <IPollSearchResult> >(); pollSearchResultFactory.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollSearchResult>(_[0] as PollSearchResultsData)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <ISearchWhereClause>(_ => Mock.Of <ISearchWhereClause>()); builder.Register <IObjectFactory <ReadOnlySwitchList <IPollSearchResultsByCategory> > >(_ => pollSearchResultsByCategoryFactory.Object); builder.Register <IObjectFactory <IPollSearchResultsByCategory> >(_ => pollSearchResultByCategoryFactory.Object); builder.Register <IObjectFactory <ReadOnlySwitchList <IPollSearchResult> > >(_ => pollSearchResultsFactory.Object); builder.Register <IObjectFactory <IPollSearchResult> >(_ => pollSearchResultFactory.Object); using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var result = DataPortal.Fetch <PollSearchResults>(PollSearchResultsQueryType.Newest); Assert.AreEqual(2, result.SearchResultsByCategory.Count, nameof(result.SearchResultsByCategory)); var firstCategory = result.SearchResultsByCategory[0]; Assert.AreEqual("a", firstCategory.Category, $"{nameof(firstCategory.Category)} a"); Assert.AreEqual(3, firstCategory.SearchResults.Count, $"{nameof(firstCategory.SearchResults)} a"); Assert.AreEqual(poll6.PollQuestion, firstCategory.SearchResults[0].Question, $"{nameof(IPollSearchResult.Question)} a 0"); Assert.AreEqual(poll2.PollQuestion, firstCategory.SearchResults[1].Question, $"{nameof(IPollSearchResult.Question)} a 1"); Assert.AreEqual(poll4.PollQuestion, firstCategory.SearchResults[2].Question, $"{nameof(IPollSearchResult.Question)} a 2"); var secondCategory = result.SearchResultsByCategory[1]; Assert.AreEqual("b", secondCategory.Category, $"{nameof(secondCategory.Category)} b"); Assert.AreEqual(3, secondCategory.SearchResults.Count, $"{nameof(secondCategory.SearchResults)} b"); Assert.AreEqual(poll5.PollQuestion, secondCategory.SearchResults[0].Question, $"{nameof(IPollSearchResult.Question)} b 0"); Assert.AreEqual(poll1.PollQuestion, secondCategory.SearchResults[1].Question, $"{nameof(IPollSearchResult.Question)} b 1"); Assert.AreEqual(poll3.PollQuestion, secondCategory.SearchResults[2].Question, nameof(IPollSearchResult.Question)); } entities.VerifyAll(); }
public void Delete() { var generator = new RandomObjectGenerator(); var userId = generator.Generate <int>(); var now = DateTime.UtcNow; var entity = EntityCreator.Create <MVPoll>(_ => { _.PollDeletedFlag = false; _.PollMinAnswers = 1; _.PollMaxAnswers = 2; _.PollStartDate = now; _.PollEndDate = now.AddDays(2); _.UserID = userId; }); entity.MVPollOptions = new List <MVPollOption> { EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>(), EntityCreator.Create <MVPollOption>() }; var polls = new InMemoryDbSet <MVPoll> { entity }; var entities = new Mock <IEntities>(MockBehavior.Strict); entities.Setup(_ => _.MVPolls).Returns(polls); entities.Setup(_ => _.SetState(It.IsAny <MVPoll>(), EntityState.Modified)); entities.Setup(_ => _.SaveChanges()).Returns(1); entities.Setup(_ => _.Dispose()); var pollOptions = new Mock <IObjectFactory <BusinessList <IPollOption> > >(); pollOptions.Setup(_ => _.FetchChild()).Returns(new BusinessList <IPollOption>()); var pollOption = new Mock <IObjectFactory <IPollOption> >(); pollOption.Setup(_ => _.FetchChild(It.IsAny <object[]>())) .Returns <object[]>(_ => DataPortal.FetchChild <PollOption>(_[0] as MVPollOption)); var builder = new ContainerBuilder(); builder.Register <IEntities>(_ => entities.Object); builder.Register <IObjectFactory <BusinessList <IPollOption> > >(_ => pollOptions.Object); builder.Register <IObjectFactory <IPollOption> >(_ => pollOption.Object); var identity = new Mock <IUserIdentity>(MockBehavior.Strict); identity.Setup(_ => _.UserID).Returns(userId); identity.Setup(_ => _.IsInRole(UserRoles.Admin)).Returns(false); var principal = new Mock <IPrincipal>(MockBehavior.Strict); principal.Setup(_ => _.Identity).Returns(identity.Object); using (principal.Object.Bind(() => ApplicationContext.User)) { using (new ObjectActivator(builder.Build(), new ActivatorCallContext()) .Bind(() => ApplicationContext.DataPortalActivator)) { var poll = new DataPortal <Poll>().Fetch(entity.PollID); poll.Delete(); poll = poll.Save(); Assert.AreEqual(2, polls.Local.Count); var deletedPoll = polls.Local[1]; Assert.IsNotNull(deletedPoll.PollDeletedDate, nameof(deletedPoll.PollDeletedDate)); Assert.IsTrue(deletedPoll.PollDeletedFlag.Value, nameof(deletedPoll.PollDeletedFlag)); } } principal.VerifyAll(); identity.VerifyAll(); entities.VerifyAll(); }