|
| 1 | +// |
| 2 | +// SpecificationExpressionTransformationIntegrationTests.cs |
| 3 | +// |
| 4 | +// Author: |
| 5 | +// Craig Fowler <craig@csf-dev.com> |
| 6 | +// |
| 7 | +// Copyright (c) 2020 Craig Fowler |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | +using System; |
| 27 | +using NHibernate; |
| 28 | +using NHibernate.Cfg; |
| 29 | +using NUnit.Framework; |
| 30 | +using NHibernate.Dialect; |
| 31 | +using NHibernate.Cfg.MappingSchema; |
| 32 | +using NHibernate.Mapping.ByCode; |
| 33 | +using System.Reflection; |
| 34 | +using NHibernate.Tool.hbm2ddl; |
| 35 | +using CSF.Specifications.Tests.Stubs; |
| 36 | +using CSF.Specifications; |
| 37 | +using NHibernate.Linq; |
| 38 | +using System.Linq; |
| 39 | + |
| 40 | +namespace CSF.Specifications.Tests.Specifications |
| 41 | +{ |
| 42 | + [TestFixture,NonParallelizable, Description("Integration tests which use NHibernate to use specifications with an actual database, to verify that specification expressions are compatible.")] |
| 43 | + public class SpecificationExpressionTransformationIntegrationTests |
| 44 | + { |
| 45 | + Configuration config; |
| 46 | + ISessionFactory sessionFactory; |
| 47 | + |
| 48 | + [OneTimeSetUp] |
| 49 | + public void OneTimeSetup() |
| 50 | + { |
| 51 | + config = new Configuration(); |
| 52 | + |
| 53 | + config.DataBaseIntegration(db => { |
| 54 | + db.Dialect<SQLiteDialect>(); |
| 55 | + db.ConnectionString = "Data Source=:memory:;Version=3;New=True;"; |
| 56 | + db.ConnectionReleaseMode = ConnectionReleaseMode.OnClose; |
| 57 | + }); |
| 58 | + config.AddMapping(GetMapping()); |
| 59 | + |
| 60 | + sessionFactory = config.BuildSessionFactory(); |
| 61 | + } |
| 62 | + |
| 63 | + HbmMapping GetMapping() |
| 64 | + { |
| 65 | + var mapper = new ModelMapper(); |
| 66 | + mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes()); |
| 67 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 68 | + } |
| 69 | + |
| 70 | + [OneTimeTearDown] |
| 71 | + public void OneTimeTeardown() |
| 72 | + { |
| 73 | + sessionFactory?.Dispose(); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void Transform_expression_returns_a_spec_which_is_compatible_with_NHibernate() |
| 78 | + { |
| 79 | + using (var session = sessionFactory.OpenSession()) |
| 80 | + { |
| 81 | + SetupQueryIntegrationTest(session); |
| 82 | + |
| 83 | + var personSpec = Spec.Expr<Person>(x => x.Name == "Jane Doe"); |
| 84 | + var petSpec = personSpec.Transform(t => t.To<Pet>(x => x.Owner)); |
| 85 | + |
| 86 | + Assert.That(session.Query<Pet>().Where(petSpec).Select(x => x.Identity).FirstOrDefault(), Is.EqualTo(1)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + void SetupQueryIntegrationTest(ISession session) |
| 91 | + { |
| 92 | + using (var tran = session.BeginTransaction()) |
| 93 | + { |
| 94 | + var exporter = new SchemaExport(config); |
| 95 | + exporter.Execute(false, true, false, session.Connection, null); |
| 96 | + tran.Commit(); |
| 97 | + } |
| 98 | + |
| 99 | + using (var tran = session.BeginTransaction()) |
| 100 | + { |
| 101 | + var pet = new Pet |
| 102 | + { |
| 103 | + Identity = 1, |
| 104 | + Owner = new Person { Identity = 1, Name = "Jane Doe" } |
| 105 | + }; |
| 106 | + session.Save(pet); |
| 107 | + session.Save(pet.Owner); |
| 108 | + session.Flush(); |
| 109 | + |
| 110 | + tran.Commit(); |
| 111 | + } |
| 112 | + |
| 113 | + session.Clear(); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments