short getType = info.type.wType;
Debug.Assert(NativeDBType.STR != getType, "Should have bound as WSTR");
- Debug.Assert(!NativeDBType.HasHighBit(getType), "CreateAccessor - unexpected high bits on datatype");
if (-1 != info.size)
{
// high mask
internal const short HighMask = unchecked((short)0xf000);
- static internal bool HasHighBit(short value)
- {
- return (0 != (HighMask & value));
- }
- /*
- static internal bool IsArray(short value) {
- return (ARRAY == (HighMask & value));
- }
- static internal bool IsByRef(short value) {
- return (BYREF == (HighMask & value));
- }
- static internal bool IsReserved(short value) {
- return (RESERVED == (HighMask & value));
- }
- static internal bool IsVector(short value) {
- return (VECTOR == (HighMask & value));
- }
- static internal int GetLowBits(short value) {
- return (value & ~HighMask);
- }
- */
-
private const string S_BINARY = "DBTYPE_BINARY"; // DBTYPE_BYTES
private const string S_BOOL = "DBTYPE_BOOL";
private const string S_BSTR = "DBTYPE_BSTR";
{
Debug.Assert(_metaDataCollectionsDataSet != null);
- //TODO: MarkAsh or EnzoL should review this code for efficency.
DataTable metaDataCollectionsTable = _metaDataCollectionsDataSet.Tables[DbMetaDataCollectionNames.MetaDataCollections];
DataColumn populationMechanismColumn = metaDataCollectionsTable.Columns[_populationMechanism];
DataColumn collectionNameColumn = metaDataCollectionsTable.Columns[DbMetaDataColumnNames.CollectionName];
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
+using Xunit;
namespace System.Data.OleDb.Tests
{
using (var cmd = (OleDbCommand)OleDbFactory.Instance.CreateCommand())
{
cmd.CommandType = commandType;
- var exception = Record.Exception(() => OleDbCommandBuilder.DeriveParameters(cmd));
- Assert.NotNull(exception);
- Assert.IsType<InvalidOperationException>(exception);
- Assert.Equal(
- string.Format("{0} DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{1}.", nameof(OleDbCommand), cmd.CommandType.ToString()),
- exception.Message);
+ AssertExtensions.Throws<InvalidOperationException>(
+ () => OleDbCommandBuilder.DeriveParameters(cmd),
+ $"{nameof(OleDbCommand)} DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{cmd.CommandType.ToString()}.");
}
}
using (var cmd = (OleDbCommand)OleDbFactory.Instance.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
- cmd.CommandText = null;
- var exception = Record.Exception(() => OleDbCommandBuilder.DeriveParameters(cmd));
- Assert.NotNull(exception);
- Assert.IsType<InvalidOperationException>(exception);
- Assert.Equal(
- string.Format("{0}: {1} property has not been initialized", nameof(OleDbCommandBuilder.DeriveParameters), nameof(cmd.CommandText)),
- exception.Message);
+ cmd.CommandText = null;
+ AssertExtensions.Throws<InvalidOperationException>(
+ () => OleDbCommandBuilder.DeriveParameters(cmd),
+ $"{nameof(OleDbCommandBuilder.DeriveParameters)}: {nameof(cmd.CommandText)} property has not been initialized");
}
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = @"SELECT * FROM " + tableName;
cmd.Connection = null;
- var exception = Record.Exception(() => OleDbCommandBuilder.DeriveParameters(cmd));
- Assert.NotNull(exception);
- Assert.IsType<InvalidOperationException>(exception);
- Assert.Equal(
- string.Format("{0}: {1} property has not been initialized.", nameof(OleDbCommandBuilder.DeriveParameters), nameof(cmd.Connection)),
- exception.Message);
+
+ AssertExtensions.Throws<InvalidOperationException>(
+ () => OleDbCommandBuilder.DeriveParameters(cmd),
+ $"{nameof(OleDbCommandBuilder.DeriveParameters)}: {nameof(cmd.Connection)} property has not been initialized.");
}
});
}
Assert.NotNull(exception);
Assert.IsType<InvalidOperationException>(exception);
Assert.Contains(
- string.Format("{0} requires an open and available Connection.", nameof(OleDbCommandBuilder.DeriveParameters)),
+ $"{nameof(OleDbCommandBuilder.DeriveParameters)} requires an open and available Connection.",
exception.Message);
}
});
{
public class OleDbCommandTests : OleDbTestBase
{
+ [ConditionalFact(Helpers.IsDriverAvailable)]
+ public void CommandType_SetInvalidValue_Throws()
+ {
+ using (var cmd = (OleDbCommand)OleDbFactory.Instance.CreateCommand())
+ {
+ AssertExtensions.Throws<ArgumentOutOfRangeException>(
+ () => cmd.CommandType = (CommandType)0,
+ $"The CommandType enumeration value, 0, is invalid.\r\nParameter name: {nameof(cmd.CommandType)}"
+ );
+ }
+ }
+
[OuterLoop]
[ConditionalFact(Helpers.IsDriverAvailable)]
public void CommandType_InvalidType_Throws()
}
}
- [ConditionalFact(Helpers.IsDriverAvailable)]
- public void GetSchema()
+ [ConditionalTheory(Helpers.IsDriverAvailable)]
+ [InlineData(nameof(DbMetaDataCollectionNames.MetaDataCollections), "CollectionName")]
+ [InlineData(nameof(DbMetaDataCollectionNames.DataSourceInformation), "CompositeIdentifierSeparatorPattern")]
+ [InlineData(nameof(DbMetaDataCollectionNames.DataTypes), "TypeName")]
+ public void GetSchema(string tableName, string columnName)
{
- using (var oleDbConnection = new OleDbConnection(ConnectionString))
- {
- oleDbConnection.Open();
-
- DataTable metaDataCollections = oleDbConnection.GetSchema(DbMetaDataCollectionNames.MetaDataCollections);
- Assert.True(metaDataCollections != null && metaDataCollections.Rows.Count > 0);
-
- DataTable metaDataSourceInfo = oleDbConnection.GetSchema(DbMetaDataCollectionNames.DataSourceInformation);
- Assert.True(metaDataSourceInfo != null && metaDataSourceInfo.Rows.Count > 0);
-
- DataTable metaDataTypes = oleDbConnection.GetSchema(DbMetaDataCollectionNames.DataTypes);
- Assert.True(metaDataTypes != null && metaDataTypes.Rows.Count > 0);
-
- DataTable schema = oleDbConnection.GetSchema();
- Assert.True(schema != null && schema.Rows.Count > 0);
-
- Assert.Throws<NotSupportedException>(
- () => oleDbConnection.GetSchema(DbMetaDataCollectionNames.MetaDataCollections, new string[] { new string('a', 5000) } ));
- }
+ DataTable schema = connection.GetSchema(tableName);
+ Assert.True(schema != null && schema.Rows.Count > 0);
+ var exception = Record.Exception(() => schema.Rows[0].Field<string>(columnName));
+ Assert.Null(exception);
+
+ AssertExtensions.Throws<ArgumentException>(
+ () => connection.GetSchema(tableName, new string[] { null }),
+ $"More restrictions were provided than the requested schema ('{tableName}') supports."
+ );
+ const string MissingColumn = "MissingColumn";
+ AssertExtensions.Throws<ArgumentException>(
+ () => schema.Rows[0].Field<IEnumerable<char>>(MissingColumn),
+ $"Column '{MissingColumn}' does not belong to table {tableName}.");
}
[ConditionalFact(Helpers.IsDriverAvailable)]
ConnectionString }.AsSpan();
File.WriteAllLines(udlFile, lines.Slice(start, length).ToArray());
- var exception = Record.Exception(() => new OleDbConnection(@"file name = " + udlFile));
- Assert.NotNull(exception);
- Assert.IsType<ArgumentException>(exception);
- Assert.Equal(
- "Invalid UDL file.",
- exception.Message);
+ AssertExtensions.Throws<ArgumentException>(
+ () => new OleDbConnection(@"file name = " + udlFile),
+ "Invalid UDL file.");
}
[ConditionalFact(Helpers.IsDriverAvailable)]
Action<bool> FillShouldThrow = (shouldFail) => {
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
- var exception = Record.Exception(() => adapter.Fill(ds, tableName));
if (shouldFail)
{
- Assert.NotNull(exception);
- Assert.IsType<InvalidOperationException>(exception);
- Assert.Equal(
- "There is already an open DataReader associated with this Command which must be closed first.",
- exception.Message);
+ AssertExtensions.Throws<InvalidOperationException>(
+ () => adapter.Fill(ds, tableName),
+ "There is already an open DataReader associated with this Command which must be closed first.");
}
else
{
- Assert.Null(exception);
+ Assert.NotNull(adapter.Fill(ds, tableName));
}
};
using (var reader = command.ExecuteReader())
Assert.True(reader.HasRows);
DataTable schema = reader.GetSchemaTable();
Assert.Equal(4, schema.Rows.Count);
- var exception = Record.Exception(() => reader.GetString(5));
- Assert.NotNull(exception);
- Assert.IsType<IndexOutOfRangeException>(exception);
- Assert.Equal(
- "Index was outside the bounds of the array.",
- exception.Message);
+ AssertExtensions.Throws<IndexOutOfRangeException>(
+ () => reader.GetString(5),
+ "Index was outside the bounds of the array.");
});
}
RunTest((reader) => {
reader.Read();
Assert.True(reader.HasRows);
- var exception = Record.Exception(() => reader["NonExistentColumn"]);
- Assert.NotNull(exception);
- Assert.IsType<IndexOutOfRangeException>(exception);
- Assert.Equal(
- "NonExistentColumn",
- exception.Message);
+ object obj;
+ AssertExtensions.Throws<IndexOutOfRangeException>(
+ () => obj = reader["NonExistentColumn"], "NonExistentColumn");
});
}
[ConditionalFact(Helpers.IsDriverAvailable)]
public void EmptyReader_SchemaOnly_EmptyReader()
{
+ const string expectedMessage = "No data exists for the row/column.";
RunTest((reader) => {
reader.Read();
Assert.False(reader.HasRows);
- var exception = Record.Exception(() => reader.GetString(1));
- Assert.NotNull(exception);
- Assert.IsType<InvalidOperationException>(exception);
- Assert.Equal(
- "No data exists for the row/column.",
- exception.Message);
-
- var values = new object[1];
- exception = Record.Exception(() => reader.GetValues(values));
- Assert.NotNull(exception);
- Assert.IsType<InvalidOperationException>(exception);
- Assert.Equal(
- "No data exists for the row/column.",
- exception.Message);
+ AssertExtensions.Throws<InvalidOperationException>(() => reader.GetString(1), expectedMessage);
+ AssertExtensions.Throws<InvalidOperationException>(() => reader.GetValues(new object[1]), expectedMessage);
+ AssertExtensions.Throws<InvalidOperationException>(() => reader.GetData(0), expectedMessage);
}, schemaOnly: true);
}
[OuterLoop]
[ConditionalFact(Helpers.IsDriverAvailable)]
- public void Depth_IsClosed_Throws()
+ public void IsClosed_CallReaderApis_Throws()
{
RunTest((reader) => {
reader.Close();
Assert.Throws<InvalidOperationException>(() => reader.Depth);
- });
- }
-
- [OuterLoop]
- [ConditionalFact(Helpers.IsDriverAvailable)]
- public void FieldCount_IsClosed_Throws()
- {
- RunTest((reader) => {
- reader.Close();
Assert.Throws<InvalidOperationException>(() => reader.FieldCount);
- });
- }
-
- [OuterLoop]
- [ConditionalFact(Helpers.IsDriverAvailable)]
- public void VisibleFieldCount_IsClosed_Throws()
- {
- RunTest((reader) => {
- reader.Close();
Assert.Throws<InvalidOperationException>(() => reader.VisibleFieldCount);
- });
- }
-
- [OuterLoop]
- [ConditionalFact(Helpers.IsDriverAvailable)]
- public void HasRows_IsClosed_Throws()
- {
- RunTest((reader) => {
- reader.Close();
Assert.Throws<InvalidOperationException>(() => reader.HasRows);
- });
- }
-
- [OuterLoop]
- [ConditionalFact(Helpers.IsDriverAvailable)]
- public void GetSchemaTable_IsClosed_Throws()
- {
- RunTest((reader) => {
- reader.Close();
Assert.Throws<InvalidOperationException>(() => reader.GetSchemaTable());
+ Assert.Throws<InvalidOperationException>(() => reader.NextResult());
});
}