Skip to content

Commit

Permalink
Add unit tests for SparseTable and pull out some common code to base …
Browse files Browse the repository at this point in the history
…class (#1379)
  • Loading branch information
Kevin Willford committed Aug 5, 2019
1 parent 2f23e65 commit 4264554
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 94 deletions.
2 changes: 1 addition & 1 deletion GVFS/GVFS.Common/Database/SparseTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public HashSet<string> GetAll()
}
catch (Exception ex)
{
throw new GVFSDatabaseException($"{nameof(PlaceholderTable)}.{nameof(this.GetAll)} Exception: {ex.ToString()}", ex);
throw new GVFSDatabaseException($"{nameof(SparseTable)}.{nameof(this.GetAll)} Exception: {ex.ToString()}", ex);
}
}

Expand Down
118 changes: 25 additions & 93 deletions GVFS/GVFS.UnitTests/Common/Database/PlaceholderTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,21 @@
namespace GVFS.UnitTests.Common.Database
{
[TestFixture]
public class PlaceholderTableTests
public class PlaceholderTableTests : TableTests<PlaceholderTable>
{
private const string DefaultExceptionMessage = "Somethind bad.";
private const string DefaultPath = "test";
private const byte PathTypeFile = 0;
private const byte PathTypePartialFolder = 1;
private const byte PathTypeExpandedFolder = 2;
private const byte PathTypePossibleTombstoneFolder = 3;
private const string DefaultSha = "1234567890123456789012345678901234567890";

[TestCase]
public void ConstructorTest()
{
Mock<IGVFSConnectionPool> mockConnectionPool = new Mock<IGVFSConnectionPool>(MockBehavior.Strict);
PlaceholderTable placeholders = new PlaceholderTable(mockConnectionPool.Object);
mockConnectionPool.VerifyAll();
}

[TestCase]
public void CreateTableTest()
{
Mock<IDbCommand> mockCommand = new Mock<IDbCommand>(MockBehavior.Strict);
mockCommand.SetupSet(x => x.CommandText = "CREATE TABLE IF NOT EXISTS [Placeholder] (path TEXT PRIMARY KEY COLLATE NOCASE, pathType TINYINT NOT NULL, sha char(40) ) WITHOUT ROWID;");
mockCommand.Setup(x => x.ExecuteNonQuery()).Returns(1);
mockCommand.Setup(x => x.Dispose());

Mock<IDbConnection> mockConnection = new Mock<IDbConnection>(MockBehavior.Strict);
mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

PlaceholderTable.CreateTable(mockConnection.Object);
mockCommand.VerifyAll();
mockConnection.VerifyAll();
}

[TestCase]
[Category(CategoryConstants.ExceptionExpected)]
public void CreateTableThrowsExceptionNotWrappedInGVFSDatabaseException()
{
Mock<IDbCommand> mockCommand = new Mock<IDbCommand>(MockBehavior.Strict);
mockCommand.SetupSet(x => x.CommandText = "CREATE TABLE IF NOT EXISTS [Placeholder] (path TEXT PRIMARY KEY COLLATE NOCASE, pathType TINYINT NOT NULL, sha char(40) ) WITHOUT ROWID;");
mockCommand.Setup(x => x.ExecuteNonQuery()).Throws(new Exception(DefaultExceptionMessage));
mockCommand.Setup(x => x.Dispose());

Mock<IDbConnection> mockConnection = new Mock<IDbConnection>(MockBehavior.Strict);
mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);

Exception ex = Assert.Throws<Exception>(() => PlaceholderTable.CreateTable(mockConnection.Object));
ex.Message.ShouldEqual(DefaultExceptionMessage);
mockCommand.VerifyAll();
mockConnection.VerifyAll();
}
protected override string CreateTableCommandString => "CREATE TABLE IF NOT EXISTS [Placeholder] (path TEXT PRIMARY KEY COLLATE NOCASE, pathType TINYINT NOT NULL, sha char(40) ) WITHOUT ROWID;";

[TestCase]
public void GetCountTest()
{
this.TestPlaceholders(
this.TestTable(
(placeholders, mockCommand) =>
{
mockCommand.SetupSet(x => x.CommandText = "SELECT count(path) FROM Placeholder;");
Expand All @@ -79,7 +38,7 @@ public void GetCountTest()
[Category(CategoryConstants.ExceptionExpected)]
public void GetCountThrowsGVFSDatabaseException()
{
this.TestPlaceholders(
this.TestTable(
(placeholders, mockCommand) =>
{
mockCommand.SetupSet(x => x.CommandText = "SELECT count(path) FROM Placeholder;");
Expand All @@ -93,7 +52,7 @@ public void GetCountThrowsGVFSDatabaseException()
[TestCase]
public void GetAllFilePathsWithNoResults()
{
this.TestPlaceholdersWithReader(
this.TestTableWithReader(
(placeholders, mockCommand, mockReader) =>
{
mockReader.Setup(x => x.Read()).Returns(false);
Expand All @@ -109,7 +68,7 @@ public void GetAllFilePathsWithNoResults()
[Category(CategoryConstants.ExceptionExpected)]
public void GetAllFilePathsThrowsGVFSDatabaseException()
{
this.TestPlaceholdersWithReader(
this.TestTableWithReader(
(placeholders, mockCommand, mockReader) =>
{
mockReader.Setup(x => x.Read()).Throws(new Exception(DefaultExceptionMessage));
Expand All @@ -124,7 +83,7 @@ public void GetAllFilePathsThrowsGVFSDatabaseException()
[TestCase]
public void GetAllFilePathsTest()
{
this.TestPlaceholdersWithReader(
this.TestTableWithReader(
(placeholders, mockCommand, mockReader) =>
{
int readCalls = 0;
Expand All @@ -149,7 +108,7 @@ public void GetAllFilePathsTest()
public void GetAllEntriesThrowsGVFSDatabaseException()
{
List<PlaceholderTable.PlaceholderData> expectedPlacholders = new List<PlaceholderTable.PlaceholderData>();
this.TestPlaceholdersWithReader(
this.TestTableWithReader(
(placeholders, mockCommand, mockReader) =>
{
mockCommand.SetupSet(x => x.CommandText = "SELECT path, pathType, sha FROM Placeholder;");
Expand All @@ -165,7 +124,7 @@ public void GetAllEntriesThrowsGVFSDatabaseException()
public void GetAllEntriesReturnsNothing()
{
List<PlaceholderTable.PlaceholderData> expectedPlacholders = new List<PlaceholderTable.PlaceholderData>();
this.TestPlaceholdersWithReader(
this.TestTableWithReader(
(placeholders, mockCommand, mockReader) =>
{
this.SetupMockReader(mockReader, expectedPlacholders);
Expand All @@ -185,7 +144,7 @@ public void GetAllEntriesReturnsOneFile()
{
List<PlaceholderTable.PlaceholderData> expectedPlacholders = new List<PlaceholderTable.PlaceholderData>();
expectedPlacholders.Add(new PlaceholderTable.PlaceholderData() { Path = DefaultPath, PathType = PlaceholderTable.PlaceholderData.PlaceholderType.File, Sha = DefaultSha });
this.TestPlaceholdersWithReader(
this.TestTableWithReader(
(placeholders, mockCommand, mockReader) =>
{
this.SetupMockReader(mockReader, expectedPlacholders);
Expand All @@ -205,7 +164,7 @@ public void GetAllEntriesReturnsOneFolder()
{
List<PlaceholderTable.PlaceholderData> expectedPlacholders = new List<PlaceholderTable.PlaceholderData>();
expectedPlacholders.Add(new PlaceholderTable.PlaceholderData() { Path = DefaultPath, PathType = PlaceholderTable.PlaceholderData.PlaceholderType.PartialFolder, Sha = null });
this.TestPlaceholdersWithReader(
this.TestTableWithReader(
(placeholders, mockCommand, mockReader) =>
{
this.SetupMockReader(mockReader, expectedPlacholders);
Expand All @@ -229,7 +188,7 @@ public void GetAllEntriesReturnsMultiple()
expectedFolderPlacholders.Add(new PlaceholderTable.PlaceholderData() { Path = "test1", PathType = PlaceholderTable.PlaceholderData.PlaceholderType.PartialFolder, Sha = null });
expectedFolderPlacholders.Add(new PlaceholderTable.PlaceholderData() { Path = "test2", PathType = PlaceholderTable.PlaceholderData.PlaceholderType.ExpandedFolder, Sha = null });
expectedFolderPlacholders.Add(new PlaceholderTable.PlaceholderData() { Path = "test3", PathType = PlaceholderTable.PlaceholderData.PlaceholderType.PossibleTombstoneFolder, Sha = null });
this.TestPlaceholdersWithReader(
this.TestTableWithReader(
(placeholders, mockCommand, mockReader) =>
{
this.SetupMockReader(mockReader, expectedFilePlacholders.Union(expectedFolderPlacholders).ToList());
Expand Down Expand Up @@ -493,7 +452,7 @@ public void AddPossibleTombstoneFolderThrowsGVFSDatabaseException()
[TestCase]
public void RemoveTest()
{
this.TestPlaceholders(
this.TestTable(
(placeholders, mockCommand) =>
{
Mock<IDbDataParameter> mockParameter = new Mock<IDbDataParameter>(MockBehavior.Strict);
Expand All @@ -520,7 +479,7 @@ public void RemoveTest()
[Category(CategoryConstants.ExceptionExpected)]
public void RemoveThrowsGVFSDatabaseException()
{
this.TestPlaceholders(
this.TestTable(
(placeholders, mockCommand) =>
{
mockCommand.SetupSet(x => x.CommandText = "DELETE FROM Placeholder WHERE path = @path;").Throws(new Exception(DefaultExceptionMessage));
Expand All @@ -531,9 +490,19 @@ public void RemoveThrowsGVFSDatabaseException()
});
}

protected override PlaceholderTable TableFactory(IGVFSConnectionPool pool)
{
return new PlaceholderTable(pool);
}

protected override void CreateTable(IDbConnection connection)
{
PlaceholderTable.CreateTable(connection);
}

private void TestPlaceholdersInsert(Action<PlaceholderTable> testCode, string path, int pathType, string sha, bool throwException = false)
{
this.TestPlaceholders(
this.TestTable(
(placeholders, mockCommand) =>
{
Mock<IDbDataParameter> mockPathParameter = new Mock<IDbDataParameter>(MockBehavior.Strict);
Expand Down Expand Up @@ -587,43 +556,6 @@ private void TestPlaceholdersInsert(Action<PlaceholderTable> testCode, string pa
});
}

private void TestPlaceholdersWithReader(Action<PlaceholderTable, Mock<IDbCommand>, Mock<IDataReader>> testCode)
{
this.TestPlaceholders(
(placeholders, mockCommand) =>
{
Mock<IDataReader> mockReader = new Mock<IDataReader>(MockBehavior.Strict);
mockReader.Setup(x => x.Dispose());
mockCommand.Setup(x => x.ExecuteReader()).Returns(mockReader.Object);
testCode(placeholders, mockCommand, mockReader);
mockReader.Verify(x => x.Dispose(), Times.Once);
mockReader.VerifyAll();
});
}

private void TestPlaceholders(Action<PlaceholderTable, Mock<IDbCommand>> testCode)
{
Mock<IDbCommand> mockCommand = new Mock<IDbCommand>(MockBehavior.Strict);
mockCommand.Setup(x => x.Dispose());

Mock<IDbConnection> mockConnection = new Mock<IDbConnection>(MockBehavior.Strict);
mockConnection.Setup(x => x.CreateCommand()).Returns(mockCommand.Object);
mockConnection.Setup(x => x.Dispose());

Mock<IGVFSConnectionPool> mockConnectionPool = new Mock<IGVFSConnectionPool>(MockBehavior.Strict);
mockConnectionPool.Setup(x => x.GetConnection()).Returns(mockConnection.Object);

PlaceholderTable placeholders = new PlaceholderTable(mockConnectionPool.Object);
testCode(placeholders, mockCommand);

mockCommand.Verify(x => x.Dispose(), Times.Once);
mockCommand.VerifyAll();
mockConnection.Verify(x => x.Dispose(), Times.Once);
mockConnection.VerifyAll();
mockConnectionPool.VerifyAll();
}

private void SetupMockReader(Mock<IDataReader> mockReader, List<PlaceholderTable.PlaceholderData> data)
{
int readCalls = -1;
Expand Down
Loading

0 comments on commit 4264554

Please sign in to comment.