Skip to content

Commit

Permalink
Merge pull request #1022 from mikependon/repodb-fixes-issue-1015
Browse files Browse the repository at this point in the history
#1015 - Added the Unit Tests.
  • Loading branch information
mikependon authored Jan 14, 2022
2 parents a4e9c1b + cabdf5d commit 61b9399
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RepoDb.Extensions;
using RepoDb.UnitTests.CustomObjects;

namespace RepoDb.UnitTests.Extensions
{
[TestClass]
public class DataEntityExtensionTest
{
[TestCleanup]
public void Cleanup()
{
PropertyValueAttributeMapper.Clear();
}

#region GetSchema

[TestMethod]
public void TestDataEntityExtensionGetSchema()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetSchema("SchemaName.TableName", dbSetting);

// Assert
Assert.AreEqual("SchemaName", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetSchemaFromQuoted()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetSchema("[SchemaName].[TableName]", dbSetting);

// Assert
Assert.AreEqual("[SchemaName]", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetSchemaFromWhitespaced()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetSchema("Schema Name.TableName", dbSetting);

// Assert
Assert.AreEqual("Schema Name", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetSchemaFromQuotedAndIsDotted()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetSchema("[Schema.Name].[TableName]", dbSetting);

// Assert
Assert.AreEqual("[Schema.Name]", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetSchemaFromQuotedAndIsWhitespaced()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetSchema("[Schema Name].[TableName]", dbSetting);

// Assert
Assert.AreEqual("[Schema Name]", schema);
}

#endregion

#region GetTableName

[TestMethod]
public void TestDataEntityExtensionGetTableName()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("TableName", dbSetting);

// Assert
Assert.AreEqual("TableName", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameFromQuoted()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("[TableName]", dbSetting);

// Assert
Assert.AreEqual("[TableName]", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameFromWhitespaced()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("Table Name", dbSetting);

// Assert
Assert.AreEqual("Table Name", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameFromDotted()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("[Table.Name]", dbSetting);

// Assert
Assert.AreEqual("[Table.Name]", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameFromQuotedAndIsWhitespaced()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("[Table Name]", dbSetting);

// Assert
Assert.AreEqual("[Table Name]", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameWithSchema()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("SchemaName.TableName", dbSetting);

// Assert
Assert.AreEqual("TableName", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameWithSchemaAndIsWhitespaced()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("SchemaName.Table Name", dbSetting);

// Assert
Assert.AreEqual("Table Name", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameWithSchemaFromQuoted()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("[SchemaName].[TableName]", dbSetting);

// Assert
Assert.AreEqual("[TableName]", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameWithSchemaFromQuotedAndIsDotted()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("[SchemaName].[Table.Name]", dbSetting);

// Assert
Assert.AreEqual("[Table.Name]", schema);
}

[TestMethod]
public void TestDataEntityExtensionGetTableNameWithSchemaFromQuotedAndIsWhitespaced()
{
// Prepare
var dbSetting = new CustomDbSetting();

// Act
var schema = DataEntityExtension.GetTableName("[SchemaName].[Table Name]", dbSetting);

// Assert
Assert.AreEqual("[Table Name]", schema);
}

#endregion
}
}
43 changes: 31 additions & 12 deletions RepoDb.Core/RepoDb/Extensions/DataEntityExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static string GetMappedName<TEntity>() =>
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <returns>The schema of the passed table name.</returns>
[Obsolete("Use the overloaded method instead.")]
public static string GetSchema(string tableName) =>
GetSchema(tableName, null);

Expand All @@ -90,14 +91,24 @@ public static string GetSchema(string tableName) =>
public static string GetSchema(string tableName,
IDbSetting dbSetting)
{
// Get the schema and table name
var index = tableName.IndexOf(CharConstant.Period);
if (index > 0)
if (tableName.IsOpenQuoted(dbSetting))
{
return tableName.Substring(0, index).AsUnquoted(true, dbSetting);
var index = tableName.IndexOf(string.Concat(dbSetting.ClosingQuote, CharConstant.Period));
if (index >= 0)
{
return tableName.Substring(0, index + 1);
}
}
else
{
var index = tableName.IndexOf(CharConstant.Period);
if (index >= 0)
{
return tableName.Substring(0, index);
}

}

// Return the unquoted
return dbSetting?.DefaultSchema;
}

Expand All @@ -106,6 +117,7 @@ public static string GetSchema(string tableName,
/// </summary>
/// <param name="tableName">The name of the table.</param>
/// <returns>The actual table name.</returns>
[Obsolete("Use the overloaded method instead.")]
public static string GetTableName(string tableName) =>
GetTableName(tableName, null);

Expand All @@ -118,18 +130,25 @@ public static string GetTableName(string tableName) =>
public static string GetTableName(string tableName,
IDbSetting dbSetting)
{
// Get the schema and table name
var index = tableName.IndexOf(CharConstant.Period);
if (index > 0)
if (tableName.IsOpenQuoted(dbSetting))
{
var index = tableName.IndexOf(string.Concat(dbSetting.ClosingQuote, CharConstant.Period));
if (index >= 0 && tableName.Length > index + 2)
{
return tableName.Substring(index + 2);
}
}
else
{
if (tableName.Length > index)
var index = tableName.IndexOf(CharConstant.Period);
if (index >= 0 && tableName.Length > index)
{
return tableName.Substring(index + 1).AsUnquoted(true, dbSetting);
return tableName.Substring(index + 1);
}

}

// Return the unquoted
return tableName.AsUnquoted(true, dbSetting);
return tableName;
}

/// <summary>
Expand Down
20 changes: 20 additions & 0 deletions RepoDb.Core/RepoDb/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public static string AsAlphaNumeric(this string value,
return alphaNumericRegex.Replace(value, "_");
}

/// <summary>
/// Check whether the string value is open-quoted.
/// </summary>
/// <param name="value">The string value to be checked.</param>
/// <param name="dbSetting">The currently in used <see cref="IDbSetting"/> object.</param>
/// <returns>True if the value is open-quoted.</returns>
public static bool IsOpenQuoted(this string value,
IDbSetting dbSetting) =>
dbSetting != null ? value.StartsWith(dbSetting.OpeningQuote) : false;

/// <summary>
/// Check whether the string value is close-quoted.
/// </summary>
/// <param name="value">The string value to be checked.</param>
/// <param name="dbSetting">The currently in used <see cref="IDbSetting"/> object.</param>
/// <returns>True if the value is close-quoted.</returns>
public static bool IsCloseQuoted(this string value,
IDbSetting dbSetting) =>
dbSetting != null ? value.EndsWith(dbSetting.ClosingQuote) : false;

/// <summary>
/// Unquotes a string.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions RepoDb.MySql/RepoDb.MySql/DbHelpers/MySqlDbHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public IEnumerable<DbField> GetFields(IDbConnection connection,
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down Expand Up @@ -209,7 +209,7 @@ public async Task<IEnumerable<DbField>> GetFieldsAsync(IDbConnection connection,
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public IEnumerable<DbField> GetFields(IDbConnection connection,
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down Expand Up @@ -209,7 +209,7 @@ public async Task<IEnumerable<DbField>> GetFieldsAsync(IDbConnection connection,
var param = new
{
TableSchema = connection.Database,
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public IEnumerable<DbField> GetFields(IDbConnection connection,
var commandText = GetCommandText();
var param = new
{
Schema = DataEntityExtension.GetSchema(tableName, m_dbSetting),
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
Schema = DataEntityExtension.GetSchema(tableName, m_dbSetting).AsUnquoted(m_dbSetting),
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down Expand Up @@ -183,8 +183,8 @@ public async Task<IEnumerable<DbField>> GetFieldsAsync(IDbConnection connection,
var commandText = GetCommandText();
var param = new
{
Schema = DataEntityExtension.GetSchema(tableName, m_dbSetting),
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting)
Schema = DataEntityExtension.GetSchema(tableName, m_dbSetting).AsUnquoted(m_dbSetting),
TableName = DataEntityExtension.GetTableName(tableName, m_dbSetting).AsUnquoted(m_dbSetting)
};

// Iterate and extract
Expand Down
Loading

0 comments on commit 61b9399

Please sign in to comment.