public void WillSaveCssToRepository() { var testable = new TestableSqlServerStore(); var content = new byte[] { 1 }; var url = "url"; var originalUrls = "originalUrls"; RequestReduceFile file = null; testable.Mock <IFileRepository>().Setup(x => x.Save(It.IsAny <RequestReduceFile>())).Callback <RequestReduceFile>(x => file = x); testable.Mock <IUriBuilder>().Setup(x => x.ParseFileName(url)).Returns("file.css"); var key = Guid.NewGuid(); var id = Hasher.Hash("file.css"); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey(url)).Returns(key); testable.ClassUnderTest.Save(content, url, originalUrls); Assert.Equal(content, file.Content); Assert.Equal("file.css", file.FileName); Assert.Equal(key, file.Key); Assert.True(DateTime.Now.Subtract(file.LastUpdated).TotalMilliseconds < 1000); Assert.Equal(originalUrls, file.OriginalName); Assert.Equal(id, file.RequestReduceFileId); }
public void WillRemoveFromReductionDictionaryIfExpired() { var testable = new TestableSqlServerStore(); var response = new Mock <HttpResponseBase>(); testable.Mock <LocalDiskStore>().Setup(x => x.SendContent("url", response.Object)).Returns(false); var id = Hasher.Hash("file.css"); testable.Mock <IUriBuilder>().Setup(x => x.ParseFileName("url")).Returns("file.css"); var key = Guid.NewGuid(); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey("url")).Returns(key); var bytes = new byte[] { 1 }; testable.Mock <IFileRepository>().Setup(x => x.SingleOrDefault <RequestReduceFile>(id)).Returns(new RequestReduceFile() { Content = bytes, IsExpired = true }); var result = testable.ClassUnderTest.SendContent("url", response.Object); Assert.True(result); response.Verify(x => x.BinaryWrite(bytes), Times.Once()); testable.Mock <IReductionRepository>().Verify(x => x.RemoveReduction(key), Times.Once()); }
public void WillExpireDbFileEntity() { var testable = new TestableSqlServerStore(); var key = Guid.NewGuid(); var file1 = new RequestReduceFile() { IsExpired = false, RequestReduceFileId = Guid.NewGuid() }; var file2 = new RequestReduceFile() { IsExpired = false, RequestReduceFileId = Guid.NewGuid() }; testable.Mock <IFileRepository>().Setup(x => x.GetFilesFromKey(key)).Returns(new RequestReduceFile[] { file1, file2 }); bool expire1 = false; bool expire2 = false; testable.Mock <IFileRepository>().Setup(x => x.Update(file1)).Callback <RequestReduceFile>( y => expire1 = y.IsExpired); testable.Mock <IFileRepository>().Setup(x => x.Update(file2)).Callback <RequestReduceFile>( y => expire2 = y.IsExpired); testable.ClassUnderTest.Flush(key); Assert.True(expire1); Assert.True(expire2); }
public void WillSwallowAndLogErrorsSavingFileToDisk() { var testable = new TestableSqlServerStore(); var response = new Mock <HttpResponseBase>(); testable.Mock <LocalDiskStore>().Setup(x => x.SendContent("url", response.Object)).Returns(false); var id = Hasher.Hash("file.css"); testable.Mock <IUriBuilder>().Setup(x => x.ParseFileName("url")).Returns("file.css"); var key = Guid.NewGuid(); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey("url")).Returns(key); var bytes = new byte[] { 1 }; testable.Mock <IFileRepository>().Setup(x => x.SingleOrDefault <RequestReduceFile>(id)).Returns(new RequestReduceFile() { Content = bytes }); Exception error = null; var innerError = new ApplicationException(); Registry.CaptureErrorAction = (x => error = x); testable.Mock <LocalDiskStore>().Setup(x => x.Save(bytes, "url", null)).Throws(innerError); testable.ClassUnderTest.SendContent("url", response.Object); Assert.Equal(innerError, error.InnerException); Assert.Contains("url", error.Message); }
public void WillGoToDBAndWriteBytesIfFileIsNotCached() { var testable = new TestableSqlServerStore(); var response = new Mock <HttpResponseBase>(); testable.Mock <LocalDiskStore>().Setup(x => x.SendContent("url", response.Object)).Returns(false); var id = Hasher.Hash("file.css"); testable.Mock <IUriBuilder>().Setup(x => x.ParseFileName("url")).Returns("file.css"); var key = Guid.NewGuid(); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey("url")).Returns(key); var bytes = new byte[] { 1 }; testable.Mock <IFileRepository>().Setup(x => x[id]).Returns(new RequestReduceFile() { Content = bytes }); var result = testable.ClassUnderTest.SendContent("url", response.Object); Assert.True(result); response.Verify(x => x.BinaryWrite(bytes), Times.Once()); testable.Mock <LocalDiskStore>().Verify(x => x.Save(bytes, "url", null), Times.Once()); }
public void WillSendThroughFileCachedIfFileIsCached() { var testable = new TestableSqlServerStore(); var response = new Mock <HttpResponseBase>(); testable.Mock <LocalDiskStore>().Setup(x => x.SendContent("url", response.Object)).Returns(true); var result = testable.ClassUnderTest.SendContent("url", response.Object); Assert.True(result); testable.Mock <IFileRepository>().Verify(x => x.SingleOrDefault <RequestReduceFile>(It.IsAny <Guid>()), Times.Never()); }
public void WillAddReductionIfFileIsNotPng() { var testable = new TestableSqlServerStore(); var content = new byte[] { 1 }; var expectedUrl = "url.css"; var originalUrls = "originalUrls"; var expectedKey = Guid.NewGuid(); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey(expectedUrl)).Returns(expectedKey); testable.ClassUnderTest.Save(content, expectedUrl, originalUrls); testable.Mock <IReductionRepository>().Verify(x => x.AddReduction(expectedKey, expectedUrl), Times.Once()); }
public void WillReturnFalseAndDeleteIfFileIsNotInRepo() { var testable = new TestableSqlServerStore(); var response = new Mock <HttpResponseBase>(); testable.Mock <LocalDiskStore>().Setup(x => x.SendContent("url", response.Object)).Returns(false); var key = Guid.NewGuid(); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey("url")).Returns(key); var result = testable.ClassUnderTest.SendContent("url", response.Object); Assert.False(result); testable.Mock <IReductionRepository>().Verify(x => x.RemoveReduction(key), Times.Once()); }
public void WillGetUrlFromStoreIfItExists() { var testable = new TestableSqlServerStore(); var guid1 = Guid.NewGuid(); var sig1 = Guid.NewGuid().RemoveDashes(); testable.Mock <IUriBuilder>().Setup(x => x.BuildResourceUrl(guid1, sig1, typeof(CssResource))).Returns("url1"); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey("file1")).Returns(guid1); testable.Mock <IUriBuilder>().Setup(x => x.ParseSignature("file1")).Returns(sig1); testable.Mock <IFileRepository>().Setup(x => x.GetActiveUrlByKey(guid1, typeof(CssResource))).Returns("file1"); var result = testable.ClassUnderTest.GetUrlByKey(guid1, typeof(CssResource)); Assert.Equal("url1", result); RRContainer.Current.Dispose(); RRContainer.Current = null; }
public void WillReturnKeysAndUrlsFromDB() { var testable = new TestableSqlServerStore(); var guid1 = Guid.NewGuid(); var guid2 = Guid.NewGuid(); var sig1 = Guid.NewGuid().RemoveDashes(); var sig2 = Guid.NewGuid().RemoveDashes(); var fileName1 = "1" + new CssResource().FileName; var fileName2 = "2" + new CssResource().FileName; testable.Mock <IUriBuilder>().Setup(x => x.BuildResourceUrl(guid1, sig1, typeof(CssResource))).Returns("url1"); testable.Mock <IUriBuilder>().Setup(x => x.BuildResourceUrl(guid2, sig2, typeof(CssResource))).Returns("url2"); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey(fileName1)).Returns(guid1); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey(fileName2)).Returns(guid2); testable.Mock <IUriBuilder>().Setup(x => x.ParseSignature(fileName1)).Returns(sig1); testable.Mock <IUriBuilder>().Setup(x => x.ParseSignature(fileName2)).Returns(sig2); testable.Mock <IFileRepository>().Setup(x => x.GetActiveFiles()).Returns(new List <string>() { fileName1, fileName2 }); var result = testable.ClassUnderTest.GetSavedUrls(); Assert.Equal(2, result.Count); Assert.True(result[guid1] == "url1"); Assert.True(result[guid2] == "url2"); RRContainer.Current = null; }
public void WillRemoveFromRepository() { var testable = new TestableSqlServerStore(); var key = Guid.NewGuid(); testable.ClassUnderTest.Flush(key); testable.Mock <IReductionRepository>().Verify(x => x.RemoveReduction(key), Times.Once()); }
public void WillGetNullFromStoreIfItDoesNotExists() { var testable = new TestableSqlServerStore(); testable.Mock <IUriBuilder>().Setup(x => x.BuildResourceUrl <CssResource>(It.IsAny <Guid>(), It.IsAny <string>())).Returns("url1"); var result = testable.ClassUnderTest.GetUrlByKey(Guid.NewGuid(), typeof(CssResource)); Assert.Null(result); }
public void WillSaveToFile() { var testable = new TestableSqlServerStore(); var content = new byte[] { 1 }; var expectedUrl = "url"; var originalUrls = "originalUrls"; testable.ClassUnderTest.Save(content, expectedUrl, originalUrls); testable.Mock <LocalDiskStore>().Verify(x => x.Save(content, expectedUrl, originalUrls), Times.Once()); }
public void WillFlushAllKeysWhenPassedGuidIsEmpty() { var testable = new TestableSqlServerStore(); var guid1 = Guid.NewGuid(); var guid2 = Guid.NewGuid(); var sig1 = Guid.NewGuid().RemoveDashes(); var sig2 = Guid.NewGuid().RemoveDashes(); var fileName1 = "1" + new CssResource().FileName; var fileName2 = "2" + new CssResource().FileName; testable.Mock <IUriBuilder>().Setup(x => x.BuildResourceUrl(guid1, sig1, typeof(CssResource))).Returns("url1"); testable.Mock <IUriBuilder>().Setup(x => x.BuildResourceUrl(guid2, sig2, typeof(CssResource))).Returns("url2"); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey(fileName1)).Returns(guid1); testable.Mock <IUriBuilder>().Setup(x => x.ParseKey(fileName2)).Returns(guid2); testable.Mock <IUriBuilder>().Setup(x => x.ParseSignature(fileName1)).Returns(sig1); testable.Mock <IUriBuilder>().Setup(x => x.ParseSignature(fileName2)).Returns(sig2); testable.Mock <IFileRepository>().Setup(x => x.GetActiveFiles()).Returns(new List <string>() { fileName1, fileName2 }); var file1 = new RequestReduceFile() { IsExpired = false, RequestReduceFileId = guid1 }; var file2 = new RequestReduceFile() { IsExpired = false, RequestReduceFileId = guid2 }; testable.Mock <IFileRepository>().Setup(x => x.GetFilesFromKey(guid1)).Returns(new RequestReduceFile[] { file1 }); testable.Mock <IFileRepository>().Setup(x => x.GetFilesFromKey(guid2)).Returns(new RequestReduceFile[] { file2 }); bool expire1 = false; bool expire2 = false; testable.Mock <IFileRepository>().Setup(x => x.Update(file1)).Callback <RequestReduceFile>( y => expire1 = y.IsExpired); testable.Mock <IFileRepository>().Setup(x => x.Update(file2)).Callback <RequestReduceFile>( y => expire2 = y.IsExpired); testable.ClassUnderTest.Flush(Guid.Empty); Assert.True(expire1); Assert.True(expire2); RRContainer.Current = null; }