Created
January 30, 2020 04:11
-
-
Save BryanWilhite/57961d097d6160447084d73239a62142 to your computer and use it in GitHub Desktop.
generating POCO classes from Oracle Table Metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MyBiz.DataAccess; | |
using MyBiz.DataAccess.Extensions; | |
using MyBiz.DataAccess.Models; | |
using MyBiz.DataAccess.Tests.Extensions; | |
using MyBiz.DataAccess.TextTemplating; | |
using MyBiz.Extensions; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using Newtonsoft.Json; | |
using Oracle.ManagedDataAccess.Client; | |
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data; | |
using System.IO; | |
using System.Linq; | |
namespace MyBiz.Services.AdvisorPortal.Tests | |
{ | |
[TestClass] | |
public class OracleTableMetadataTest | |
{ | |
/// <summary> | |
/// Gets or sets the test context. | |
/// </summary> | |
/// <value>The test context.</value> | |
public TestContext TestContext { get; set; } | |
/// <summary> | |
/// Initializes the test. | |
/// </summary> | |
[TestInitialize] | |
public void InitializeTest() | |
{ | |
this.TestContext.RemovePreviousTestResults(); | |
} | |
[TestCategory("Integration")] | |
[TestMethod] | |
[TestProperty("csFileTemplate", @"OracleTableMetadata\{tableOrViewName}.cs")] | |
[TestProperty("metaJsonFileTemplate", @"OracleTableMetadata\{tableOrViewName}.json")] | |
[TestProperty("tableOrViewName", "V_CP_ACCOUNTLIST")] | |
public void ShouldGenerateModelDefinition() | |
{ | |
var projectsFolder = this.TestContext.ShouldGetProjectsFolder(this.GetType()); | |
#region test properties: | |
var csFileTemplate = this.TestContext.Properties["csFileTemplate"].ToString(); | |
var metaJsonFileTemplate = this.TestContext.Properties["metaJsonFileTemplate"].ToString(); | |
var tableOrViewName = this.TestContext.Properties["tableOrViewName"].ToString(); | |
#endregion | |
var csFile = csFileTemplate.Replace("{tableOrViewName}", tableOrViewName.ToCamelCaseFromUnderscores()); | |
csFile = Path.Combine(projectsFolder, this.GetType().Namespace, csFile); | |
var inputJsonFile = metaJsonFileTemplate.Replace("{tableOrViewName}", tableOrViewName); | |
inputJsonFile = Path.Combine(projectsFolder, this.GetType().Namespace, inputJsonFile); | |
this.TestContext.ShouldFindFile(inputJsonFile); | |
var metadata = JsonConvert.DeserializeObject<IEnumerable<OracleTableMetadata>>(File.ReadAllText(inputJsonFile)); | |
Assert.IsTrue(metadata.Any(), "The expected table metadata is not here."); | |
var t4 = new OracleEntityGenerator(metadata); | |
var cs = t4.TransformText(); | |
File.WriteAllText(csFile, cs); | |
this.TestContext.ShouldFindFile(csFile); | |
} | |
[TestCategory("Integration")] | |
[TestMethod] | |
[TestProperty("csFileTemplate", @"OracleTableMetadata\{tableOrViewName}Map.cs")] | |
[TestProperty("metaJsonFileTemplate", @"OracleTableMetadata\{tableOrViewName}.json")] | |
[TestProperty("tableOrViewName", "V_CP_ACCOUNTLIST")] | |
public void ShouldGenerateModelMappings() | |
{ | |
var projectsFolder = this.TestContext.ShouldGetProjectsFolder(this.GetType()); | |
#region test properties: | |
var csFileTemplate = this.TestContext.Properties["csFileTemplate"].ToString(); | |
var metaJsonFileTemplate = this.TestContext.Properties["metaJsonFileTemplate"].ToString(); | |
var tableOrViewName = this.TestContext.Properties["tableOrViewName"].ToString(); | |
#endregion | |
var csFile = csFileTemplate.Replace("{tableOrViewName}", tableOrViewName.ToCamelCaseFromUnderscores()); | |
csFile = Path.Combine(projectsFolder, this.GetType().Namespace, csFile); | |
var inputJsonFile = metaJsonFileTemplate.Replace("{tableOrViewName}", tableOrViewName); | |
inputJsonFile = Path.Combine(projectsFolder, this.GetType().Namespace, inputJsonFile); | |
this.TestContext.ShouldFindFile(inputJsonFile); | |
var metadata = JsonConvert.DeserializeObject<IEnumerable<OracleTableMetadata>>(File.ReadAllText(inputJsonFile)); | |
Assert.IsTrue(metadata.Any(), "The expected table metadata is not here."); | |
var t4 = new OracleEntityMappingGenerator(metadata); | |
var cs = t4.TransformText(); | |
File.WriteAllText(csFile, cs); | |
this.TestContext.ShouldFindFile(csFile); | |
} | |
[TestCategory("Integration")] | |
[TestMethod] | |
[TestProperty("connectionStringAlias", "ADVPVD")] | |
[TestProperty("encryptionMetaJsonFile", @"encryption-meta-user\encryptionMetadata_IAPPS.json")] | |
[TestProperty("invariantProviderName", "Oracle.ManagedDataAccess.Client")] | |
[TestProperty("metaJsonFileTemplate", @"OracleTableMetadata\{tableName}.json")] | |
[TestProperty("tableOrViewName", "V_CP_ACCOUNTLIST")] | |
public void ShouldLoadOracleTableMetadata() | |
{ | |
var projectsFolder = this.TestContext.ShouldGetProjectsFolder(this.GetType()); | |
#region test properties: | |
var connectionStringAlias = this.TestContext.Properties["connectionStringAlias"].ToString(); | |
var encryptionMetaJsonFile = this.TestContext.Properties["encryptionMetaJsonFile"].ToString(); | |
encryptionMetaJsonFile = Path.Combine(projectsFolder, this.GetType().Namespace, encryptionMetaJsonFile); | |
var invariantProviderName = this.TestContext.Properties["invariantProviderName"].ToString(); | |
var metaJsonFileTemplate = new UriTemplate(this.TestContext.Properties["metaJsonFileTemplate"].ToString()); | |
var tableOrViewName = this.TestContext.Properties["tableOrViewName"].ToString(); | |
#endregion | |
var settings = ConfigurationManager.ConnectionStrings[connectionStringAlias]; | |
var connectionString = settings.ToConnectionString(connectionStringAlias, encryptionMetaJsonFile); | |
this.TestContext.WriteLine("connecting to {0}...", connectionString); | |
var outputJsonFile = metaJsonFileTemplate.ToLocalPath(tableOrViewName); | |
outputJsonFile = Path.Combine(projectsFolder, this.GetType().Namespace, outputJsonFile); | |
this.TestContext.ShouldFindFile(outputJsonFile); | |
var metadata = new List<OracleTableMetadata>(); | |
using (var connection = CommonDbmsUtility.GetConnection(invariantProviderName, connectionString)) | |
{ | |
connection.Open(); | |
Assert.AreEqual(connection.State, ConnectionState.Open, "The expected Open Connection State is not here."); | |
var @params = new[] | |
{ | |
new OracleParameter("tableOrViewName", tableOrViewName), | |
}; | |
OracleCommand command = null; | |
IDataReader reader = null; | |
try | |
{ | |
command = new OracleCommand(OracleTableMetadata.OracleSysAllTabColumnsSql, connection as OracleConnection); | |
command.Parameters.AddRange(@params); | |
reader = command.ExecuteReader(); | |
while (reader.Read()) | |
{ | |
var meta = reader.ToOracleTableMetadata(); | |
var validationresults = meta.ToValidationResults(); | |
validationresults.ForEachInEnumerable(i => this.TestContext.WriteLine("VALIDATION ERROR: {0}", i)); | |
metadata.Add(meta); | |
} | |
} | |
finally | |
{ | |
if (command != null) command.Dispose(); | |
if (reader != null) reader.Dispose(); | |
} | |
Assert.IsTrue(metadata.Any(), "The expected table metadata is not here."); | |
metadata.ForEachInEnumerable(i => this.TestContext.WriteLine("meta: {0}", i)); | |
var json = JsonConvert.SerializeObject(metadata); | |
File.WriteAllText(outputJsonFile, json); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment