public void AddWordAllowsUserToAddWordIfUserIsCurrentEditor() { // Setup var store = Global.GetInMemoryStore(); var userId = "users/1"; var word = "New"; var story = FakeEntityFactory.GetGenericStory(userId); story.Lock.UserId = userId; story.Lock.LockedDate = DateTime.Now.AddMinutes(-8); using (var session = store.OpenSession()) { session.Store(story); session.SaveChanges(); Global.UpdateIndex<Story>(session); } // Act StoryRepository repository = new StoryRepository(store); var result = repository.AddWord(story.Id, word, userId); // Assert Assert.AreEqual(result.ErrorCode, StoryErrorCode.Success); Assert.IsTrue(result.Story.Paragraphs.Last().EndsWith(word)); }
static void Main(string[] args) { var story = File.ReadLines("../../Story.txt"); string storyID = ""; foreach (var paragraph in story) { bool newParagraph = true; if (!string.IsNullOrEmpty(paragraph)) { Console.WriteLine("Saving Paragraph"); var words = paragraph.Split(' '); foreach (string word in words) { if (!string.IsNullOrEmpty(word)) { Console.WriteLine("Saving word"); StoryRepository repository = new StoryRepository(); var result = repository.AddWord(storyID, word, users.PickRandom<string>(), newParagraph); newParagraph = false; storyID = result.Story.Id; } } } } }
public void UserCanLockAfterWordHasBeenAdded() { // Setup var user1 = "users/1"; var user2 = "users/2"; var story = FakeEntityFactory.GetGenericStory("users/23"); IDocumentStore store = Global.GetInMemoryStore(); using (var session = store.OpenSession()) { session.Store(story); session.SaveChanges(); } var repository = new StoryRepository(store); using (var session = store.OpenSession()) { var firstLockResult = repository.LockStory(story.Id, user1); var saveStory = session.Load<Story>(story.Id); Assert.IsTrue(saveStory.HasEditor); Assert.AreEqual(saveStory.Lock.UserId, user1); var secondLockResult = repository.LockStory(story.Id, user2); saveStory = session.Load<Story>(story.Id); Assert.AreEqual(secondLockResult, StoryErrorCode.StoryLockedForEditing); Assert.AreEqual(saveStory.Lock.UserId, user1); } using (var session = store.OpenSession()) { var firstAddWordResult = repository.AddWord(story.Id, "word", user1); var saveStory = session.Load<Story>(story.Id); Assert.AreEqual(firstAddWordResult.ErrorCode, StoryErrorCode.Success); Assert.IsFalse(saveStory.HasEditor); } using (var session = store.OpenSession()) { var thirdLockResult = repository.LockStory(story.Id, user2); var saveStory = session.Load<Story>(story.Id); Assert.AreEqual(thirdLockResult, StoryErrorCode.Success); Assert.IsTrue(saveStory.HasEditor); Assert.AreEqual(saveStory.Lock.UserId, user2); } // Act // Assert }
public void AddWordReturnsProperErrorCodeIfStoryIsNotFound() { // Setup IDocumentStore store = Global.GetInMemoryStore(); StoryRepository repository = new StoryRepository(store); // Act var result = repository.AddWord("story/id", "Add", "users/1"); // Assert Assert.AreEqual(result.ErrorCode, StoryErrorCode.StoryNotFoundInRepository); }
public void AddWordTenMinutesAfterLockReturnsError() { // Setup var store = Global.GetInMemoryStore(); var userId = "users/1"; var story = FakeEntityFactory.GetGenericStory(userId); story.Lock.UserId = userId; story.Lock.LockedDate = DateTime.Now.AddMinutes(-15); using (var session = store.OpenSession()) { session.Store(story); session.SaveChanges(); Global.UpdateIndex<Story>(session); } StoryRepository repository = new StoryRepository(store); var result = repository.AddWord(story.Id, "New", userId); // Act // Assert Assert.AreEqual(result.ErrorCode, StoryErrorCode.TenMinuteLockWindowHasClosed); }
public void AddWordThrowsExceptionsIfWordOrUserIdIsNullOrEmpty() { // Setup var store = Global.GetInMemoryStore(); StoryRepository repository = new StoryRepository(store); // Assert Assert.Throws<ArgumentNullException>(() => repository.AddWord("story/1", "", "userId"), "word"); Assert.Throws<ArgumentNullException>(() => repository.AddWord("story/2", null, "userId"), "word"); Assert.Throws<ArgumentNullException>(() => repository.AddWord("story/1", "Once", ""), "userId"); Assert.Throws<ArgumentNullException>(() => repository.AddWord("story/2", "Once", null), "userId"); }
public void StartNewParagraphTest() { // Setup IDocumentStore store = Global.GetInMemoryStore(); var repository = new StoryRepository(store); var wordToAdd = "Hey"; var story = FakeEntityFactory.GetGenericStory("users/1"); var numberofParagraphs = story.Paragraphs.Count; using (var session = store.OpenSession()) { session.Store(story); session.SaveChanges(); } // Act repository.AddWord(story.Id, wordToAdd, "users/1", true); // Assert using (var session = store.OpenSession()) { Global.UpdateIndex<Story>(session); var resultStory = session.Load<Story>(story.Id); Assert.AreEqual(resultStory.Paragraphs.Count, numberofParagraphs + 1); Assert.AreEqual(resultStory.Paragraphs[numberofParagraphs], wordToAdd); } }
public void AddWordWontAllowTheSameUserToAddTwoConsecutiveWords() { // Setup Story story = new Story(); string userId = "users/1"; story.EditHistory.Add(new EditHistory() { DateAdded = DateTime.Now.AddHours(-4), UserId = userId }); IDocumentStore store = Global.GetInMemoryStore(); using (var session = store.OpenSession()) { session.Store(story); session.SaveChanges(); } StoryRepository repository = new StoryRepository(store); // Act var result = repository.AddWord(story.Id, "Add", userId); // Assert Assert.AreEqual(result.ErrorCode, StoryErrorCode.UserAddedTheLastWordInThisStory); }
public void AddWordToStoryWithCurrentEditorThrowsException() { // Setup Story story = new Story(); story.Paragraphs.Add("Here is the first paragraph of the story. Content doesn't really matter at the moment"); story.Lock.UserId = "users/2"; story.Lock.LockedDate = DateTime.Now; IDocumentStore store = Global.GetInMemoryStore(); StoryRepository repository = new StoryRepository(store); SaveStory(story, store); repository.AddWord(story.Id, "word", "users/1"); }
public void AddWordToOneParagraphExistingStory() { // Setup Story story = new Story(); story.Paragraphs.Add("Here is the first paragraph of the story. Content doesn't really matter at the moment"); var store = Global.GetInMemoryStore(); SaveStory(story, store); var rep = new StoryRepository(store); // Act rep.AddWord(story.Id, "added.", "users/1"); // Assert using (var session = store.OpenSession()) { var savedStory = session.Load<Story>(story.Id); Assert.AreEqual(savedStory.Paragraphs[0], "Here is the first paragraph of the story. Content doesn't really matter at the moment added."); } }
public void AddWordToNewStory() { // Setup var store = Global.GetInMemoryStore(); StoryRepository repository = new StoryRepository(store); string userId = "users/2"; string word = "Once"; // Act var result = repository.AddWord("", word, userId); // Assert using (var session = store.OpenSession()) { var story = session.Load<Story>(result.Story.Id); Assert.AreEqual(story.LastEditorId, userId); Assert.IsNotNull(story.EditHistory); Assert.Greater(story.EditHistory[0].DateAdded, DateTime.Now.AddMinutes(-1)); Assert.AreEqual(story.EditHistory[0].ParagraphIndex, 0); Assert.AreEqual(story.EditHistory[0].ParagraphNumber, 1); Assert.AreEqual(story.EditHistory[0].UserId, userId); Assert.AreEqual(story.EditHistory.Count, 1); Assert.AreEqual(story.Id, result.Story.Id); Assert.AreEqual(story.Paragraphs.Count, 1); Assert.AreEqual(story.Paragraphs[0], word); } }
public void AddWordToMultiParagraphExistingStory() { // Setup Story story = new Story(); story.Paragraphs.Add("Here is the first paragraph of the story. Content doesn't really matter at the moment"); story.Paragraphs.Add("Here is the second paragraph of the story. Content doesn't really matter at the moment"); var store = Global.GetInMemoryStore(); SaveStory(story, store); StoryRepository repository = new StoryRepository(store); // Act repository.AddWord(story.Id, "Added", "users/1"); // Assert using (var session = store.OpenSession()) { var savedStory = session.Load<Story>(story.Id); Assert.IsTrue(savedStory.Paragraphs[1] == "Here is the second paragraph of the story. Content doesn't really matter at the moment Added"); } }
public void AddWordToExistingUpdatesEditHistory() { // Setup Story story = new Story(); story.Paragraphs.Add("Here is the first paragraph of the story. Content doesn't really matter at the moment"); var store = Global.GetInMemoryStore(); SaveStory(story, store); StoryRepository repository = new StoryRepository(store); // Act repository.AddWord(story.Id, "Added", "users/1"); // Assert using (var session = store.OpenSession()) { var savedStory = session.Load<Story>(story.Id); Assert.Greater(savedStory.EditHistory[0].DateAdded, DateTime.Now.AddMinutes(-1)); Assert.AreEqual(savedStory.EditHistory[0].ParagraphIndex, 86); Assert.AreEqual(savedStory.EditHistory[0].ParagraphNumber, 1); Assert.AreEqual(savedStory.EditHistory[0].UserId, "users/1"); } }