Skip to content

Commit

Permalink
- BugFix: Seeding questions now correctly creates the CreatedByUserId.
Browse files Browse the repository at this point in the history
- Added Json question index and test fact.
- Updated RavenDb to v606-U.
  • Loading branch information
PureKrome committed Jan 24, 2012
1 parent d91867b commit 3693d92
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 76 deletions.
11 changes: 5 additions & 6 deletions Code/RavenOverflow.FakeData/FakeQuestions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,16 @@ public static ICollection<Question> CreateFakeQuestions(IList<string> userIds, i

public static Question CreateAFakeQuestion(string userId, IList<string> answerUserIds)
{
Condition.Requires(userId).IsNotNullOrEmpty();

Question fakeQuestion = Builder<Question>
.CreateNew()
.With(x => x.Id = null)
.With(x => x.Subject = GetRandom.Phrase(GetRandom.Int(10, 50)))
.With(x => x.Content = GetRandom.Phrase(GetRandom.Int(30, 500)))
.And(
x =>
x.CreatedByUserId =
string.IsNullOrEmpty(userId) ? null : userId)
.And(
x => x.CreatedOn = GetRandom.DateTime(DateTime.UtcNow.AddMonths(-1), DateTime.UtcNow.AddMinutes(-5)))
.And(x => x.CreatedByUserId = userId)
.And(x => x.CreatedOn = GetRandom.DateTime(DateTime.UtcNow.AddMonths(-1),
DateTime.UtcNow.AddMinutes(-5)))
.And(x => x.NumberOfViews = GetRandom.PositiveInt(10000))
.And(x => x.Tags = FakeTags.ToRandomList(GetRandom.Int(1, 5)))
.And(x => x.Vote = CreateAFakeVote())
Expand Down
37 changes: 37 additions & 0 deletions Code/RavenOverflow.Tests/ControllerUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using CuttingEdge.Conditions;
using Moq;
using Raven.Client;
using RavenOverflow.Web.Controllers;
using RavenOverflow.Web.Models.Authentication;

namespace RavenOverflow.Tests
{
public static class ControllerUtilities
{
// Reference: http://nerddinnerbook.s3.amazonaws.com/Part12.htm
// Yes .. Nerd Dinner to the rescue! and we come full circle...
public static T HomeController<T>(IDocumentSession documentSession,
string userId = null,
string displayName = null,
string[] roles = null) where T : AbstractController, new()
{
Condition.Requires(documentSession);

// Some fake Authentication stuff.
var customIdentity = new CustomIdentity(userId, displayName);
var customPrincipal = new CustomPrincipal(customIdentity, roles);

var mockControllerContext = new Mock<ControllerContext>();
mockControllerContext.Setup(x => x.HttpContext.User).Returns(customPrincipal);

var homeController = new T(documentSession) { ControllerContext = mockControllerContext.Object };

return homeController;
}
}
}
57 changes: 44 additions & 13 deletions Code/RavenOverflow.Tests/Controllers/HomeControllerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using CuttingEdge.Conditions;
using Moq;
using Raven.Client;
using RavenOverflow.Core.Entities;
using RavenOverflow.FakeData;
using RavenOverflow.Web.Controllers;
using RavenOverflow.Web.Indexes;
using RavenOverflow.Web.Models.Authentication;
using RavenOverflow.Web.Models.AutoMapping;
using RavenOverflow.Web.Models.ViewModels;
using Xunit;
Expand All @@ -34,7 +31,7 @@ public void GivenSomeQuestions_Index_ReturnsTheMostRecentQuestions()
using (IDocumentSession documentSession = DocumentStore.OpenSession())
{
// Arrange.
HomeController homeController = new HomeController(documentSession);
var homeController = new HomeController(documentSession);
ControllerUtilities.SetUpControllerContext(homeController);

// Act.
Expand All @@ -51,7 +48,7 @@ public void GivenSomeQuestions_Index_ReturnsTheMostRecentQuestions()

// Make sure all the items are ordered correctly.
DateTime? previousQuestion = null;
foreach (var question in model.QuestionListViewModel.Questions)
foreach (QuestionWithDisplayName question in model.QuestionListViewModel.Questions)
{
if (previousQuestion.HasValue)
{
Expand All @@ -70,7 +67,7 @@ public void GivenSomeQuestions_Index_ReturnsTheMostRecentQuestions()
// * CreatedByUserId - this is randomized when fakes are created.
// * CreatedOn - these fakes were made AFTER the Stored data.
// ASSUMPTION: the first 5 fixed questions are the first 5 documents in the Document Store.
var question = model.QuestionListViewModel.Questions[i];
QuestionWithDisplayName question = model.QuestionListViewModel.Questions[i];
Assert.Equal(fixedQuestions[i].Subject, question.Subject);
Assert.Equal(fixedQuestions[i].Content, question.Content);
Assert.Equal(fixedQuestions[i].NumberOfViews, question.NumberOfViews);
Expand All @@ -87,7 +84,7 @@ public void GivenSomeQuestions_Index_ReturnsTheMostRecentPopularTagsInTheLast30D
using (IDocumentSession documentSession = DocumentStore.OpenSession())
{
// Arrange.
HomeController homeController = new HomeController(documentSession);
var homeController = new HomeController(documentSession);
ControllerUtilities.SetUpControllerContext(homeController);

// Act.
Expand Down Expand Up @@ -125,7 +122,7 @@ public void GivenAnAuthenticatedUserWithSomeFavouriteTags_Index_ReturnsAFavourit
{
// Arrange.
// Note: we're faking that a user has authenticated.
HomeController homeController = new HomeController(documentSession);
var homeController = new HomeController(documentSession);
ControllerUtilities.SetUpControllerContext(homeController, displayName: "Pure.Krome");


Expand Down Expand Up @@ -156,7 +153,7 @@ public void GivenNoAuthenticatedUser_Index_ReturnsFavouriteTagsViewModelWithNoTa
{
// Arrange.
// Note: we're faking that no user has been authenticated.
HomeController homeController = new HomeController(documentSession);
var homeController = new HomeController(documentSession);
ControllerUtilities.SetUpControllerContext(homeController);

// Act.
Expand Down Expand Up @@ -185,7 +182,7 @@ public void GivenSomeQuestionsAndAnExistingTag_Tags_ReturnsAListOfTaggedQuestion
{
// Arrange.
const string tag = "ravendb";
HomeController homeController = new HomeController(documentSession);
var homeController = new HomeController(documentSession);
ControllerUtilities.SetUpControllerContext(homeController);

// Act.
Expand Down Expand Up @@ -217,7 +214,7 @@ public void GivenSomeQuestionsAndAnExistingTag_Search_ReturnsAListOfTags()

// Arrange.
const string tag = "ravendb";
HomeController homeController = new HomeController(documentSession);
var homeController = new HomeController(documentSession);
ControllerUtilities.SetUpControllerContext(homeController);

// Act.
Expand Down Expand Up @@ -245,7 +242,7 @@ public void GivenSomeQuestionsAndAnExistingPartialTag_Search_ReturnsAListOfTagge

// Arrange.
const string tag = "ravne"; // Hardcoded Typo.
HomeController homeController = new HomeController(documentSession);
var homeController = new HomeController(documentSession);
ControllerUtilities.SetUpControllerContext(homeController);

// Act.
Expand All @@ -261,8 +258,42 @@ public void GivenSomeQuestionsAndAnExistingPartialTag_Search_ReturnsAListOfTagge
}
}

public void GivenSomeQuestionsForAUserWithATag_Search_ReturnsSomeQuestions()
[Fact]
public void GivenSomeQuestionsAndNoDisplayNameAndNoTags_Index_ReturnsAJsonViewOfMostRecentQuestions()
{
using (IDocumentSession documentSession = DocumentStore.OpenSession())
{
// Arrange.
var homeController = new HomeController(documentSession);
ControllerUtilities.SetUpControllerContext(homeController);

// Act.
// Note: this should return a list of the 20 most recent.
JsonResult result = homeController.IndexJson(null, null);

// Assert.
Assert.NotNull(result);
var questions = result.Data as IList<QuestionWithDisplayName>;
Assert.NotNull(questions);
Assert.Equal(20, questions.Count);

// Now lets Make sure each one is ok.
DateTime? previousDate = null;
foreach (var question in questions)
{
if (previousDate.HasValue)
{
Assert.True(previousDate.Value > question.CreatedOn);
}

previousDate = question.CreatedOn;
Assert.NotNull(question.DisplayName);
Assert.NotNull(question.Id);
Assert.NotNull(question.CreatedByUserId);
Assert.NotNull(question.Subject);
Assert.NotNull(question.Content);
}
}
}
}

Expand Down
61 changes: 25 additions & 36 deletions Code/RavenOverflow.Tests/Controllers/QuestionControllerFacts.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc;
using AutoMapper;
using CuttingEdge.Conditions;
using Moq;
using Raven.Client;
using RavenOverflow.Core.Entities;
using RavenOverflow.Core.Services;
using RavenOverflow.FakeData;
using RavenOverflow.Services;
using RavenOverflow.Web.Areas.Question.Controllers;
using RavenOverflow.Web.Areas.Question.Models.ViewModels;
using RavenOverflow.Web.Controllers;
using RavenOverflow.Web.Indexes;
using RavenOverflow.Web.Models.Authentication;
using RavenOverflow.Web.Models.AutoMapping;
using RavenOverflow.Web.Models.ViewModels;
using Xunit;

namespace RavenOverflow.Tests.Controllers
Expand All @@ -39,14 +28,15 @@ public void GivenAnInValidQuestion_Create_ReturnsAResultView()
{
// Arrange.
IQuestionService questionService = new QuestionService(documentSession);
QuestionsController questionsController = new QuestionsController(documentSession, questionService);
var questionsController = new QuestionsController(documentSession, questionService);
ControllerUtilities.SetUpControllerContext(questionsController);
CreateInputModel createInputModel = new CreateInputModel
{
Content = "Some content",
Subject = null, // RuRoh - dat ist missin'
Tags = "tag1 tag2 tag3-3-3"
};
var createInputModel = new CreateInputModel
{
Content = "Some content",
Subject = null,
// RuRoh - dat ist missin'
Tags = "tag1 tag2 tag3-3-3"
};

// Now pretend the model binding raised an error with the input model.
questionsController.ModelState.AddModelError("key", "error message");
Expand All @@ -67,14 +57,14 @@ public void GivenAValidQuestionAndNoOneIsLoggedIn_Create_ReturnsAResultView()
{
// Arrange.
IQuestionService questionService = new QuestionService(documentSession);
QuestionsController questionsController = new QuestionsController(documentSession, questionService);
var questionsController = new QuestionsController(documentSession, questionService);
ControllerUtilities.SetUpControllerContext(questionsController);
CreateInputModel createInputModel = new CreateInputModel
{
Content = "Some content",
Subject = "Subject",
Tags = "tag1 tag2 tag3-3-3"
};
var createInputModel = new CreateInputModel
{
Content = "Some content",
Subject = "Subject",
Tags = "tag1 tag2 tag3-3-3"
};
// Act.
var result = questionsController.Create(createInputModel) as ViewResult;

Expand All @@ -83,22 +73,22 @@ public void GivenAValidQuestionAndNoOneIsLoggedIn_Create_ReturnsAResultView()
Assert.Equal("Create", result.ViewName);
}
}

[Fact]
public void GivenAValidQuestionAndALoggedInUser_Create_AddsTheQuestionAndRedicects()
{
using (IDocumentSession documentSession = DocumentStore.OpenSession())
{
// Arrange.
IQuestionService questionService = new QuestionService(documentSession);
QuestionsController questionsController = new QuestionsController(documentSession, questionService);
var questionsController = new QuestionsController(documentSession, questionService);
ControllerUtilities.SetUpControllerContext(questionsController, "users/1");
CreateInputModel createInputModel = new CreateInputModel
{
Content = "Some content",
Subject = "Subject",
Tags = "tag1 tag2 tag3-3-3"
};
var createInputModel = new CreateInputModel
{
Content = "Some content",
Subject = "Subject",
Tags = "tag1 tag2 tag3-3-3"
};
// Act.
var result = questionsController.Create(createInputModel) as RedirectToRouteResult;

Expand All @@ -108,7 +98,6 @@ public void GivenAValidQuestionAndALoggedInUser_Create_AddsTheQuestionAndRedicec
}
}

// ReSharper restore InconsistentNaming
}

// ReSharper restore InconsistentNaming
}
11 changes: 6 additions & 5 deletions Code/RavenOverflow.Tests/RavenDbTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ private static void CreateSeedData(IDocumentStore documentStore)
{
Condition.Requires(documentStore).IsNotNull();

// Users.
ICollection<User> users = FakeUsers.CreateFakeUsers(50);

// Questions.
ICollection<Question> questions = FakeQuestions.CreateFakeQuestions(users.Select(x => x.Id).ToList());


using (IDocumentSession documentSession = documentStore.OpenSession())
{
// Users.
ICollection<User> users = FakeUsers.CreateFakeUsers(50);
StoreFakeEntities(users, documentSession);

// Questions.
ICollection<Question> questions = FakeQuestions.CreateFakeQuestions(users.Select(x => x.Id).ToList());
StoreFakeEntities(questions, documentSession);

documentSession.SaveChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ActionResult Create()
return View("Create", viewModel);
}

[HttpPost, RavenActionFilter, Authorize]
[HttpPost, RavenDb, Authorize]
public ActionResult Create(CreateInputModel inputModel)
{
try
Expand Down Expand Up @@ -59,5 +59,7 @@ public ActionResult Create(CreateInputModel inputModel)
return View("Create", viewModel);
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public ActionResult AuthenticateWithFacebook()
return Redirect(facebookAuthenticationUri.ToString());
}

[RavenActionFilter]
[RavenDb]
public ActionResult FacebookAuthenticationCallback()
{
FacebookOAuthResult facebookOAuthResult;
Expand Down
Loading

0 comments on commit 3693d92

Please sign in to comment.