public void Test_GetQueries(bool nullPerm, bool nullTypes) { CachingQueryRepository repository; Mock <IQueryRepository> mockRepository; const long testSubjectId = 1; EntityRef testPermission = nullPerm ? null : new EntityRef(2); long testPermissionId = nullPerm ? -1 : testPermission.Id; long[] entities = nullTypes ? null : new long[] { 3, 4 }; IEnumerable <AccessRuleQuery> expectedResult = new AccessRuleQuery[0]; mockRepository = new Mock <IQueryRepository>(MockBehavior.Strict); mockRepository.Setup(mr => mr.GetQueries(testSubjectId, testPermission, entities)) .Returns(() => expectedResult); repository = new CachingQueryRepository(mockRepository.Object); Assert.That(repository.GetQueries(1, testPermission, entities), Is.EqualTo(expectedResult)); Assert.That(repository.Cache, Has.Exactly(1).Property("Key").EqualTo(new SubjectPermissionTypesTuple(testSubjectId, testPermissionId, entities)) .And.Property("Value").EqualTo(expectedResult)); Assert.That(repository.GetQueries(1, testPermission, entities), Is.EqualTo(expectedResult)); Assert.That(repository.Cache, Has.Exactly(1).Property("Key").EqualTo(new SubjectPermissionTypesTuple(testSubjectId, testPermissionId, entities)) .And.Property("Value").EqualTo(expectedResult)); mockRepository.VerifyAll(); }
public void TestQueriesGetFiltered(QueryFilter queryFilter, int mockResultToMatch, bool nullParams) { FilteringQueryRepository filteringRepository; Mock <IQueryRepository> mockRepository; long subjectId = 1; EntityRef permissionId = nullParams ? null : new EntityRef(2); long typeId = 3; IList <long> securableTypes = nullParams ? null : new List <long> { typeId }; StructuredQuery queryWithCurrentUser; StructuredQuery queryWithoutCurrentUser; AccessRuleQuery[] mockResults; IEnumerable <AccessRuleQuery> result; queryWithoutCurrentUser = TestQueries.Entities(new EntityRef(typeId)); queryWithCurrentUser = TestQueries.Entities(new EntityRef(typeId)); queryWithCurrentUser.Conditions.Add(new QueryCondition { Operator = ConditionType.CurrentUser }); mockResults = new AccessRuleQuery[] { new AccessRuleQuery(1, 2, 3, queryWithoutCurrentUser, false), new AccessRuleQuery(4, 5, 6, queryWithCurrentUser, false) }; mockRepository = new Mock <IQueryRepository>(MockBehavior.Strict); mockRepository.Setup(qr => qr.GetQueries(subjectId, permissionId, securableTypes)).Returns(mockResults); filteringRepository = new FilteringQueryRepository(mockRepository.Object, queryFilter); result = filteringRepository.GetQueries(subjectId, permissionId, securableTypes); Assert.That(result, Is.EquivalentTo(new[] { mockResults[mockResultToMatch] })); }