public void OpenWriteThrowsOnNullOrEmptyHash()
        {
            GitLooseFilesDictionary dict = new GitLooseFilesDictionary(new InMemoryFileSystem(), new NullCompressionStrategy());

            Assert.Throws <ArgumentException>(() => dict.OpenWrite(null, create: false))
            .WithParamName("hash");
            Assert.Throws <ArgumentException>(() => dict.OpenWrite(String.Empty, create: false))
            .WithParamName("hash");
        }
        public void OpenNonExistantKeyForWriteWithoutCreateThrowsKeyNotFound()
        {
            // Arrange
            InMemoryFileSystem      fs = new InMemoryFileSystem();
            GitLooseFilesDictionary db = new GitLooseFilesDictionary(fs, new NullCompressionStrategy());

            // Act/Assert
            Assert.Throws <KeyNotFoundException>(() => new StreamWriter(db.OpenWrite("abcdefghijk", create: false)));
        }
        public void HashesLessThan3CharactersThrowWhenNeededToCreateObjects()
        {
            // Arrange
            InMemoryFileSystem      fs = new InMemoryFileSystem();
            GitLooseFilesDictionary db = new GitLooseFilesDictionary(fs, new NullCompressionStrategy());

            fs.WriteTestFile(@"xy", "Foo");

            // Act
            Assert.Equal(
                "Hash 'xy' does not exist and is not long enough to create a new entry in the database\r\nParameter name: partialHash",
                Assert.Throws <ArgumentException>(() => db.OpenWrite("xy", create: true)).Message);
        }
        public void OpenWriteUsesCompressionStrategyToWrapStreamForCompression()
        {
            // Arrange
            Mock <ICompressionStrategy> mockStrategy = new Mock <ICompressionStrategy>(MockBehavior.Strict);
            Stream expected = new MemoryStream();

            mockStrategy.Setup(s => s.WrapStreamForCompression(It.IsAny <Stream>()))
            .Returns(expected);
            InMemoryFileSystem      fs = new InMemoryFileSystem();
            GitLooseFilesDictionary db = new GitLooseFilesDictionary(fs, mockStrategy.Object);

            // Act
            using (Stream actual = db.OpenWrite("test", create: true))
            {
                Assert.Same(expected, actual);
            }
        }
        public void OpenNonExistantKeyForWriteWithCreateCreatesNewData()
        {
            // Arrange
            InMemoryFileSystem      fs = new InMemoryFileSystem();
            GitLooseFilesDictionary db = new GitLooseFilesDictionary(fs, new NullCompressionStrategy());

            // Assume
            Assert.False(fs.Exists(@"ab\cdefghijk"));

            // Act
            using (Stream strm = db.OpenWrite("abcdefghijk", create: true))
                using (StreamWriter writer = new StreamWriter(strm))
                {
                    writer.Write("FooBarBaz");
                }

            // Assert
            Assert.Equal("FooBarBaz", fs.ReadTestFile(@"ab\cdefghijk"));
        }
        public void OpenExistingKeyForWriteAndEditData()
        {
            // Arrange
            InMemoryFileSystem      fs = new InMemoryFileSystem();
            GitLooseFilesDictionary db = new GitLooseFilesDictionary(fs, new NullCompressionStrategy());

            fs.WriteTestFile(@"ab\cdefghijk", "Foo");

            // Act
            using (Stream strm = db.OpenWrite("abcdefghijk", create: false))
                using (StreamWriter writer = new StreamWriter(strm))
                {
                    strm.Seek(2, SeekOrigin.Begin);
                    writer.Write("Bar");
                }

            // Assert
            Assert.Equal("FoBar", fs.ReadTestFile(@"ab\cdefghijk"));
        }