Skip to content

Commit

Permalink
Improve test coverage: GetIndexInfo, set boolean column to true and f…
Browse files Browse the repository at this point in the history
…alse, create EsentException with inner exception, use mock API to test SystemParameters, error-handling for JetCreateIndex2

git-svn-id: https://managedese.svn.codeplex.com/svn@24524 25d8fd2b-6a69-44ad-a716-4a7b2595fe98
  • Loading branch information
SND\laurionb_cp authored and SND\laurionb_cp committed May 29, 2009
1 parent db27441 commit 29a79c6
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 3 deletions.
1 change: 0 additions & 1 deletion .tfs-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
<add recursive='true'>TestResults</add>
<add recursive='true'>app.manifest</add>
<add recursive='true'>*.ncb</add>
<add recursive='true'>*.vsmdi</add>
<add recursive='true'>*.lst</add>
</ignore>
4 changes: 4 additions & 0 deletions EsentInterop/Esent.vsmdi
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<TestLists xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2006">
<TestList name="Lists of Tests" id="8c43106b-9dc1-4907-a29f-aa66a61bf5b6" />
</TestLists>
1 change: 1 addition & 0 deletions EsentInterop/EsentInteropTests/EsentInteropTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<Compile Include="IndexRangeFromTableidTests.cs" />
<Compile Include="SeekTests.cs" />
<Compile Include="SimplePerfTest.cs" />
<Compile Include="SystemParameterTests.cs" />
<Compile Include="TableTests.cs" />
<Compile Include="UnicodeIndexTests.cs" />
<Compile Include="UnicodePathTests.cs" />
Expand Down
14 changes: 14 additions & 0 deletions EsentInterop/EsentInteropTests/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Isam.Esent;
using Microsoft.Isam.Esent.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -17,6 +19,18 @@ namespace InteropApiTests
[TestClass]
public class ExceptionTests
{
/// <summary>
/// Verify that creating an EsentException with an innner exception sets
/// the inner exception property.
/// </summary>
[TestMethod]
[Priority(0)]
public void VerifyEsentExceptionConstructorSetsInnerException()
{
var ex = new EsentException("foo", new OutOfMemoryException("InnerException"));
Assert.AreEqual("InnerException", ex.InnerException.Message);
}

/// <summary>
/// Verify that the error passed into the constructor is set in the error
/// property.
Expand Down
53 changes: 51 additions & 2 deletions EsentInterop/EsentInteropTests/HelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -874,10 +874,30 @@ public void SetNullString()
/// </summary>
[TestMethod]
[Priority(1)]
public void SetBoolean()
public void SetBooleanTrue()
{
JET_COLUMNID columnid = this.columnidDict["boolean"];
bool expected = Any.Boolean;
bool expected = true;

Api.JetBeginTransaction(this.sesid);
Api.JetPrepareUpdate(this.sesid, this.tableid, JET_prep.Insert);
Api.SetColumn(this.sesid, this.tableid, columnid, expected);
this.UpdateAndGotoBookmark();
Api.JetCommitTransaction(this.sesid, CommitTransactionGrbit.LazyFlush);

bool actual = BitConverter.ToBoolean(Api.RetrieveColumn(this.sesid, this.tableid, columnid), 0);
Assert.AreEqual(expected, actual);
}

/// <summary>
/// Test setting a column from a boolean.
/// </summary>
[TestMethod]
[Priority(1)]
public void SetBooleanFalse()
{
JET_COLUMNID columnid = this.columnidDict["boolean"];
bool expected = false;

Api.JetBeginTransaction(this.sesid);
Api.JetPrepareUpdate(this.sesid, this.tableid, JET_prep.Insert);
Expand Down Expand Up @@ -1492,6 +1512,35 @@ public void GetIndexInformationOneIndex()
Api.JetRollback(this.sesid, RollbackTransactionGrbit.None);
}

/// <summary>
/// Get index information for one index
/// </summary>
[TestMethod]
[Priority(1)]
public void GetIndexInformationByTableNameOneIndex()
{
string indexname = "myindex";
string indexdef = "+ascii\0\0";
CreateIndexGrbit grbit = CreateIndexGrbit.IndexUnique;

Api.JetBeginTransaction(this.sesid);
Api.JetCreateIndex(this.sesid, this.tableid, indexname, grbit, indexdef, indexdef.Length, 100);
IEnumerable<IndexInfo> indexes = Api.GetTableIndexes(this.sesid, this.dbid, this.table);

// There should be only one index
IndexInfo info = indexes.Single();
Assert.AreEqual(indexname, info.Name);
Assert.AreEqual(grbit, info.Grbit);

Assert.AreEqual(1, info.IndexSegments.Length);
Assert.IsTrue(0 == string.Compare("ascii", info.IndexSegments[0].ColumnName, true));
Assert.IsTrue(info.IndexSegments[0].IsAscending);
Assert.AreEqual(JET_coltyp.LongText, info.IndexSegments[0].Coltyp);
Assert.IsTrue(info.IndexSegments[0].IsASCII);

Api.JetRollback(this.sesid, RollbackTransactionGrbit.None);
}

/// <summary>
/// Get index information for one index
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions EsentInterop/EsentInteropTests/ParameterCheckingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,44 @@ public void JetCreateIndexThrowsExceptionWhenKeyDescriptionLengthIsTooLong()
Api.JetCreateIndex(this.sesid, this.tableid, "BadIndex,", CreateIndexGrbit.None, "+foo\0", 77, 100);
}

/// <summary>
/// Check that an exception is thrown when JetCreateIndex2 gets
/// null indexcreates.
/// </summary>
[TestMethod]
[Priority(1)]
[ExpectedException(typeof(ArgumentNullException))]
public void JetCreateIndex2ThrowsExceptionWhenIndexcreatesAreNull()
{
Api.JetCreateIndex2(this.sesid, this.tableid, null, 0);
}

/// <summary>
/// Check that an exception is thrown when JetCreateIndex2 gets
/// a negative indexcreate count.
/// </summary>
[TestMethod]
[Priority(1)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void JetCreateIndex2ThrowsExceptionWhenNumIndexcreatesIsNegative()
{
var indexcreates = new[] { new JET_INDEXCREATE() };
Api.JetCreateIndex2(this.sesid, this.tableid, indexcreates, -1);
}

/// <summary>
/// Check that an exception is thrown when JetCreateIndex2 gets
/// an indexcreate count that is too long.
/// </summary>
[TestMethod]
[Priority(1)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void JetCreateIndex2ThrowsExceptionWhenNumIndexcreatesIsTooLong()
{
var indexcreates = new[] { new JET_INDEXCREATE() };
Api.JetCreateIndex2(this.sesid, this.tableid, indexcreates, indexcreates.Length+1);
}

/// <summary>
/// Check that an exception is thrown when JetDeleteColumn gets a
/// null column name.
Expand Down
174 changes: 174 additions & 0 deletions EsentInterop/EsentInteropTests/SystemParameterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//-----------------------------------------------------------------------
// <copyright file="SystemParameterTests.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation.
// </copyright>
//-----------------------------------------------------------------------

using Microsoft.Isam.Esent.Interop;
using Microsoft.Isam.Esent.Interop.Implementation;
using Microsoft.Isam.Esent.Interop.Vista;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Rhino.Mocks;

namespace InteropApiTests
{
/// <summary>
/// Test the SystemParameters class. To avoid changing global parameters
/// this is tested with a mock IJetApi.
/// </summary>
[TestClass]
public class SystemParameterTests
{
/// <summary>
/// Mock object repository.
/// </summary>
private MockRepository repository;

/// <summary>
/// The real IJetApi, saved in Setup and restored in Teardown.
/// </summary>
private IJetApi savedApi;

/// <summary>
/// Mock API object.
/// </summary>
private IJetApi mockApi;

/// <summary>
/// Initialization method. Setup the mock API.
/// </summary>
[TestInitialize]
public void Setup()
{
this.savedApi = Api.Impl;
this.repository = new MockRepository();
this.mockApi = this.repository.DynamicMock<IJetApi>();

var mockCapabilities = new JetCapabilities
{
SupportsLargeKeys = true,
SupportsUnicodePaths = true,
SupportsVistaFeatures = true,
SupportsWindows7Features = true,
};
SetupResult.For(this.mockApi.Capabilities).Return(mockCapabilities);

Api.Impl = this.mockApi;
}

/// <summary>
/// Cleanup after a test. This restores the saved API.
/// </summary>
[TestCleanup]
public void Teardown()
{
Api.Impl = this.savedApi;
}

/// <summary>
/// Verify that setting the property sets the system parameter
/// </summary>
[TestMethod]
[Priority(0)]
public void VerifySettingCacheSizeMax()
{
Expect.Call(
this.mockApi.JetSetSystemParameter(
JET_INSTANCE.Nil, JET_SESID.Nil, JET_param.CacheSizeMax, 64, null)).Return(0);
this.repository.ReplayAll();
SystemParameters.CacheSizeMax = 64;
this.repository.VerifyAll();
}

/// <summary>
/// Verify that setting the property sets the system parameter
/// </summary>
[TestMethod]
[Priority(0)]
public void VerifySettingCacheSize()
{
Expect.Call(
this.mockApi.JetSetSystemParameter(
JET_INSTANCE.Nil, JET_SESID.Nil, JET_param.CacheSize, 64, null)).Return(0);
this.repository.ReplayAll();
SystemParameters.CacheSize = 64;
this.repository.VerifyAll();
}

/// <summary>
/// Verify that setting the property sets the system parameter
/// </summary>
[TestMethod]
[Priority(0)]
public void VerifySettingCacheSizeMin()
{
Expect.Call(
this.mockApi.JetSetSystemParameter(
JET_INSTANCE.Nil, JET_SESID.Nil, JET_param.CacheSizeMin, 64, null)).Return(0);
this.repository.ReplayAll();
SystemParameters.CacheSizeMin = 64;
this.repository.VerifyAll();
}

/// <summary>
/// Verify that setting the property sets the system parameter
/// </summary>
[TestMethod]
[Priority(0)]
public void VerifySettingDatabasePageSize()
{
Expect.Call(
this.mockApi.JetSetSystemParameter(
JET_INSTANCE.Nil, JET_SESID.Nil, JET_param.DatabasePageSize, 4096, null)).Return(0);
this.repository.ReplayAll();
SystemParameters.DatabasePageSize = 4096;
this.repository.VerifyAll();
}

/// <summary>
/// Verify that setting the property sets the system parameter
/// </summary>
[TestMethod]
[Priority(0)]
public void VerifySettingMaxInstances()
{
Expect.Call(
this.mockApi.JetSetSystemParameter(
JET_INSTANCE.Nil, JET_SESID.Nil, JET_param.MaxInstances, 12, null)).Return(0);
this.repository.ReplayAll();
SystemParameters.MaxInstances = 12;
this.repository.VerifyAll();
}

/// <summary>
/// Verify that setting the property sets the system parameter
/// </summary>
[TestMethod]
[Priority(0)]
public void VerifySettingConfiguration()
{
Expect.Call(
this.mockApi.JetSetSystemParameter(
JET_INSTANCE.Nil, JET_SESID.Nil, VistaParam.Configuration, 0, null)).Return(0);
this.repository.ReplayAll();
SystemParameters.Configuration = 0;
this.repository.VerifyAll();
}

/// <summary>
/// Verify that setting the property sets the system parameter
/// </summary>
[TestMethod]
[Priority(0)]
public void VerifySettingEnableAdvanced()
{
Expect.Call(
this.mockApi.JetSetSystemParameter(
JET_INSTANCE.Nil, JET_SESID.Nil, VistaParam.EnableAdvanced, 1, null)).Return(0);
this.repository.ReplayAll();
SystemParameters.EnableAdvanced = true;
this.repository.VerifyAll();
}
}
}

0 comments on commit 29a79c6

Please sign in to comment.