Commits

Roger Kratz committed 5e95188

fix for NHE-99

Comments (0)

Files changed (2)

Src/NHibernate.Envers.Tests/Integration/Query/MaximalizePropertyQueryTest.cs

 using NHibernate.Envers.Query;
 using NHibernate.Envers.Tests.Entities;
 using NUnit.Framework;
+using SharpTestsEx;
 
 namespace NHibernate.Envers.Tests.Integration.Query
 {
 	{
 		private int id1;
 		private int id2;
+		private int id3;
 
 		public MaximalizePropertyQueryTest(string strategyType) : base(strategyType)
 		{
 		{
 			var site1 = new StrIntTestEntity { Str = "a", Number = 10 };
 			var site2 = new StrIntTestEntity { Str = "b", Number = 15 };
+			var site3 = new StrIntTestEntity { Str = "c", Number = 42 };
 			using (var tx = Session.BeginTransaction())
 			{
 				id1 = (int) Session.Save(site1);
 				id2 = (int) Session.Save(site2);
+				id3 = (int) Session.Save(site3);
 				tx.Commit();
 			}
 			using (var tx = Session.BeginTransaction())
 		}
 
 		[Test]
-		public void VerifyMaximzeWithEqId()
+		public void VerifyMaximzeWithIdEq()
 		{
-			var revs_id1 = AuditReader().CreateQuery()
+			var revsId1 = AuditReader().CreateQuery()
 						.ForRevisionsOfEntity(typeof (StrIntTestEntity), false, true)
 						.AddProjection(AuditEntity.RevisionNumber())
 						.Add(AuditEntity.Property("Number").Maximize()
 							.Add(AuditEntity.Id().Eq(id2)))
 						.GetResultList();
-			CollectionAssert.AreEqual(new[]{2, 3, 4}, revs_id1);
+			CollectionAssert.AreEqual(new[]{2, 3, 4}, revsId1);
 		}
 
 		[Test]
 						.GetResultList();
 			CollectionAssert.AreEquivalent(new[]{2}, result);
 		}
+
+		[Test]
+		public void VerifyMaximizeInDisjunction()
+		{
+			var idsToQuery = new[] {id1, id3};
+			var disjuction = AuditEntity.Disjunction();
+			foreach (var id in idsToQuery)
+			{
+				disjuction.Add(AuditEntity.RevisionNumber().Maximize().Add(AuditEntity.Id().Eq(id)));
+			}
+			var result = AuditReader().CreateQuery()
+				.ForRevisionsOf<StrIntTestEntity>(true)
+				.Add(disjuction)
+				.Results();
+			var idsSeen = new HashSet<int>();
+			foreach (var entity in result)
+			{
+				var id = entity.Id;
+				idsToQuery.Should().Contain(id);
+				idsSeen.Add(id).Should().Be.True();
+			}
+		}
 	}
 }

Src/NHibernate.Envers/Query/Criteria/AggregatedAuditExpression.cs

 
 			CriteriaTools.CheckPropertyNotARelation(auditCfg, entityName, propertyName);
 
+			// Make sure our conditions are ANDed together even if the parent Parameters have a different connective	
+	      var subParams = parameters.AddSubParameters(Parameters.AND);
 			// This will be the aggregated query, containing all the specified conditions
 			var subQb = qb.NewSubQueryBuilder();
 
 			// aggregated one.
 			foreach (var versionsCriteria in criterions)
 			{
-				versionsCriteria.AddToQuery(auditCfg, entityName, qb, parameters);
+				versionsCriteria.AddToQuery(auditCfg, entityName, qb, subParams);
 				versionsCriteria.AddToQuery(auditCfg, entityName, subQb, subQb.RootParameters);
 			}
 
 			}
 
 			// Adding the constrain on the result of the aggregated criteria
-			parameters.AddWhere(propertyName, "=", subQb);
+			subParams.AddWhere(propertyName, "=", subQb);
 		}
 	}
 }