PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NHibernate.Test/NHSpecificTest/NH2664/Fixture.cs

https://github.com/okb/nhibernate-core
C# | 130 lines | 103 code | 20 blank | 7 comment | 5 complexity | 2db2123f9d66ebc13792d9053482c5e8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0, CC-BY-SA-3.0
  1. using System.Collections;
  2. using System.Linq;
  3. using NHibernate.Linq;
  4. using NUnit.Framework;
  5. using System.Linq.Expressions;
  6. using System;
  7. namespace NHibernate.Test.NHSpecificTest.NH2664
  8. {
  9. [TestFixture]
  10. public class Fixture : TestCase
  11. {
  12. protected override string MappingsAssembly
  13. {
  14. get { return "NHibernate.Test"; }
  15. }
  16. protected override IList Mappings
  17. {
  18. get
  19. {
  20. return new[]
  21. {
  22. "NHSpecificTest.NH2664.Mappings.hbm.xml"
  23. };
  24. }
  25. }
  26. /// <summary>
  27. /// push some data into the database
  28. /// Really functions as a save test also
  29. /// </summary>
  30. protected override void OnSetUp()
  31. {
  32. base.OnSetUp();
  33. using (var session = OpenSession())
  34. {
  35. using (var tran = session.BeginTransaction())
  36. {
  37. Product product = new Product();
  38. product.ProductId = "1";
  39. product.Properties["Name"] = "First Product";
  40. product.Properties["Description"] = "First Description";
  41. session.Save(product);
  42. product = new Product();
  43. product.ProductId = "2";
  44. product.Properties["Name"] = "Second Product";
  45. product.Properties["Description"] = "Second Description";
  46. session.Save(product);
  47. product = new Product();
  48. product.ProductId = "3";
  49. product.Properties["Name"] = "val";
  50. product.Properties["Description"] = "val";
  51. session.Save(product);
  52. tran.Commit();
  53. }
  54. }
  55. }
  56. protected override void OnTearDown()
  57. {
  58. base.OnTearDown();
  59. using (var session = OpenSession())
  60. {
  61. using (var tran = session.BeginTransaction())
  62. {
  63. session.Delete("from Product");
  64. tran.Commit();
  65. }
  66. }
  67. }
  68. [Test]
  69. public void Query_DynamicComponent()
  70. {
  71. using (var session = OpenSession())
  72. {
  73. var product =
  74. (from p in session.Query<Product>() where p.Properties["Name"] == "First Product" select p).Single();
  75. Assert.IsNotNull(product);
  76. Assert.AreEqual("First Product", product.Properties["Name"]);
  77. }
  78. }
  79. [Test]
  80. public void Multiple_Query_Does_Not_Cache()
  81. {
  82. using (var session = OpenSession())
  83. {
  84. // Query by name
  85. var product1 = (from p in session.Query<Product>()
  86. where p.Properties["Name"] == "First Product"
  87. select p).Single();
  88. Assert.That(product1.ProductId, Is.EqualTo("1"));
  89. // Query by description (this test is to verify that the dictionary
  90. // index isn't cached from the query above.
  91. var product2 = (from p in session.Query<Product>()
  92. where p.Properties["Description"] == "Second Description"
  93. select p).Single();
  94. Assert.That(product2.ProductId, Is.EqualTo("2"));
  95. }
  96. }
  97. [Test]
  98. public void Different_Key_In_DynamicComponentDictionary_Returns_Different_Keys()
  99. {
  100. using (var session = OpenSession())
  101. {
  102. Expression<Func<IEnumerable>> key1 = () => (from a in session.Query<Product>() where a.Properties["Name"] == "val" select a);
  103. Expression<Func<IEnumerable>> key2 = () => (from a in session.Query<Product>() where a.Properties["Description"] == "val" select a);
  104. var nhKey1 = new NhLinqExpression(key1.Body, session.SessionFactory);
  105. var nhKey2 = new NhLinqExpression(key2.Body, session.SessionFactory);
  106. Assert.AreNotEqual(nhKey1.Key, nhKey2.Key);
  107. }
  108. }
  109. }
  110. }