PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/LLBL Pro v3.1/AW.LLBLGen.DataContextDriver.Tests/LLBLGenStaticDriverTest.cs

#
C# | 353 lines | 261 code | 39 blank | 53 comment | 16 complexity | c7e867ae05b18bd53b3f38c7e5866465 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MIT, GPL-2.0, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Common;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Xml.Linq;
  8. using AW.Data.FactoryClasses;
  9. using AW.Data.Linq;
  10. using AW.Helper;
  11. using AW.Helper.LLBL;
  12. using AW.LLBLGen.DataContextDriver.Static;
  13. using LINQPad.Extensibility.DataContext;
  14. using Microsoft.VisualStudio.TestTools.UnitTesting;
  15. using Moq;
  16. using Northwind.DAL;
  17. using Northwind.DAL.EntityClasses;
  18. using Northwind.DAL.SqlServer;
  19. using SD.LLBLGen.Pro.ORMSupportClasses;
  20. using CustomerEntity = AW.Data.EntityClasses.CustomerEntity;
  21. using ElementCreator = Northwind.DAL.FactoryClasses.ElementCreator;
  22. using EntityType = AW.Data.EntityType;
  23. namespace AW.LLBLGen.DataContextDriver.Tests
  24. {
  25. ///<summary>
  26. /// This is a test class for LLBLGenStaticDriverTest and is intended to contain all LLBLGenStaticDriverTest Unit Tests
  27. ///</summary>
  28. [TestClass]
  29. public class LLBLGenStaticDriverTest
  30. {
  31. ///<summary>
  32. /// Gets or sets the test context which provides information about and functionality for the current test run.
  33. ///</summary>
  34. public TestContext TestContext { get; set; }
  35. #region Additional test attributes
  36. //
  37. //You can use the following additional attributes as you write your tests:
  38. //
  39. //Use ClassInitialize to run code before running the first test in the class
  40. //[ClassInitialize()]
  41. //public static void MyClassInitialize(TestContext testContext)
  42. //{
  43. //}
  44. //
  45. //Use ClassCleanup to run code after all tests in a class have run
  46. //[ClassCleanup()]
  47. //public static void MyClassCleanup()
  48. //{
  49. //}
  50. //
  51. //Use TestInitialize to run code before running each test
  52. //[TestInitialize()]
  53. //public void MyTestInitialize()
  54. //{
  55. //}
  56. //
  57. //Use TestCleanup to run code after each test has run
  58. //[TestCleanup()]
  59. //public void MyTestCleanup()
  60. //{
  61. //}
  62. //
  63. #endregion
  64. ///<summary>
  65. /// A test for GetSchema using AW
  66. ///</summary>
  67. [TestMethod]
  68. public void GetAWSchemaTest()
  69. {
  70. var customType = typeof (LinqMetaData);
  71. var explorerItems = GetSchemaTest<EntityType>(customType, EntityFactoryFactory.GetFactory, true);
  72. var customerExplorerItem = explorerItems.First(e => e.Text == EntityHelper.GetNameFromEntityEnum(EntityType.CustomerEntity));
  73. IEntityCore customerEntity = new CustomerEntity();
  74. var msDescription = customerEntity.CustomPropertiesOfType.Values.First();
  75. StringAssert.Contains(customerExplorerItem.ToolTipText, msDescription);
  76. var firstExplorerItem = customerExplorerItem.Children.First();
  77. Assert.IsFalse(string.IsNullOrWhiteSpace(firstExplorerItem.ToolTipText));
  78. var firstField = (IEntityField) customerEntity.Fields[0];
  79. StringAssert.Contains(firstExplorerItem.ToolTipText, firstField.SourceColumnName);
  80. StringAssert.Contains(customerExplorerItem.ToolTipText, firstField.SourceObjectName);
  81. StringAssert.Contains(customerExplorerItem.ToolTipText, firstField.SourceCatalogName);
  82. StringAssert.Contains(customerExplorerItem.ToolTipText, firstField.SourceSchemaName);
  83. var individualExplorerItem = explorerItems.First(e => e.Text.Contains(EntityHelper.GetNameFromEntityEnum(EntityType.IndividualEntity)));
  84. foreach (var explorerItem in customerExplorerItem.Children)
  85. {
  86. var item = individualExplorerItem.Children.Single(c => c.Text == explorerItem.Text);
  87. Assert.AreEqual(explorerItem.ToolTipText, item.ToolTipText);
  88. }
  89. }
  90. ///<summary>
  91. /// A test for GetSchema using AW
  92. ///</summary>
  93. [TestMethod]
  94. public void GetAWSchemaPropertiesTest()
  95. {
  96. var customType = typeof (LinqMetaData);
  97. GetSchemaTest<EntityType>(customType, EntityFactoryFactory.GetFactory, false);
  98. }
  99. ///<summary>
  100. /// A test for GetSchema using Northwind
  101. ///</summary>
  102. [TestMethod]
  103. public void GetNorthwindSchemaFieldsTest()
  104. {
  105. var explorerItems = GetSchemaTest<Northwind.DAL.EntityType>(typeof (Northwind.DAL.Linq.LinqMetaData),
  106. Northwind.DAL.FactoryClasses.EntityFactoryFactory.GetFactory, true);
  107. var customerExplorerItem = TestNorthWindToolTips(explorerItems);
  108. var employeeExplorerItem = customerExplorerItem.Children.Single(e => e.Text == "EmployeesViaOrders");
  109. Assert.AreEqual(ExplorerIcon.ManyToMany, employeeExplorerItem.Icon);
  110. }
  111. private static ExplorerItem TestNorthWindToolTips(List<ExplorerItem> explorerItems, bool testForiegnKey = true)
  112. {
  113. var customerName = EntityHelper.GetNameFromEntityEnum(Northwind.DAL.EntityType.CustomerEntity);
  114. var explorerItem = explorerItems.First(e => e.Text == customerName);
  115. var orderExplorerItem = explorerItems.Single(e => e.Text == EntityHelper.GetNameFromEntityEnum(Northwind.DAL.EntityType.OrderEntity));
  116. var customerNavigator = orderExplorerItem.Children.Single(e => e.Text == customerName);
  117. var customerEntitytype = typeof (Northwind.DAL.EntityClasses.CustomerEntity);
  118. var displayNameAttributes = MetaDataHelper.GetDisplayNameAttributes(customerEntitytype).ToList();
  119. foreach (var displayNameAttribute in displayNameAttributes)
  120. {
  121. StringAssert.Contains(explorerItem.ToolTipText, displayNameAttribute.DisplayName);
  122. StringAssert.Contains(customerNavigator.ToolTipText, displayNameAttribute.DisplayName);
  123. }
  124. var descriptionAttributes = MetaDataHelper.GetDescriptionAttributes(customerEntitytype).ToList();
  125. foreach (var descriptionAttribute in descriptionAttributes)
  126. {
  127. StringAssert.Contains(explorerItem.ToolTipText, descriptionAttribute.Description);
  128. StringAssert.Contains(customerNavigator.ToolTipText, descriptionAttribute.Description);
  129. }
  130. var orderEntitytype = typeof (OrderEntity);
  131. var orderPropertiesToShowInSchema = LLBLGenDriverHelper.GetPropertiesToShowInSchema(orderEntitytype);
  132. var customerPropertyDescriptor = orderPropertiesToShowInSchema.Single(p => p.Name == customerName);
  133. StringAssert.Contains(customerNavigator.ToolTipText, customerPropertyDescriptor.Description);
  134. StringAssert.Contains(customerNavigator.ToolTipText, customerPropertyDescriptor.DisplayName);
  135. if (testForiegnKey)
  136. StringAssert.Contains(customerNavigator.ToolTipText, OrderFieldIndex.CustomerId.ToString());
  137. var first = explorerItem.Children.First();
  138. Assert.IsFalse(string.IsNullOrWhiteSpace(first.ToolTipText));
  139. var customerPropertiesToShowInSchema = LLBLGenDriverHelper.GetPropertiesToShowInSchema(customerEntitytype);
  140. var description = customerPropertiesToShowInSchema.First().Description;
  141. Assert.IsFalse(String.IsNullOrEmpty(description));
  142. Assert.IsTrue(first.ToolTipText.Contains(description));
  143. var orderDetailExplorerItem = explorerItems.Single(e => e.Text == EntityHelper.GetNameFromEntityEnum(Northwind.DAL.EntityType.OrderDetailEntity));
  144. var quantityExplorerItem = orderDetailExplorerItem.Children.Single(ei => ei.DragText == "Quantity");
  145. StringAssert.Contains(quantityExplorerItem.ToolTipText, StringConstants.QuantityDescription);
  146. return explorerItem;
  147. }
  148. [TestMethod]
  149. public void GetNorthwindElementCreatorSchemaFieldsTest()
  150. {
  151. var explorerItems = GetSchemaTest<Northwind.DAL.EntityType>(typeof (ElementCreator),
  152. Northwind.DAL.FactoryClasses.EntityFactoryFactory.GetFactory, true);
  153. TestNorthWindToolTips(explorerItems);
  154. }
  155. [TestMethod]
  156. public void GetNorthwindSchemaPropertiesTest()
  157. {
  158. var explorerItems = GetSchemaTest<Northwind.DAL.EntityType>(typeof (Northwind.DAL.Linq.LinqMetaData),
  159. Northwind.DAL.FactoryClasses.EntityFactoryFactory.GetFactory, false);
  160. TestNorthWindToolTips(explorerItems, false);
  161. }
  162. private static List<ExplorerItem> GetSchemaTest<TEnum>(Type customType, Func<TEnum, IEntityFactoryCore> entityFactoryFactory, bool useFields)
  163. {
  164. var entityTypes = GeneralHelper.EnumAsEnumerable<TEnum>();
  165. var target = new LLBLGenStaticDriver();
  166. var mockedIConnectionInfo = MockedIConnectionInfo(useFields);
  167. var actual = target.GetSchema(mockedIConnectionInfo.Object, customType);
  168. Assert.AreEqual(entityTypes.Length, actual.Count);
  169. var orderedEnumerable = entityTypes.OrderBy(et => EntityHelper.GetNameFromEntityName(et.ToString()));
  170. var index = 0;
  171. foreach (var entityType in orderedEnumerable)
  172. {
  173. var entityFactory = entityFactoryFactory(entityType);
  174. var entity = entityFactory.Create();
  175. var fieldNames = entity.Fields.Cast<IEntityFieldCore>().Select(f => f.Name).Distinct();
  176. var navigatorProperties = useFields ? EntityHelper.GetNavigatorProperties(entity) : MetaDataHelper.GetPropertyDescriptors(entity.GetType()).FilterByIsEnumerable(typeof (IEntityCore));
  177. var explorerItem = actual[index];
  178. index++;
  179. var entityName = entityFactory.ForEntityName + " - " + explorerItem.Text;
  180. var notCoreCount = navigatorProperties.Count(er => !EntityHelper.IsEntityCore(er));
  181. var coreCount = navigatorProperties.Count(EntityHelper.IsEntityCore);
  182. var propertyCount = explorerItem.Children.Count(ei => ei.Kind == ExplorerItemKind.Property);
  183. var collectionLinkCount = explorerItem.Children.Count(ei => ei.Kind == ExplorerItemKind.CollectionLink);
  184. var referenceLinkCount = explorerItem.Children.Count(ei => ei.Kind == ExplorerItemKind.ReferenceLink);
  185. if (useFields)
  186. {
  187. Assert.AreEqual(fieldNames.Count(), propertyCount, entityName + " - fieldNames -" + fieldNames.JoinAsString());
  188. Assert.AreEqual(notCoreCount, collectionLinkCount, entityName + " - Many navigator");
  189. Assert.AreEqual(coreCount, referenceLinkCount, entityName + " - single navigator");
  190. }
  191. else
  192. {
  193. Assert.IsTrue(fieldNames.Count() <= propertyCount, entityName + " - fieldNames -" + fieldNames.JoinAsString());
  194. Assert.IsTrue(notCoreCount <= collectionLinkCount, entityName + " - Many navigator");
  195. Assert.IsTrue(coreCount <= referenceLinkCount, entityName + " - single navigator");
  196. }
  197. }
  198. return actual;
  199. }
  200. private static Mock<IConnectionInfo> MockedIConnectionInfo(bool useFields)
  201. {
  202. var mockedICustomTypeInfo = new Mock<ICustomTypeInfo>();
  203. var mockedIConnectionInfo = new Mock<IConnectionInfo>();
  204. var mockedIDatabaseInfo = new Mock<IDatabaseInfo>();
  205. var xElementDriverData = new XElement("DriverData");
  206. var xElementUseFields = new XElement(ConnectionDialog.ElementNameUseFields) {Value = useFields.ToString()};
  207. xElementDriverData.Add(xElementUseFields);
  208. mockedIConnectionInfo.Setup(ci => ci.DriverData).Returns(xElementDriverData);
  209. mockedIConnectionInfo.Setup(ci => ci.CustomTypeInfo).Returns(mockedICustomTypeInfo.Object);
  210. mockedIConnectionInfo.Setup(ci => ci.DatabaseInfo).Returns(mockedIDatabaseInfo.Object);
  211. ConnectionDialog.SetDriverDataValue(mockedIConnectionInfo.Object, ConnectionDialog.ElementNameAdaptertype, "Northwind.DAL.SqlServer.DataAccessAdapter");
  212. ConnectionDialog.SetDriverDataValue(mockedIConnectionInfo.Object, ConnectionDialog.ElementNameAdapterAssembly, "Northwind.DAL.SqlServer.dll");
  213. return mockedIConnectionInfo;
  214. }
  215. //[TestMethod]
  216. //public void GetFieldsToShowInSchemaTest()
  217. //{
  218. // var fieldsToShowInSchema = LLBLGenStaticDriver.GetFieldsToShowInSchema(typeof (Northwind.DAL.EntityClasses.CustomerEntity));
  219. // Assert.IsFalse(String.IsNullOrEmpty(fieldsToShowInSchema.First().Description));
  220. //}
  221. [TestMethod]
  222. public void GetPropertiesToShowInSchemaTest()
  223. {
  224. var propertiesToShowInSchema = LLBLGenDriverHelper.GetPropertiesToShowInSchema(typeof (Northwind.DAL.EntityClasses.CustomerEntity));
  225. Assert.IsFalse(String.IsNullOrEmpty(propertiesToShowInSchema.First().Description));
  226. }
  227. [TestMethod]
  228. public void GetPropertiesToShowInSchemaBuddyTest()
  229. {
  230. var type = typeof (OrderDetailEntity);
  231. MetaDataHelper.AddAssociatedMetadataProvider(type);
  232. var descriptionAttributes = MetaDataHelper.GetDescriptionAttributes(type, OrderDetailFieldIndex.Quantity.ToString());
  233. Assert.IsTrue(descriptionAttributes.Any());
  234. var propertiesToShowInSchema = LLBLGenDriverHelper.GetPropertiesToShowInSchema(type);
  235. Assert.IsFalse(String.IsNullOrEmpty(propertiesToShowInSchema.GetFieldPropertyDescriptor(OrderDetailFieldIndex.Quantity.ToString()).Description));
  236. }
  237. [TestCategory("Winforms"), TestMethod]
  238. public void ConnectionDialogTest()
  239. {
  240. var mockedIConnectionInfo = MockedIConnectionInfo(true);
  241. ConnectionDialog.SetDriverDataValue(mockedIConnectionInfo.Object, ConnectionDialog.ElementNameAdapterAssembly, Path.GetFullPath("Northwind.DAL.SqlServer.dll"));
  242. var connectionDialog = new ConnectionDialog(mockedIConnectionInfo.Object, false);
  243. connectionDialog.ChooseAdapterOrFactoryClass("AdapterAssembly");
  244. }
  245. [TestMethod]
  246. public void SetSQLTranslationWriterTest()
  247. {
  248. var marshalByRefClass = new MarshalByRefClass();
  249. marshalByRefClass.SetSQLTranslationWriterTest();
  250. }
  251. [TestMethod]
  252. public void SetSQLTranslationWriterDomainIsolatorTest()
  253. {
  254. var domainIsolator = new DomainIsolator("friendlyName");
  255. domainIsolator.GetInstance<MarshalByRefClass>().SetSQLTranslationWriterTest();
  256. }
  257. [TestCategory("OrmProfiler"), TestMethod]
  258. public void GetFactoryAfterInterceptorCoreInitializeDomainIsolatorTest()
  259. {
  260. var appDomainSetup = new AppDomainSetup
  261. {
  262. ApplicationBase = Environment.GetEnvironmentVariable("TEMP")
  263. };
  264. var domainIsolator = new DomainIsolator("friendlyName", appDomainSetup);
  265. domainIsolator.GetInstance<MarshalByRefClass>().GetFactoryAfterInterceptorCoreInitializeTest();
  266. }
  267. [TestCategory("OrmProfiler"), TestMethod]
  268. public void GetFactoryAfterInterceptorCoreInitializeTest()
  269. {
  270. var marshalByRefClass = new MarshalByRefClass();
  271. marshalByRefClass.GetFactoryAfterInterceptorCoreInitializeTest();
  272. }
  273. }
  274. internal class MarshalByRefClass : MarshalByRefObject
  275. {
  276. public void GetFactoryAfterInterceptorCoreInitializeTest()
  277. {
  278. var programFilesPathx86 = Environment.GetEnvironmentVariable("ProgramFiles(x86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  279. var ormProfilerAssemblyFilePath = Path.Combine(Path.Combine(programFilesPathx86, ProfilerHelper.SolutionsDesignOrmProfilerPath), ProfilerHelper.OrmProfilerAssemblyFileName);
  280. Assert.IsTrue(File.Exists(ormProfilerAssemblyFilePath), ormProfilerAssemblyFilePath);
  281. var interceptorAssembly = Assembly.LoadFrom(ormProfilerAssemblyFilePath);
  282. var type = interceptorAssembly.GetType(ProfilerHelper.OrmProfilerInterceptorTypeName);
  283. Assert.IsNotNull(type);
  284. ProfilerHelper.InterceptorCoreInitialize(type);
  285. //CommonEntityBase.Initialize();
  286. //Assert.AreEqual(type, Type.GetType(type.AssemblyQualifiedName));
  287. //Assert.AreEqual(type, Type.GetType(ProfilerHelper.OrmProfilerInterceptorAssemblyQualifiedTypeName));
  288. DbProviderFactories.GetFactory("System.Data.SqlClient");
  289. }
  290. public void SetSQLTranslationWriterTest()
  291. {
  292. var dataAccessAdapter = new DataAccessAdapter();
  293. var eventInfo = dataAccessAdapter.GetType().GetEvent(SQLTraceEventArgs.SqlTraceEventName);
  294. Assert.IsNotNull(eventInfo);
  295. TextWriter writer = null;
  296. var handler2 = new Action<object, EventArgs>((sender, e) =>
  297. {
  298. if (e != null)
  299. {
  300. //var x = 1 + 2;
  301. //SQLTraceEventHandler(sender, e);
  302. SQLTraceEventArgs.WriteSQLTranslation(writer, null);
  303. }
  304. });
  305. Delegate.CreateDelegate(eventInfo.EventHandlerType, writer, handler2.Method);
  306. EventHandler<EventArgs> handler3 = (sender, e) => SQLTraceEventArgs.WriteSQLTranslation(writer, e);
  307. Delegate.CreateDelegate(eventInfo.EventHandlerType, writer, handler3.Method);
  308. var handler4 = new EventHandler<EventArgs>((sender, e) => SQLTraceEventArgs.WriteSQLTranslation(writer, e));
  309. Delegate.CreateDelegate(eventInfo.EventHandlerType, writer, handler4.Method);
  310. var llblGenStaticDriver = new LLBLGenStaticDriver();
  311. llblGenStaticDriver.SubscribeToSqlTraceEvent(dataAccessAdapter, eventInfo, writer);
  312. }
  313. }
  314. }