public void AddsCommentIfChangeExists() { // Arrange DateTime start = DateTime.UtcNow; var comments = CreateService(); var chg = new FileAddition() { FileName = "foo" }; chg.Comments = new List<Comment>(); comments.MockData.Changes.Add(chg); comments.MockData.SaveChanges(); // Act var returned = comments.CreateComment(chg.Id, 42, "body", 24); // Assert Comment added = comments.MockData.Changes.Single().Comments.Single(); Assert.Equal(added, returned, new PropertyEqualityComparer()); Assert.Equal(added, new Comment() { Content = "body", DiffLineIndex = 42, UserId = 24, PostedOn = added.PostedOn // Don't care about exact PostedOn, so force it to pass this equality check. }, new PropertyEqualityComparer()); // Do a sanity check on the posted on to make sure it's not MinValue or something like that. Assert.True(added.PostedOn <= DateTime.UtcNow && added.PostedOn >= start); }
public void ReturnsChangeWithSpecifiedIdIfPresent() { // Arrange var changes = CreateService(); var added = new FileAddition() { FileName = "florb" }; changes.MockData.Changes.Add(added); changes.MockData.SaveChanges(); // Act FileChange fetched = changes.GetChange(added.Id); // Assert Assert.Equal(added, fetched, new PropertyEqualityComparer()); }
public void ReturnsNullIfChangeIdDoesNotExist() { // Arrange var comments = CreateService(); var chg = new FileAddition() { FileName = "foo" }; comments.MockData.Changes.Add(chg); comments.MockData.SaveChanges(); // Act var comment = comments.CreateComment(chg.Id + 42, 0, "body", 0); // Assert Assert.Null(comment); }
public void ReadsFromInputAndPassesToConverter() { // Arrange var service = CreateService(); var expected = new FileAddition(); var input = new StringReader("abc"); var set = new DiffSet(new FileDiff("Orig", "Mod")); service.MockReader.Setup(r => r.Read(input)).Returns(set); service.MockConverter.Setup(c => c.ConvertFile(set.Files.First())).Returns(expected); // Act var actual = service.CreateFromGitDiff(input).SingleOrDefault(); // Assert Assert.Same(expected, actual); }
/// <summary> /// Loads image data from a local file and set it to current image data. /// </summary> /// <param name="item">Target item</param> internal async Task LoadSetAsync(FileItemViewModel item) { _tokenSourceLoading?.TryCancel(); // Canceling already canceled source will be simply ignored. try { _tokenSourceLoading = new CancellationTokenSourcePlus(); byte[] data = null; if (item.IsAvailableLocal && item.CanLoadDataLocal) { data = await FileAddition.ReadAllBytesAsync(ComposeLocalPath(item), _tokenSourceLoading.Token); } CurrentItem = item; CurrentImageData = data; } catch (OperationCanceledException) { // None. } catch (FileNotFoundException) { item.Status = FileStatus.NotCopied; item.IsAliveLocal = false; } catch (IOException) { item.IsAvailableLocal = false; } catch (Exception ex) { Debug.WriteLine($"Failed to load image data from local file.\r\n{ex}"); throw new UnexpectedException("Failed to load image data from local file.", ex); } finally { _tokenSourceLoading?.Dispose(); } }
/// <summary> /// Load image data from local file of a specified item and set it to current image data. /// </summary> /// <param name="item">Target item</param> private async Task LoadSetFileBaseAsync(FileItemViewModel item) { var localPath = ComposeLocalPath(item); try { tokenSourceLoading = new CancellationTokenSource(); isTokenSourceLoadingDisposed = false; byte[] data = null; if (item.CanLoadDataLocal) { data = await FileAddition.ReadAllBytesAsync(localPath, tokenSourceLoading.Token); } CurrentItem = item; CurrentImageData = data; } catch (FileNotFoundException) { item.Status = FileStatus.NotCopied; item.IsAliveLocal = false; } catch (IOException) { item.CanLoadDataLocal = false; } finally { if (tokenSourceLoading != null) { isTokenSourceLoadingDisposed = true; tokenSourceLoading.Dispose(); } } }
public void Returns404IfNoChangeWithId() { // Arrange var c = CreateController(); var chg = new FileAddition() { FileName = "blorg", Comments = new List<Comment>() }; c.MockData.Changes.Add(chg); c.MockData.SaveChanges(); // Act var result = c.Post(chg.Id + 42, 0, "blarg"); // Assert Assert.Equal(HttpStatusCode.NotFound, result.StatusCode); Assert.False(chg.Comments.Any()); }
public void Returns201AddsCommentAndReturnsCommentIfChangeExists() { // Arrange var c = CreateController(); var chg = new FileAddition() { FileName = "blorg", Comments = new List<Comment>() }; c.MockData.Changes.Add(chg); c.MockData.SaveChanges(); // Act var result = c.Post(chg.Id, 0, "blarg"); // Assert Assert.Equal(HttpStatusCode.Created, result.StatusCode); Assert.Equal(chg.Comments.Single().Content, "blarg"); Assert.Equal(Assert.IsType<CommentModel>(result.GetObjectContent()).Body, "blarg"); }
public void ReturnsNullIfNoSuchChange() { // Arrange var changes = CreateService(); var added = new FileAddition() { FileName = "florb" }; changes.MockData.Changes.Add(added); changes.MockData.SaveChanges(); // Act FileChange fetched = changes.GetChange(added.Id + 42); // Assert Assert.Null(fetched); }
public void ReturnsOkWithFileDiffModelWithCommentsIfCommentsFoundForChange() { // Arrange var c = CreateController(); var add = new FileAddition() { FileName = "blorg", Diff = "blarg", Comments = new List<Comment>() { new Comment() { DiffLineIndex = 0, Content = "blorg", UserId = ApiTestData.LoggedInUser.Id, User = ApiTestData.LoggedInUser }, new Comment() { DiffLineIndex = 2, Content = "blarg", UserId = ApiTestData.NotLoggedInUser.Id, User = ApiTestData.NotLoggedInUser } } }; var expected = new FileDiffModel() { Lines = new List<LineDiffModel>() { new LineDiffModel() { Index = 0, Comments = new List<CommentModel>() }, new LineDiffModel() { Index = 1, Comments = new List<CommentModel>() }, new LineDiffModel() { Index = 2, Comments = new List<CommentModel>() }, } }; c.MockData.Changes.Add(add); c.MockData.SaveChanges(); c.MockDiff.Setup(d => d.ParseFileDiff("blorg", "blarg")) .Returns(expected); // Act var result = c.Get(add.Id); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); var actual = Assert.IsType<FileDiffModel>(result.GetObjectContent()); Assert.Same(expected, actual); Assert.Equal(actual.Lines[0].Comments.Single().Body, "blorg"); Assert.True(actual.Lines[0].Comments.Single().IsAuthor); Assert.False(actual.Lines[1].Comments.Any()); Assert.Equal(actual.Lines[2].Comments.Single().Body, "blarg"); Assert.False(actual.Lines[2].Comments.Single().IsAuthor); }
public void ReturnsOkWithFileDiffModelWithNoCommentsIfNoCommentsFoundForChange() { // Arrange var c = CreateController(); var add = new FileAddition() { FileName = "blorg", Diff = "blarg" }; var expected = new FileDiffModel(); c.MockData.Changes.Add(add); c.MockData.SaveChanges(); c.MockDiff.Setup(d => d.ParseFileDiff("blorg", "blarg")) .Returns(expected); // Act var result = c.Get(add.Id); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); Assert.Same(expected, result.GetObjectContent()); }